macros

Evan Simpson evan at tokenexchange.com
Tue Jan 18 17:59:06 EST 2005


Jeremy Bowers wrote:
> You know, Guido might as well give in now on the Macro issue. If he
> doesn't come up with something himself, apparently we'll just hack
> bytecode.

Ah, hacking bytecode isn't where it's at anymore.  These days, you use 
the compiler package and munge the AST.  Hygenic!

That said, the big brake on macros implemented either of these ways is 
the fact that the source still has to be syntactically valid python. 
You can make a macro FOOBAR that works in these ways:

   x = FOOBAR(some, arguments) # Pseudo-function

   if FOOBAR:
       a.suite # Pseudo-'if' statement

   { FOOBAR: expr } # Pseudo-throwaway dict

...but not these:

   FOOBAR:
      a.suite # Suite that explicity depends on FOOBAR for meaning

   x = a FOOBAR b # expression with pseudo-keyword

What we need is a generic suite statement, something like:

gensuite ::= [decorators] "do:" suite

...where the suite is treated like the body of an anonymous function 
that is called and discarded immediately after it is defined.  In other 
words, the code

   @f2
   do: pass

...is roughly equivalent to:

   @f2
   def <unique name>(): pass
   <unique name>()
   del <unique name>

Then we could abuse the heck out of it by perverting the AST of

   @FOOBAR
   do:
     stuff

That's never going to happen, tho.

Cheers,

Evan @ 4-am




More information about the Python-list mailing list