示例#1
0
    def _consumeNumber(self,
                       stream: StringStream) -> Optional[NumberExpression]:
        if not self._can_consumeDigit(stream):
            return None

        token = stream.next()
        while self._can_consumeDigit(stream):
            token += stream.next()
        return token
	def f():      
		stream = StringStream(expression)
		_numberParser = NumberParser()
		_operatorParser = OperatorParser()

		number = _numberParser.parse(stream)		
		operator = _operatorParser.parse(stream)		
		number = _numberParser.parse(stream)		

		if stream.has_chars():
			return "Stream should now be empty"
示例#3
0
    def parse(self, stream: StringStream) -> Optional[NumberExpression]:
        if not self.can_consume(stream):
            return None

        token: str = stream.peek()
        if token.isspace():
            stream.next()
            return self.parse(stream)

        isNegative = self._isNegativeSign(token)

        if isNegative:
            stream.next()

        numberToken = self._consumeNumber(stream)
        return NumberExpression(numberToken, isNegative)
示例#4
0
    def f():

        stream = StringStream(expression)

        _stack = ParserStack()
        _logic = ParserStackInteractor(_stack)
        _numberParser = NumberParser()
        _operatorParser = OperatorParser()

        _expressionParser = ExpressionParser(_logic, _numberParser,
                                             _operatorParser)
        result = _expressionParser.parse(stream)
        if not result.evaluate() == expectedResult:
            return "Result is: %s. \n object is: %s" % (str(
                result.evaluate()), str(result))
示例#5
0
    def parse(self, stream: StringStream) -> Optional[Operator]:
        if not self.can_consume(stream):
            return None

        token: str = stream.next()

        if token == '+':
            return AddOperator()
        elif token == '-':
            return SubtractOperator()
        elif token == '*':
            return MultiplyOperator()
        elif token == '/':
            return DivideOperator()
        elif token.isspace():
            return self.parse(stream)
示例#6
0
    def parse(self, stream: StringStream) -> Optional[Expression]:
        while stream.has_chars() and not stream.peek() == ')':

            if not self._can_consume(stream):
                stream.next()
                continue

            if self._consume_number(stream):
                continue

            self._consume_operator(stream)

            if stream.peek() == '(':
                stream.next()
                self._evaluate_bracket_expression(stream)

        return self._tree.create_expression()
def testStream():
		stream = StringStream("1+1")
		
		def f():
			stream.peek()
			stream.next()
			stream.peek()
			stream.next()
			stream.peek()
			stream.peek()
			stream.peek()
			stream.next()
			stream.peek()
			stream.peek()

			if stream.has_chars():
				return "Stream should be empty"

		return utils.Test("Test that stream operators work", f)
示例#8
0
 def _can_consumeDigit(self, stream: StringStream) -> bool:
     token = stream.peek()
     return self._isSingleDigitNum(token)
示例#9
0
 def can_consume(self, stream: StringStream) -> bool:
     token = stream.peek()
     return self._isSingleDigitNum(token) or self._isNegativeSign(token)
示例#10
0
 def _is_subtract_character(self, stream: StringStream) -> bool:
     return self._stack_interactor.query.is_awaiting_operator(
     ) and stream.peek() == '-'
示例#11
0
 def _is_open_bracket(self, stream: StringStream) -> bool:
     return stream.peek() == '('
示例#12
0
def evaluate(expressionString):
    stream = StringStream(expressionString)
    return parse(stream).evaluate()
示例#13
0
 def can_consume(self, stream: StringStream) -> bool:
     token = stream.peek()
     return self._isOperator(token)