-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.py
315 lines (223 loc) · 8.89 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
def printNode(optionalNone):
if optionalNone is None:
return '"None"'
elif isinstance(optionalNone, str):
return '"' + optionalNone + '"'
else:
return repr(optionalNone)
class P_empty:
def __init__(self):
self.fun = lambda local: ""
class P_start:
def __init__(self, block):
self.block = block
def __str__(self):
return "P_start(" + str(self.block) + ")"
def __repr__(self):
return '{"P_start" :[' + printNode(self.block) + ']}'
class P_statements:
def __init__(self, statement, statements):
self.statement = statement
self.statements = statements
def has_statements(self):
if self.statements is None:
return False
else:
return True
def __str__(self):
return "P_statements(" + str(self.statement) + "," + str(self.statements) + ")"
def __repr__(self):
return '{"P_statements" :[' + printNode(self.statement) + "," + printNode(self.statements) + ']}'
class P_call_function:
def __init__(self, fun, name, params):
self.fun = fun
self.name = name
self.params = params
def __str__(self):
return "P_call_function(" + str(self.name) + "," + str(self.params) + ")"
def __repr__(self):
return '{"P_call_function" :[' + printNode(self.name) + "," + printNode(self.params) + ']}'
class P_params:
def __init__(self, typeo, name, params):
self.typeo = typeo
self.name = name
self.params = params
def has_params(self):
if self.params is None:
return False
else:
return True
def __str__(self):
return "P_params(" + str(self.typeo) + "," + str(self.name) + "," + str(self.params) + ")"
def __repr__(self):
return '{"P_params" :[' + printNode(self.typeo) + "," + printNode(self.name) + "," + printNode(self.params) + ']}'
class P_statement_reassign:
def __init__(self, fun, name, statement):
self.fun = fun
self.name = name
self.statement = statement
def __str__(self):
return "P_statement_reassign(" + str(self.name) + "," + str(self.statement) + ")"
def __repr__(self):
return '{"P_statement_reassign" :[' + printNode(self.name) + "," + printNode(self.statement) + ']}'
class P_statement_assign:
def __init__(self, fun, typeo, name, statement):
self.fun = fun
self.typeo = typeo
self.name = name
self.statement = statement
def __str__(self):
return "P_statement_assign(" + str(self.typeo) + "," + str(self.name) + "," + str(self.statement) + ")"
def __repr__(self):
return '{"P_statement_assign" :[' + printNode(self.typeo) + "," + printNode(self.name) + "," + printNode(self.statement) + ']}'
class P_define:
def __init__(self, fun, name, params, statement):
self.fun = fun
self.name = name
self.params = params
self.statement = statement
def __str__(self):
return "P_define(" + str(self.name) + "," + str(self.params) + "," + str(self.statement) + ")"
def __repr__(self):
return '{"P_define" :[' + printNode(self.name) + "," + printNode(self.params) + "," + printNode(self.statement) + ']}'
class P_while_statement:
def __init__(self, fun, epxression, statement):
self.fun = fun
self.epxression = epxression
self.statement = statement
def __str__(self):
return "P_while_statement(" + str(self.epxression) + "," + str(self.statement) + ")"
def __repr__(self):
return '{"P_while_statement" :[' + printNode(self.epxression) + "," + printNode(self.statement) + ']}'
class P_if_statement:
def __init__(self, fun, expression, statement, statement2):
self.fun = fun
self.expression = expression
self.statement = statement
self.statement2 = statement2
def __str__(self):
return "P_if_statement(" + str(self.expression) + "," + str(self.statement) + ")"
def __repr__(self):
return '{"P_if_statement" :[' + printNode(self.expression) + "," + printNode(self.statement) + "," + printNode(self.statement) + ']}'
class P_for_statement:
def __init__(self, fun, init, condition, update, action):
self.fun = fun
self.init = init
self.condition = condition
self.update = update
self.action = action
def __str__(self):
return "P_for_statement(" + str(self.init) + "," + str(self.condition) + "," + str(self.update) + "," + str(self.action) + ")"
def __str__(self):
return '{"P_for_statement" :[' + printNode(self.init) + "," + printNode(self.condition) + "," + printNode(self.update) + "," + printNode(self.action) + ']}'
class P_casual_statements:
def __init__(self, operation):
self.operation = operation
def __str__(self):
return "P_casual_statements(" + str(self.operation) + ")"
def __repr__(self):
return '{"P_casual_statements" :[' + printNode(self.operation) + ']}'
class P_blocks:
def __init__(self, block, blocks):
self.block = block
self.blocks = blocks
def has_blocks(self):
if self.blocks is None:
return False
else:
return True
def __str__(self):
return "P_blocks(" + str(self.block) + "," + str(self.blocks) + ")"
def __repr__(self):
return '{"P_blocks" :[' + printNode(self.block) + "," + printNode(self.blocks) + ']}'
class P_block:
def __init__(self, blocks, casual_statement):
self.blocks = blocks
self.casual_statement = casual_statement
def has_blocks(self):
if self.blocks is None:
return False
else:
return True
def has_casual_statement(self):
if self.casual_statement is None:
return False
else:
return True
def __str__(self):
return "P_block(" + str(self.blocks) + "," + str(self.casual_statement) + ")"
def __repr__(self):
return '{"P_block" :[' + printNode(self.blocks) + "," + printNode(self.casual_statement) + ']}'
class P_statement_expr:
def __init__(self, fun, expression):
self.fun = fun
self.expression = expression
def __str__(self):
return "P_statement_expr(" + str(self.expression) + ")"
def __repr__(self):
return '{"P_statement_expr" :[' + printNode(self.expression) + ']}'
class P_expression_binop:
def __init__(self, fun, expr1, op, expr2):
self.fun = fun
self.expr1 = expr1
self.op = op
self.expr2 = expr2
def __str__(self):
return "P_expression_binop(" + str(self.expr1) + "," + str(self.op) + "," + str(self.expr2) + ")"
def __repr__(self):
return '{"P_expression_binop" :[' + printNode(self.expr1) + "," + printNode(self.op) + "," + printNode(self.expr2) + ']}'
def __eq__(self, other):
return self.expr1 == other.expr1 and self.op == other.op and self.expr2 == other.expr2
def __hash__(self):
return self.expr1.__hash__() * 2137 + self.op.__hash__() * 420 + self.expr2.__hash__() * 69
class P_expression_unop:
def __init__(self, fun, op, expr):
self.fun = fun
self.expr = expr
self.op = op
def __str__(self):
return "P_expression_unop(" + str(self.op) + "," + str(self.expr) + ")"
def __repr__(self):
return '{"P_expression_unop" :[' + printNode(self.op) + "," + printNode(self.expr) + ']}'
def __eq__(self, other):
return self.expr == other.expr and self.op == other.op
class P_expression_group:
def __init__(self, fun, expr):
self.fun = fun
self.expr = expr
def __str__(self):
return "P_expression_group(" + str(self.expr) + ")"
def __repr__(self):
return '{"P_expression_group" :[' + printNode(self.expr) + ']}'
class P_expression_uminus:
def __init__(self, fun, expr):
self.fun = fun
self.expr = expr
def __str__(self):
return "P_expression_uminus(" + str(self.expr) + ")"
def __repr__(self):
return '{"P_expression_uminus" :[' + printNode(self.expr) + ']}'
class P_expression_number:
def __init__(self, fun, typeo, val):
self.fun = fun
self.typeo = typeo
self.val = val
def __str__(self):
return "P_expression_number(" + str(self.typeo) + "," + str(self.val) + ")"
def __repr__(self):
return '{"P_expression_number" :[' + printNode(self.typeo) + ',' + printNode(self.val) + ']}'
class P_expression_name:
def __init__(self, fun, name):
self.fun = fun
self.expr = name
def __str__(self):
return "P_expression_name(" + str(self.expr) + ")"
def __repr__(self):
return '{"P_expression_name" :[' + printNode(self.expr) + ']}'
class P_print:
def __init__(self, fun):
self.fun = fun
def __str__(self):
return "P_print()"
def __repr__(self):
return '{"P_print"}'