Is it possible to use DMD as a library to compile strings at runtime?

Basile B. b2.temp at gmx.com
Fri Jan 31 14:25:30 UTC 2020


On Friday, 31 January 2020 at 11:19:37 UTC, Saurabh Das wrote:
> I see that DUB has DMD as a library package, but I was not able 
> to understand how to use it.
>
> Is it possible to use DMD as a library within a D program to 
> compile a string to machine code and run the compiled code at 
> runtime?
>
> Thanks,
> Saurabh

Fundamentally DMD as a library is a front-end. Jitting is to the 
backend side.
You'll be able to lex and parse the source to get an AST, to 
perform the semantic passes on this AST and that's all.

Then to run this code you would need to make an AST visitor that 
will generate the binary code to execute. Even using a 
specialized library with jitting abilities, such as LLVM-d [1] or 
libfirm-d [2], this would be *quite* a journey.

[1] https://github.com/MoritzMaxeiner/llvm-d
[2] https://gitlab.com/basile.b/libfirm-d


More information about the Digitalmars-d-learn mailing list