Mercurial > lcfOS
diff python/ppci/c3/parser.py @ 394:988f3fb861e4
c3 code generator rewrite
author | Windel Bouwman |
---|---|
date | Thu, 22 May 2014 08:14:12 +0200 |
parents | 6ae782a085e0 |
children | fb3c1f029b30 |
line wrap: on
line diff
--- a/python/ppci/c3/parser.py Sat May 17 21:17:40 2014 +0200 +++ b/python/ppci/c3/parser.py Thu May 22 08:14:12 2014 +0200 @@ -23,7 +23,7 @@ self.tokens = tokens self.token = self.tokens.__next__() try: - self.parsePackage() + self.parse_package() self.mod.ok = True # Valid until proven wrong :) return self.mod except CompilerError as e: @@ -68,26 +68,28 @@ self.mod.imports.append(name) self.Consume(';') - def parsePackage(self): + def parse_package(self): + """ Parse a package definition """ self.Consume('module') name = self.Consume('ID') self.Consume(';') self.mod = Package(name.val, name.loc) self.currentPart = self.mod while self.Peak != 'END': - self.parseTopLevel() + self.parse_top_level() self.Consume('END') - def parseTopLevel(self): + def parse_top_level(self): + """ Parse toplevel declaration """ if self.Peak == 'function': self.parse_function_def() elif self.Peak == 'var': - self.parseVarDef() + self.parse_variable_def() # TODO handle variable initialization elif self.Peak == 'const': self.parseConstDef() elif self.Peak == 'type': - self.parseTypeDef() + self.parse_type_def() elif self.Peak == 'import': self.parseImport() else: @@ -155,7 +157,7 @@ raise Exception() return theT - def parseTypeDef(self): + def parse_type_def(self): self.Consume('type') newtype = self.parse_type_spec() typename = self.Consume('ID') @@ -164,7 +166,8 @@ self.addDeclaration(df) # Variable declarations: - def parseVarDef(self): + def parse_variable_def(self): + """ Parse variable declaration """ self.Consume('var') t = self.parse_type_spec() for name in self.parseIdSequence(): @@ -172,7 +175,6 @@ v.loc = name.loc self.addDeclaration(v) self.Consume(';') - return Empty() def parseConstDef(self): self.Consume('const') @@ -234,7 +236,7 @@ self.Consume(')') return Switch(condition, loc) - def parseWhile(self): + def parse_while(self): loc = self.Consume('while').loc self.Consume('(') condition = self.Expression() @@ -275,7 +277,7 @@ if self.Peak == 'if': return self.parse_if() elif self.Peak == 'while': - return self.parseWhile() + return self.parse_while() elif self.Peak == 'for': return self.parse_for() elif self.Peak == 'switch': @@ -285,7 +287,8 @@ elif self.hasConsumed(';'): return Empty() elif self.Peak == 'var': - return self.parseVarDef() + self.parse_variable_def() + return Empty() elif self.Peak == 'return': return self.parseReturn() else: @@ -391,6 +394,7 @@ return self.UnaryExpression() def sizeof_expression(self): + """ Compiler internal function to determine size of a type """ loc = self.Consume('sizeof').loc self.Consume('(') typ = self.parse_type_spec()