-
Notifications
You must be signed in to change notification settings - Fork 2
/
type_check_Ctup.py
66 lines (61 loc) · 2.09 KB
/
type_check_Ctup.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 type_check_Cwhile import TypeCheckCwhile
from utils import Allocate, Begin, GlobalValue, Collect, TupleType, Bottom
class TypeCheckCtup(TypeCheckCwhile):
def check_type_equal(self, t1, t2, e):
match t1:
case TupleType(ts1):
match t2:
case TupleType(ts2):
for (ty1, ty2) in zip(ts1,ts2):
self.check_type_equal(ty1, ty2, e)
case Bottom():
pass
case _:
raise Exception('error: ' + repr(t1) + ' != ' + repr(t2) \
+ ' in ' + repr(e))
case _:
super().check_type_equal(t1, t2, e)
def type_check_exp(self, e, env):
match e:
case Allocate(length, typ):
return typ
case GlobalValue(name):
return int
case Subscript(tup, Constant(index), Load()):
tup_t = self.type_check_atm(tup, env)
match tup_t:
case TupleType(ts):
return ts[index]
case Bottom():
return Bottom()
case _:
raise Exception('error, expected a tuple, not ' + repr(tup_t))
case Call(Name('len'), [tup]):
tup_t = self.type_check_atm(tup, env)
match tup_t:
case TupleType(ts):
return int
case Bottom():
return Bottom()
case _:
raise Exception('error, expected a tuple, not ' + repr(tup_t))
case _:
return super().type_check_exp(e, env)
def type_check_stmt(self, s, env):
match s:
case Collect(size):
pass
case Assign([Subscript(tup, Constant(index), Store())], value):
tup_t = self.type_check_atm(tup, env)
value_t = self.type_check_atm(value, env)
match tup_t:
case TupleType(ts):
self.check_type_equal(ts[index], value_t, s)
case Bottom():
pass
case _:
raise Exception('type_check_stmt: expected a tuple, not ' \
+ repr(tup_t))
case _:
return super().type_check_stmt(s, env)