Mercurial > lcfOS
view python/c3/astnodes.py @ 289:bd2593de3ff8
Semifix burn2
author | Windel Bouwman |
---|---|
date | Thu, 21 Nov 2013 15:46:50 +0100 |
parents | a747a45dcd78 |
children |
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 'MODULE {}'.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) class ImportDesignator(Designator): def __init__(self, tname, vname, loc): super().__init__(tname, loc) self.vname = vname def __repr__(self): return 'IMPORT DESIGNATOR {}:{}'.format(self.tname, self.vname) """ 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 - function type """ class Type(Node): pass 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): """ A type that points to data of some other 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): """ A named type indicating another 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) class LocalVariable(Variable): def __init__(self, name, typ): super().__init__(name, typ) self.isLocal = True class FormalParameter(Variable): def __init__(self, name, typ): super().__init__(name, typ) self.isParameter = True # 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): return 'VAR USE {}'.format(self.target) 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'