What are AST Macros?

pillsy pillsbury at gmail.com
Mon Jul 12 17:00:12 PDT 2010


== Quote from Steven Schveighoffer (schveiguy at yahoo.com)'s article:

> On Mon, 12 Jul 2010 16:22:28 -0400, bearophile <bearophileHUGS at lycos.com>
> wrote:
[...]
> > 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.

That's not such a great example. The problems will start piling up when you have more complicated arguments.
Say you want your macro to look like it has an argument list with two entries, called like

     foo(x, y)

which acts like a string mixin, and receives "x, y" as the argument string. Then you split at the comma, and
you're fine. You go on with your day, and the next thing you know someone is complaining that your foo macro
doesn't work because it chokes on

    foo(bar(a, b), c)

and

    foo("d, e", f)

By the time you deal with all the possibilities, you'll have written most of a parser. That parser has gotta parse
the text into *something*, and it'll probably be a tree.

There's no reason you can't use library functions to parse strings containing D code into that tree; indeed,
having access to such functions in the standard library would be useful in all sorts of ways. RIght now D is only
a couple steps away from where it would need to be in order to allow people to build a really useful macro
system in libraries.

Cheers,
Pillsy
[...]





More information about the Digitalmars-d mailing list