Mercurial > lcfOS
annotate python/ppci/c3/visitor.py @ 307:e609d5296ee9
Massive rewrite of codegenerator
author | Windel Bouwman |
---|---|
date | Thu, 12 Dec 2013 20:42:56 +0100 |
parents | b145f8e6050b |
children | 04cf4d26a3bc |
rev | line source |
---|---|
163 | 1 from .astnodes import * |
2 | |
288 | 3 |
163 | 4 class Visitor: |
228 | 5 """ |
215 | 6 Visitor that can visit all nodes in the AST |
7 and run pre and post functions. | |
8 """ | |
9 def visit(self, node, f_pre=None, f_post=None): | |
10 self.f_pre = f_pre | |
11 self.f_post = f_post | |
12 self.do(node) | |
13 | |
14 def do(self, node): | |
306 | 15 # Run pre function: |
228 | 16 if self.f_pre: |
215 | 17 self.f_pre(node) |
18 | |
228 | 19 # Descent into subnodes: |
20 if type(node) is Package: | |
215 | 21 for decl in node.declarations: |
22 self.do(decl) | |
228 | 23 elif type(node) is Function: |
215 | 24 for s in node.declarations: |
25 self.do(s) | |
306 | 26 self.do(node.typ) |
215 | 27 self.do(node.body) |
307 | 28 elif type(node) is Compound: |
230 | 29 for s in node.statements: |
30 self.do(s) | |
307 | 31 elif type(node) is If: |
228 | 32 self.do(node.condition) |
33 self.do(node.truestatement) | |
306 | 34 self.do(node.falsestatement) |
307 | 35 elif type(node) is While: |
36 self.do(node.condition) | |
37 self.do(node.statement) | |
38 elif type(node) is Assignment: | |
39 self.do(node.lval) | |
40 self.do(node.rval) | |
228 | 41 elif type(node) is FunctionCall: |
42 for arg in node.args: | |
43 self.do(arg) | |
306 | 44 self.do(node.proc) |
307 | 45 elif type(node) is Return: |
228 | 46 self.do(node.expr) |
47 elif type(node) is Binop: | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
48 self.do(node.a) |
228 | 49 self.do(node.b) |
50 elif type(node) is Unop: | |
51 self.do(node.a) | |
52 elif type(node) is ExpressionStatement: | |
222 | 53 self.do(node.ex) |
228 | 54 elif type(node) is TypeCast: |
222 | 55 self.do(node.a) |
307 | 56 self.do(node.to_type) |
306 | 57 elif type(node) is Member: |
225 | 58 self.do(node.base) |
228 | 59 elif type(node) is Deref: |
225 | 60 self.do(node.ptr) |
228 | 61 elif type(node) is Constant: |
230 | 62 self.do(node.value) |
306 | 63 elif type(node) is DefinedType: |
64 self.do(node.typ) | |
65 elif isinstance(node, Variable): | |
66 self.do(node.typ) | |
67 elif type(node) is PointerType: | |
68 self.do(node.ptype) | |
69 elif type(node) is StructureType: | |
70 for m in node.mems: | |
71 self.do(m.typ) | |
72 elif type(node) is FunctionType: | |
73 for pt in node.parametertypes: | |
74 self.do(pt) | |
75 self.do(node.returntype) | |
307 | 76 elif type(node) in [Identifier, Literal, Empty]: |
230 | 77 # Those nodes do not have child nodes. |
78 pass | |
228 | 79 else: |
230 | 80 raise Exception('Could not visit "{0}"'.format(node)) |
163 | 81 |
228 | 82 # run post function |
83 if self.f_post: | |
215 | 84 self.f_post(node) |
85 | |
288 | 86 |
87 class AstPrinter: | |
88 """ Prints an AST as text """ | |
89 def printAst(self, pkg): | |
90 self.indent = 0 | |
91 visitor = Visitor() | |
92 visitor.visit(pkg, self.print1, self.print2) | |
93 | |
94 def print1(self, node): | |
95 print(' ' * self.indent + str(node)) | |
96 self.indent += 2 | |
97 | |
98 def print2(self, node): | |
99 self.indent -= 2 |