-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreePrinter.py
163 lines (132 loc) · 4.88 KB
/
TreePrinter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import AST
def addToClass(cls):
def decorator(func):
setattr(cls, func.__name__, func)
return func
return decorator
class TreePrinter:
@addToClass(AST.Node)
def printTree(self):
raise Exception("printTree not defined in class " + self.__class__.__name__)
@addToClass(AST.BinExpr)
def printTree(self):
pass
@addToClass(AST.Program)
def printTree(self, indent=""):
self.constructions.printTree(indent)
@addToClass(AST.Construction)
def printTree(self, indent):
self.code.printTree(indent)
@addToClass(AST.Declaration)
def printTree(self, indent=""):
print(indent + "DECL")
self.value.printTree(indent + " | ")
@addToClass(AST.Init)
def printTree(self, indent=""):
print(indent + " = ")
print(indent + " | " + self.ID)
self.expr.printTree(indent + " | ")
@addToClass(AST.ExpressionList)
@addToClass(AST.DeclarationList)
@addToClass(AST.ConstructionList)
@addToClass(AST.InitList)
@addToClass(AST.InstructionList)
@addToClass(AST.ArgList)
def printTree(self, indent=""):
if len(self.list) > 0:
for i in self.list:
i.printTree(indent)
@addToClass(AST.Instruction)
def printTree(self, indent=""):
self.instruction.printTree(indent)
@addToClass(AST.LabeledInstr)
def printTree(self, indent=""):
print(indent + " : ")
print(indent + " | " + self.id)
self.instruction.printTree(indent + " | ")
@addToClass(AST.Assignment)
def printTree(self, indent=""):
print(indent + " = ")
print(indent + " | " + self.id)
self.expression.printTree(indent + " | ")
@addToClass(AST.ChoiceInstr)
def printTree(self, indent=""):
print(indent + "IF")
self.condition.printTree(indent + " | ")
self.instruction.printTree(indent + " | ")
if self.instruction_else is not None:
print(indent + "ELSE")
self.instruction_else.printTree(indent + " | ")
@addToClass(AST.WhileInstr)
def printTree(self, indent=""):
print(indent + "WHILE")
self.condition.printTree(indent + " | ")
self.instruction.printTree(indent + " | ")
@addToClass(AST.RepeatInstr)
def printTree(self, indent=""):
print(indent + "REPEAT")
self.instructions.printTree(indent + " | ")
print(indent + "UNTIL")
self.condition.printTree(indent + " | ")
@addToClass(AST.PrintInstr)
def printTree(self, indent=""):
print(indent + "PRINT")
self.expression.printTree(indent + " | ")
@addToClass(AST.ReturnInstr)
def printTree(self, indent=""):
print(indent + "RETURN")
self.expression.printTree(indent + " | ")
@addToClass(AST.ContinueInstr)
def printTree(self, indent=""):
print(indent + "CONTINUE")
@addToClass(AST.BreakInstr)
def printTree(self, indent=""):
print(indent + "BREAK")
@addToClass(AST.CompoundInstr)
def printTree(self, indent=""):
self.declarations.printTree(indent)
self.instructions.printTree(indent)
@addToClass(AST.Condition)
def printTree(self, indent=""):
self.expression.printTree(indent)
@addToClass(AST.Expression)
def printTree(self, indent=""):
if self.expression is not None:
if type(self.expression) == str:
print(indent + self.expression)
else:
self.expression.printTree(indent)
if self.left is not None:
if hasattr(self.left, "printTree"):
self.left.printTree(indent + " | ")
else:
print(indent + " | " + self.left)
if self.right is not None:
if hasattr(self.right, "printTree"):
self.right.printTree(indent + " | ")
else:
print(indent + " | " + self.right)
@addToClass(AST.Float)
@addToClass(AST.String)
@addToClass(AST.Integer)
@addToClass(AST.Const)
def printTree(self, indent=""):
print(indent + self.value)
@addToClass(AST.Variable)
def printTree(self, indent=""):
print(indent + self.name)
@addToClass(AST.Fundef)
def printTree(self, indent=""):
print(indent + "FUNDEF")
print(indent + " | " + self.id)
print(indent + " | " + "RET " + self.type)
self.args.printTree(indent + " | ")
self.instr.printTree(indent + " | ")
@addToClass(AST.Funcall)
def printTree(self, indent=""):
print(indent + "FUNCALL")
print(indent + " | " + self.id)
self.args.printTree(indent + " | ")
@addToClass(AST.Arg)
def printTree(self, indent=""):
print(indent + "ARG " + self.name)