Mercurial > lcfOS
diff python/ppci/c3/astnodes.py @ 306:b145f8e6050b
Start on c3 rewrite
author | Windel Bouwman |
---|---|
date | Mon, 09 Dec 2013 19:00:21 +0100 |
parents | 6753763d3bec |
children | e609d5296ee9 |
line wrap: on
line diff
--- a/python/ppci/c3/astnodes.py Fri Dec 06 13:50:38 2013 +0100 +++ b/python/ppci/c3/astnodes.py Mon Dec 09 19:00:21 2013 +0100 @@ -9,11 +9,14 @@ class Node: + """ Base class of all nodes in a AST """ pass # Variables, parameters, local variables, constants and named types: class Symbol(Node): + """ Symbol is the base class for all named things like variables, + functions, constants and types and modules """ def __init__(self, name): self.name = name self.refs = [] @@ -38,47 +41,21 @@ 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): + """ Base class of all types """ pass class NamedType(Type, Symbol): + """ Some types are named, for example a user defined type (typedef) + and built in types. That is why this class derives from both Type + and Symbol. """ def __init__(self, name): Symbol.__init__(self, name) class BaseType(NamedType): + """ Built in type """ def __init__(self, name): super().__init__(name) @@ -87,6 +64,7 @@ class FunctionType(Type): + """ Function blueprint, defines argument types and return type """ def __init__(self, parametertypes, returntype): self.parametertypes = parametertypes self.returntype = returntype @@ -99,7 +77,7 @@ 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) + assert isinstance(ptype, Type) or isinstance(ptype, Expression) self.ptype = ptype def __repr__(self): @@ -108,17 +86,17 @@ class StructField: def __init__(self, name, typ): + assert type(name) is str self.name = name self.typ = typ self.offset = 0 class StructureType(Type): + """ Struct type consisting of several named members """ def __init__(self, mems): self.mems = mems - for mem in mems: - assert type(mem) is StructField - assert type(mem.name) is str + assert all(type(mem) is StructField for mem in mems) def hasField(self, name): for mem in self.mems: @@ -168,7 +146,6 @@ def __init__(self, name, typ): super().__init__(name) self.typ = typ - self.ival = None self.isLocal = False self.isReadOnly = False self.isParameter = False @@ -227,7 +204,8 @@ return 'TYPECAST {}'.format(self.to_type) -class FieldRef(Expression): +class Member(Expression): + """ Field reference of some object, can also be package selection """ def __init__(self, base, field, loc): super().__init__(loc) assert isinstance(base, Expression) @@ -236,10 +214,11 @@ self.field = field def __repr__(self): - return 'FIELD {}.{}'.format(self.base, self.field) + return 'MEMBER {}.{}'.format(self.base, self.field) class Unop(Expression): + """ Operation on one operand """ def __init__(self, op, a, loc): super().__init__(loc) assert isinstance(a, Expression) @@ -252,6 +231,7 @@ class Binop(Expression): + """ Expression taking two operands and one operator """ def __init__(self, a, op, b, loc): super().__init__(loc) assert isinstance(a, Expression), type(a) @@ -265,16 +245,19 @@ return 'BINOP {}'.format(self.op) -class VariableUse(Expression): +class Identifier(Expression): + """ Reference to some identifier, can be anything from package, variable + function or type, any named thing! """ def __init__(self, target, loc): super().__init__(loc) self.target = target def __repr__(self): - return 'VAR USE {}'.format(self.target) + return 'ID {}'.format(self.target) class Literal(Expression): + """ Constant value or string """ def __init__(self, val, loc): super().__init__(loc) self.val = val @@ -284,6 +267,7 @@ class FunctionCall(Expression): + """ Call to a some function """ def __init__(self, proc, args, loc): super().__init__(loc) self.proc = proc @@ -295,11 +279,22 @@ # Statements class Statement(Node): + """ Base class of all statements """ def __init__(self, loc): self.loc = loc +class EmptyStatement(Statement): + """ Empty statement which does nothing! """ + def __init__(self): + super().__init__(None) + + def __repr__(self): + return 'NOP' + + class CompoundStatement(Statement): + """ Statement consisting of a sequence of other statements """ def __init__(self, statements): super().__init__(None) self.statements = statements @@ -322,8 +317,8 @@ class Assignment(Statement): def __init__(self, lval, rval, loc): super().__init__(loc) - assert isinstance(lval, Node) - assert isinstance(rval, Node) + assert isinstance(lval, Expression) + assert isinstance(rval, Expression) self.lval = lval self.rval = rval