DIP 50 - AST macros

luka8088 luka8088 at owave.net
Tue Nov 19 10:32:37 PST 2013


On 13.11.2013. 13:47, Jacob Carlborg wrote:
> On 2013-11-13 09:15, luka8088 wrote:
>> I think that string concatenation is enough (at least for now), and if
>> you want another syntax for templates you can write a macro for that.
> 
> Strings are far from enough. Then you have missed the whole idea. It's
> not supposed to be syntax sugar for string mixins.
> 

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. 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);

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.

So if you would want to have conditional template you could write:

int i = 5;
int j;
bool b = false;

mixin(t{
  $j = $i;
  static if ($b) {
    $j++;
  }
});


More information about the Digitalmars-d mailing list