DIP 50 - AST macros

Rikki Cattermole alphaglosined at gmail.com
Mon Nov 11 00:31:13 PST 2013


On Monday, 11 November 2013 at 08:12:59 UTC, Jacob Carlborg wrote:
> On 2013-11-11 08:46, Rikki Cattermole wrote:
>
>> One of our targets for AST macros should be the ability to 
>> replicate
>> roughly linq from c# / .net.
>>
>> An example syntax for use with AST could be:
>>
>> auto data = [5, 7, 9];
>> int[] data2;
>> query {
>>  from value in data
>>  where value >= 6
>>  add to data2
>> }
>>
>> Could be unwrapped to:
>>
>> auto data = [5, 7, 9];
>> int[] data2;
>> foreach(value; data) {
>>  if (value >= 6) data2 ~= value;
>> }
>>
>> This isn't a thought out design but it should at least be a 
>> target or a
>> possibility.
>
> Absolutely. One of my favorite examples is the database query:
>
> auto person = Person.where(e => e.name == "John");
>
> Which translates to the following SQL:
>
> select * from person where name = 'John'
>
>> Also c#'s get set should be rather similar as well.
>> Example:
>>
>> getset {
>>  public int id;
>>  private bool exit;
>> }
>>
>> Would translate to:
>>
>> private int id;
>> private bool exit;
>> @property {
>>  void id(int v) {this.id = v;}
>>  void exit(bool v) { this.exit = v; }
>>  int id() { return this.id; }
>>  bool exit() { return this.exit; }
>> }
>>
>> This would definitely open new possibilities up.
>> Disclaimer I don't like c# or .net but I am partial to these 
>> features.
>
> That's quite similar one of the examples, I like to call it 
> "property shortcut":
>
> http://wiki.dlang.org/DIP50#Attribute_macros
>
>> At current point I think the DIP does have the necessary 
>> features to
>> implement this. However it would be nice for safety to be able 
>> to get
>> all scope variables of where the macro was initiated from. 
>> Being able to
>> check for if a variable exists could provide much needed 
>> compile safety
>> and better error messages.
>
> Why not? There's quite a lot that is not specified in this DIP. 
> Mostly because I haven't decided/figured out how it should work 
> exactly. Of course, any help is always appreciated.

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.

Also can you alias a macro?

alias fragment = tostring;

That would make things a lot simpler creating nice structures of 
them.

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

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.



More information about the Digitalmars-d mailing list