In a conversation on #twisted about Logix and how cool it is, Dash suggested that a tool to convert Logix code to the equivalent Python would be useful. In other words, a Python un-parser. Before generating bytecode, Logix (and a few other obscure specialized languages) builds a Python abstract syntax tree, and uses the compiler module in the standard library to render that syntax tree into Python bytecode. Pyunparse takes a Python AST and outputs Python source code.Caveat emptor: Pyunparse doesn't always get it right and has bugs. It needs refactoring badly, and an unparsed 2.4 standard library passes most of the unit tests, but not all. I'm throwing this version out there as-is, since I think people will still find it useful, especially for debugging applications that use the AST classes in the standard library. Eventually, I'd like to roll this into some sort of refactoring/pretty-printing tool, with support for various manipulations of the AST before generating code. Right now it's really more of an un-pretty-printer, so unparsed functions with long signatures will extend beyond 80 lines. The above-mentioned refactoring of pyunparse should make pretty-printing source code easier, with some sort of declarative syntax for formatting rules. The source for pyunparse is here for those who'd like to give it a try. (I'll register an actual package with PyPI when I can unparse the whole standard library and still have the unit tests pass.) last change 2005-01-25 16:39:12 |
pyunparse takes a Python AST and outputs Python source code. It can be used as a debugging aid for languages implemented in Python, such as Logix, or as the back-end of a code generation tool. |
© 2005, Andrew R. Gross