예제 #1
0
 def scanNumber(self):
     self.isDigit = False
     self.wholePart = 0.0
     self.fractionalPart = 0.0
     self.fractionalMultiplier = 0.1
     self.past_decimal = False
     while self.EndExp is False and self.isDigitOrDecimal(self.CharNext):
         if self.CharNext == '.':
             if self.past_decimal:
                 raise CalcExceptions(
                     "badly formed number - multiple decimal points")
             self.past_decimal = True
         elif self.isDigitOrDecimal(self.CharNext):
             self.isDigit = True
             if self.past_decimal is False:
                 self.wholePart = int(self.wholePart) * 10 + int(
                     self.CharNext)
             else:
                 self.fractionalPart += int(
                     self.CharNext) * self.fractionalMultiplier
                 self.fractionalMultiplier /= 10.0
         self.advCursor()
     if self.isDigit is False:
         raise CalcExceptions(
             "badly formed number - decimal point with no digits")
     return self.wholePart + self.fractionalPart
예제 #2
0
 def parse(self, tokens):
     if len(tokens) == 1:
         raise CalcExceptions("Parser received an empty token list.")
     self.tokens = tokens
     self.currentToken = self.tokens[0]
     eTree = self.parseExpression()
     if self.currentToken is not None:
         raise CalcExceptions("Unconsumed tokens at end of expression.")
     return eTree
예제 #3
0
 def parseNumber(self):
     if self.currentToken is None:
         raise CalcExceptions(
             'Reached end of tokens while expecting a number.')
     if self.currentToken.getType() == TokenType.NUMBER_TOKEN:
         value = self.currentToken.getValue()
         self.consumeToken()
         return expressionTree.NumberExpressionTree(value)
     else:
         raise CalcExceptions("Expected a number.")
예제 #4
0
 def parseFactor(self):
     if self.currentToken is None:
         raise CalcExceptions(
             'Reached end of tokens while expecting a number.')
     factor = None
     if self.currentToken.getType(
     ) == TokenType.PAREN_TOKEN and self.currentToken.getValue() == '(':
         self.consumeToken()
         factor = self.parseExpression()
         if self.currentToken is None or self.currentToken.getValue(
         ) != ')':
             raise CalcExceptions('Badly formed parenthesized expression.')
         self.consumeToken()
     elif self.currentToken.getType(
     ) == TokenType.OPERATOR_TOKEN and self.currentToken.getValue() == '~':
         self.consumeToken()
         subExpr = self.parseNumber()
         factor = expressionTree.OperatorExpressionTree('~', None, subExpr)
     else:
         factor = self.parseNumber()
     return factor