view python/c3/astnodes.py @ 271:cf7d5fb7d9c8

Reorganization
author Windel Bouwman
date Tue, 20 Aug 2013 18:56:02 +0200
parents 6ed3d3a82a63
children e64bae57cda8
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 = []
        self.imports = []
    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
        self.offset = 0


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):
        return self.findField(name).typ

    def fieldOffset(self, name):
        return self.findField(name).offset

    def findField(self, name):
        for mem in self.mems:
            if name == mem.name:
                return mem
        raise KeyError(name)

    def __repr__(self):
        return 'STRUCT'

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)


# 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):
    def __init__(self, loc):
        self.loc = loc


class Deref(Expression):
    def __init__(self, ptr, loc):
        super().__init__(loc)
        assert isinstance(ptr, Expression)
        self.ptr = ptr
    def __repr__(self):
      return 'DEREF {}'.format(self.ptr)


class TypeCast(Expression):
    def __init__(self, to_type, x, loc):
        super().__init__(loc)
        self.to_type = to_type
        self.a = x
    def __repr__(self):
        return 'TYPECAST {}'.format(self.to_type)


class FieldRef(Expression):
    def __init__(self, base, field, loc):
        super().__init__(loc)
        assert isinstance(base, Expression)
        assert isinstance(field, str)
        self.base = base
        self.field = field
    def __repr__(self):
      return 'FIELD {}.{}'.format(self.base, self.field)


class Unop(Expression):
    def __init__(self, op, a, loc):
        super().__init__(loc)
        assert isinstance(a, Expression)
        assert isinstance(op, str)
        self.a = a
        self.op = op
    def __repr__(self):
      return 'UNOP {}'.format(self.op)

class Binop(Expression):
    def __init__(self, a, op, b, loc):
        super().__init__(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'

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

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

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

class FunctionCall(Expression):
    def __init__(self, proc, args, loc):
        super().__init__(loc)
        self.proc = proc
        self.args = args
    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):
        super().__init__(None)
        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'