view 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 source

"""
AST nodes for the c3 language.
"""

class Node:
   pass 

class Designator(Node):
   def __init__(self, tname):
      self.tname = tname
   def __repr__(self):
      return 'DESIGNATOR {0}'.format(self.tname)

"""
Type classes
"""

class Type(Node):
   def isType(self, b):
      return isType(self, b)

class BaseType(Type):
  def __init__(self, name):
    self.name = name
  def __repr__(self):
    return '[TYPE {0}]'.format(self.name)

class FunctionType(Type):
   def __init__(self, parameters, returntype):
      self.parameters = parameters
      self.returntype = returntype
   def __repr__(self):
      return '[FUNCTYPE {0} RET {1}]'.format(self.parameters, self.returntype)

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)

# Variables, parameters, local variables, constants:
class Symbol(Node):
   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, 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, 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} 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):
      super().__init__(name)
      self.body = block
      self.typ = typ
   def __repr__(self):
      return 'PROCEDURE {0} {1}'.format(self.name, self.typ)

# Operations / Expressions:
class Unop(Node):
   def __init__(self, a, op):
      self.a = a
      self.op = op 
   def __repr__(self):
      return 'UNOP {0}'.format(self.op)

class Binop(Node):
   def __init__(self, a, op, b):
      self.a = a
      self.b = b
      self.op = op # Operation: '+', '-', '*', '/', 'mod'
   def __repr__(self):
      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):
      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):
   def __init__(self, name):
      self.name = name
   def __repr__(self):
      return 'PACKAGE {0}'.format(self.name)

# Statements
class CompoundStatement(Node):
   def __init__(self, statements):
      self.statements = statements
   def __repr__(self):
      return 'COMPOUND STATEMENT'

class EmptyStatement(Node):
   def __repr__(self):
      return 'NOP'

class ReturnStatement(Node):
   def __init__(self, expr):
      self.expr = expr
   def __repr__(self):
      return 'RETURN STATEMENT'

class Assignment(Node):
   def __init__(self, lval, rval):
      self.lval = lval
      self.rval = rval
   def __repr__(self):
      return 'ASSIGNMENT'

class ProcedureCall(Node):
  def __init__(self, proc, args):
    self.proc = proc
    self.args = args
  def __repr__(self):
    return 'CALL {0} '.format(self.proc)

class IfStatement(Node):
   def __init__(self, condition, truestatement, falsestatement):
      self.condition = condition
      self.truestatement = truestatement
      self.falsestatement = falsestatement
   def __repr__(self):
      return 'IF-statement'

class WhileStatement(Node):
   def __init__(self, condition, statements):
      self.condition = condition
      self.dostatements = statements
   def __repr__(self):
      return 'WHILE-statement'