annotate python/c3/visitor.py @ 186:46d62dadd61b

Improved testsuite
author Windel Bouwman
date Sat, 25 May 2013 14:26:25 +0200
parents 5fd02aa38b42
children c1ccb1cb4cef
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)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
27 elif type(node) is FunctionCall:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
28 for arg in node.args:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
29 self.visit(arg)
163
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
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 169
diff changeset
43 elif type(node) is WhileStatement:
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 169
diff changeset
44 self.visit(node.condition)
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 169
diff changeset
45 self.visit(node.dostatement)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
46 else:
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 176
diff changeset
47 raise Exception('UNK visit "{0}"'.format(node))
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
48 self.f2(node)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
49