view python/c3/astnodes.py @ 228:7f18ed9b6b7e

Removal of emptystatement class
author Windel Bouwman
date Sat, 13 Jul 2013 11:12:24 +0200
parents 82dfe6a32717
children 88a1e0baef65
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.
"""

from ppci import SourceLocation

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 -> basic type (built in)
- struct type -> a composite type that contains a list of named fields 
            of other types
- pointer type -> a type that points to some other type
- typedef type -> a named type indicating another 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):
        assert isinstance(ptype, Type) or isinstance(ptype, Designator)
        self.ptype = ptype
    def __repr__(self):
        return '({}*)'.format(self.ptype)

class StructField:
    def __init__(self, name, typ):
        self.name = name
        self.typ = typ

class StructureType(Type):
    def __init__(self, mems):
        self.mems = mems
        for mem in mems:
            assert type(mem) is StructField
            assert type(mem.name) is str
    def hasField(self, name):
        for mem in self.mems:
            if name == mem.name:
                return True
        return False
    def fieldType(self, name):
        for mem in self.mems:
            if name == mem.name:
                return mem.typ
        raise Exception()


class DefinedType(Type):
    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):
        self.to_type = to_type
        self.a = x
    def __repr__(self):
        return 'TYPECAST'

# 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 'Var {} [{}]'.format(self.name, self.typ)

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

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

# Operations / Expressions:
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)
        assert isinstance(op, str)
        self.a = a
        self.op = op 
        self.loc = loc
    def __repr__(self):
      return 'UNOP {}'.format(self.op)

class Binop(Expression):
    def __init__(self, a, op, b, loc):
        assert isinstance(a, Expression), type(a)
        assert isinstance(b, Expression)
        assert isinstance(op, str)
        self.a = a
        self.b = b
        self.op = op # Operation: '+', '-', '*', '/', 'mod'
        self.loc = loc

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

class VariableUse(Expression):
    def __init__(self, target, loc):
        self.target = target
        self.loc = loc
    def __repr__(self):
        nm = self.target
        return 'VAR USE {}'.format(nm)

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

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

# Statements
class Statement(Node):
    def __init__(self, loc):
        self.loc = loc


class CompoundStatement(Statement):
    def __init__(self, statements):
        self.statements = statements
        for s in self.statements:
            assert isinstance(s, Statement)

    def __repr__(self):
        return 'COMPOUND STATEMENT'


class ReturnStatement(Statement):
    def __init__(self, expr, loc):
        super().__init__(loc)
        self.expr = expr

    def __repr__(self):
        return 'RETURN STATEMENT'

class Assignment(Statement):
    def __init__(self, lval, rval, loc):
        super().__init__(loc)
        assert isinstance(lval, Node)
        assert isinstance(rval, Node)
        self.lval = lval
        self.rval = rval

    def __repr__(self):
        return 'ASSIGNMENT'

class ExpressionStatement(Statement):
    def __init__(self, ex, loc):
        super().__init__(loc)
        self.ex = ex

    def __repr__(self):
        return 'Epression'


class IfStatement(Statement):
    def __init__(self, condition, truestatement, falsestatement, loc):
        super().__init__(loc)
        self.condition = condition
        self.truestatement = truestatement
        self.falsestatement = falsestatement

    def __repr__(self):
        return 'IF-statement'


class WhileStatement(Statement):
    def __init__(self, condition, statement, loc):
        super().__init__(loc)
        self.condition = condition
        self.statement = statement

    def __repr__(self):
        return 'WHILE-statement'