diff python/c3/typecheck.py @ 166:da0087b82fbe

Improved type checking
author Windel Bouwman
date Fri, 22 Mar 2013 16:15:31 +0100
parents 598d3888a11c
children 0b5b2ee6b435
line wrap: on
line diff
--- a/python/c3/typecheck.py	Fri Mar 22 15:12:38 2013 +0100
+++ b/python/c3/typecheck.py	Fri Mar 22 16:15:31 2013 +0100
@@ -22,7 +22,7 @@
          pass
       elif type(sym) in [IfStatement, WhileStatement]:
          if not equalTypes(sym.condition.typ, boolType):
-            self.diag.error('Condition must be a boolean expression', sym.condition.loc)
+            self.diag.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)
@@ -30,7 +30,8 @@
          pass
       elif type(sym) is ProcedureCall:
          # Check arguments:
-
+         if sym.proc:
+            pass
          # determine return type:
          sym.typ = sym.proc.typ.returntype
       elif type(sym) is VariableUse:
@@ -43,6 +44,8 @@
             sym.typ = intType
          elif type(sym.val) is float:
             sym.typ = doubleType
+         elif type(sym.val) is bool:
+            sym.typ = boolType
          else:
             self.diag.error('Unknown literal type', sym.loc)
       elif type(sym) is Binop:
@@ -57,6 +60,12 @@
             sym.typ = boolType
             if not equalTypes(sym.a.typ, sym.b.typ):
                self.diag.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)
+            if not equalTypes(sym.b.typ, boolType):
+               self.diag.error('Must be {0}'.format(boolType), sym.b.loc)
          else:
             sym.typ = voidType
             print('unknown binop', sym.op)
@@ -64,7 +73,11 @@
          # check initial value type:
          # TODO
          pass
-      elif type(sym) in [EmptyStatement, CompoundStatement, Package]:
+      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)
+         
+      elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function]:
          pass
       else:
          print('Unknown type check', sym)