Metaprogramming without templates

Stefan Koch uplink.coder at googlemail.com
Mon Jun 21 11:00:42 UTC 2021


On Monday, 21 June 2021 at 10:36:56 UTC, Basile B. wrote:

> So whavetever is the magic trick used, there's actually no 
> magic. Either 1 or 2.
>
> 1. you need to parse new code to produce an ast. In D this is 
> currently like mixin which goes by the lexical stage or more 
> simply the .d you pass to the compiler.
>
> 2. you need to clone an ast to run semantic on it with a 
> specific scope. In D this is currently like template and that 
> requires to clone an ast so that the semantic will decorate the 
> ast in the right ~~context~~ scope (e.g expression.type).
>
> so eventually this would be something like something new that 
> allows stage 2 without stage 1 ? AST macros ?

Ideally you would not need stage 2 either, at least not always.
If all you are doing is using core.reflect (for example to get 
debug info).
Then no modifications of the AST is required.

What I am doing is essentially runtime reflection (like java and 
go).
However since the divide between runtime and compile-time is 
broken via CTFE,
We can use this information to splice code into the existing AST.
(which is what mixins do)
Whether I have to copy the AST or can solve for a resolution 
schedule that doesn't conflict is currently to be explored.
It's not easy.
It is however a great way to debug inconsistencies in DMDs AST.


More information about the Digitalmars-d mailing list