def test_literal_expression(self): # 5; tokens = [ Token(TokenType.NUMBER, "5", 5.0, 0), Token(TokenType.SEMICOLON, ";", None, 0), Token(TokenType.EOF, "", None, 0), ] parser = Parser(tokens) statements = parser.parse() self.assertEqual(len(statements), 1, "Expected 1 statement") expression_statement = statements[0] self.assertTrue(isinstance(expression_statement, Expression)) literal_expression = expression_statement.expression self.assertTrue(isinstance(literal_expression, LiteralExpr)) self.assertEqual(literal_expression.value, 5)
def test_print_statement(self): # print 20; tokens = [ Token(TokenType.PRINT, "print", None, 0), Token(TokenType.NUMBER, "20", 20.0, 0), Token(TokenType.SEMICOLON, ";", None, 0), Token(TokenType.EOF, "", None, 0), ] parser = Parser(tokens) statements = parser.parse() self.assertEqual(len(statements), 1, "Expected 1 statement") self.assertTrue(isinstance(statements[0], Print)) expression = statements[0].expression self.assertTrue(isinstance(expression, LiteralExpr)) self.assertEqual(expression.value, 20)
def scan_tokens(self): """Parses all tokens until EOF for the provided source code string.""" while not self._is_at_end(): self.start = self.current self.scan_token() self.tokens.append(Token(TokenType.EOF, "", None, self.line)) return self.tokens
def test_binary_expression(self): # 10 <= 20; tokens = [ Token(TokenType.NUMBER, "10", 10.0, 0), Token(TokenType.LESS_EQUAL, "<=", None, 0), Token(TokenType.NUMBER, "20", 20.0, 0), Token(TokenType.SEMICOLON, ";", None, 0), Token(TokenType.EOF, "", None, 0), ] parser = Parser(tokens) statements = parser.parse() self.assertEqual(len(statements), 1, "Expected 1 statement") expression = statements[0].expression self.assertTrue(isinstance(expression, BinaryExpr)) self.assertEqual(expression.left.value, 10) self.assertEqual(expression.operator.token_type, TokenType.LESS_EQUAL) self.assertEqual(expression.right.value, 20)
def scan_tokens(self): while not self.is_at_end(): self.start = self.current self.scan_token() self.tokens.append(Token(TokenType.EOF, "", None, self.line)) return self.tokens
def add_token(self, type: TokenType, literal: object = None): text = self.source[self.start:self.current] self.tokens.append(Token(type, text, literal, self.line))
def _add_token(self, token_type, literal=None): text = self.source[self.start:self.current] self.tokens.append(Token(token_type, text, literal, self.line))
def setUp(self): self.env = Environment() self.x = Token(TokenType.VAR, "x", "x", 1) self.undefined_var = Token(TokenType.VAR, "undefined_var", "undefined_var", 1)