[Python-Dev] AST optimizer implemented in Python

Stefan Behnel stefan_ml at behnel.de
Sun Aug 12 10:05:02 CEST 2012


Stefan Behnel, 12.08.2012 08:00:
> Victor Stinner, 11.08.2012 20:30:
>> I started to implement an AST optimizer in Python. It's easy to create
>> a new AST tree, so I'm surprised that I didn't find any existing
>> project.
>>
>> https://bitbucket.org/haypo/misc/src/tip/python/ast_optimizer.py
> 
> Since you're about to do pretty much the same thing, here is the code that
> Cython uses for its static tree optimisations. It's not based on Python's
> own AST, but most of the concepts should apply more or less directly to
> that as well.
> 
> https://github.com/cython/cython/blob/master/Cython/Compiler/Optimize.py

Another thing I think I should mention is my TreePath implementation,
mostly stealing from Fredrik Lundh's ElementPath.

https://github.com/cython/cython/blob/master/Cython/Compiler/TreePath.py

We basically only use it for test assertions against the AST, for example here:

https://github.com/cython/cython/blob/master/tests/run/builtinslice.pyx

But it wouldn't be all that hard to extend it into a pattern matching tool,
so that you could basically say "if this expression matches the current
subtree, then jump to optimiser a), for another expression use optimiser
b), else leave things untouched".

Stefan




More information about the Python-Dev mailing list