-
Notifications
You must be signed in to change notification settings - Fork 2
/
interp_Lvar.py
38 lines (34 loc) · 988 Bytes
/
interp_Lvar.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
from ast import *
from utils import input_int
from interp_Lint import InterpLint
class InterpLvar(InterpLint):
def interp_exp(self, e, env):
match e:
case Name(id):
return env[id]
case _:
return super().interp_exp(e, env)
def interp_stmts(self, ss, env):
if len(ss) == 0:
return
match ss[0]:
case Assign([lhs], value):
env[lhs.id] = self.interp_exp(value, env)
return self.interp_stmts(ss[1:], env)
case _:
return super().interp_stmts(ss, env)
def interp(self, p):
match p:
case Module(body):
self.interp_stmts(body, {})
case _:
raise Exception('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 = InterpLvar()
interp.interp_Lvar(p)