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

Fixed tests
author Windel Bouwman
date Fri, 12 Jul 2013 17:42:39 +0200
parents 240111e0456f
children 7f18ed9b6b7e
line wrap: on
line diff
--- a/python/c3/typecheck.py	Fri Jul 12 17:25:31 2013 +0200
+++ b/python/c3/typecheck.py	Fri Jul 12 17:42:39 2013 +0200
@@ -15,6 +15,15 @@
             return a.name == b.name
         elif type(a) is PointerType:
             return equalTypes(a.ptype, b.ptype)
+        elif type(a) is StructureType:
+            if len(a.mems) != len(b.mems):
+                return False
+            for amem, bmem in zip(a.mems, b.mems):
+                if not equalTypes(amem.typ, bmem.typ):
+                    return False
+            return True
+        else:
+            raise Exception('Type compare not implemented')
     return False
 
 def canCast(fromT, toT):
@@ -118,34 +127,38 @@
                 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):
-               if equalTypes(sym.a.typ, intType):
-                  sym.typ = sym.a.typ
-               else:
-                  self.error('Can only add integers', sym.loc)
-                  sym.typ = intType
+            sym.lvalue = False
+            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.error('Can only add integers', sym.loc)
+                      sym.typ = intType
+                else:
+                   # assume void here? TODO: throw exception!
+                   sym.typ = intType
+                   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 {} != {}'.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):
+                   self.error('Must be {0}'.format(boolType), sym.a.loc)
+                if not equalTypes(sym.b.typ, boolType):
+                   self.error('Must be {0}'.format(boolType), sym.b.loc)
+            elif sym.op in ['|', '&']:
+                sym.typ = intType
+                sym.lvalue = False
+                if equalTypes(sym.a.typ, sym.b.typ):
+                    if not equalTypes(sym.a.typ, intType):
+                        self.error('Can only add integers', sym.loc)
+                else:
+                   self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc)
             else:
-               # assume void here? TODO: throw exception!
-               sym.typ = intType
-               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 {} != {}'.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):
-               self.error('Must be {0}'.format(boolType), sym.a.loc)
-            if not equalTypes(sym.b.typ, boolType):
-               self.error('Must be {0}'.format(boolType), sym.b.loc)
-         elif sym.op in ['|', '&']:
-                sym.typ = intType
-                raise Exception('Not implemented')
-                # TODO: elaborate?
-         else:
-            raise Exception('Unknown binop {0}'.format(sym.op))
+                raise Exception('Unknown binop {0}'.format(sym.op))
         elif type(sym) is Variable:
          # check initial value type:
          # TODO