comparison python/c3/typecheck.py @ 227:82dfe6a32717

Fixed tests
author Windel Bouwman
date Fri, 12 Jul 2013 17:42:39 +0200
parents 240111e0456f
children 7f18ed9b6b7e
comparison
equal deleted inserted replaced
226:240111e0456f 227:82dfe6a32717
13 if type(a) is type(b): 13 if type(a) is type(b):
14 if type(a) is BaseType: 14 if type(a) is BaseType:
15 return a.name == b.name 15 return a.name == b.name
16 elif type(a) is PointerType: 16 elif type(a) is PointerType:
17 return equalTypes(a.ptype, b.ptype) 17 return equalTypes(a.ptype, b.ptype)
18 elif type(a) is StructureType:
19 if len(a.mems) != len(b.mems):
20 return False
21 for amem, bmem in zip(a.mems, b.mems):
22 if not equalTypes(amem.typ, bmem.typ):
23 return False
24 return True
25 else:
26 raise Exception('Type compare not implemented')
18 return False 27 return False
19 28
20 def canCast(fromT, toT): 29 def canCast(fromT, toT):
21 if isinstance(fromT, PointerType) and isinstance(toT, PointerType): 30 if isinstance(fromT, PointerType) and isinstance(toT, PointerType):
22 return True 31 return True
116 sym.typ = intType 125 sym.typ = intType
117 else: 126 else:
118 self.error('Cannot select field {} of non-structure type {}'.format(sym.field, basetype), sym.loc) 127 self.error('Cannot select field {} of non-structure type {}'.format(sym.field, basetype), sym.loc)
119 sym.typ = intType 128 sym.typ = intType
120 elif type(sym) is Binop: 129 elif type(sym) is Binop:
121 sym.lvalue = False 130 sym.lvalue = False
122 if sym.op in ['+', '-', '*', '/']: 131 if sym.op in ['+', '-', '*', '/']:
123 if equalTypes(sym.a.typ, sym.b.typ): 132 if equalTypes(sym.a.typ, sym.b.typ):
124 if equalTypes(sym.a.typ, intType): 133 if equalTypes(sym.a.typ, intType):
125 sym.typ = sym.a.typ 134 sym.typ = sym.a.typ
126 else: 135 else:
127 self.error('Can only add integers', sym.loc) 136 self.error('Can only add integers', sym.loc)
128 sym.typ = intType 137 sym.typ = intType
138 else:
139 # assume void here? TODO: throw exception!
140 sym.typ = intType
141 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
142 elif sym.op in ['>', '<', '==', '<=', '>=']:
143 sym.typ = boolType
144 if not equalTypes(sym.a.typ, sym.b.typ):
145 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
146 elif sym.op in ['or', 'and']:
147 sym.typ = boolType
148 if not equalTypes(sym.a.typ, boolType):
149 self.error('Must be {0}'.format(boolType), sym.a.loc)
150 if not equalTypes(sym.b.typ, boolType):
151 self.error('Must be {0}'.format(boolType), sym.b.loc)
152 elif sym.op in ['|', '&']:
153 sym.typ = intType
154 sym.lvalue = False
155 if equalTypes(sym.a.typ, sym.b.typ):
156 if not equalTypes(sym.a.typ, intType):
157 self.error('Can only add integers', sym.loc)
158 else:
159 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
129 else: 160 else:
130 # assume void here? TODO: throw exception! 161 raise Exception('Unknown binop {0}'.format(sym.op))
131 sym.typ = intType
132 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
133 elif sym.op in ['>', '<', '==', '<=', '>=']:
134 sym.typ = boolType
135 if not equalTypes(sym.a.typ, sym.b.typ):
136 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
137 elif sym.op in ['or', 'and']:
138 sym.typ = boolType
139 if not equalTypes(sym.a.typ, boolType):
140 self.error('Must be {0}'.format(boolType), sym.a.loc)
141 if not equalTypes(sym.b.typ, boolType):
142 self.error('Must be {0}'.format(boolType), sym.b.loc)
143 elif sym.op in ['|', '&']:
144 sym.typ = intType
145 raise Exception('Not implemented')
146 # TODO: elaborate?
147 else:
148 raise Exception('Unknown binop {0}'.format(sym.op))
149 elif type(sym) is Variable: 162 elif type(sym) is Variable:
150 # check initial value type: 163 # check initial value type:
151 # TODO 164 # TODO
152 pass 165 pass
153 elif type(sym) is TypeCast: 166 elif type(sym) is TypeCast: