Mercurial > lcfOS
annotate python/c3/visitor.py @ 225:1c7364bd74c7
Fixed pointer deref
author | Windel Bouwman |
---|---|
date | Thu, 11 Jul 2013 07:42:30 +0200 |
parents | c3f1ce8b638f |
children | 7f18ed9b6b7e |
rev | line source |
---|---|
163 | 1 from .astnodes import * |
2 | |
3 class Visitor: | |
215 | 4 """ |
5 Visitor that can visit all nodes in the AST | |
6 and run pre and post functions. | |
7 """ | |
8 def visit(self, node, f_pre=None, f_post=None): | |
9 self.f_pre = f_pre | |
10 self.f_post = f_post | |
11 self.do(node) | |
12 | |
13 def do(self, node): | |
163 | 14 # Run visitor: |
215 | 15 if self.f_pre: |
16 self.f_pre(node) | |
17 | |
163 | 18 # Descent into subnodes: |
19 if type(node) is Package: | |
215 | 20 for decl in node.declarations: |
21 self.do(decl) | |
163 | 22 elif type(node) is Function: |
215 | 23 for s in node.declarations: |
24 self.do(s) | |
25 self.do(node.body) | |
163 | 26 elif type(node) is CompoundStatement: |
27 for s in node.statements: | |
215 | 28 self.do(s) |
163 | 29 elif type(node) is IfStatement: |
215 | 30 self.do(node.condition) |
31 self.do(node.truestatement) | |
32 self.do(node.falsestatement) | |
167 | 33 elif type(node) is FunctionCall: |
34 for arg in node.args: | |
215 | 35 self.do(arg) |
163 | 36 elif type(node) is Assignment: |
215 | 37 self.do(node.lval) |
38 self.do(node.rval) | |
163 | 39 elif type(node) is ReturnStatement: |
215 | 40 self.do(node.expr) |
163 | 41 elif type(node) is Binop: |
215 | 42 self.do(node.a) |
43 self.do(node.b) | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
44 elif type(node) is Unop: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
45 self.do(node.a) |
222 | 46 elif type(node) is ExpressionStatement: |
47 self.do(node.ex) | |
48 elif type(node) is TypeCast: | |
49 self.do(node.a) | |
225 | 50 elif type(node) is FieldRef: |
51 self.do(node.base) | |
52 elif type(node) is Deref: | |
53 self.do(node.ptr) | |
166 | 54 elif type(node) is Constant: |
215 | 55 self.do(node.value) |
225 | 56 elif type(node) in [EmptyStatement, VariableUse, Variable, Literal, FunctionType, DefinedType]: |
163 | 57 # Those nodes do not have child nodes. |
58 pass | |
176 | 59 elif type(node) is WhileStatement: |
215 | 60 self.do(node.condition) |
61 self.do(node.statement) | |
163 | 62 else: |
215 | 63 raise Exception('Could not visit "{0}"'.format(node)) |
163 | 64 |
215 | 65 # run post function |
66 if self.f_post: | |
67 self.f_post(node) | |
68 |