forked from IUCompilerCourse/python-student-support-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp_Lint.py
79 lines (72 loc) · 2.27 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
from ast import *
from utils import input_int
def interp_exp(e):
match e:
case BinOp(left, Add(), right):
l = interp_exp(left); r = interp_exp(right)
return l + r
case BinOp(left, Sub(), right):
l = interp_exp(left); r = interp_exp(right)
return l - r
case UnaryOp(USub(), v):
return - interp_exp(v)
case Constant(value):
return value
case Call(Name('input_int'), []):
return int(input())
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)
def interp(p):
match p:
case Module(body):
for s in body:
interp_stmt(s)
# 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 l + r
case BinOp(left, Sub(), right):
l = self.interp_exp(left, env); r = self.interp_exp(right, env)
return l - r
case UnaryOp(USub(), v):
return - self.interp_exp(v, env)
case Constant(value):
return value
case Call(Name('input_int'), []):
return int(input())
case _:
raise Exception('error in interp_exp, unexpected ' + repr(e))
def interp_stmts(self, ss, env):
if len(ss) == 0:
return
match ss[0]:
case Expr(Call(Name('print'), [arg])):
val = self.interp_exp(arg, env)
print(val, end='')
return self.interp_stmts(ss[1:], env)
case Expr(value):
self.interp_exp(value, env)
return self.interp_stmts(ss[1:], env)
case _:
raise Exception('error in interp_stmts, unexpected ' + repr(ss[0]))
def interp(self, p):
match p:
case Module(body):
self.interp_stmts(body, {})
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)