annotate python/c3/visitor.py @ 166:da0087b82fbe

Improved type checking
author Windel Bouwman
date Fri, 22 Mar 2013 16:15:31 +0100
parents 8104fc8b5e90
children 0b5b2ee6b435
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)
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 163
diff changeset
38 elif type(node) is Constant:
da0087b82fbe Improved type checking
Windel Bouwman
parents: 163
diff changeset
39 self.visit(node.value)
da0087b82fbe Improved type checking
Windel Bouwman
parents: 163
diff changeset
40 elif type(node) in [EmptyStatement, VariableUse, Variable, Literal, FunctionType]:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
41 # Those nodes do not have child nodes.
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
42 pass
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
43 else:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
44 print('UNK visit', node)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
45 self.f2(node)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
46
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
47