D parsing

Jacob Carlborg doob at me.com
Thu Nov 7 05:50:56 PST 2013


On 2013-11-07 00:23, Philippe Sigaud wrote:

> Well, I remember him saying that he doesn't want everybody and their dog
> to change the D syntax. If we get macros, that's exactly what we can do.
> For example, telling the compiler (or another tool), to rewrite
>
> int a := 1;
>
> into
>
> immutable a = 1;

It depends on what the AST macros allows you to do. I guess everyone 
here has her/his own definition.

> But that means the original not-D code cannot be compiled by anyone with
> a D compiler anymore (obviously). And soon everybody is transforming the
> grammar according to what they think is cool.
> That's his PoV IIRC.
>
> I'm more interested in getting to ability to define my own lowerings: in
> the same way that scope statements or foreach over ranges are rewritten
> into lower-level D code by the comiler, I would like to be able to
> define my own syntactic constructs.

My idea of AST macros[1] is quite similar to those in Scala and a bit 
similar to what you want. The most important thing to remember is that 
AST macros cannot create new syntax. It only works at the semantic level 
of the language. That means the above example you wrote doesn't not 
work, since that isn't legal D syntax. What would work is doing 
something like this:

auto a = Person.where(e => e.name == "John");

In this case "where" is a macro. Which translates the given AST into the 
following SQL query:

select * from person where name = 'John'

Another thing I would really like is to be able to pass a delegate to a 
function after argument list, if the function takes a delegate as its 
last parameter:

void each (T) (T[] arr, void delegate (T) dg);

[1, 2, 3].each(e) {
     writeln(e);
}

Of courses, the compiler needs to be able to inline the delegate.

> Plus, everybody here know that AST macros can cure world hunger :)
> Wouldn't that be nice?

Of course :)

[1] https://dl.dropboxusercontent.com/u/18386187/ast_macros.html

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list