comparison python/c3/typecheck.py @ 222:c3f1ce8b638f

Fixup of parser
author Windel Bouwman
date Tue, 09 Jul 2013 17:36:31 +0200
parents 848c4b15fd0b
children 1c7364bd74c7
comparison
equal deleted inserted replaced
221:848c4b15fd0b 222:c3f1ce8b638f
8 if type(a) is BaseType: 8 if type(a) is BaseType:
9 return a.name == b.name 9 return a.name == b.name
10 elif type(a) is PointerType: 10 elif type(a) is PointerType:
11 return equalTypes(a.ptype, b.ptype) 11 return equalTypes(a.ptype, b.ptype)
12 return False 12 return False
13
14 def canCast(fromT, toT):
15 if isinstance(fromT, PointerType) and isinstance(toT, PointerType):
16 return True
17 elif fromT is intType and isinstance(toT, PointerType):
18 return True
19 return False
13 20
14 class TypeChecker: 21 class TypeChecker:
15 def __init__(self, diag): 22 def __init__(self, diag):
16 self.diag = diag 23 self.diag = diag
17 24
116 print('unknown binop', sym.op) 123 print('unknown binop', sym.op)
117 elif type(sym) is Variable: 124 elif type(sym) is Variable:
118 # check initial value type: 125 # check initial value type:
119 # TODO 126 # TODO
120 pass 127 pass
128 elif type(sym) is TypeCast:
129 if canCast(sym.a.typ, sym.to_type):
130 sym.typ = sym.to_type
131 else:
132 self.error('Cannot cast {} to {}'.format(sym.a.typ, sym.to_type))
121 elif type(sym) is Constant: 133 elif type(sym) is Constant:
122 if not equalTypes(sym.typ, sym.value.typ): 134 if not equalTypes(sym.typ, sym.value.typ):
123 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc) 135 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc)
124 elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType]: 136 elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType, ExpressionStatement]:
125 pass 137 pass
126 else: 138 else:
127 raise Exception('Unknown type check {0}'.format(sym)) 139 raise Exception('Unknown type check {0}'.format(sym))
128 140