Mercurial > lcfOS
diff python/c3/typecheck.py @ 225:1c7364bd74c7
Fixed pointer deref
author | Windel Bouwman |
---|---|
date | Thu, 11 Jul 2013 07:42:30 +0200 |
parents | c3f1ce8b638f |
children | 240111e0456f |
line wrap: on
line diff
--- a/python/c3/typecheck.py Tue Jul 09 17:59:15 2013 +0200 +++ b/python/c3/typecheck.py Thu Jul 11 07:42:30 2013 +0200 @@ -34,19 +34,19 @@ return self.ok def check2(self, sym): - if type(sym) in [IfStatement, WhileStatement]: - if not equalTypes(sym.condition.typ, boolType): - self.error('Condition must be of type {0}'.format(boolType), sym.condition.loc) - elif type(sym) is Assignment: + if type(sym) in [IfStatement, WhileStatement]: + if not equalTypes(sym.condition.typ, boolType): + self.error('Condition must be of type {0}'.format(boolType), sym.condition.loc) + elif type(sym) is Assignment: if not equalTypes(sym.lval.typ, sym.rval.typ): self.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc) if not sym.lval.lvalue: self.error('No valid lvalue {}'.format(sym.lval), sym.lval.loc) #if sym.rval.lvalue: # self.error('Right hand side must be an rvalue', sym.rval.loc) - elif type(sym) is ReturnStatement: - pass - elif type(sym) is FunctionCall: + elif type(sym) is ReturnStatement: + pass + elif type(sym) is FunctionCall: if sym.proc: # Check arguments: ngiv = len(sym.args) @@ -62,13 +62,14 @@ sym.typ = sym.proc.typ.returntype else: sym.typ = intType - elif type(sym) is VariableUse: + elif type(sym) is VariableUse: sym.lvalue = True - if sym.target: + if type(sym.target) is Variable: sym.typ = sym.target.typ else: + print('warning {} has no target, defaulting to int'.format(sym)) sym.typ = intType - elif type(sym) is Literal: + elif type(sym) is Literal: sym.lvalue = False if type(sym.val) is int: sym.typ = intType @@ -77,22 +78,35 @@ elif type(sym.val) is bool: sym.typ = boolType else: - self.error('Unknown literal type', sym.loc) - elif type(sym) is Unop: + raise Exception('Unknown literal type'.format(sym.val)) + elif type(sym) is Unop: if sym.op == '&': sym.typ = PointerType(sym.a.typ) sym.lvalue = False - elif sym.op == '*': - # pointer deref - sym.lvalue = True - if type(sym.a.typ) is PointerType: - sym.typ = sym.a.typ.ptype + else: + raise Exception('Unknown unop {0}'.format(sym.op)) + elif type(sym) is Deref: + # pointer deref + sym.lvalue = True + # check if the to be dereferenced variable is a pointer type: + if type(sym.ptr.typ) is PointerType: + sym.typ = sym.ptr.typ.ptype + else: + self.error('Cannot dereference non-pointer type {}'.format(sym.ptr.typ), sym.loc) + sym.typ = intType + elif type(sym) is FieldRef: + basetype = sym.base.typ + sym.lvalue = True + if type(basetype) is StructureType: + if basetype.hasField(sym.field): + sym.typ = basetype.fieldType(sym.field) else: + self.error('{} does not contain field {}'.format(basetype, symfield), sym.loc) sym.typ = intType - self.error('Cannot dereference non-pointer type {}'.format(sym.a.typ), sym.loc) else: - print('unknown unop', sym.op) - elif type(sym) is Binop: + self.error('Cannot select field {} of non-structure type {}'.format(sym.field, basetype), sym.loc) + sym.typ = intType + elif type(sym) is Binop: sym.lvalue = False if sym.op in ['+', '-', '*', '/']: if equalTypes(sym.a.typ, sym.b.typ): @@ -104,11 +118,11 @@ else: # assume void here? TODO: throw exception! sym.typ = intType - self.error('Types unequal', sym.loc) + self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc) elif sym.op in ['>', '<', '==', '<=', '>=']: sym.typ = boolType if not equalTypes(sym.a.typ, sym.b.typ): - self.error('Types unequal', sym.loc) + self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc) elif sym.op in ['or', 'and']: sym.typ = boolType if not equalTypes(sym.a.typ, boolType): @@ -117,24 +131,24 @@ self.error('Must be {0}'.format(boolType), sym.b.loc) elif sym.op in ['|', '&']: sym.typ = intType + raise Exception('Not implemented') # TODO: elaborate? else: - sym.typ = voidType - print('unknown binop', sym.op) - elif type(sym) is Variable: + raise Exception('Unknown binop {0}'.format(sym.op)) + elif type(sym) is Variable: # check initial value type: # TODO pass - elif type(sym) is TypeCast: + elif type(sym) is TypeCast: if canCast(sym.a.typ, sym.to_type): sym.typ = sym.to_type else: self.error('Cannot cast {} to {}'.format(sym.a.typ, sym.to_type)) - elif type(sym) is Constant: - if not equalTypes(sym.typ, sym.value.typ): - self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc) - elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType, ExpressionStatement]: + elif type(sym) is Constant: + if not equalTypes(sym.typ, sym.value.typ): + self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc) + elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType, ExpressionStatement, DefinedType]: pass - else: + else: raise Exception('Unknown type check {0}'.format(sym))