Mercurial > lcfOS
diff 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 diff
--- a/python/c3/astnodes.py Sun Jun 30 19:00:41 2013 +0200 +++ b/python/c3/astnodes.py Fri Jul 05 11:18:48 2013 +0200 @@ -1,18 +1,40 @@ """ -AST nodes for the c3 language. +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): - self.tname = tname - def __repr__(self): - return 'DESIGNATOR {}'.format(self.tname) + 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): @@ -33,6 +55,14 @@ 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 @@ -42,28 +72,28 @@ # Variables, parameters, local variables, constants: class Symbol(Node): - def __init__(self, name): + def __init__(self, name): self.name = name self.refs = [] - def addRef(self, r): + def addRef(self, r): self.refs.append(r) - @property - def References(self): + @property + def References(self): return self.refs class Constant(Symbol): - def __init__(self, name, typ, value): + def __init__(self, name, typ, value): super().__init__(name) self.typ = typ self.value = value - def __repr__(self): + def __repr__(self): return 'CONSTANT {0} = {1}'.format(self.name, self.value) class Variable(Symbol): - def __init__(self, name, typ, ival=None): + def __init__(self, name, typ): super().__init__(name) self.typ = typ - self.ival = ival + self.ival = None self.isLocal = False self.isReadOnly = False self.isParameter = False @@ -72,11 +102,13 @@ # Procedure types class Function(Symbol): - """ Actual implementation of a function """ - def __init__(self, name): - super().__init__(name) - def __repr__(self): - return '{}'.format(self.name) + """ 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): @@ -87,32 +119,29 @@ return 'UNOP {}'.format(self.op) class Binop(Node): - def __init__(self, a, op, b): + 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): + 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): + def __init__(self, val, loc): self.val = val + self.loc = loc def __repr__(self): return 'LITERAL {}'.format(self.val) -# Modules -class Package(Node): - def __init__(self, name): - self.name = name - def __repr__(self): - return 'PACKAGE {}'.format(self.name) # Statements class CompoundStatement(Node): @@ -132,31 +161,35 @@ return 'RETURN STATEMENT' class Assignment(Node): - def __init__(self, lval, rval): + 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): + 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): + 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): + def __init__(self, condition, statement, loc): self.condition = condition - self.dostatement = statement + self.statement = statement + self.loc = loc def __repr__(self): return 'WHILE-statement'