No need to dive in Python.asdl ! Then you need to go after the ASDL parser, and then you need to figure out what to do with it (although this is a worthy exercise in itself). You can stay in Python thanks to the amazing ast module.
Be sure to check out Elia Bendersky 's article on this subject .
Here is an example that he gives where the AST is built, and eval from scratch!
import ast node = ast.Expression(ast.BinOp( ast.Str('xy'), ast.Mult(), ast.Num(3))) fixed = ast.fix_missing_locations(node) codeobj = compile(fixed, '<string>', 'eval') print eval(codeobj)
ast.NodeTransformer
Check out the ast.NodeTransformer class if you want to convert an existing AST.
Here is an example from the postwhich blog above that changes the values โโof the strings to be added with str:
class MyTransformer(ast.NodeTransformer): def visit_Str(self, node): return ast.Str('str: ' + node.s)
source share