diff python/c3/astnodes.py @ 230:88a1e0baef65

Added some tests for IR-code
author Windel Bouwman
date Sat, 13 Jul 2013 19:53:44 +0200
parents 7f18ed9b6b7e
children e41e4109addd
line wrap: on
line diff
--- a/python/c3/astnodes.py	Sat Jul 13 11:13:01 2013 +0200
+++ b/python/c3/astnodes.py	Sat Jul 13 19:53:44 2013 +0200
@@ -65,10 +65,13 @@
     def __repr__(self):
         return '({}*)'.format(self.ptype)
 
+
 class StructField:
     def __init__(self, name, typ):
         self.name = name
         self.typ = typ
+        self.offset = 0
+
 
 class StructureType(Type):
     def __init__(self, mems):
@@ -76,17 +79,27 @@
         for mem in mems:
             assert type(mem) is StructField
             assert type(mem.name) is str
+
     def hasField(self, name):
         for mem in self.mems:
             if name == mem.name:
                 return True
         return False
+
     def fieldType(self, name):
+        return self.findField(name).typ
+
+    def fieldOffset(self, name):
+        return self.findField(name).offset
+
+    def findField(self, name):
         for mem in self.mems:
             if name == mem.name:
-                return mem.typ
-        raise Exception()
+                return mem
+        raise KeyError(name)
 
+    def __repr__(self):
+        return 'STRUCT'
 
 class DefinedType(Type):
     def __init__(self, name, typ, loc):
@@ -94,15 +107,10 @@
         self.name = name
         self.typ = typ
         self.loc = loc
+
     def __repr__(self):
         return 'Named type {0} of type {1}'.format(self.name, self.typ)
 
-class TypeCast(Node):
-    def __init__(self, to_type, x):
-        self.to_type = to_type
-        self.a = x
-    def __repr__(self):
-        return 'TYPECAST'
 
 # Variables, parameters, local variables, constants:
 class Symbol(Node):
@@ -147,69 +155,82 @@
 
 # Operations / Expressions:
 class Expression(Node):
-    pass
+    def __init__(self, loc):
+        self.loc = loc
+
 
 class Deref(Expression):
     def __init__(self, ptr, loc):
+        super().__init__(loc)
         assert isinstance(ptr, Expression)
         self.ptr = ptr
-        self.loc = loc
     def __repr__(self):
       return 'DEREF {}'.format(self.ptr)
 
+
+class TypeCast(Expression):
+    def __init__(self, to_type, x, loc):
+        super().__init__(loc)
+        self.to_type = to_type
+        self.a = x
+    def __repr__(self):
+        return 'TYPECAST {}'.format(self.to_type)
+
+
 class FieldRef(Expression):
     def __init__(self, base, field, loc):
+        super().__init__(loc)
         assert isinstance(base, Expression)
         assert isinstance(field, str)
         self.base = base
         self.field = field
-        self.loc = loc
     def __repr__(self):
       return 'FIELD {}.{}'.format(self.base, self.field)
 
+
 class Unop(Expression):
     def __init__(self, op, a, loc):
+        super().__init__(loc)
         assert isinstance(a, Expression)
         assert isinstance(op, str)
         self.a = a
-        self.op = op 
-        self.loc = loc
+        self.op = op
     def __repr__(self):
       return 'UNOP {}'.format(self.op)
 
 class Binop(Expression):
     def __init__(self, a, op, b, loc):
+        super().__init__(loc)
         assert isinstance(a, Expression), type(a)
         assert isinstance(b, Expression)
         assert isinstance(op, str)
         self.a = a
         self.b = b
         self.op = op # Operation: '+', '-', '*', '/', 'mod'
-        self.loc = loc
 
     def __repr__(self):
         return 'BINOP {}'.format(self.op)
 
 class VariableUse(Expression):
     def __init__(self, target, loc):
+        super().__init__(loc)
         self.target = target
-        self.loc = loc
     def __repr__(self):
         nm = self.target
         return 'VAR USE {}'.format(nm)
 
 class Literal(Expression):
     def __init__(self, val, loc):
+        super().__init__(loc)
         self.val = val
-        self.loc = loc
     def __repr__(self):
         return 'LITERAL {}'.format(self.val)
 
 class FunctionCall(Expression):
     def __init__(self, proc, args, loc):
+        super().__init__(loc)
         self.proc = proc
         self.args = args
-        self.loc = loc
     def __repr__(self):
         return 'CALL {0} '.format(self.proc)