view python/c3/astnodes.py @ 193:f091e7d70996

Added even more checks
author Windel Bouwman
date Sun, 26 May 2013 23:58:59 +0200
parents 46d62dadd61b
children 62386bcee1ba
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 '{0}'.format(self.name)

class FunctionType(Type):
   def __init__(self, parametertypes, returntype):
      self.parametertypes = parametertypes
      self.returntype = returntype
   def __repr__(self):
      params = ', '.join([str(v) for v in self.parametertypes])
      return '{1} f({0})'.format(params, 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 '{0}'.format(self.name)

# Procedure types
class Function(Symbol):
   """ Actual implementation of a function """
   def __init__(self, name):
      super().__init__(name)
   def __repr__(self):
      return '{0}'.format(self.name)

# 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):
      return 'BINOP {0}'.format(self.op)

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 FunctionCall(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, statement):
      self.condition = condition
      self.dostatement = statement
   def __repr__(self):
      return 'WHILE-statement'