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