-
Notifications
You must be signed in to change notification settings - Fork 2
/
type_check_Pif.py
65 lines (62 loc) · 2.3 KB
/
type_check_Pif.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
from ast import *
from type_check_Pvar import TypeCheckPvar, check_type_equal
from utils import *
class TypeCheckPif(TypeCheckPvar):
def type_check_exp(self, e, env):
match e:
case Constant(value) if isinstance(value, bool):
return bool
case IfExp(test, body, orelse):
test_t = self.type_check_exp(test, env)
check_type_equal(bool, test_t, test)
body_t = self.type_check_exp(body, env)
orelse_t = self.type_check_exp(orelse, env)
check_type_equal(body_t, orelse_t, e)
return body_t
case BinOp(left, Sub(), right):
l = self.type_check_exp(left, env)
check_type_equal(l, int, left)
r = self.type_check_exp(right, env)
check_type_equal(r, int, right)
return int
case UnaryOp(Not(), v):
t = self.type_check_exp(v, env)
check_type_equal(t, bool, v)
return bool
case BoolOp(op, values):
left = values[0]; right = values[1]
l = self.type_check_exp(left, env)
check_type_equal(l, bool, left)
r = self.type_check_exp(right, env)
check_type_equal(r, bool, right)
return bool
case Compare(left, [cmp], [right]) if isinstance(cmp, Eq) or isinstance(cmp, NotEq):
l = self.type_check_exp(left, env)
r = self.type_check_exp(right, env)
check_type_equal(l, r, e)
return bool
case Compare(left, [cmp], [right]):
l = self.type_check_exp(left, env)
check_type_equal(l, int, left)
r = self.type_check_exp(right, env)
check_type_equal(r, int, right)
return bool
case Let(Name(x), rhs, body):
t = self.type_check_exp(rhs, env)
new_env = dict(env); new_env[x] = t
return self.type_check_exp(body, new_env)
case _:
return super().type_check_exp(e, env)
def type_check_stmts(self, ss, env):
if len(ss) == 0:
return
match ss[0]:
case If(test, body, orelse):
test_t = self.type_check_exp(test, env)
check_type_equal(bool, test_t, test)
body_t = self.type_check_stmts(body, env)
orelse_t = self.type_check_stmts(orelse, env)
check_type_equal(body_t, orelse_t, ss[0])
return self.type_check_stmts(ss[1:], env)
case _:
return super().type_check_stmts(ss, env)