view python/c3/astnodes.py @ 213:003c8a976fff

Merge of semantics and parser again ..
author Windel Bouwman
date Fri, 05 Jul 2013 11:18:48 +0200
parents 62386bcee1ba
children c1ccb1cb4cef
line wrap: on
line source

"""
AST (abstract syntax tree) nodes for the c3 language.
The tree is build by the parser.
Then it is checked
Finally code is generated from it.
"""

class Node:
   pass 

# Modules
class Package(Node):
    def __init__(self, name, loc):
        self.name = name
        self.loc = loc
        self.declarations = []
    def __repr__(self):
        return 'PACKAGE {}'.format(self.name)

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

"""
Type classes

types must be comparable.

There are the following types:
- base type
- struct type
- pointer type
- typedef type
- function type
"""

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 '{}'.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 PointerType(Type):
    def __init__(self, ptype):
        self.ptype = ptype

class StructureType(Type):
    def __init__(self, mems):
        self.mems = mems

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):
      super().__init__(name)
      self.typ = typ
      self.ival = None
      self.isLocal = False
      self.isReadOnly = False
      self.isParameter = False
   def __repr__(self):
      return '{}'.format(self.name)

# Procedure types
class Function(Symbol):
    """ Actual implementation of a function """
    def __init__(self, name, loc):
        super().__init__(name)
        self.loc = loc

    def __repr__(self):
        return '{}'.format(self.name)

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

class Binop(Node):
   def __init__(self, a, op, b, loc):
      self.a = a
      self.b = b
      self.op = op # Operation: '+', '-', '*', '/', 'mod'
      self.loc = loc
   def __repr__(self):
      return 'BINOP {}'.format(self.op)

class VariableUse(Node):
   def __init__(self, target, loc):
      self.target = target
      self.loc = loc
   def __repr__(self):
      nm = self.target.name if hasattr(self.target, 'name') else ''
      return 'VAR USE {}'.format(nm)

class Literal(Node):
   def __init__(self, val, loc):
      self.val = val
      self.loc = loc
   def __repr__(self):
      return 'LITERAL {}'.format(self.val)


# 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, loc):
      self.lval = lval
      self.rval = rval
      self.loc = loc
   def __repr__(self):
      return 'ASSIGNMENT'

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

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

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