What are AST Macros?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Jul 12 08:35:36 PDT 2010


On 07/12/2010 09:57 AM, pillsy wrote:
> == Quote from Steven Schveighoffer (schveiguy at yahoo.com)'s article
>> On Mon, 12 Jul 2010 09:33:34 -0400, Ellery Newcomer
>> <ellery-newcomer at utulsa.edu>  wrote:
> [...]
>>> I think the big thing about macros is you don't have to
>>> worry about lexing and parsing.
>
>>> if<A>  is of the form (Assignable, Assignable, ... ),
>>> and<B>  of the form (AssignExp, AssignExp, ... )
>
>>> how do you readily rewrite
>
>>> mixin("<A>  =<B>;");
>
>>> to
>
>>> (<A>[0] =<B>[0],<A>[1] =<B>[1], ... )
>
>>> without a metric shit ton of library support?
>
>>> With the most bare bones of AST macros (maybe a ctfe
>>> function that gets passed a AST* and returns a AST*
>>> or something equivalent), it's pretty trivial. And
>>> you don't need to wrap it in a string literal.
>
>> But is that common? Is there a common need to parse mixin
>> strings and replace with other strings?
>
> In my experience with Lisp macros (which are fundamentally AST
> macros) doing that sort of thing is fairly common when it's easy.
>
>> Even if it is, can't a string
>> mixin system do exactly that?
>
> I think it can, actually. Having library functions pick up the
> slack may not be a terrible idea at all. Parsing a string into an
> AST is just a function, transforming that AST into another AST is a
> function, and transforming the resulting AST back into a string
> that gets mixed in is a third function.
>
> I see two problems with this plan, both which may be addressable
> without too much trouble.
>
> One problem is that string mixins look terrible, what with them
> being string literals. Having some sort of syntactic support for
> using macros would be a major improvement. I liked your "macro"
> keyword suggestion from elsethread.
>
> It requires a lot of straight-up hackery. Consider retard's
> suggested way for creating a unique name. Being able to do this is
> crucial to writing macros that simultaneously do non-trivial things
> and are robust, and the inability to create unique names is one
> reason C preprocessor macros are so lame. It would be far nicer if
> you could use some sort of __traits form to get a unique name (and
> then that could be wrapped in a string-mixin macro)!
>
>> Also, to me, it's trivial to understand string manipulation
>> where the end result is the language I already know, vs.
>> having to learn how the compiler represents syntax.
>
> String manipulation scales very poorly, it's error prone, and it's
> tedious.
>
> Cheers,
> Pillsy

Though I sympathise with all of the above, I should add that I have 
looked at languages that feature AST macros (Lisp, Scheme, Dylan) and 
such macros are difficult to create and read as well. We're not looking 
at day and night improvement there.

Andrei


More information about the Digitalmars-d mailing list