Mercurial > lcfOS
diff python/c3/parser.py @ 296:9417caea2eb3
Directorized some backend files
author | Windel Bouwman |
---|---|
date | Sun, 01 Dec 2013 13:36:58 +0100 |
parents | 6aa721e7b10b |
children |
line wrap: on
line diff
--- a/python/c3/parser.py Thu Nov 28 21:10:32 2013 +0100 +++ b/python/c3/parser.py Sun Dec 01 13:36:58 2013 +0100 @@ -1,6 +1,14 @@ import logging from .lexer import Lexer -from . import astnodes +from .astnodes import FieldRef, Literal, TypeCast, Unop, Binop +from .astnodes import Assignment, ExpressionStatement, CompoundStatement +from .astnodes import ReturnStatement, WhileStatement, IfStatement +from .astnodes import FunctionType, Function, FormalParameter +from .astnodes import StructureType, DefinedType, PointerType +from .astnodes import Constant, Variable +from .astnodes import StructField, Deref +from .astnodes import Package, ImportDesignator +from .astnodes import Designator, VariableUse, FunctionCall from ppci import CompilerError @@ -67,7 +75,7 @@ self.Consume('module') name = self.Consume('ID') self.Consume(';') - self.mod = astnodes.Package(name.val, name.loc) + self.mod = Package(name.val, name.loc) self.currentPart = self.mod while self.Peak != 'END': self.parseTopLevel() @@ -92,9 +100,9 @@ name = self.Consume('ID') if self.hasConsumed(':'): name2 = self.Consume('ID') - return astnodes.ImportDesignator(name.val, name2.val, name.loc) + return ImportDesignator(name.val, name2.val, name.loc) else: - return astnodes.Designator(name.val, name.loc) + return Designator(name.val, name.loc) # Type system def parseTypeSpec(self): @@ -107,18 +115,18 @@ while self.Peak != '}': mem_t = self.parseTypeSpec() mem_n = self.Consume('ID').val - mems.append(astnodes.StructField(mem_n, mem_t)) + mems.append(StructField(mem_n, mem_t)) while self.hasConsumed(','): mem_n = self.Consume('ID').val - mems.append(astnodes.StructField(mem_n, mem_t)) + mems.append(StructField(mem_n, mem_t)) self.Consume(';') self.Consume('}') - theT = astnodes.StructureType(mems) + theT = StructureType(mems) else: theT = self.parseDesignator() # Check for pointer suffix: while self.hasConsumed('*'): - theT = astnodes.PointerType(theT) + theT = PointerType(theT) return theT def parseTypeDef(self): @@ -126,7 +134,7 @@ newtype = self.parseTypeSpec() typename = self.Consume('ID') self.Consume(';') - df = astnodes.DefinedType(typename.val, newtype, typename.loc) + df = DefinedType(typename.val, newtype, typename.loc) self.addDeclaration(df) # Variable declarations: @@ -135,7 +143,7 @@ t = self.parseTypeSpec() def parseVar(): name = self.Consume('ID') - v = astnodes.Variable(name.val, t) + v = Variable(name.val, t) v.loc = name.loc if self.hasConsumed('='): v.ival = self.Expression() @@ -152,7 +160,7 @@ name = self.Consume('ID') self.Consume('=') val = self.Expression() - c = astnodes.Constant(name.val, t, val) + c = Constant(name.val, t, val) c.loc = name.loc parseConst() while self.hasConsumed(','): @@ -164,7 +172,7 @@ loc = self.Consume('function').loc returntype = self.parseTypeSpec() fname = self.Consume('ID').val - f = astnodes.Function(fname, loc) + f = Function(fname, loc) self.addDeclaration(f) savePart = self.currentPart self.currentPart = f @@ -174,7 +182,7 @@ def parseParameter(): typ = self.parseTypeSpec() name = self.Consume('ID') - param = astnodes.FormalParameter(name.val, typ) + param = FormalParameter(name.val, typ) param.loc = name.loc self.addDeclaration(param) parameters.append(param) @@ -183,7 +191,7 @@ parseParameter() self.Consume(')') paramtypes = [p.typ for p in parameters] - f.typ = astnodes.FunctionType(paramtypes, returntype) + f.typ = FunctionType(paramtypes, returntype) f.body = self.parseCompoundStatement() self.currentPart = savePart @@ -199,7 +207,7 @@ no = self.parseCompoundStatement() else: no = None - return astnodes.IfStatement(condition, yes, no, loc) + return IfStatement(condition, yes, no, loc) def parseWhileStatement(self): loc = self.Consume('while').loc @@ -207,16 +215,16 @@ condition = self.Expression() self.Consume(')') statements = self.parseCompoundStatement() - return astnodes.WhileStatement(condition, statements, loc) + return WhileStatement(condition, statements, loc) def parseReturnStatement(self): loc = self.Consume('return').loc if self.Peak == ';': - expr = astnodes.Literal(0, loc) + expr = Literal(0, loc) else: expr = self.Expression() self.Consume(';') - return astnodes.ReturnStatement(expr, loc) + return ReturnStatement(expr, loc) def parseCompoundStatement(self): self.Consume('{') @@ -226,7 +234,7 @@ if s is None: continue statements.append(s) - return astnodes.CompoundStatement(statements) + return CompoundStatement(statements) def Statement(self): # Determine statement type based on the pending token: @@ -251,9 +259,9 @@ # We enter assignment mode here. loc = self.Consume('=').loc rhs = self.Expression() - return astnodes.Assignment(x, rhs, loc) + return Assignment(x, rhs, loc) else: - return astnodes.ExpressionStatement(x, x.loc) + return ExpressionStatement(x, x.loc) # Expression section: # We not implement these C constructs: @@ -266,7 +274,7 @@ while self.Peak == 'or': loc = self.Consume('or').loc e2 = self.LogicalAndExpression() - exp = astnodes.Binop(exp, 'or', e2, loc) + exp = Binop(exp, 'or', e2, loc) return exp def LogicalAndExpression(self): @@ -274,7 +282,7 @@ while self.Peak == 'and': loc = self.Consume('and').loc o2 = self.EqualityExpression() - o = astnodes.Binop(o, 'and', o2, loc) + o = Binop(o, 'and', o2, loc) return o def EqualityExpression(self): @@ -282,7 +290,7 @@ while self.Peak in ['<', '==', '>']: op = self.Consume(self.Peak) ee2 = self.SimpleExpression() - ee = astnodes.Binop(ee, op.typ, ee2, op.loc) + ee = Binop(ee, op.typ, ee2, op.loc) return ee def SimpleExpression(self): @@ -291,7 +299,7 @@ while self.Peak in ['>>', '<<']: op = self.Consume(self.Peak) e2 = self.AddExpression() - e = astnodes.Binop(e, op.typ, e2, op.loc) + e = Binop(e, op.typ, e2, op.loc) return e def AddExpression(self): @@ -299,7 +307,7 @@ while self.Peak in ['+', '-']: op = self.Consume(self.Peak) e2 = self.Term() - e = astnodes.Binop(e, op.typ, e2, op.loc) + e = Binop(e, op.typ, e2, op.loc) return e def Term(self): @@ -307,7 +315,7 @@ while self.Peak in ['*', '/']: op = self.Consume(self.Peak) t2 = self.BitwiseOr() - t = astnodes.Binop(t, op.typ, t2, op.loc) + t = Binop(t, op.typ, t2, op.loc) return t def BitwiseOr(self): @@ -315,7 +323,7 @@ while self.Peak in ['|']: op = self.Consume(self.Peak) b = self.BitwiseAnd() - a = astnodes.Binop(a, op.typ, b, op.loc) + a = Binop(a, op.typ, b, op.loc) return a def BitwiseAnd(self): @@ -323,7 +331,7 @@ while self.Peak in ['&']: op = self.Consume(self.Peak) b = self.CastExpression() - a = astnodes.Binop(a, op.typ, b, op.loc) + a = Binop(a, op.typ, b, op.loc) return a # Domain of unary expressions: @@ -341,7 +349,7 @@ self.Consume('(') ce = self.Expression() self.Consume(')') - return astnodes.TypeCast(t, ce, loc) + return TypeCast(t, ce, loc) else: return self.UnaryExpression() @@ -350,9 +358,9 @@ op = self.Consume(self.Peak) ce = self.CastExpression() if op.val == '*': - return astnodes.Deref(ce, op.loc) + return Deref(ce, op.loc) else: - return astnodes.Unop(op.typ, ce, op.loc) + return Unop(op.typ, ce, op.loc) else: return self.PostFixExpression() @@ -369,14 +377,14 @@ while self.hasConsumed(','): args.append(self.Expression()) self.Consume(')') - pfe = astnodes.FunctionCall(pfe, args, pfe.loc) + pfe = FunctionCall(pfe, args, pfe.loc) elif self.hasConsumed('->'): field = self.Consume('ID') - pfe = astnodes.Deref(pfe, pfe.loc) - pfe = astnodes.FieldRef(pfe, field.val, field.loc) + pfe = Deref(pfe, pfe.loc) + pfe = FieldRef(pfe, field.val, field.loc) elif self.hasConsumed('.'): field = self.Consume('ID') - pfe = astnodes.FieldRef(pfe, field.val, field.loc) + pfe = FieldRef(pfe, field.val, field.loc) else: raise Exception() return pfe @@ -388,17 +396,17 @@ return e elif self.Peak == 'NUMBER': val = self.Consume('NUMBER') - return astnodes.Literal(val.val, val.loc) + return Literal(val.val, val.loc) elif self.Peak == 'REAL': val = self.Consume('REAL') - return astnodes.Literal(val.val, val.loc) + return Literal(val.val, val.loc) elif self.Peak == 'true': val = self.Consume('true') - return astnodes.Literal(True, val.loc) + return Literal(True, val.loc) elif self.Peak == 'false': val = self.Consume('false') - return astnodes.Literal(False, val.loc) + return Literal(False, val.loc) elif self.Peak == 'ID': d = self.parseDesignator() - return astnodes.VariableUse(d, d.loc) + return VariableUse(d, d.loc) self.Error('Expected NUM, ID or (expr), got {0}'.format(self.Peak))