Mercurial > lcfOS
view python/c3/astnodes.py @ 220:3f6c30a5d234
Major change in expression parsing to enable pointers and structs
author | Windel Bouwman |
---|---|
date | Sat, 06 Jul 2013 21:32:20 +0200 |
parents | 8b2e5f3cd579 |
children | 848c4b15fd0b |
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 - 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 def __repr__(self): return '({}*)'.format(self.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 'Var {}'.format(self.name) # 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 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.name if hasattr(self.target, 'name') else '' 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): pass class CompoundStatement(Statement): def __init__(self, statements): self.statements = statements def __repr__(self): return 'COMPOUND STATEMENT' class EmptyStatement(Statement): def __repr__(self): return 'NOP' class ReturnStatement(Statement): def __init__(self, expr, loc): self.expr = expr self.loc = loc def __repr__(self): return 'RETURN STATEMENT' class Assignment(Node): def __init__(self, lval, rval, loc): assert isinstance(lval, Node) assert isinstance(rval, Node) assert isinstance(loc, SourceLocation) self.lval = lval self.rval = rval self.loc = loc def __repr__(self): return 'ASSIGNMENT' class IfStatement(Statement): 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(Statement): def __init__(self, condition, statement, loc): self.condition = condition self.statement = statement self.loc = loc def __repr__(self): return 'WHILE-statement'