-
Notifications
You must be signed in to change notification settings - Fork 39
/
type_check_Lproxy.py
171 lines (166 loc) · 6.52 KB
/
type_check_Lproxy.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import ast
from ast import *
from type_check_Lany import TypeCheckLany
from utils import *
import typing
class TypeCheckLproxy(TypeCheckLany):
def check_type_equal(self, t1, t2, e):
if t1 == Bottom() or t2 == Bottom():
return
match t1:
case ProxyOrListType(elt1):
match t2:
case ProxyOrListType(elt2):
self.check_type_equal(elt1, elt2, e)
case _:
raise Exception('error: ' + repr(t1) + ' != ' + repr(t2) \
+ ' in ' + repr(e))
case ProxyOrTupleType(ps1):
match t2:
case ProxyOrTupleType(ps2):
for (p1,p2) in zip(ps1, ps2):
self.check_type_equal(p1, p2, e)
case _:
raise Exception('error: ' + repr(t1) + ' != ' + repr(t2) \
+ ' in ' + repr(e))
case _:
super().check_type_equal(t1, t2, e)
def parse_type_annot(self, annot):
match annot:
case ProxyOrTupleType(elt_types):
return ProxyOrTupleType([self.parse_type_annot(t) for t in elt_types])
case ProxyOrListType(elt_type):
return ProxyOrListType(self.parse_type_annot(elt_type))
case _:
return super().parse_type_annot(annot)
def type_check_exp(self, e, env):
match e:
# raw_tuple is just like Tuple
case RawTuple(es):
ts = [self.type_check_exp(e, env) for e in es]
e.has_type = TupleType(ts)
return e.has_type
case TupleProxy(tup, reads, source, target):
self.check_exp(tup, source, env)
self.type_check_exp(reads, env)
# TODO: check the types of reads and writes
return target
case ListProxy(lst, read, write, source, target):
self.check_exp(lst, source, env)
self.type_check_exp(read, env)
self.type_check_exp(write, env)
# TODO: check the types of read and write
return target
case InjectTuple(value):
tup_ty = self.type_check_exp(value, env)
match tup_ty:
case TupleType(ts):
return ProxyOrTupleType(ts)
case _:
raise Exception('type_check error ' + repr(e))
case InjectTupleProxy(value, tup_ty):
self.type_check_exp(value, env)
# TODO: check the type of the value
match tup_ty:
case TupleType(ts):
return ProxyOrTupleType(ts)
case _:
raise Exception('type_check error ' + repr(e))
case InjectList(value):
list_ty = self.type_check_exp(value, env)
match list_ty:
case ListType(elt_ty):
return ProxyOrListType(elt_ty)
case _:
raise Exception('type_check error ' + repr(e))
case InjectListProxy(value, list_ty):
self.type_check_exp(value, env)
# TODO: check the type of the value
match list_ty:
case ListType(elt_ty):
return ProxyOrListType(elt_ty)
case _:
raise Exception('type_check error ' + repr(e))
case Call(Name('is_tuple_proxy'), [arg]):
arg_ty = self.type_check_exp(arg, env)
match arg_ty:
case ProxyOrTupleType(ts):
return BoolType()
case _:
raise Exception('type_check error: expected ProxyOrTupleType, not ' + repr(arg_ty)
+ ' in ' + repr(e))
case Call(Name('is_array_proxy'), [arg]):
arg_ty = self.type_check_exp(arg, env)
match arg_ty:
case ProxyOrListType(ts):
return BoolType()
case _:
raise Exception('type_check error: expected ProxyOrListType, not ' + repr(arg_ty))
case Call(Name('proxy_tuple_load'), [tup, Constant(index)]):
tup_ty = self.type_check_exp(tup, env)
match tup_ty:
case ProxyOrTupleType(ts):
return ts[index]
case _:
raise Exception('type_check error: expected ProxyOrTupleType, not ' + repr(tup_ty))
case Call(Name('project_tuple'), [arg]):
arg_ty = self.type_check_exp(arg, env)
match arg_ty:
case ProxyOrTupleType(ts):
return TupleType(ts)
case _:
raise Exception('type_check error: expected ProxyOrTupleType, not ' + repr(arg_ty))
case Call(Name('proxy_tuple_len'), [tup]):
tup_ty = self.type_check_exp(tup, env)
match tup_ty:
case ProxyOrTupleType(ts):
return IntType()
case _:
raise Exception('type_check error: expected ProxyOrListType, not ' + repr(tup_ty))
case Call(Name('project_array'), [arg]):
arg_ty = self.type_check_exp(arg, env)
match arg_ty:
case ProxyOrListType(elt_ty):
return ListType(elt_ty)
case _:
raise Exception('type_check error: expected ProxyOrListType, not ' + repr(arg_ty))
case Call(Name('proxy_array_len'), [tup]):
tup_ty = self.type_check_exp(tup, env)
match tup_ty:
case ProxyOrListType(ts):
return IntType()
case _:
raise Exception('type_check error: expected ProxyOrListType, not ' + repr(tup_ty))
case Call(Name('proxy_array_load'), [lst, index]):
lst_ty = self.type_check_exp(lst, env)
index_ty = self.type_check_exp(index, env)
self.check_type_equal(index_ty, IntType(), index)
match lst_ty:
case ProxyOrListType(ty):
return ty
case _:
raise Exception('proxy_array_load expected a proxy-or-list, not ' + repr(lst_ty))
case Call(Name('proxy_array_store'), [tup, index, value]):
tup_t = self.type_check_exp(tup, env)
value_t = self.type_check_exp(value, env)
index_ty = self.type_check_exp(index, env)
self.check_type_equal(index_ty, IntType(), index)
match tup_t:
case ProxyOrListType(ty):
self.check_type_equal(ty, value_t, e)
return VoidType()
case Bottom():
return VoidType()
case _:
raise Exception('type_check_exp: in proxy_array_store unexpected '
+ repr(tup_t))
case Call(Name('any_store'), [tup, index, value]):
tup_t = self.type_check_exp(tup, env)
self.check_type_equal(tup_t, AnyType(), tup)
value_t = self.type_check_exp(value, env)
self.check_type_equal(value_t, AnyType(), value)
index_ty = self.type_check_exp(index, env)
self.check_type_equal(index_ty, IntType(), index)
return VoidType()
case _:
return super().type_check_exp(e, env)