DIP 50 - AST macros

Jacob Carlborg doob at me.com
Sat Nov 23 02:40:23 PST 2013


On Friday, 22 November 2013 at 23:43:26 UTC, luka8088 wrote:

> Yes. I still do. And I think that <[ ... ]> should contain 
> semantically
> valid code. In my opinion, if you wish to be able to write 
> anything
> else, the way to go would be: write it using q{ ... }, parse it
> yourself, build a valid D AST yourself, give D AST to the 
> compiler using
> mixin().

Then q{ } or <[ ]> would be very limited. Not even templets need 
to contain semantically valid code, as long as it's not 
instantiated. Usually the idea with AST macros is to take code 
that is not valid (semantically) and create a meaning for it 
(make it valid). If it's already valid in the first place why use 
AST macros at all? Just use the code as is.

Hmm, it could be useful to only allow semantically valid code and 
use macros to add new things to the code. But again, very 
limiting.

> Currently mixin() only accepts a string that contains a valid D 
> code.

The AST returned from a macro need to be valid D code as well.

> So if you have a custom parser for your DSL you need to 
> generate a D code
> from that DSL and then pass it to the compiler using mixin() in 
> order
> for the compiler to parse it again. Double parsing could be 
> skipped if
> mixin() would accept already built D AST and on the other hand 
> parsing
> DSL and building D AST yourself would allow maximum flexibility.

You can already do that today with string mixins, although you 
would need to convert the AST back to a string first.

--
/Jacob Carlborg


More information about the Digitalmars-d mailing list