-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexpr.py
459 lines (328 loc) · 12.5 KB
/
expr.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
"""Expressions (for a calculator)
M Young, January 2019
Revised March 2019 for compiler project;
Revised May 2019 to add comparison operations
"""
# Global variable NO_VALUE is defined below after IntConst
# One global environment (scope) for
# the calculator
ENV = dict()
def env_clear():
"""Clear all variables in calculator memory"""
global ENV
ENV = dict()
class UndefinedVariable(Exception):
"""Raised when expression tries to use a variable that
is not in ENV
"""
pass
class Expr(object):
"""Abstract base class of all expressions."""
def eval(self) -> "IntConst":
"""Implementations of eval should return an integer constant."""
raise NotImplementedError("Each concrete Expr class must define 'eval'")
def __str__(self) -> str:
"""Implementations of __str__ should return the expression in algebraic notation"""
raise NotImplementedError("Each concrete Expr class must define __str__")
def __repr__(self) -> str:
"""Implementations of __repr__ should return a string that looks like
the constructor, e.g., Plus(IntConst(5), IntConst(4))
"""
raise NotImplementedError(f"Class {self.__class__.__name__} doesn't define __repr__")
def __eq__(self, other: "Expr") -> bool:
raise NotImplementedError("__eq__ method not defined for class")
class IntConst(Expr):
def __init__(self, value: int):
self.value = value
def __str__(self) -> str:
return str(self.value)
def __repr__(self) -> str:
return f"IntConst({self.value})"
def eval(self) -> "IntConst":
return self
def __eq__(self, other: Expr):
return isinstance(other, IntConst) and self.value == other.eval().value
# Globals should normally go at the beginning of the file, but we needed
# IntConst to define this one.
NO_VALUE = IntConst(7777) # Just an unlikely value to get randomly
class BinOp(Expr):
"""Abstract base class for binary operators +, *, /, -"""
def __init__(self, left, right):
self.left = left
self.right = right
def eval(self) -> "IntConst":
"""Each concrete subclass must define _apply(int, int)->int"""
left_val = self.left.eval()
right_val = self.right.eval()
return IntConst(self._apply(left_val.value, right_val.value))
def __str__(self) -> str:
"""Implementations of __str__ should return the expression in algebraic notation"""
return f"({str(self.left)} {self.opsym} {str(self.right)})"
def __repr__(self) -> str:
"""Implementations of __repr__ should return a string that looks like
the constructor, e.g., Plus(IntConst(5), IntConst(4))
"""
return f"{self.__class__.__name__}({repr(self.left)}, {repr(self.right)})"
def __eq__(self, other: "Expr") -> bool:
return type(self) == type(other) and \
self.left == other.left and \
self.right == other.right
def _opcode(self) -> str:
"""Which operation code do we use in the generated assembly code?"""
raise NotImplementedError("Each binary operator should define the _opcode method")
class Plus(BinOp):
"""left + right"""
def __init__(self, left: Expr, right: Expr):
super().__init__(left, right)
self.opsym = "+"
def _apply(self, left: int, right: int) -> int:
return left + right
class Minus(BinOp):
"""left - right"""
def __init__(self, left: Expr, right: Expr):
super().__init__(left, right)
self.opsym = "-"
def _apply(self, left: int, right: int) -> int:
return left - right
class Times(BinOp):
"""left * right"""
def __init__(self, left: Expr, right: Expr):
super().__init__(left, right)
self.opsym = "*"
def _apply(self, left: int, right: int) -> int:
return left * right
class Div(BinOp):
"""left // right"""
def __init__(self, left: Expr, right: Expr):
super().__init__(left, right)
self.opsym = "/"
def _apply(self, left: int, right: int) -> int:
return left // right
class UnOp(Expr):
"""Abstract base class for unary operators ~, @"""
def __init__(self, left: Expr):
self.left = left
def eval(self) -> "IntConst":
"""Each concrete subclass must define _apply(int, int)->int"""
left_val = self.left.eval()
return IntConst(self._apply(left_val.value))
def __str__(self) -> str:
"""Implementations of __str__ should return the expression in algebraic notation"""
return f"({self.opsym}{str(self.left)})"
def __repr__(self) -> str:
"""Implementations of __repr__ should return a string that looks like
the constructor, e.g., Plus(IntConst(5), IntConst(4))
"""
return f"{self.__class__.__name__}({repr(self.left)})"
def __eq__(self, other: "Expr") -> bool:
return type(self) == type(other) and \
self.left == other.left
class Neg(UnOp):
"""~left"""
def __init__(self, left: Expr):
super().__init__(left)
self.opsym = "~"
def _apply(self, left: int) -> int:
return 0 - left
class Abs(UnOp):
"""Absolute value, represented as @"""
def __init__(self, left: Expr):
super().__init__(left)
self.opsym = "@"
def _apply(self, left: int) -> int:
return abs(left)
class Var(Expr):
def __init__(self, name: str):
self.name = name
def __str__(self):
return self.name
def __repr__(self):
return f"Var({self.name})"
def eval(self):
global ENV
if self.name in ENV:
return ENV[self.name]
else:
raise UndefinedVariable(f"{self.name} has not been assigned a value")
def assign(self, value: IntConst):
ENV[self.name] = value
class Assign(Expr):
"""Assignment: x = E represented as Assign(x, E)"""
def __init__(self, left: Var, right: Expr):
assert isinstance(left, Var) # Can only assign to variables!
self.left = left
self.right = right
def __str__(self) -> str:
return f"{self.left} = {self.right}"
def __repr__(self) -> str:
return f"Assign({repr(self.left)}, {repr(self.right)})"
def eval(self) -> IntConst:
r_val = self.right.eval()
self.left.assign(r_val)
return r_val
class Control(Expr):
"""Control flow nodes (while, if, ...).
Control flow constructs have one or more blocks of statements
and may have a controlling predicate. For predicates,
we take zero as false, and any other value as true.
Control constructs don't have actual values (they would be 'None'
in Python and 'void' in C or C++), so we return 0
from eval.
"""
pass
# Note PyCharm will complain that Control doesn't implement all
# abstract methods, but that's because Control is itself an
# abstract base class ... the abstract methods should be implemented
# in its subclasses.
class Seq(Control):
"""exp ; exp"""
def __init__(self, left, right):
""" exp ; exp """
self.left = left
self.right = right
def __str__(self):
return f"{{\n{self.left}\n{self.right} }}"
def __repr__(self):
return f"Seq({repr(self.left)}, {repr(self.right)}"
def eval(self) -> IntConst:
"""Just evaluate in order"""
discard = self.left.eval()
return self.right.eval()
class Print(Control):
"""Print a value. Returns the value."""
def __init__(self, expr: Expr):
"""Print e"""
self.expr = expr
def __str__(self):
return f"print {self.expr};"
def __repr__(self):
return f"Print({repr(self.expr)})"
def eval(self) -> IntConst:
result = self.expr.eval()
print(f"Quack!: {result.value}")
return result
class Read(Expr):
"""Read a value from input"""
def __init__(self):
pass
def __str__(self):
return "(read)"
def __repr__(self):
return "Read()"
def eval(self) -> IntConst:
val = input("Quack! Gimme an int! ")
return IntConst(int(val))
class Comparison(Control):
"""A relational operation that may yield 'true' or 'false',
In the interpreter, relational operators ==, >=, etc
return an integer 0 for False or 1 for True, and the "if" and "while"
constructs use that value.
In the compiler, "if" and "while" delegate that branching
to the relational construct, i.e., x < y does not create
a value in a register but rather causes a jump if y - x
is positive. Condition code is the condition code for
the conditional JUMP after a subtraction, e.g., Z for
equality, P for >, PZ for >=.
For each comparison, we give two condition codes: One if
we want to branch when the condition is true, and another
if we want to branch when the condition is false.
(Currently the compiler only uses the cond_code_false
conditions, because it is jumping to the 'else' branch
or out of the loop.)
"""
def __init__(self, left: Expr, right: Expr):
self.left = left
self.right = right
def __str__(self) -> str:
# Fix this up when you implement code generation
return f"{str(self.left)} <comparison> {str(self.right)}"
def __repr__(self) -> str:
return f"{self.__class__.__name__}({repr(self.left)}, {repr(self.right)})"
def __eq__(self, other: "Expr") -> bool:
return type(self) == type(other) and \
self.left == other.left and \
self.right == other.right
def eval(self) -> "IntConst":
"""In the interpreter, relations return 0 or 1.
Each concrete subclass must define _apply(int, int)->int
"""
left_val = self.left.eval()
right_val = self.right.eval()
return IntConst(self._apply(left_val.value, right_val.value))
class EQ(Comparison):
"""left == right"""
def _apply(self, left: int, right: int) -> int:
return 1 if left == right else 0
class NE(Comparison):
"""left != right"""
def _apply(self, left: int, right: int) -> int:
return 1 if left != right else 0
class GT(Comparison):
"""left > right"""
def _apply(self, left: int, right: int) -> int:
return 1 if left > right else 0
class GE(Comparison):
"""left >= right"""
def _apply(self, left: int, right: int) -> int:
return 1 if left >= right else 0
class LT(Comparison):
def _apply(self, left: int, right: int) -> int:
return 1 if left < right else 0
class LE(Comparison):
def _apply(self, left: int, right: int) -> int:
return 1 if left <= right else 0
class While(Control):
"""Classic while loop."""
def __init__(self, cond: Comparison, expr: Expr):
"""While cond do expr"""
self.cond = cond
self.expr = expr
def __str__(self):
return f"while {self.cond} do\n{self.expr}\nod"
def __repr__(self):
return f"While({repr(self.cond)}, {repr(self.expr)})"
def eval(self) -> IntConst:
"""
Repeat 'expr' part while 'cond' part evaluates to a non-zero
value. Returns value of last statement executed.
"""
last = NO_VALUE
cond_val = self.cond.eval()
while cond_val.value != 0:
last = self.expr.eval()
cond_val = self.cond.eval()
return last
class Pass(Control):
"""
The 'else' part of an 'if' statement is optional. This node
is a stand-in for the empty block ... it does nothing.
"""
def __init__(self):
"""La la la la la I can't hear you"""
return
def __repr__(self):
return "pass"
def __str__(self):
return "pass"
def eval(self) -> IntConst:
"""Does nothing, has no value."""
return NO_VALUE
class If(Control):
"""If with optional Else (no elif)"""
def __init__(self, cond, thenpart, elsepart=Pass()):
"""if cond then block else block fi"""
self.cond = cond
self.thenpart = thenpart
self.elsepart = elsepart
def __str__(self):
return "if {} then\n{}\nelse\n{}\nfi".format(self.cond, self.thenpart, self.elsepart)
def __repr__(self):
return f"If({repr((self.cond))}, {repr(self.thenpart)}, {repr(self.elsepart)})"
def eval(self) -> IntConst:
"""If statement. Returns nothing. """
cond_value = self.cond.eval()
if cond_value.value != 0:
result = self.thenpart.eval()
else:
result = self.elsepart.eval()
return result