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