DMD as a library package can now run through all semantic phases

timotheecour timothee.cour2 at gmail.com
Sat Jan 27 00:47:01 UTC 2018


On Friday, 26 January 2018 at 18:40:23 UTC, Seb wrote:
> In case someone wants to play with DMD as a library, it got a 
> lot easier as of today.
> Here's an example:
>
> ```
> #!/usr/bin/env dub
> /+dub.sdl:
> dependency "dmd" version="~master"
> +/
> void main()
> {
>     import dmd.frontend;
>     import std.algorithm : each;
>     import std.stdio;
>
>     // Sets DMD's global variables. Only required to be called 
> once
>     // In the future this might be done automatically (e.g. 
> module constructor or initOnce)
>     initDMD;
>
>     // Search for the predefined import paths of your host 
> compiler (DMD and LDC are supported)
>     findImportPaths.each!addImport;
>
>     // Load a module
>     // (if no second argument is given, the file will be opened 
> and read)
>     auto m = parseModule("test.d", q{
>         void foo()
>         {
>             foreach (i; 0..10) {}
>         }
>     });
>
>     // Run through all semantic phases
>     m.fullSemantic;
>     m.prettyPrint.writeln;
> }
> ```
>
>
> Want to know what it prints? Run the file!
> Spoiler: it's similar to new the AST feature at run.dlang.io:
>
> https://run.dlang.io/is/mwU67O
>
> __Warning__: the DMD DUB package is still experimental and at 
> the moment should only be used by alpha warriors.
>
> See also:
>
> https://dlang.org/phobos-prerelease/dmd_frontend.html (will 
> work soon)
> https://dlang.org/library-prerelease/dmd/frontend.html (Ddox 
> documentation, works already)
>
> Huge thanks and credits goes to Jacob Carlborg and Razvan Nitu 
> as well.

on OSX:
chmod u+x main.d
./main.d

WARNING: A deprecated branch based version specification is used 
for the dependency dmd. Please use numbered versions instead. 
Also note that you can still use the dub.selections.json file to 
override a certain dependency to use a branch instead.
Invalid source/import path: 
/Users/timothee/.dub/packages/dmd-master/dmd/generated/dub
core.exception.AssertError at ../../../../../.dub/packages/dmd-master/dmd/src/dmd/frontend.d(208): No valid config found.



More information about the Digitalmars-d mailing list