-
Notifications
You must be signed in to change notification settings - Fork 38
/
type_check_Lcast.py
44 lines (41 loc) · 1.49 KB
/
type_check_Lcast.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
import ast
from ast import *
from type_check_Llambda import TypeCheckLlambda
from type_check_Lgrad import TypeCheckLgrad
from utils import *
import typing
class TypeCheckLcast(TypeCheckLlambda):
def parse_type_annot(self, annot):
match annot:
case AnyType():
return AnyType()
case _:
return super().parse_type_annot(annot)
def type_check_exp(self, e, env):
#trace('*** type_check_exp: ' + str(e) + '\nenv: ' + str(env))
match e:
case Cast(value, source, target):
self.check_exp(value, source, env)
TypeCheckLgrad().check_consistent(source, target, e)
return target
case AnnLambda(params, returns, body):
new_env = {x:t for (x,t) in env.items()}
for (x,t) in params:
new_env[x] = t
return_t = self.type_check_exp(body, new_env)
self.check_type_equal(returns, return_t, e)
return FunctionType([t for (x,t) in params], return_t)
case Call(Name('any_load'), [tup, index]):
self.check_exp(tup, AnyType(), env)
self.check_exp(index, IntType(), env)
return AnyType()
case Call(Name('any_store'), [tup, index, value]):
self.check_exp(tup, AnyType(), env)
self.type_check_exp(value, env)
self.check_exp(index, IntType(), env)
return VoidType()
case Call(Name('any_len'), [tup]):
self.check_exp(tup, AnyType(), env)
return IntType()
case _:
return super().type_check_exp(e, env)