RFC -- HYGIENIC MACROS IN PYTHON

Samuele Pedroni pedronis at bluewin.ch
Tue Feb 5 11:58:43 EST 2002


Hi.

I have toyed with similar ideas, but never
moved to implementation. Sadly I don't know
if I have time left to invest on this :(.

Anyway here is a list of
open issues to consider, probably
you also know them all but for the
moment I cannot say much more :(
[Sorry if some sounds peremptory,
is just hurry]

1. Python does not have
block closures with non-local returns
like Smalltalk, they would be
probably a more natural extension
than macros but probably
not enough pythonic anyway.
They are probably a useful missing tool
for implemeting macros and
one should cope with that.

2. Python has a very late binding semantics
and compilation is fully  trasparent - everything:
definitions, retrivieving values from other
modules etc happen at runtime. So the
most natural macros would operate
as if expanded at runtime, given 1.
this seems difficult to implement.
Conclusion: macros implementation
probably must break compilation transparence,
and require inter modules references
at compilation time.  Some model ideas:

[assuming the implementation
uses import hooks]
a) having separated macro-defs-only
  files
b) a macro definition (at its compilation-time) is transformed
in code that at runtime produces some kind
of first-class macro object, probably normal imports
and lookup for macro defs should be
separated (?), basically the macro handling mechanism
uses at compilation time the information in the macro
objects in already imported (in classic sense)
modules ( or force import).

3. Given the absence of closure blocks
local vars clashes should be handled through
renaming (?) (issues?)

4. A problem is how to refer efficiently
to definition context values

* macro def  "module" *

def mytest(): ...

macro def of myguard: BLOCK as
if mytest(): BLOCK


*  macro usage "module" *

macro from ... import myguard

myguard:
  ...

how to refer efficiently and
"safely" to mytest in macro expansion?
[ differently from CL in Python symbols
are not first-class :(, the problem is
that python offer only the aforementioned
very late binding]

5. error handling and reporting

regards, Samuele Pedroni.





More information about the Python-list mailing list