Metaprogramming without templates

Basile B. b2.temp at gmx.com
Mon Jun 21 10:36:56 UTC 2021


On Sunday, 20 June 2021 at 22:51:06 UTC, Stefan Koch wrote:
> Good Evening,
>
> As you know from my previous typefunction project I am trying 
> to make CTFE more usable for proper metaprogramming (as opposed 
> to just string manipulation and string mixins).
> Since I do believe that working with structure and rich 
> data-types is more useful than working with only strings 
> essentially.
>
> The goal is to be able to write regular procedural code, to 
> introspect existing or generate new code.
>
> This I want to achieve with core.reflect and core.codegen, 
> which will be part of druntime and give access to compiler/CTFE 
> integrated reflection capabilities.
>
> in core.reflect.decl, for example you would see:
> ```
> import core.reflect.node; // Empty base class is defined in 
> there
> enum DeclarationKind
> {
>     FunctionDeclatation,
>     /+
>     StructDeclaration,
>     UnionDeclarartion,
>     ClassDeclarartion,
>     VariableDeclaration,
>     AliasDeclaration,
>     TemplateDeclaration,
>     +/
> }
> class Declaration : Node
> {
>     string name;
>     DeclarationKind kind;
>     Attribute[] attributes;
>     Linkage linkage;
>     string comment;
> }
>
> Declaration[] DeclarationsFromString(string s);
> ```
>
> Which can then be used like so:
> ```
> pragma(msg, DeclarationsFromString(
>  q{
>   int var = 3;
>   void* myFunc (void*) {};
>  }
> )[1].name);
> ```
> and it would output the name if the second Declarartion which 
> is:
> "myFunc"
>
> this functionality does already work as of a few hours ago.
> However it runs into forward reference issues even more than 
> type functions did.
> And I assume I'll first have to make advances on dealing with 
> complicated forward reference graphs before I can show anything 
> neat.

I'm looking forward to this but I thing the same technics are 
always involved.
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 ?





More information about the Digitalmars-d mailing list