def mul(self): result = self.unary() while self.current_token().type == TokenType.MUL or self.current_token( ).type == TokenType.DIV: op = self.advance() rh = self.unary() result = BE(result, op, rh) return result
def add(self): result = self.mul() while self.current_token( ).type == TokenType.PLUS or self.current_token( ).type == TokenType.MINUS: op = self.advance() rh = self.mul() result = BE(result, op, rh) return result
def test_multiplying_two_numbers(self): input = tokenize('3 * 45') expected = BE(3.0, Token(TokenType.MUL, '*'), 45.0) self.assertEqual(expected, Parser(input).expr())
def test_subtracting_two_numbers(self): input = tokenize('33 - 5') expected = BE(33.0, Token(TokenType.MINUS, '-'), 5.0) self.assertEqual(expected, Parser(input).expr())
def test_adding_numbers(self): input = tokenize('3 + 45 + 8') expected = BE(BE(3.0, Token(TokenType.PLUS, '+'), 45.0), Token(TokenType.PLUS, '+'), 8.0) self.assertEqual(expected, Parser(input).expr())
def test_correctness_of_sequences_with_parenthesis(self): input = tokenize('2 * (14 + 8)') expected = BE(2.0, Token(TokenType.MUL, '*'), BE(14.0, Token(TokenType.PLUS, '+'), 8.0)) self.assertEqual(expected, Parser(input).expr())
def test_correctness_of_sequences(self): input = tokenize('3 + 45 * 8') expected = BE(3.0, Token(TokenType.PLUS, '+'), BE(45.0, Token(TokenType.MUL, '*'), 8.0)) self.assertEqual(expected, Parser(input).expr())
def test_negative_parenthesis(self): input = tokenize('-(18/5)') expected = UE(Token(TokenType.MINUS, '-'), BE(18.0, Token(TokenType.DIV, '/'), 5.0)) self.assertEqual(expected, Parser(input).expr())
def test_dividing_two_numbers(self): input = tokenize('30 / 5') expected = BE(30.0, Token(TokenType.DIV, '/'), 5.0) self.assertEqual(expected, Parser(input).expr())