was A PYTHON PREPROCESSOR - now editing Python Abstract Syntax Trees

Shae Erisson shapr at uab.edu
Thu May 25 12:53:50 EDT 2000


"Kris J. Zaragoza" wrote:

> The better path, I believe, is to build a macro system or metalanguage
> into Python itself, operating on the syntax tree after parsing, but
> before actual compilation.  This would allow incredible amounts of
> power in transforming the code while avoiding all the problems that
> come with slicing and dicing text.  Common Lisp and Scheme have shown
> the power of this approach.  It's quite common in the Lisp world to
> use macros to tailor the language to the problem, rather than the
> problem to the language.  Why not do the same in Python?  (Mind you,
> this is easier said than done, and I understand how difficult this is,
> but I'm allowed to dream. :)
> 
> Just imagine a large system where you're regularly repeating certain
> idioms, such as complex exception handling.  Here's a purely
> hypothetical example.

<snip>

> All of the proper error handling would be taken care of by the
> user-defined constructs "withDatabaseConnection" and
> "withResponseStream".
> 
> Now, as for what the macro/metalanguage would actually look like, I
> have no clue. :-)
> 
> Thoughts?

Well, it'd look like much like Python.

I'm in the beginning stages of a Python Refactoring Browser, and one of
the issues is writing an AST match and transform module, that works as
similarly as possible to the one in the original refactoring browser
(RB)[1]. I don't think it will be extremely difficult. The original RB
uses some simplistic syntax (here translated into python) such as:

`var to match any variable, 
`var = `var + 1
x = x + 1 # will match
x = y + 1 # will not match

`@expr matches any expression no matter how complex:
`@instance1.join(`@instance2)
string.join(myList) # will match

The symbolic names such as var from `var and expr from `@expr end up
holding the part of the AST that they match so that you can operate on
them later.

AST operations would just be part of a macro/meta language for Python
though.

[1] the original refactoring browser:
http://st-www.cs.uiuc.edu/~brant/Refactory/RefactoringBrowser.html
The RB was written by: http://st-www.cs.uiuc.edu/~droberts/ as part of
his thesis.
-- 
sHae mAtijs eRisson (sHae at wEbwitchEs.coM) gEnius fOr hIre
   bRing mE fIve sQuirrels aNd nO oNe wIll gEt hUrt
13:00pm up 1 season, 1 squirrel, load average: 1+1j 0.5+2j 0.2+0.5j



More information about the Python-list mailing list