22from utils import CProgram , Goto , trace , Bottom , Let
33import copy
44
5- def check_type_equal (t1 , t2 , e ):
5+ class TypeCheckCif :
6+
7+ def check_type_equal (self , t1 , t2 , e ):
68 if t1 == Bottom () or t2 == Bottom ():
7- pass
9+ pass
810 elif t1 != t2 :
9- raise Exception ('error: ' + repr (t1 ) + ' != ' + repr (t2 ) \
10- + ' in ' + repr (e ))
11-
12- class TypeCheckCif :
11+ raise Exception ('error: ' + repr (t1 ) + ' != ' + repr (t2 ) \
12+ + ' in ' + repr (e ))
1313
1414 def type_check_atm (self , e , env ):
1515 match e :
@@ -30,35 +30,35 @@ def type_check_exp(self, e, env):
3030 return self .type_check_atm (e , env )
3131 case IfExp (test , body , orelse ):
3232 test_t = self .type_check_exp (test , env )
33- check_type_equal (bool , test_t , test )
33+ self . check_type_equal (bool , test_t , test )
3434 body_t = self .type_check_exp (body , env )
3535 orelse_t = self .type_check_exp (orelse , env )
36- check_type_equal (body_t , orelse_t , e )
36+ self . check_type_equal (body_t , orelse_t , e )
3737 return body_t
3838 case BinOp (left , op , right ) if isinstance (op , Add ) or isinstance (op , Sub ):
3939 l = self .type_check_atm (left , env )
40- check_type_equal (l , int , e )
40+ self . check_type_equal (l , int , e )
4141 r = self .type_check_atm (right , env )
42- check_type_equal (r , int , e )
42+ self . check_type_equal (r , int , e )
4343 return int
4444 case UnaryOp (USub (), v ):
4545 t = self .type_check_atm (v , env )
46- check_type_equal (t , int , e )
46+ self . check_type_equal (t , int , e )
4747 return int
4848 case UnaryOp (Not (), v ):
4949 t = self .type_check_exp (v , env )
50- check_type_equal (t , bool , e )
50+ self . check_type_equal (t , bool , e )
5151 return bool
5252 case Compare (left , [cmp ], [right ]) if isinstance (cmp , Eq ) or isinstance (cmp , NotEq ):
5353 l = self .type_check_atm (left , env )
5454 r = self .type_check_atm (right , env )
55- check_type_equal (l , r , e )
55+ self . check_type_equal (l , r , e )
5656 return bool
5757 case Compare (left , [cmp ], [right ]):
5858 l = self .type_check_atm (left , env )
59- check_type_equal (l , int , left )
59+ self . check_type_equal (l , int , left )
6060 r = self .type_check_atm (right , env )
61- check_type_equal (r , int , right )
61+ self . check_type_equal (r , int , right )
6262 return bool
6363 case Call (Name ('input_int' ), []):
6464 return int
@@ -79,18 +79,18 @@ def type_check_stmt(self, s, env):
7979 case Assign ([lhs ], value ):
8080 t = self .type_check_exp (value , env )
8181 if lhs .id in env :
82- check_type_equal (env .get (lhs .id , Bottom ()), t , s )
82+ self . check_type_equal (env .get (lhs .id , Bottom ()), t , s )
8383 else :
8484 env [lhs .id ] = t
8585 case Expr (Call (Name ('print' ), [arg ])):
8686 t = self .type_check_exp (arg , env )
87- check_type_equal (t , int , s )
87+ self . check_type_equal (t , int , s )
8888 case Expr (value ):
8989 self .type_check_exp (value , env )
9090 case If (Compare (left , [cmp ], [right ]), body , orelse ):
9191 left_t = self .type_check_atm (left , env )
9292 right_t = self .type_check_atm (right , env )
93- check_type_equal (left_t , right_t , s ) # not quite strict enough
93+ self . check_type_equal (left_t , right_t , s ) # not quite strict enough
9494 self .type_check_stmts (body , env )
9595 self .type_check_stmts (orelse , env )
9696 case Goto (label ):
0 commit comments