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