-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast1.py
369 lines (311 loc) · 10.7 KB
/
ast1.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#3aeee2ffb6bdcec698011572b6bbcaf180807419
##### AST classes ######
# Classes to provide the proper output for various COOL structures #
# Program (AST)
# output the class list
class CoolProgram:
def __init__(self, class_list):
self.class_list = class_list
def __repr__(self):
# print the elements in the class list
return repr(self.class_list)
# List
# output the number of elements and then each element
class CoolList:
def __init__(self, elements):
self.elements = elements
self.size = len(elements)
def __repr__(self):
output = str(self.size) + "\n"
for e in self.elements:
output += repr(e)
return output
# Class
# output the class name, no_inherits/ inherits and the superclass, and then
# the list of features
class CoolClass:
def __init__(self, name, feature_list=None, superclass=None):
self.name = name # should print as an id
self.superclass = superclass # also print as id
self.feature_list = feature_list # print as a list
def __repr__(self):
output = repr(self.name)
if self.superclass == None:
output += "no_inherits\n"
else:
output += "inherits\n" + repr(self.superclass)
if self.feature_list == None:
output += "0\n"
else:
output += repr(self.feature_list)
return output
# Identifier
# Output the line number of the id from the source file and the name of the id
class CoolIdentifier:
def __init__(self, lineno, id_string):
self.lineno = lineno
self.id_string = id_string
def __repr__(self):
output = str(self.lineno) + "\n"
output += self.id_string + "\n"
return output
# Feature
# output the name of the feature then:
# attribute_no_init with the name and type
# attribute_init with the name, type, and init expression
# method with the name, formals list, type, and body expression
class CoolFeature:
def __init__(self, name, feat_type, init=None, formal_list=None, body=None):
self.name = name
self.feat_type = feat_type
self.init = init
self.formal_list = formal_list
self.body = body
def __repr__(self):
if self.init == None:
if self.body == None:
output = "attribute_no_init\n"
output += repr(self.name) + repr(self.feat_type)
else:
output = "method\n"
output += repr(self.name)
if self.formal_list == None:
output += "0\n"
else:
output += repr(self.formal_list)
output += repr(self.feat_type) + repr(self.body)
else:
output = "attribute_init\n"
output += repr(self.name) + repr(self.feat_type) + repr(self.init)
return output
# Formal
# output the name and type
class CoolFormal:
def __init__(self, name, form_type):
self.name = name
self.form_type = form_type
def __repr__(self):
output = repr(self.name) + repr(self.form_type)
return output
# Expressions - all begin with outputting the line number
# Assign Expression
# output assign with the variable and the rhs expression
class CoolExprAssign:
def __init__(self, lineno, var, rhs):
self.lineno = lineno
self.var = var
self.rhs = rhs
def __repr__(self):
output = str(self.lineno) + "\n"
output += "assign\n"
output += repr(self.var) + repr(self.rhs)
return output
# Dispatch Expression
# output:
# dynamic_dispatch with the expression, method name, and args list
# static_dispatch with the expression, type, method name, and args list
# self_dispatch with method name and args list
class CoolExprDispatch:
def __init__(self, lineno, method, args=None, e=None, static_type=None):
self.lineno = lineno
self.e = e
self.method = method
self.args = args
self.static_type = static_type
def __repr__(self):
output = str(self.lineno) + "\n"
if self.static_type == None:
if self.e == None:
output += "self_dispatch\n"
output += repr(self.method)
if self.args == None:
output += "0\n"
else:
output += repr(self.args)
else:
output += "dynamic_dispatch\n"
output += repr(self.e) + repr(self.method)
if self.args == None:
output += "0\n"
else:
output += repr(self.args)
else:
output += "static_dispatch\n"
output += repr(self.e) + repr(self.static_type) + repr(self.method)
if self.args == None:
output += "0\n"
else:
output += repr(self.args)
return output#
# If Expression
# output if, the predicate, the then expression, and the else expression
class CoolExprIf:
def __init__(self, lineno, predicate, if_then, if_else):
self.lineno = lineno
self.predicate = predicate
self.if_then = if_then
self.if_else = if_else
def __repr__(self):
output = str(self.lineno) + "\n"
output += "if\n"
output += repr(self.predicate) + repr(self.if_then) + repr(self.if_else)
return output
# While Expression
# output while, the predicate, and the body expression
class CoolExprWhile:
def __init__(self, lineno, predicate, body):
self.lineno = lineno
self.predicate = predicate
self.body = body
def __repr__(self):
output = str(self.lineno) + "\n"
output += "while\n"
output += repr(self.predicate) + repr(self.body)
return output
# Block Expression
# output block and the body expression list
class CoolExprBlock:
def __init__(self, lineno, body):
self.lineno = lineno
self.body = body
def __repr__(self):
output = str(self.lineno) + "\n"
output += "block\n"
output += repr(self.body)
return output
# New Expression
# output new and the type
class CoolExprNew:
def __init__(self, lineno, new_class):
self.lineno = lineno
self.new_class = new_class
def __repr__(self):
output = str(self.lineno) + "\n"
output += "new\n"
output += repr(self.new_class)
return output
# Math-related expressions
# output the operation then:
# operation with the x expression and the y expression
# operation with the x expression
class CoolExprIsVoidMathConditions:
def __init__(self, lineno, operation, x, y=None):
self.lineno = lineno
self.operation = operation # string with the word for the math in the expr
self.x = x
self.y = y
def __repr__(self):
output = str(self.lineno) + "\n"
output += self.operation + "\n"
output += repr(self.x)
if self.y != None:
output += repr(self.y)
return output
# Integer Expression
# output integer with the int constant
class CoolExprInt:
def __init__(self, lineno, val):
self.lineno = lineno
self.val = val
def __repr__(self):
output = str(self.lineno) + "\n"
output += "integer\n"
output += self.val + "\n"
return output
# String Expression
# output string with the string constant
class CoolExprString:
def __init__(self, lineno, val):
self.lineno = lineno
self.val = val
def __repr__(self):
output = str(self.lineno) + "\n"
output += "string\n"
output += self.val + "\n"
return output
# Identifier Expression
# output identifier with the associated identifier
class CoolExprID:
def __init__(self, lineno, val):
self.lineno = lineno
self.val = val
def __repr__(self):
output = str(self.lineno) + "\n"
output += "identifier\n"
output += repr(self.val)
return output
# Boolean Expression
# output the boolean
class CoolExprBool:
def __init__(self, lineno, val):
self.lineno = lineno
self.val = val
def __repr__(self):
output = str(self.lineno) + "\n"
output += self.val + "\n"
return output
# Let Expression
# output let with the binding list
class CoolExprLet:
def __init__(self, lineno, body, binding_list=None):
self.lineno = lineno
self.body = body
self.binding_list = binding_list
# Then output the binding list. To output a binding, do either:
# let_binding_no_init \n variable:identifier type:identifier
# let_binding_init \n variable:identifier type:identifier value:exp
# Finally, output the expression that is the body of the let.
def __repr__(self):
output = str(self.lineno) + "\nlet\n"
if self.binding_list == None:
output += "0\n"
else:
output += repr(self.binding_list)
output += repr(self.body)
return output
# Let Binding
# output:
# let_binding_no_init with the variable and the type
# let_binding_init wiht the variable, type, and value expression
class CoolExprBinding:
def __init__(self, var, var_type, init_val=None):
self.var = var
self.var_type = var_type
self.init_val = init_val
def __repr__(self):
if self.init_val == None:
output = "let_binding_no_init\n"
output += repr(self.var) + repr(self.var_type)
else:
output = "let_binding_init\n"
output += repr(self.var) + repr(self.var_type) + repr(self.init_val)
return output
# Case Expression
# output case with the case expression and case list
class CoolExprCase:
def __init__(self, lineno, case_expr, case_list=None):
self.lineno = lineno
self.case_expr = case_expr
self.case_list = case_list
def __repr__(self):
output = str(self.lineno) + "\n"
output += "case\n"
output += repr(self.case_expr)
if self.case_list == None:
output += "0\n"
else:
output += repr(self.case_list)
return output
# Case Element
# output the variable, the type, and the body expression
class CoolExprCaseElement:
def __init__(self, lineno, var, var_type, body):
self.lineno = lineno
self.var = var
self.var_type = var_type
self.body = body
def __repr__(self):
output = repr(self.var)
output += repr(self.var_type)
output += repr(self.body)
return output