changeset 227:82dfe6a32717

Fixed tests
author Windel Bouwman
date Fri, 12 Jul 2013 17:42:39 +0200
parents 240111e0456f
children 7f18ed9b6b7e
files python/c3/analyse.py python/c3/astnodes.py python/c3/codegenerator.py python/c3/parser.py python/c3/typecheck.py python/testc3.py
diffstat 6 files changed, 62 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/python/c3/analyse.py	Fri Jul 12 17:25:31 2013 +0200
+++ b/python/c3/analyse.py	Fri Jul 12 17:42:39 2013 +0200
@@ -84,7 +84,7 @@
             return t
         elif type(t) is Designator:
             t = self.resolveDesignator(t, scope)
-            return self.resolveType(t)
+            return self.resolveType(t, scope)
         elif isinstance(t, Type):
             # Already resolved??
             return t
--- a/python/c3/astnodes.py	Fri Jul 12 17:25:31 2013 +0200
+++ b/python/c3/astnodes.py	Fri Jul 12 17:42:39 2013 +0200
@@ -64,20 +64,26 @@
     def __repr__(self):
         return '({}*)'.format(self.ptype)
 
+class StructField:
+    def __init__(self, name, typ):
+        self.name = name
+        self.typ = typ
+
 class StructureType(Type):
     def __init__(self, mems):
         self.mems = mems
-        for ft, fn in mems:
-            assert type(fn) is str
+        for mem in mems:
+            assert type(mem) is StructField
+            assert type(mem.name) is str
     def hasField(self, name):
-        for ft, fn in self.mems:
-            if name == fn:
+        for mem in self.mems:
+            if name == mem.name:
                 return True
         return False
     def fieldType(self, name):
-        for ft, fn in self.mems:
-            if name == fn:
-                return ft
+        for mem in self.mems:
+            if name == mem.name:
+                return mem.typ
         raise Exception()
 
 
--- a/python/c3/codegenerator.py	Fri Jul 12 17:25:31 2013 +0200
+++ b/python/c3/codegenerator.py	Fri Jul 12 17:42:39 2013 +0200
@@ -166,6 +166,11 @@
             ins = ir.Load(addr, tmp)
             self.builder.addIns(ins)
             return tmp
+        elif type(expr) is astnodes.FieldRef:
+            tmp = self.builder.newTmp('struct_mem' + expr.field)
+            #ins = ir.BinaryOperator(
+            # TODO: add offset?
+            return tmp
         elif type(expr) is astnodes.Literal:
             tmp = self.builder.newTmp()
             ins = ir.ImmLoad(tmp, expr.val)
--- a/python/c3/parser.py	Fri Jul 12 17:25:31 2013 +0200
+++ b/python/c3/parser.py	Fri Jul 12 17:42:39 2013 +0200
@@ -90,10 +90,10 @@
             while self.Peak != '}':
                 mem_t = self.parseTypeSpec()
                 mem_n = self.Consume('ID').val
-                mems.append((mem_t, mem_n))
+                mems.append(astnodes.StructField(mem_n, mem_t))
                 while self.hasConsumed(','):
                     mem_n = self.Consume('ID').val
-                    mems.append((mem_t, mem_n))
+                    mems.append(astnodes.StructField(mem_n, mem_t))
                 self.Consume(';')
             self.Consume('}')
             theT = astnodes.StructureType(mems)
--- 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
--- a/python/testc3.py	Fri Jul 12 17:25:31 2013 +0200
+++ b/python/testc3.py	Fri Jul 12 17:42:39 2013 +0200
@@ -281,7 +281,7 @@
          type my_struct* my_sptr;
          var int* pa;
 
-         function void t(int a, double b, my_sptr x)
+         function void t(int a, int b, my_sptr x)
          {
             var my_struct *msp;