Mercurial > lcfOS
comparison python/c3/visitor.py @ 215:c1ccb1cb4cef
Major changes in c3 frontend
author | Windel Bouwman |
---|---|
date | Fri, 05 Jul 2013 13:00:03 +0200 |
parents | 46d62dadd61b |
children | 3f6c30a5d234 |
comparison
equal
deleted
inserted
replaced
214:6875360e8390 | 215:c1ccb1cb4cef |
---|---|
1 from .astnodes import * | 1 from .astnodes import * |
2 | 2 |
3 class Visitor: | 3 class Visitor: |
4 """ Visitor that visits all nodes in the ast and runs the function 'f' """ | 4 """ |
5 def __init__(self, f1, f2): | 5 Visitor that can visit all nodes in the AST |
6 self.f1 = f1 | 6 and run pre and post functions. |
7 self.f2 = f2 | 7 """ |
8 def visit(self, node): | 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): | |
9 # Run visitor: | 14 # Run visitor: |
10 self.f1(node) | 15 if self.f_pre: |
16 self.f_pre(node) | |
17 | |
11 # Descent into subnodes: | 18 # Descent into subnodes: |
12 if type(node) is Package: | 19 if type(node) is Package: |
13 for s in node.scope: | 20 for decl in node.declarations: |
14 self.visit(s) | 21 self.do(decl) |
15 elif type(node) is Function: | 22 elif type(node) is Function: |
16 for s in node.scope: | 23 for s in node.declarations: |
17 self.visit(s) | 24 self.do(s) |
18 self.visit(node.typ) | 25 self.do(node.body) |
19 self.visit(node.body) | |
20 elif type(node) is CompoundStatement: | 26 elif type(node) is CompoundStatement: |
21 for s in node.statements: | 27 for s in node.statements: |
22 self.visit(s) | 28 self.do(s) |
23 elif type(node) is IfStatement: | 29 elif type(node) is IfStatement: |
24 self.visit(node.condition) | 30 self.do(node.condition) |
25 self.visit(node.truestatement) | 31 self.do(node.truestatement) |
26 self.visit(node.falsestatement) | 32 self.do(node.falsestatement) |
27 elif type(node) is FunctionCall: | 33 elif type(node) is FunctionCall: |
28 for arg in node.args: | 34 for arg in node.args: |
29 self.visit(arg) | 35 self.do(arg) |
30 elif type(node) is Assignment: | 36 elif type(node) is Assignment: |
31 self.visit(node.lval) | 37 self.do(node.lval) |
32 self.visit(node.rval) | 38 self.do(node.rval) |
33 elif type(node) is ReturnStatement: | 39 elif type(node) is ReturnStatement: |
34 self.visit(node.expr) | 40 self.do(node.expr) |
35 elif type(node) is Binop: | 41 elif type(node) is Binop: |
36 self.visit(node.a) | 42 self.do(node.a) |
37 self.visit(node.b) | 43 self.do(node.b) |
38 elif type(node) is Constant: | 44 elif type(node) is Constant: |
39 self.visit(node.value) | 45 self.do(node.value) |
40 elif type(node) in [EmptyStatement, VariableUse, Variable, Literal, FunctionType]: | 46 elif type(node) in [EmptyStatement, VariableUse, Variable, Literal, FunctionType]: |
41 # Those nodes do not have child nodes. | 47 # Those nodes do not have child nodes. |
42 pass | 48 pass |
43 elif type(node) is WhileStatement: | 49 elif type(node) is WhileStatement: |
44 self.visit(node.condition) | 50 self.do(node.condition) |
45 self.visit(node.dostatement) | 51 self.do(node.statement) |
46 else: | 52 else: |
47 raise Exception('UNK visit "{0}"'.format(node)) | 53 raise Exception('Could not visit "{0}"'.format(node)) |
48 self.f2(node) | |
49 | 54 |
55 # run post function | |
56 if self.f_post: | |
57 self.f_post(node) | |
58 |