DMD as a library package can now run through all semantic phases
Amorphorious
Amorphorious at gmail.com
Tue Jan 30 00:21:09 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.
What does this actually allow one to do besides parse d code and
print it? Can it compile D code at runtime? Interface external D
code in unique ways at compile time(similar to string mixins but
possibly better)?
Just curious what the point is and what box it opens.
More information about the Digitalmars-d
mailing list