DIP 50 - AST macros

luka8088 luka8088 at owave.net
Wed Nov 20 14:25:40 PST 2013


On 20.11.2013. 9:04, Jacob Carlborg wrote:
> On 2013-11-19 21:54, luka8088 wrote:
> 
>> Well, do think about that :)
>>
>> auto f = e => e.name == "John";
>> if (true)
>>    f = e => e.name == "Jack";
>>
>> auto person = Person.where(f);
>>
>> I can think of a many use cases where conditional query generation is
>> required. But I don't see how this could be done using AST macros.
> 
> Using the Rails plugin, which I've got this idea from, I would not make
> the lambda conditional but the whole statement, translated to D:
> 
> auto person = Person.scoped();
> 
> if (true)
>     person = person.where(e => e.name == "Jack");
> 
> else
>     person = person.where(e => e.name == "John");

Ok, I see. Yes, I tried to think of a case where this could not work but
I was unable to find one.

> 
>> Um, sorry. I don't understand the question.
>>
>> This example (and suggestion) was suppose to show that we could allow
>> AST mixins as well as string mixins. It should behave like string mixins
>> but the main difference is that AST is structured so it much cleaner to
>> manipulate.
> 
> What I was trying to say is, what don't you like about my suggestion. Is
> it that the "mixin" keyword isn't used.
> 

If I understood you correctly, the issue with current way DSLs are
implemented is that code needs to be parsed two times. First time DSL
author parses it and creates D code from it, and second time D compiler
parses that D code and compiles it. What I would suggest in this case is
that instead of intercepting the compiler and "fixing" the semantics
before it is verified we allow the user to build D Ast and give it to
the compiler. That is why I used the mixin in my example.

Ast dCodeAst = dslToD!(q{
  if true
    do()
  else
    dont()
    dont2()
  whatever()
});

// manipulate dCodeAst

mixin(dCodeAst);

The point of this example is that mixin() accepts Ast instead of string.
That way, we parse our DSL to D Ast and give it straight to the compiler
and everything is done only once!


More information about the Digitalmars-d mailing list