DIP 50 - AST macros

luka8088 luka8088 at owave.net
Sat Nov 23 18:35:22 PST 2013


On 23.11.2013. 11:40, Jacob Carlborg wrote:
> On Friday, 22 November 2013 at 23:43:26 UTC, luka8088 wrote:
> 
> 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.
> 

I don't think so. What I was proposing is to split the problem into
sub-problems and then find the best method for each one. Templates are
one of them and they solve the problem of not so pretty string
concatenation and that is way they should contain only valid D code.

I don't think that writing a non valid code and then making in valid in
the compilation process should be allowed. Only transforming one valid
code into another valid code but hygienically. I don't have a concrete
example why that is a bad idea, I can only speak from experience. So
this is only my opinion, not an argument.

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

Yeah. I was referring to
http://forum.dlang.org/post/l5vcct$2lit$1@digitalmars.com

So for example, the problem from referred post could be addressed by
defining as AST struct type that is accessible to user and making
mixin() accept both string and AST struct. That is also why I wrote
http://forum.dlang.org/post/l6n91e$29hd$1@digitalmars.com


It seems to me now that we don't understand each other so well :) Maybe
we should put in more examples?


More information about the Digitalmars-d mailing list