Re: progress on new AST
[email protected] (Jeremy Hylton) Wed, 10 Apr 2002 18:55:18 -0400
| Newsgroups | gmane.comp.python.compiler |
|---|---|
| Message-ID | <[email protected]> |
>>>>> "FB" == Finn Bock <[email protected]> writes: FB> [Jeremy Hylton] >> There is a python.asdl that defines an AST that is reasonably >> complete, although it has rough edges (slices, etc.). FB> Keep in mind that I'm a newbie at reading asdl, I'd recommend you read Dan Wang's DSL 97 paper: http://www.cs.princeton.edu/~danwang/Papers/dsl97/dsl97-abstract.html. It's an easy read. It describes the ASDL syntax and shows small examples of an AST and code generated for C and Java. FB> Keep in mind that I'm a newbie at reading asdl, but how is it FB> expressed that a 'Module' contain a list of 'stmts', while a FB> FunctionDef only contain one 'name'? Your question pointed out an embarassing bug in the python.asdl file :-). If we take an example "constructor" (with fix applied): stmt = ClassDef(identifier name, expr* bases, stmt* body) The lhs is the name of the type, the rhs is a constructor signature. The constructor takes three arguments. The type is on the left, the name is on the right. identifier is a builtin type. expr and stmt are defined in python.asdl. There are two type modifiers * and ?. The * means sequence of 0 or more. The ? means optional. So a class has a single name, an arbitrary number of base class expressions, and an arbitrary number of stmts. The bug is that Module, FunctionDef, and ClassDef were define to contain a single statement. I'm sure that's what confused you. >> I've also written a simple C code generator that turns the ast >> definition into C code that defines structs and constructor >> functions. FB> I'm playing around with generating java code and all the needed FB> information seems to be available, but I can't quite make sense FB> of the basic idea behind the datastructures we are generating FB> from. What is a Sum and what is a Product in this sense? A Sum is a set of type constructors -- so stmt is a sum type. A Product is like listcomp -- a single unnamed constructor. For a sum type, a value can be any one of the constructors. For a product, there is only one constructor. The DSL paper represents a sum as a C union with a struct element for each constructor. It is silent on products, but I've chosen to represent it as a single struct. Feel free to check in any Java-generating code in the sandbox. Jeremy