comparison python/c3/typecheck.py @ 272:e64bae57cda8

refactor ir
author Windel Bouwman
date Sat, 31 Aug 2013 17:58:54 +0200
parents c4370696ccc7
children 1c7c1e619be8
comparison
equal deleted inserted replaced
271:cf7d5fb7d9c8 272:e64bae57cda8
31 for amem, bmem in zip(a.mems, b.mems): 31 for amem, bmem in zip(a.mems, b.mems):
32 if not equalTypes(amem.typ, bmem.typ): 32 if not equalTypes(amem.typ, bmem.typ):
33 return False 33 return False
34 return True 34 return True
35 else: 35 else:
36 raise Exception('Type compare not implemented') 36 raise Exception('Type compare for {} not implemented'.format(type(a)))
37 return False 37 return False
38 38
39 def canCast(fromT, toT): 39 def canCast(fromT, toT):
40 fromT = theType(fromT) 40 fromT = theType(fromT)
41 toT = theType(toT) 41 toT = theType(toT)
97 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc) 97 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc)
98 # determine return type: 98 # determine return type:
99 sym.typ = sym.proc.typ.returntype 99 sym.typ = sym.proc.typ.returntype
100 elif type(sym) is VariableUse: 100 elif type(sym) is VariableUse:
101 sym.lvalue = True 101 sym.lvalue = True
102 if type(sym.target) is Variable: 102 if isinstance(sym.target, Variable):
103 sym.typ = sym.target.typ 103 sym.typ = sym.target.typ
104 else: 104 else:
105 print('warning {} has no target, defaulting to int'.format(sym)) 105 print('warning {} has no target, defaulting to int'.format(sym))
106 sym.typ = intType 106 sym.typ = intType
107 elif type(sym) is Literal: 107 elif type(sym) is Literal:
170 self.error('Must be {0}'.format(boolType), sym.a.loc) 170 self.error('Must be {0}'.format(boolType), sym.a.loc)
171 if not equalTypes(sym.b.typ, boolType): 171 if not equalTypes(sym.b.typ, boolType):
172 self.error('Must be {0}'.format(boolType), sym.b.loc) 172 self.error('Must be {0}'.format(boolType), sym.b.loc)
173 else: 173 else:
174 raise Exception('Unknown binop {0}'.format(sym.op)) 174 raise Exception('Unknown binop {0}'.format(sym.op))
175 elif type(sym) is Variable: 175 elif isinstance(sym, Variable):
176 # check initial value type: 176 # check initial value type:
177 # TODO 177 # TODO
178 pass 178 pass
179 elif type(sym) is TypeCast: 179 elif type(sym) is TypeCast:
180 if canCast(sym.a.typ, sym.to_type): 180 if canCast(sym.a.typ, sym.to_type):