annotate python/c3/visitor.py @ 225:1c7364bd74c7

Fixed pointer deref
author Windel Bouwman
date Thu, 11 Jul 2013 07:42:30 +0200
parents c3f1ce8b638f
children 7f18ed9b6b7e
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:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
4 """
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
5 Visitor that can visit all nodes in the AST
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
6 and run pre and post functions.
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
7 """
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
8 def visit(self, node, f_pre=None, f_post=None):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
9 self.f_pre = f_pre
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
10 self.f_post = f_post
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
11 self.do(node)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
12
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
13 def do(self, node):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
14 # Run visitor:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
15 if self.f_pre:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
16 self.f_pre(node)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
17
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
18 # Descent into subnodes:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
19 if type(node) is Package:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
20 for decl in node.declarations:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
21 self.do(decl)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
22 elif type(node) is Function:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
23 for s in node.declarations:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
24 self.do(s)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
25 self.do(node.body)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
26 elif type(node) is CompoundStatement:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
27 for s in node.statements:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
28 self.do(s)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
29 elif type(node) is IfStatement:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
30 self.do(node.condition)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
31 self.do(node.truestatement)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
32 self.do(node.falsestatement)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
33 elif type(node) is FunctionCall:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
34 for arg in node.args:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
35 self.do(arg)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
36 elif type(node) is Assignment:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
37 self.do(node.lval)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
38 self.do(node.rval)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
39 elif type(node) is ReturnStatement:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
40 self.do(node.expr)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
41 elif type(node) is Binop:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
42 self.do(node.a)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
43 self.do(node.b)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
44 elif type(node) is Unop:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
45 self.do(node.a)
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
46 elif type(node) is ExpressionStatement:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
47 self.do(node.ex)
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
48 elif type(node) is TypeCast:
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
49 self.do(node.a)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
50 elif type(node) is FieldRef:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
51 self.do(node.base)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
52 elif type(node) is Deref:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
53 self.do(node.ptr)
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 163
diff changeset
54 elif type(node) is Constant:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
55 self.do(node.value)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
56 elif type(node) in [EmptyStatement, VariableUse, Variable, Literal, FunctionType, DefinedType]:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
57 # Those nodes do not have child nodes.
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
58 pass
176
5fd02aa38b42 Added while loop code generation
Windel Bouwman
parents: 169
diff changeset
59 elif type(node) is WhileStatement:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
60 self.do(node.condition)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
61 self.do(node.statement)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
62 else:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
63 raise Exception('Could not visit "{0}"'.format(node))
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
64
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
65 # run post function
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
66 if self.f_post:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
67 self.f_post(node)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
68