DIP 50 - AST macros

Rikki Cattermole alphaglosined at gmail.com
Mon Nov 11 01:38:16 PST 2013


On Monday, 11 November 2013 at 09:24:31 UTC, Jacob Carlborg wrote:
> On 2013-11-11 09:31, Rikki Cattermole wrote:
>
>> Theres a few other things I think would need to be cleared up.
>> For example take this code:
>>
>> shader {
>>  program
>>  vertex {
>>   #version 150
>>   void main() {
>>   }
>>  }
>>  fragment {
>>   #version 150
>>   void main() {
>>   }
>>  }
>> }
>>
>> At this point the vertex / fragment segments would be treated 
>> as strings
>> instead of calling their macros. So perhaps a rule to identify 
>> macros
>> inside macros calls? It would save a lot of time for parsing 
>> reasons.
>
> They way I see that is the AST of the whole block would be 
> passed to "shader". I have though a bit about macros inside 
> macros, but I haven't come to a conclusion. The easiest way 
> from a design point of view seems to be that "shader" to 
> basically return the whole AST it receives plus any addition it 
> needs to do. The the "vertex" and "fragment" macros are 
> expanded recursively.
>
>> Also can you alias a macro?
>>
>> alias fragment = tostring;
>>
>> That would make things a lot simpler creating nice structures 
>> of them.
>
> I don't see why not.
>
>> Perhaps a condition on a macro like the if's we got for 
>> templates would
>> be useful in the sense of being able to say:
>>
>> if (lexer.compareLine(0, "[a-zA-Z]{1}[a-zA-Z_0-9]*") &&
>> lexer.isSymbol(1, SymbolTypes.Macro, vertex))
>
> I don't think I understand how this should be used.
>
>> This would require us to develop a lexer library. But if done 
>> right I
>> don't see why it wouldn't be usable for more than just D macro 
>> checks.
>> Preferably also for e.g. c/c++ wink wink for when D's front 
>> end is in D.

An example of this might be:

macro foo (Context context, Ast!(string) str)
if (lexer.isSymbol(0, SymbolTypes.Class))
{
     return str;
}

class Bar {
  int i;
}

foo {
  Bar(7);
}
The above code would succeed however the below code will give a 
compiler error stating that the given macro is not found.

struct Haz {
  int i;
}

foo {
  Haz(9);
}

The above code is based on the assumption of a D lexer in phobos.
This is simpler set of code which won't require a lexer.

macro foo (Context context, Ast!(string) str)
if (str == "working")
{
     return "";
}

foo {fails}
foo {working}
In these cases its a simple string test to determine if the text 
given is specified value.

In the original example I gave, I was using regex to make the 
point of validation for a given line passed to the macro.


More information about the Digitalmars-d mailing list