@@ -171,6 +171,45 @@ def type_check_exp(self, e, env):
171171 e .has_type = ListType (elt_ty )
172172 return e .has_type
173173
174+ case Call (Name ('array_len' ), [tup ]):
175+ tup_t = self .type_check_exp (tup , env )
176+ tup .has_type = tup_t
177+ match tup_t :
178+ case ListType (ty ):
179+ return IntType ()
180+ case Bottom ():
181+ return Bottom ()
182+ case AnyType ():
183+ return IntType ()
184+ case _:
185+ raise Exception ('array_len: unexpected ' + repr (tup_t ))
186+ case Call (Name ('array_load' ), [lst , index ]):
187+ lst_ty = self .type_check_exp (lst , env )
188+ index_ty = self .type_check_exp (index , env )
189+ self .check_consistent (index_ty , IntType (), index )
190+ match lst_ty :
191+ case ListType (ty ):
192+ return ty
193+ case AnyType ():
194+ return AnyType ()
195+ case _:
196+ raise Exception ('array_load: unexpected ' + repr (lst_ty ))
197+ case Call (Name ('array_store' ), [tup , index , value ]):
198+ tup_t = self .type_check_exp (tup , env )
199+ value_t = self .type_check_exp (value , env )
200+ index_ty = self .type_check_exp (index , env )
201+ self .check_consistent (index_ty , IntType (), index )
202+ match tup_t :
203+ case ListType (ty ):
204+ self .check_consistent (ty , value_t , e )
205+ return VoidType ()
206+ case Bottom ():
207+ return VoidType ()
208+ case AnyType ():
209+ return VoidType ()
210+ case _:
211+ raise Exception ('type_check_exp: unexpected ' + repr (tup_t ))
212+
174213 # Cases for Llambda
175214 case Lambda (params , body ):
176215 self .check_exp (e , AnyType (), env )
@@ -185,18 +224,35 @@ def type_check_exp(self, e, env):
185224 case _:
186225 raise Exception ('type_check_exp: in arity, unexpected ' + \
187226 repr (func_t ))
227+
228+ # primitives introduced by the resolve pass
229+ case Call (Name ('any_load' ), [tup , index ]):
230+ self .check_exp (tup , AnyType (), env )
231+ self .check_exp (index , IntType (), env )
232+ return AnyType ()
233+ case Call (Name ('any_store' ), [tup , index , value ]):
234+ self .check_exp (tup , AnyType (), env )
235+ self .type_check_exp (value , env )
236+ self .check_exp (index , IntType (), env )
237+ return VoidType ()
238+ case Call (Name ('any_len' ), [tup ]):
239+ self .check_exp (tup , AnyType (), env )
240+ return IntType ()
241+
188242 # Cases for Lfun (last because the below Call pattern is general)
189243 case FunRef (id , arity ):
190244 return env [id ]
191245 case Call (func , args ):
192246 func_t = self .type_check_exp (func , env )
193- args_t = [self .type_check_exp (arg , env ) for arg in args ]
194247 match func_t :
195248 case FunctionType (params_t , return_t ):
196- for (arg_t , param_t ) in zip (args_t , params_t ):
197- self .check_consistent (param_t , arg_t , e )
249+ for (arg , param_t ) in zip (args , params_t ):
250+ self .check_exp (arg , param_t , env )
251+ # for (arg_t, param_t) in zip(args_t, params_t):
252+ # self.check_consistent(param_t, arg_t, e)
198253 return return_t
199254 case AnyType ():
255+ args_t = [self .type_check_exp (arg , env ) for arg in args ]
200256 anys = [AnyType () for _ in args_t ]
201257 fun_ty = FunctionType (anys , AnyType ())
202258 return AnyType ()
@@ -209,6 +265,7 @@ def type_check_exp(self, e, env):
209265 def check_exp (self , e , ty , env ):
210266 match e :
211267 case Lambda (params , body ):
268+ trace ('check_exp: ' + str (e ) + '\n expected type: ' + str (ty ))
212269 if isinstance (params , ast .arguments ):
213270 new_params = [a .arg for a in params .args ]
214271 e .args = new_params
0 commit comments