What are AST Macros?

Steven Schveighoffer schveiguy at yahoo.com
Mon Jul 12 07:08:13 PDT 2010


On Mon, 12 Jul 2010 09:33:34 -0400, Ellery Newcomer  
<ellery-newcomer at utulsa.edu> wrote:

> On 07/12/2010 07:30 AM, Steven Schveighoffer wrote:
>> On Mon, 12 Jul 2010 08:12:31 -0400, bearophile
>> <bearophileHUGS at lycos.com> wrote:
>>
>>> Steven Schveighoffer:
>>>> If people agree that AST macros are superseded by mixins,
>>>
>>> String mixins are a hack, just a bit better than C preprocessor
>>> (because they are scoped), they are not a replacement of clean macros.
>>
>> Interesting statement, can you back it up? :) What can you do with a
>> macro that you can't do with a mixin?
>>
>> -Steve
>
> 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?  It seems like the means that AST macros  
operate, but is that the ultimate goal?  Even if it is, can't a string  
mixin system do exactly that?

You say a ton of library support is bad, but why is that?  If the end  
result is the same, what does it matter if you implement it via a library  
vs. the compiler?  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.

I haven't really used AST macros.  Does anyone have a good practical  
example of how it's used in other languages to compare with string  
mixins?  Your example seems trivially solved via simple library functions  
such as split or using regex.

-Steve


More information about the Digitalmars-d mailing list