-
Notifications
You must be signed in to change notification settings - Fork 38
/
interp_Lif.py
66 lines (61 loc) · 1.89 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
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 Begin(ss, e):
self.interp_stmts(ss, env)
return self.interp_exp(e, env)
case _:
return super().interp_exp(e, env)
def interp_stmt(self, s, env, cont):
match s:
case If(test, body, orelse):
match self.interp_exp(test, env):
case True:
return self.interp_stmts(body + cont, env)
case False:
return self.interp_stmts(orelse + cont, env)
case _:
return super().interp_stmt(s, env, cont)