forked from IUCompilerCourse/python-student-support-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp_Lint.py
94 lines (86 loc) · 2.89 KB
/
interp_Lint.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
from ast import *
from utils import input_int, add64, sub64, neg64
def interp_exp(e):
match e:
case BinOp(left, Add(), right):
l = interp_exp(left); r = interp_exp(right)
return add64(l, r)
case BinOp(left, Sub(), right):
l = interp_exp(left); r = interp_exp(right)
return sub64(l, r)
case UnaryOp(USub(), v):
return neg64(interp_exp(v))
case Constant(value):
return value
case Call(Name('input_int'), []):
return input_int()
case _:
raise Exception('error in interp_exp, unexpected ' + repr(e))
def interp_stmt(s):
match s:
case Expr(Call(Name('print'), [arg])):
print(interp_exp(arg))
case Expr(value):
interp_exp(value)
case _:
raise Exception('error in interp_stmt, unexpected ' + repr(s))
def interp(p):
match p:
case Module(body):
for s in body:
interp_stmt(s)
case _:
raise Exception('error in interp, unexpected ' + repr(p))
# This version is for InterpLvar to inherit from
class InterpLint:
def interp_exp(self, e, env):
match e:
case BinOp(left, Add(), right):
l = self.interp_exp(left, env); r = self.interp_exp(right, env)
return add64(l, r)
case BinOp(left, Sub(), right):
l = self.interp_exp(left, env); r = self.interp_exp(right, env)
return sub64(l, r)
case UnaryOp(USub(), v):
return neg64(self.interp_exp(v, env))
case Constant(value):
return value
case Call(Name('input_int'), []):
return input_int()
case _:
raise Exception('error in interp_exp, unexpected ' + repr(e))
# The cont parameter is a list of statements that are the
# continuaton of the current statement s.
# We use this continuation-passing approach because
# it enables the handling of Goto in interp_Cif.py.
def interp_stmt(self, s, env, cont):
match s:
case Expr(Call(Name('print'), [arg])):
val = self.interp_exp(arg, env)
print(val, end='')
return self.interp_stmts(cont, env)
case Expr(value):
self.interp_exp(value, env)
return self.interp_stmts(cont, env)
case _:
raise Exception('error in interp_stmt, unexpected ' + repr(s))
def interp_stmts(self, ss, env):
match ss:
case []:
return 0
case [s, *ss]:
return self.interp_stmt(s, env, ss)
def interp(self, p):
match p:
case Module(body):
self.interp_stmts(body, {})
case _:
raise Exception('error in interp, unexpected ' + repr(p))
if __name__ == "__main__":
eight = Constant(8)
neg_eight = UnaryOp(USub(), eight)
read = Call(Name('input_int'), [])
ast1_1 = BinOp(read, Add(), neg_eight)
pr = Expr(Call(Name('print'), [ast1_1]))
p = Module([pr])
interp(p)