diff python/c3/astnodes.py @ 225:1c7364bd74c7

Fixed pointer deref
author Windel Bouwman
date Thu, 11 Jul 2013 07:42:30 +0200
parents c3f1ce8b638f
children 240111e0456f
line wrap: on
line diff
--- a/python/c3/astnodes.py	Tue Jul 09 17:59:15 2013 +0200
+++ b/python/c3/astnodes.py	Thu Jul 11 07:42:30 2013 +0200
@@ -59,6 +59,7 @@
 
 class PointerType(Type):
     def __init__(self, ptype):
+        assert isinstance(ptype, Type) or isinstance(ptype, Designator)
         self.ptype = ptype
     def __repr__(self):
         return '({}*)'.format(self.ptype)
@@ -66,13 +67,26 @@
 class StructureType(Type):
     def __init__(self, mems):
         self.mems = mems
+    def hasField(self, name):
+        for fn, ft in self.mems:
+            if name == fn:
+                return True
+        return False
+    def fieldType(self, name):
+        for fn, ft in self.mems:
+            if name == fn:
+                return ft
+        raise Exception()
+
 
 class DefinedType(Type):
-   def __init__(self, name, typ):
-      self.name = name
-      self.typ = typ
-   def __repr__(self):
-      return 'Named type {0} of type {1}'.format(self.name, self.typ)
+    def __init__(self, name, typ, loc):
+        assert isinstance(name, str)
+        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):
@@ -109,7 +123,7 @@
       self.isReadOnly = False
       self.isParameter = False
    def __repr__(self):
-      return 'Var {}'.format(self.name)
+      return 'Var {} [{}]'.format(self.name, self.typ)
 
 # Procedure types
 class Function(Symbol):
@@ -126,6 +140,24 @@
 class Expression(Node):
     pass
 
+class Deref(Expression):
+    def __init__(self, ptr, loc):
+        assert isinstance(ptr, Expression)
+        self.ptr = ptr
+        self.loc = loc
+    def __repr__(self):
+      return 'DEREF {}'.format(self.ptr)
+
+class FieldRef(Expression):
+    def __init__(self, base, field, 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):
         assert isinstance(a, Expression)