5

For example, if I was to create a node called "Something" that I wanted to use in a Python AST tree, where and which changes would I have to add to Python source code in order to do that?

I know that I should start out with Python.asdl where the AST grammar is defined. Then I should move onto ast.c.

Unfortunately, I am unsure where exactly I have to make the changes in the ast.c file in order to implement the node.

Also, for simplicity, lets say that I just want the node as a placeholder, meaning that it should not do anything except be able to insert itself into a tree.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
Nonomus
  • 127
  • 1
  • 9

1 Answers1

2

No need to dive into Python.asdl yet! Then you have to go chasing around for an ASDL parser and then you have to figure out what to do with it (although this is a worthy exercise in itself). You can stay within Python thanks to the amazing ast module.

Be sure to check out Eli Bendersky's article on the matter.

Here is an example he provides where an AST is constructed and eval'd 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 transform an existing AST.

Here is an example from the above blog postwhich modifies string values to be prepended with str:.

class MyTransformer(ast.NodeTransformer):
    def visit_Str(self, node):
        return ast.Str('str: ' + node.s)
mvanveen
  • 9,754
  • 8
  • 33
  • 42
  • 1
    I am familiar with Eli Bendersky's article on this; however, it deals with a different area than what I'm talking about. I am hoping to actually add a new TYPE of node into it. So, for example, there is ast.Str(s), and I want to make it so there is ast.Undefined(). – Nonomus Feb 08 '12 at 12:36