Mercurial > lcfOS
diff python/c3/typecheck.py @ 186:46d62dadd61b
Improved testsuite
author | Windel Bouwman |
---|---|
date | Sat, 25 May 2013 14:26:25 +0200 |
parents | 4348da5ca307 |
children | c1ccb1cb4cef |
line wrap: on
line diff
--- a/python/c3/typecheck.py Fri May 24 20:45:03 2013 +0200 +++ b/python/c3/typecheck.py Sat May 25 14:26:25 2013 +0200 @@ -13,8 +13,14 @@ def __init__(self, diag): self.diag = diag self.visitor = Visitor(self.precheck, self.check2) + def error(self, msg, loc): + """ Wrapper that registers the message and marks the result invalid """ + self.diag.error(msg, loc) + self.ok = False def checkPackage(self, pkg): - self.visitor.visit(pkg) + self.ok = True + self.visitor.visit(pkg) + return self.ok def precheck(self, sym): pass def check2(self, sym): @@ -22,10 +28,10 @@ pass elif type(sym) in [IfStatement, WhileStatement]: if not equalTypes(sym.condition.typ, boolType): - self.diag.error('Condition must be of type {0}'.format(boolType), sym.condition.loc) + 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.diag.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc) + self.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc) elif type(sym) is ReturnStatement: pass elif type(sym) is FunctionCall: @@ -35,11 +41,11 @@ ptypes = sym.proc.typ.parametertypes nreq = len(ptypes) if ngiv != nreq: - self.diag.error('Function {2}: {0} arguments required, {1} given'.format(nreq, ngiv, sym.proc.name), sym.loc) + self.error('Function {2}: {0} arguments required, {1} given'.format(nreq, ngiv, sym.proc.name), sym.loc) else: for a, at in zip(sym.args, ptypes): if not equalTypes(a.typ, at): - self.diag.error('Got {0}, expected {1}'.format(a.typ, at), a.loc) + self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc) # determine return type: sym.typ = sym.proc.typ.returntype else: @@ -57,29 +63,29 @@ elif type(sym.val) is bool: sym.typ = boolType else: - self.diag.error('Unknown literal type', sym.loc) + self.error('Unknown literal type', sym.loc) elif type(sym) is Binop: if sym.op in ['+', '-', '*', '/']: if equalTypes(sym.a.typ, sym.b.typ): if equalTypes(sym.a.typ, intType): sym.typ = sym.a.typ else: - self.diag.error('Can only add integers', sym.loc) + self.error('Can only add integers', sym.loc) sym.typ = intType else: # assume void here? TODO: throw exception! sym.typ = intType - self.diag.error('Types unequal', sym.loc) + self.error('Types unequal', sym.loc) elif sym.op in ['>', '<', '==', '<=', '>=']: sym.typ = boolType if not equalTypes(sym.a.typ, sym.b.typ): - self.diag.error('Types unequal', sym.loc) + self.error('Types unequal', sym.loc) elif sym.op in ['or', 'and']: sym.typ = boolType if not equalTypes(sym.a.typ, boolType): - self.diag.error('Must be {0}'.format(boolType), sym.a.loc) + self.error('Must be {0}'.format(boolType), sym.a.loc) if not equalTypes(sym.b.typ, boolType): - self.diag.error('Must be {0}'.format(boolType), sym.b.loc) + self.error('Must be {0}'.format(boolType), sym.b.loc) else: sym.typ = voidType print('unknown binop', sym.op) @@ -89,10 +95,10 @@ pass elif type(sym) is Constant: if not equalTypes(sym.typ, sym.value.typ): - self.diag.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc) + self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc) elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType]: pass else: - print('Unknown type check', sym) + raise Exception('Unknown type check {0}'.format(sym))