comparison python/c3/visitor.py @ 163:8104fc8b5e90

Added visitor to c3
author Windel Bouwman
date Mon, 18 Mar 2013 20:13:57 +0100
parents
children da0087b82fbe
comparison
equal deleted inserted replaced
162:d8c735dc31f9 163:8104fc8b5e90
1 from .astnodes import *
2
3 class Visitor:
4 """ Visitor that visits all nodes in the ast and runs the function 'f' """
5 def __init__(self, f1, f2):
6 self.f1 = f1
7 self.f2 = f2
8 def visit(self, node):
9 # Run visitor:
10 self.f1(node)
11 # Descent into subnodes:
12 if type(node) is Package:
13 for s in node.scope:
14 self.visit(s)
15 elif type(node) is Function:
16 for s in node.scope:
17 self.visit(s)
18 self.visit(node.typ)
19 self.visit(node.body)
20 elif type(node) is CompoundStatement:
21 for s in node.statements:
22 self.visit(s)
23 elif type(node) is IfStatement:
24 self.visit(node.condition)
25 self.visit(node.truestatement)
26 self.visit(node.falsestatement)
27 elif type(node) is ProcedureCall:
28 pass
29 # TODO
30 elif type(node) is Assignment:
31 self.visit(node.lval)
32 self.visit(node.rval)
33 elif type(node) is ReturnStatement:
34 self.visit(node.expr)
35 elif type(node) is Binop:
36 self.visit(node.a)
37 self.visit(node.b)
38 elif type(node) in [EmptyStatement, Constant, VariableUse, Variable, Literal, FunctionType]:
39 # Those nodes do not have child nodes.
40 pass
41 else:
42 print('UNK visit', node)
43 self.f2(node)
44
45