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

Seb seb at wilzba.ch
Fri Jan 26 18:40:23 UTC 2018


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.


More information about the Digitalmars-d mailing list