DIP 50 - AST macros

Jacob Carlborg doob at me.com
Tue Nov 19 12:32:11 PST 2013


On 2013-11-19 19:32, luka8088 wrote:

> Oh, I see. It seems that I indeed missed the point.
>
> It seems to me that this DIP could to be granulated into: AST
> reflection, AST manipulation and AST template. The reason for this, as
> far as I see, is that although they overlap in some cases, in other they
> could be used independently and it could help with understanding.
>
> Regarding AST reflection and AST manipulation, I have been thinking and
> found a few situations that bugs me:
>
> In the example of
>
> auto person = Person.where(e => e.name == "John");
>
> what would be the response of:
>
> auto f = e => e.name == "John";
> auto person = Person.where(f);
>
> I guess it should be a compiler error because f could be modified at
> runtime and f's body could be hidden.

I haven't thought about that. But AST macros work at compile time

> So basically AST macros are
> something that look like D but actually are not D. This seems to me like
> an example that look good as a toy example but fails on the larger scale
> so I agree with Water in this matter (unless better
> examples/explanations are provided). Or maybe I am not seeing it clear
> enough.
>
> But! Regarding AST templates, I think something like the following would
> be a great syntax sugar:
>
> int i = 5;
>
> Ast a = t{ // t as template
>    int j = $i;
>    int k = $i + $i;
> };
>
> // ... alter ast here if necessary
>
> mixin(a);

So you don't like that it's not a "mixin" there with AST macros?

> Where syntax (and semantic) in template must be strictly D. The only
> difference would be $ sign which would allow reference to symbols
> outside the template.

If the semantics need to be valid this is very limited. Even more 
limiting than what we have now with string mixins. Note that with AST 
macros, the input needs to syntactically valid. The resulting AST of the 
macro needs to both be syntactically and semantically valid D code.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list