Mercurial > lcfOS
diff python/ppci/c3/parser.py @ 307:e609d5296ee9
Massive rewrite of codegenerator
author | Windel Bouwman |
---|---|
date | Thu, 12 Dec 2013 20:42:56 +0100 |
parents | b145f8e6050b |
children | 2e7f55319858 |
line wrap: on
line diff
--- a/python/ppci/c3/parser.py Mon Dec 09 19:00:21 2013 +0100 +++ b/python/ppci/c3/parser.py Thu Dec 12 20:42:56 2013 +0100 @@ -1,8 +1,8 @@ import logging from ppci import CompilerError from .astnodes import Member, Literal, TypeCast, Unop, Binop -from .astnodes import Assignment, ExpressionStatement, CompoundStatement -from .astnodes import ReturnStatement, WhileStatement, IfStatement +from .astnodes import Assignment, ExpressionStatement, Compound +from .astnodes import Return, While, If, Empty from .astnodes import FunctionType, Function, FormalParameter from .astnodes import StructureType, DefinedType, PointerType from .astnodes import Constant, Variable @@ -10,7 +10,6 @@ from .astnodes import Package from .astnodes import Identifier from .astnodes import FunctionCall -from .astnodes import EmptyStatement class Parser: @@ -146,7 +145,7 @@ v.loc = name.loc self.addDeclaration(v) self.Consume(';') - return EmptyStatement() + return Empty() def parseConstDef(self): self.Consume('const') @@ -184,56 +183,56 @@ self.Consume(')') paramtypes = [p.typ for p in parameters] f.typ = FunctionType(paramtypes, returntype) - f.body = self.parseCompoundStatement() + f.body = self.parseCompound() self.currentPart = savePart - def parseIfStatement(self): + def parseIf(self): loc = self.Consume('if').loc self.Consume('(') condition = self.Expression() self.Consume(')') yes = self.Statement() - no = self.Statement() if self.hasConsumed('else') else EmptyStatement() - return IfStatement(condition, yes, no, loc) + no = self.Statement() if self.hasConsumed('else') else Empty() + return If(condition, yes, no, loc) - def parseWhileStatement(self): + def parseWhile(self): loc = self.Consume('while').loc self.Consume('(') condition = self.Expression() self.Consume(')') statements = self.Statement() - return WhileStatement(condition, statements, loc) + return While(condition, statements, loc) - def parseReturnStatement(self): + def parseReturn(self): loc = self.Consume('return').loc if self.Peak == ';': expr = Literal(0, loc) else: expr = self.Expression() self.Consume(';') - return ReturnStatement(expr, loc) + return Return(expr, loc) - def parseCompoundStatement(self): + def parseCompound(self): self.Consume('{') statements = [] while not self.hasConsumed('}'): statements.append(self.Statement()) - return CompoundStatement(statements) + return Compound(statements) def Statement(self): # Determine statement type based on the pending token: if self.Peak == 'if': - return self.parseIfStatement() + return self.parseIf() elif self.Peak == 'while': - return self.parseWhileStatement() + return self.parseWhile() elif self.Peak == '{': - return self.parseCompoundStatement() + return self.parseCompound() elif self.hasConsumed(';'): - return EmptyStatement() + return Empty() elif self.Peak == 'var': return self.parseVarDef() elif self.Peak == 'return': - return self.parseReturnStatement() + return self.parseReturn() else: x = self.UnaryExpression() if self.Peak == '=':