-
Notifications
You must be signed in to change notification settings - Fork 0
/
AST.py
297 lines (217 loc) · 7.02 KB
/
AST.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/python
class Node(object):
def __str__(self):
return self.printTree()
def accept(self, visitor):
return visitor.visit(self)
def __init__(self):
self.children = ()
self.i = 0
self.n = 1
def __iter__(self):
return self
def __next__(self):
if self.i < self.n:
self.i += 1
return self
else:
raise StopIteration()
class NodeList(Node):
list = []
def __init__(self, list=None):
self.list = []
self.children = ()
self.add_to_list(list)
self.i = 0
self.n = len(self.list)
def add_to_list(self, list):
if list is not None:
self.list.append(list)
self.children = tuple(self.list)
def __iter__(self):
return self
def __next__(self):
if self.i < self.n:
i = self.i
self.i += 1
return self.list.list[i]
else:
raise StopIteration()
class BinExpr(Node):
def __init__(self, op, left, right, line, token=None):
super().__init__()
self.token = token
self.op = op
self.left = left
self.right = right
self.line = line
# if you want to use somewhere generic_visit method instead of visit_XXX in visitor
# definition of children field is required in each class from AST
self.children = (left, right)
class Program(Node):
def __init__(self, constructions):
super().__init__()
self.constructions = constructions
self.children = tuple(constructions.list)
class ConstructionList(NodeList):
def __init__(self, constructions=None):
super().__init__(constructions)
class Construction(Node):
def __init__(self, code):
super().__init__()
self.code = code
self.children = tuple(code)
class DeclarationList(NodeList):
def __init__(self, declarations=None):
super().__init__(declarations)
class Declaration(Node):
def __init__(self, type, value, line):
super().__init__()
self.type = type
self.value = value
self.line = line
self.children = tuple(value.list)
class InitList(NodeList):
def __init__(self, inits=None):
super().__init__(inits)
class Init(Node):
def __init__(self, ID, expr, line):
super().__init__()
self.ID = ID
self.expr = expr
self.line = line
self.children = tuple(expr)
class InstructionList(NodeList):
def __init__(self, instructions=None):
super().__init__(instructions)
class Instruction(Node):
def __init__(self, instruction):
super().__init__()
self.instruction = instruction
self.children = tuple(instruction)
class PrintInstr(Node):
def __init__(self, type, expression, line):
super().__init__()
self.expression = expression
self.type = type
self.line = line
self.children = tuple(expression)
class LabeledInstr(Node):
def __init__(self, id, instruction):
super().__init__()
self.id = id
self.instruction = instruction
self.children = tuple(instruction)
class Assignment(Node):
def __init__(self, id, expression, line):
super().__init__()
self.expression = expression
self.id = id
self.line = line
self.children = tuple(expression)
class ChoiceInstr(Node):
def __init__(self, condition, instruction1, instruction2=None):
super().__init__()
self.condition = condition
self.instruction = instruction1
self.instruction_else = instruction2
self.children = (instruction1, instruction2)
class WhileInstr(Node):
def __init__(self, condition, instruction):
super().__init__()
self.condition = condition
self.instruction = instruction
self.children = (condition, instruction)
class RepeatInstr(Node):
def __init__(self, instructions, condition, line):
super().__init__()
self.instructions = instructions
self.line = line
self.condition = condition
class ReturnInstr(Node):
def __init__(self, expression, line):
super().__init__()
self.expression = expression
self.line = line
class ContinueInstr(Node):
def __init__(self, line):
super().__init__()
self.line = line
class BreakInstr(Node):
def __init__(self, line):
super().__init__()
self.line = line
class CompoundInstr(Node):
def __init__(self, declarations, instructions, line):
super().__init__()
self.declarations = declarations
self.instructions = instructions
self.line = line
self.children = (declarations, instructions)
class Condition(Node):
def __init__(self, expression):
super().__init__()
self.expression = expression
self.children = tuple(expression)
class ExpressionList(NodeList):
def __init__(self, expr=None):
super().__init__(expr)
class Expression(Node):
def __init__(self, left, expression, right, idE=None):
super().__init__()
self.left = left
self.expression = expression
self.right = right
self.id = idE
self.children = (left, expression, right)
class Const(Node):
def __init__(self, value):
super().__init__()
self.value = value
self.children = tuple(value)
class Integer(Const):
def __init__(self, value, line):
super().__init__(value)
self.value = value
self.line = line
class Float(Const):
def __init__(self, value, line):
super().__init__(value)
self.value = value
self.line = line
class String(Const):
def __init__(self, value, line):
super().__init__(value)
self.value = value
self.line = line
self.children = tuple(value)
class Variable(Node):
def __init__(self, name, line):
super().__init__()
self.name = name
# self.children = tuple(name)
self.line = line
class Fundef(Node):
def __init__(self, id, args, instr, return_type, line):
super().__init__()
self.id = id
self.args = args
self.instr = instr
self.type = return_type
self.line = line
self.children = (args, instr)
class Funcall(Node):
def __init__(self, id, args, line):
super().__init__()
self.id = id
self.args = args
self.line = line
class ArgList(NodeList):
def __init__(self, arg=None, line = None):
super().__init__(arg)
self.line = line
class Arg(Node):
def __init__(self, name, type, line):
self.name = name
self.type = type
self.line = line