-
Notifications
You must be signed in to change notification settings - Fork 2
/
interp_Lif.py
70 lines (65 loc) · 2 KB
/
interp_Lif.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
from ast import *
from interp_Lvar import InterpLvar
from utils import *
class InterpLif(InterpLvar):
def interp_cmp(self, cmp):
match cmp:
case Lt():
return lambda x, y: x < y
case LtE():
return lambda x, y: x <= y
case Gt():
return lambda x, y: x > y
case GtE():
return lambda x, y: x >= y
case Eq():
return lambda x, y: x == y
case NotEq():
return lambda x, y: x != y
def interp_exp(self, e, env):
match e:
case IfExp(test, body, orelse):
match self.interp_exp(test, env):
case True:
return self.interp_exp(body, env)
case False:
return self.interp_exp(orelse, env)
case UnaryOp(Not(), v):
return not self.interp_exp(v, env)
case BoolOp(And(), values):
left = values[0]; right = values[1]
match self.interp_exp(left, env):
case True:
return self.interp_exp(right, env)
case False:
return False
case BoolOp(Or(), values):
left = values[0]; right = values[1]
match self.interp_exp(left, env):
case True:
return True
case False:
return self.interp_exp(right, env)
case Compare(left, [cmp], [right]):
l = self.interp_exp(left, env)
r = self.interp_exp(right, env)
return self.interp_cmp(cmp)(l, r)
case Let(Name(x), rhs, body):
v = self.interp_exp(rhs, env)
new_env = dict(env)
new_env[x] = v
return self.interp_exp(body, new_env)
case _:
return super().interp_exp(e, env)
def interp_stmts(self, ss, env):
if len(ss) == 0:
return
match ss[0]:
case If(test, body, orelse):
match self.interp_exp(test, env):
case True:
return self.interp_stmts(body + ss[1:], env)
case False:
return self.interp_stmts(orelse + ss[1:], env)
case _:
return super().interp_stmts(ss, env)