Mercurial > lcfOS
diff python/c3/parser.py @ 166:da0087b82fbe
Improved type checking
author | Windel Bouwman |
---|---|
date | Fri, 22 Mar 2013 16:15:31 +0100 |
parents | 598d3888a11c |
children | 0b5b2ee6b435 |
line wrap: on
line diff
--- a/python/c3/parser.py Fri Mar 22 15:12:38 2013 +0100 +++ b/python/c3/parser.py Fri Mar 22 16:15:31 2013 +0100 @@ -8,8 +8,8 @@ class Parser: """ Parses sourcecode into an abstract syntax tree (AST) """ - def __init__(self, sema, diag): - self.sema = sema + def __init__(self, diag): + self.sema = semantics.Semantics(diag) self.diag = diag def parseSource(self, source): self.initLex(source) @@ -17,6 +17,7 @@ self.parsePackage() except CompilerError as e: self.diag.addDiag(e) + return self.sema.mod def Error(self, msg): raise CompilerError(msg, self.token.loc) # Lexer helpers: @@ -40,11 +41,17 @@ return False def NextToken(self): t = self.token - if t.typ != 'END': self.token = self.tokens.__next__() + if t.typ != 'END': + self.token = self.tokens.__next__() return t def initLex(self, source): self.tokens = lexer.tokenize(source) # Lexical stage self.token = self.tokens.__next__() + def skipToSemi(self, tt): + while self.Peak != tt and self.Peak != 'END': + self.NextToken() + if self.Peak == tt: + self.Consume(tt) def parsePackage(self): self.Consume('package') @@ -172,7 +179,9 @@ self.Consume('{') statements = [] while not self.hasConsumed('}'): - statements.append(self.parseStatement()) + s = self.parseStatement() + if not type(s) is astnodes.EmptyStatement: + statements.append(s) return astnodes.CompoundStatement(statements) def parseStatement(self): @@ -201,6 +210,13 @@ # Parsing expressions: def parseExpression(self): return self.parseBinopRhs(self.parsePrimary(), 0) + # TODO: use this error handling: + try: + return self.parseBinopRhs(self.parsePrimary(), 0) + except CompilerError as e: + self.diag.addDiag(e) + self.skipToSemi(';') + return astnodes.Literal(0) def parsePrimary(self): if self.hasConsumed('('): e = self.parseExpression() @@ -212,6 +228,12 @@ elif self.Peak == 'REAL': val = self.Consume('REAL') return self.sema.actOnNumber(val.val, val.loc) + elif self.Peak == 'true': + val = self.Consume('true') + return self.sema.actOnNumber(True, val.loc) + elif self.Peak == 'false': + val = self.Consume('false') + return self.sema.actOnNumber(False, val.loc) elif self.Peak == 'ID': d = self.parseDesignator() if self.Peak == '(':