What are AST Macros?

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


On Mon, 12 Jul 2010 16:22:28 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Steven Schveighoffer:
>> bearophile:
>> > 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?
>
> In the meantime others have already given you some answers, I can add  
> one example. The Brainfuck language allows you to do every kind of  
> thing, I have seen even compilers written in it, but its usage is very  
> unhandy, it's bug-prone, requires lot of code to do even simple things,  
> and it's very hard to modify and debug programs written in it. What I  
> meant is that string mixins in theory allow you to do many things, but  
> in practice you can't use them for complex tasks, and you can't modify  
> and fix the resulting code if you try to use them for more complex  
> tasks. They are not a long-term solution for a language that seriously  
> wants to improve over C and its preprocessor macros, they are a hack.

Brainfuck is basically a toy example of a language.  Nobody uses it for  
serious work.  Mixins are much better than a hack, the syntax of using  
them is just not polished.  They are easy to use/understand because a)  
people understand the language and b) people understand string  
manipulation.

>
> In my opinion the std.bitmanip.bitfields souce code is already past the  
> decency limit for string mixins, and I hope they will be replaced by  
> something better.

How would AST macros make std.bitmanip.bitfields easier to understand?  I  
have never seen it before, and I understood it after a couple minutes  
reading.  I bet the AST version would be actually harder to understand.

Most of the "ugliness" is the part that builds the accessors because it  
contains so much concatenation, but that is what I was proposing.  Having  
used php for the last year in my job, there is one thing I appreciate:  
with a language that is focused on string manipulation, it is so much  
better to be able to simply output variables inside strings rather than  
having to exit a quotation, and use a concatenation operator.  With the  
unused keyword macro, we can make string mixins much easier to  
write/understand without the need to add AST macros.

I'll give you another example -- javascript and HTML editing.  Most people  
would prefer to just use the innerHTML component of an element than have  
to use the DOM methods to create individual elements and add them as  
children, etc.

> I don't agree with Andrei when he says that CLisp macros (and even more  
> Scheme macros that are hygienic too) are as hairy and unmantenable as  
> code that uses string mixins heavily.

I don't have much experience with these languages except that in college  
while taking a scheme course, I wanted to create a bonfire out of all  
parentheses keys.

-Steve


More information about the Digitalmars-d mailing list