1

Does anyone know a tool I can use to generate Python code from AST?

I have successfully written a tokenizer and a parser, but I'm looking for a more Pythonic way to generate Python code from that. I've used LLVMPY before, and I was wondering if there is something like that I can use to generate Python code instead of LLVM IR.

Your suggestions will be highly appreciated :-)

EDIT

This is not Python AST. It's from my own language, so I know there isn't a tool that can magically understand it. I'm just looking for something that can meet me half-way in terms of generating Source Code.

For example:

Instead of concatenating strings to produce a = 1 + 2, have something like a = builder.add(1,2). Basically anything that would save me the pain of generating strings and handling indents manually. Is there such?

I already have a class that consumes the AST but I find myself having to do something like this everywhere:

...
elif node.type == "Assign":
    _buffer = self.descend(node.args[0])
    _buffer += self.descend(node.args[1])
    _buffer += self.descend(node.args[2])
    return _buffer

elif node.type == "BinOp":
    _buffer = self.descend(node.args[1])
    _buffer += node.args[0]
    _buffer += self.descend(node.args[2])
    return _buffer
...

This gets even more tideous when I have to implement bigger objects like Functions and Classes. I am just wondering if there isn't a better way to do this.

Sthe
  • 2,575
  • 2
  • 31
  • 48
  • 1
    there is module ast in standard python libraray if that helps. Have you checked this out? – Luka Rahne May 06 '14 at 22:20
  • I'm trying to generate Python code from a Toy programming language I've written for experiments. I looked at that module, but I couldn't see how it can be of help. – Sthe May 06 '14 at 22:25
  • 2
    When you say "Python code" do you mean the text source code, or bytecode? When you say "AST", do you mean Python's AST (as implemented in the `ast` module) or the AST of your own language? – Blckknght May 06 '14 at 22:44
  • If this is your own AST, then you would be responsible for writing your own Python code generator. There's no way a generic tool could understand your own specific AST. – nneonneo May 06 '14 at 22:50
  • I have added more details to the question. @Blckknght, I'm trying to generate `Source Code` from a custom built AST. – Sthe May 06 '14 at 23:08
  • *“This is not Python AST. It's from my own language”* – How should a random tool know how *your* language and *your custom AST* works? – poke May 06 '14 at 23:08
  • @poke: I've added more details clarifying my question above. I'm not looking for something to do all the work, but something I can use within those `IF` statements like how you would do with `LLVMpy`. Am I making sense? – Sthe May 06 '14 at 23:18
  • This question is confusing on several fronts. 0) You have an AST for your own language, OK. 1) Implemented in what? (I'm guessing Python from the answers you seem to like, but that sure wasn't clear in your question). 2) You want to translate your langauge to Python; there is no hint of how your language is different or similar to Python, and differences will be the main source of trouble, 3) You seem to want to do the translation as simple tree walk; without knowing 2, there's no apriori reason to beleive that will work, 4) You finally focus on the tree walk as "clumsy" as the problem. – Ira Baxter May 07 '14 at 13:25
  • 2
    ... Focusing on the question in the title, see http://stackoverflow.com/q/3455456/120163 for the issues I would really expect you to be facing. – Ira Baxter May 07 '14 at 13:27
  • @IraBaxter, I realise now that this question may have lacked a lot of important details, but my issue has been resolved now. I think what I really needed was the right approach to generate Python source code from the AST that isn't compatible with the Python's implementation. Thanks for the links as well :-) and sorry for the delay, it's elections day in South Africa. – Sthe May 07 '14 at 16:06
  • Please revise the question header; it is seriously misleading even if you did get an answer you liked. Other people will see it and waste time reading the question only to realize it doesn't apply to them. You are asking this in public. – Ira Baxter May 07 '14 at 22:25
  • Thanks for pointing that out. Title modified. How's that? – Sthe May 08 '14 at 10:10

1 Answers1

2

There is python module ast that enables this. If you are doing own tokaniser, you might just need evaluator of ast and compile() that evaluate ast. There is also unparser that generates python code from ast and can be found in python source repo. unparse.py

Luka Rahne
  • 10,336
  • 3
  • 34
  • 56
  • 1
    There's certainly some pointers in the right direction in this answer, but you make no distinction whatsoever between a Python AST and an AST for the toy language that @Sthe mentions. – Lukas Graf May 06 '14 at 22:51
  • 2
    Note a more recent (Python 3 compatible) version of the `unparse` module is [here](http://hg.python.org/cpython/file/default/Tools/parser/unparse.py). – Blckknght May 06 '14 at 23:25
  • I think unparse.py is actually what I was looking for guys. Thank you for your help :-) – Sthe May 07 '14 at 04:38