Mercurial > lcfOS
annotate python/ppci/c3/visitor.py @ 315:084cccaa5deb
Added console and screen
author | Windel Bouwman |
---|---|
date | Sat, 21 Dec 2013 10:03:01 +0100 |
parents | 04cf4d26a3bc |
children | 5477e499b039 |
rev | line source |
---|---|
163 | 1 from .astnodes import * |
2 | |
288 | 3 |
163 | 4 class Visitor: |
228 | 5 """ |
215 | 6 Visitor that can visit all nodes in the AST |
7 and run pre and post functions. | |
8 """ | |
9 def visit(self, node, f_pre=None, f_post=None): | |
10 self.f_pre = f_pre | |
11 self.f_post = f_post | |
12 self.do(node) | |
13 | |
14 def do(self, node): | |
306 | 15 # Run pre function: |
228 | 16 if self.f_pre: |
215 | 17 self.f_pre(node) |
18 | |
228 | 19 # Descent into subnodes: |
20 if type(node) is Package: | |
215 | 21 for decl in node.declarations: |
22 self.do(decl) | |
228 | 23 elif type(node) is Function: |
215 | 24 for s in node.declarations: |
25 self.do(s) | |
306 | 26 self.do(node.typ) |
215 | 27 self.do(node.body) |
307 | 28 elif type(node) is Compound: |
230 | 29 for s in node.statements: |
30 self.do(s) | |
307 | 31 elif type(node) is If: |
228 | 32 self.do(node.condition) |
33 self.do(node.truestatement) | |
306 | 34 self.do(node.falsestatement) |
307 | 35 elif type(node) is While: |
36 self.do(node.condition) | |
37 self.do(node.statement) | |
315 | 38 elif type(node) is For: |
39 self.do(node.init) | |
40 self.do(node.condition) | |
41 self.do(node.final) | |
42 self.do(node.statement) | |
307 | 43 elif type(node) is Assignment: |
44 self.do(node.lval) | |
45 self.do(node.rval) | |
228 | 46 elif type(node) is FunctionCall: |
47 for arg in node.args: | |
48 self.do(arg) | |
306 | 49 self.do(node.proc) |
307 | 50 elif type(node) is Return: |
228 | 51 self.do(node.expr) |
52 elif type(node) is Binop: | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
53 self.do(node.a) |
228 | 54 self.do(node.b) |
55 elif type(node) is Unop: | |
56 self.do(node.a) | |
57 elif type(node) is ExpressionStatement: | |
222 | 58 self.do(node.ex) |
228 | 59 elif type(node) is TypeCast: |
222 | 60 self.do(node.a) |
307 | 61 self.do(node.to_type) |
306 | 62 elif type(node) is Member: |
225 | 63 self.do(node.base) |
228 | 64 elif type(node) is Deref: |
225 | 65 self.do(node.ptr) |
228 | 66 elif type(node) is Constant: |
313 | 67 self.do(node.typ) |
230 | 68 self.do(node.value) |
306 | 69 elif type(node) is DefinedType: |
70 self.do(node.typ) | |
71 elif isinstance(node, Variable): | |
72 self.do(node.typ) | |
73 elif type(node) is PointerType: | |
74 self.do(node.ptype) | |
75 elif type(node) is StructureType: | |
76 for m in node.mems: | |
77 self.do(m.typ) | |
78 elif type(node) is FunctionType: | |
79 for pt in node.parametertypes: | |
80 self.do(pt) | |
81 self.do(node.returntype) | |
307 | 82 elif type(node) in [Identifier, Literal, Empty]: |
230 | 83 # Those nodes do not have child nodes. |
84 pass | |
228 | 85 else: |
230 | 86 raise Exception('Could not visit "{0}"'.format(node)) |
163 | 87 |
228 | 88 # run post function |
89 if self.f_post: | |
215 | 90 self.f_post(node) |
91 | |
288 | 92 |
93 class AstPrinter: | |
94 """ Prints an AST as text """ | |
313 | 95 def printAst(self, pkg, f): |
96 self.indent = 2 | |
97 self.f = f | |
288 | 98 visitor = Visitor() |
99 visitor.visit(pkg, self.print1, self.print2) | |
100 | |
101 def print1(self, node): | |
313 | 102 print(' ' * self.indent + str(node), file=self.f) |
288 | 103 self.indent += 2 |
104 | |
105 def print2(self, node): | |
106 self.indent -= 2 |