annotate python/c3/visitor.py @ 288:a747a45dcd78

Various styling work
author Windel Bouwman
date Thu, 21 Nov 2013 14:26:13 +0100
parents e64bae57cda8
children
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
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
3
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
4 class Visitor:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
5 """
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
6 Visitor that can visit all nodes in the AST
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
7 and run pre and post functions.
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
8 """
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
9 def visit(self, node, f_pre=None, f_post=None):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
10 self.f_pre = f_pre
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
11 self.f_post = f_post
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
12 self.do(node)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
13
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
14 def do(self, node):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
15 # Run visitor:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
16 if self.f_pre:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
17 self.f_pre(node)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
18
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
19 # Descent into subnodes:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
20 if type(node) is Package:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
21 for decl in node.declarations:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
22 self.do(decl)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
23 elif type(node) is Function:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
24 for s in node.declarations:
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
25 self.do(s)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
26 self.do(node.body)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
27 elif type(node) is CompoundStatement:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
28 for s in node.statements:
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
29 self.do(s)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
30 elif type(node) is IfStatement:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
31 self.do(node.condition)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
32 self.do(node.truestatement)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
33 if node.falsestatement:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
34 self.do(node.falsestatement)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
35 elif type(node) is FunctionCall:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
36 for arg in node.args:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
37 self.do(arg)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
38 elif type(node) is Assignment:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
39 self.do(node.lval)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
40 self.do(node.rval)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
41 elif type(node) is ReturnStatement:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
42 self.do(node.expr)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
43 elif type(node) is Binop:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 215
diff changeset
44 self.do(node.a)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
45 self.do(node.b)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
46 elif type(node) is Unop:
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
47 self.do(node.a)
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
48 elif type(node) is ExpressionStatement:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
49 self.do(node.ex)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
50 elif type(node) is TypeCast:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 220
diff changeset
51 self.do(node.a)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
52 elif type(node) is FieldRef:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
53 self.do(node.base)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
54 elif type(node) is Deref:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
55 self.do(node.ptr)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
56 elif type(node) is Constant:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
57 self.do(node.value)
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
58 elif type(node) in [VariableUse, Variable, Literal, FunctionType,
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
59 DefinedType, FormalParameter, LocalVariable]:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
60 # Those nodes do not have child nodes.
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
61 pass
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
62 elif type(node) is WhileStatement:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
63 self.do(node.condition)
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
64 self.do(node.statement)
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
65 else:
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
66 raise Exception('Could not visit "{0}"'.format(node))
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents:
diff changeset
67
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
68 # run post function
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 225
diff changeset
69 if self.f_post:
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
70 self.f_post(node)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 186
diff changeset
71
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
72
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
73 class AstPrinter:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
74 """ Prints an AST as text """
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
75 def printAst(self, pkg):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
76 self.indent = 0
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
77 visitor = Visitor()
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
78 visitor.visit(pkg, self.print1, self.print2)
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
79
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
80 def print1(self, node):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
81 print(' ' * self.indent + str(node))
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
82 self.indent += 2
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
83
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
84 def print2(self, node):
a747a45dcd78 Various styling work
Windel Bouwman
parents: 272
diff changeset
85 self.indent -= 2