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

rikki cattermole rikki at cattermole.co.nz
Sat Jan 27 04:14:10 UTC 2018


On 26/01/2018 6:40 PM, 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.

Is it possible to reset the compiler to the point of uninitialized/newly 
initialized?

For reuse in a single process.


More information about the Digitalmars-d mailing list