diff python/c3/astnodes.py @ 163:8104fc8b5e90

Added visitor to c3
author Windel Bouwman
date Mon, 18 Mar 2013 20:13:57 +0100
parents 9683a4cd848f
children 598d3888a11c
line wrap: on
line diff
--- a/python/c3/astnodes.py	Sun Mar 10 11:36:55 2013 +0100
+++ b/python/c3/astnodes.py	Mon Mar 18 20:13:57 2013 +0100
@@ -3,19 +3,7 @@
 """
 
 class Node:
-   location = None
-   def getChildren(self):
-      children = []
-      members = dir(self)
-      for member in members:
-         member = getattr(self, member)
-         if isinstance(member, Node):
-            children.append(member)
-         elif type(member) is list:
-            for mi in member:
-               if isinstance(mi, Node):
-                  children.append(mi)
-      return children
+   pass 
 
 class Designator(Node):
    def __init__(self, tname):
@@ -26,32 +14,6 @@
 """
 Type classes
 """
-def isType(a, b):
-   """ Compare types a and b and check if they are equal """
-   if type(a) is type(b):
-      if type(a) is BaseType:
-         return (a.name == b.name) and (a.size == b.size)
-      elif type(a) is ProcedureType:
-         if len(a.parameters) != len(b.parameters):
-            print('Number of parameters does not match')
-            return False
-         for aparam, bparam in zip(a.parameters, b.parameters):
-            if not isType(aparam.typ, bparam.typ):
-               print('Parameter {0} does not match parameter {1}'.format(aparam, bparam))
-               return False
-         if a.result is None:
-            # TODO: how to handle a None return type??
-            pass
-         if not isType(a.result, b.result):
-            print('Procedure return value mismatch {0} != {1}'.format(a.result, b.result))
-            return False
-         return True
-      else:
-         print(a)
-         print(b)
-         Error('Not implemented {0}'.format(a))
-   else:
-      return False
 
 class Type(Node):
    def isType(self, b):
@@ -68,7 +30,7 @@
       self.parameters = parameters
       self.returntype = returntype
    def __repr__(self):
-      return '[PROCTYPE {0} RET {1}]'.format(self.parameters, self.returntype)
+      return '[FUNCTYPE {0} RET {1}]'.format(self.parameters, self.returntype)
 
 class DefinedType(Type):
    def __init__(self, name, typ):
@@ -79,36 +41,45 @@
 
 # Variables, parameters, local variables, constants:
 class Symbol(Node):
-   pass
+   def __init__(self, name):
+      self.name = name
+      self.refs = []
+   def addRef(self, r):
+      self.refs.append(r)
+   @property
+   def References(self):
+      return self.refs
 
 class Constant(Symbol):
-   def __init__(self, value, name=None):
-      self.name = name
+   def __init__(self, name, typ, value):
+      super().__init__(name)
+      self.typ = typ
       self.value = value
    def __repr__(self):
       return 'CONSTANT {0} = {1}'.format(self.name, self.value)
 
 class Variable(Symbol):
-   def __init__(self, name, typ):
-      self.name = name
+   def __init__(self, name, typ, ival=None):
+      super().__init__(name)
       self.typ = typ
+      self.ival = ival
       self.isLocal = False
       self.isReadOnly = False
       self.isParameter = False
    def __repr__(self):
-      return 'VAR {0} : {1}'.format(self.name, self.typ)
+      return 'VAR {0} : {1} usage: {2}'.format(self.name, self.typ, self.References)
 
 # Procedure types
 class Function(Symbol):
    """ Actual implementation of a function """
    def __init__(self, name, typ=None, block=None):
-      self.name = name
+      super().__init__(name)
       self.body = block
       self.typ = typ
    def __repr__(self):
       return 'PROCEDURE {0} {1}'.format(self.name, self.typ)
 
-# Operations:
+# Operations / Expressions:
 class Unop(Node):
    def __init__(self, a, op):
       self.a = a
@@ -122,13 +93,21 @@
       self.b = b
       self.op = op # Operation: '+', '-', '*', '/', 'mod'
    def __repr__(self):
-      return 'BINOP {0}'.format(self.op)
+      typ = self.typ if hasattr(self, 'typ') else ''
+      return 'BINOP {0} {1}'.format(self.op, typ)
 
 class VariableUse(Node):
    def __init__(self, target):
       self.target = target
    def __repr__(self):
-      return 'VAR USE {0}'.format(self.target)
+      nm = self.target.name if hasattr(self.target, 'name') else ''
+      return 'VAR USE {0}'.format(nm)
+
+class Literal(Node):
+   def __init__(self, val):
+      self.val = val
+   def __repr__(self):
+      return 'LITERAL {0}'.format(self.val)
 
 # Modules
 class Package(Node):