def test_string(): program = ast.Program(statements=[ ast.LetStatement(token=tokens.Token(typ=tokens.LET, literal="let"), name=ast.Identifier(token=tokens.Token( typ=tokens.IDENT, literal="my_var"), value="my_var"), value=ast.Identifier(token=tokens.Token( typ=tokens.IDENT, literal="another_var"), value="another_var")) ]) expected = "let my_var = another_var;" assert str( program ) == expected, f"str(program) wrong. got '{str(program)}'' but expected '{expected}'"
def parse_for_expression(self) -> ast.Expression: cur_token = self.cur_token if not self.expect_peek(tokens.LPAREN): return None self.next_token() ident = ast.Identifier(token=self.cur_token, value=self.cur_token.literal) if not self.expect_peek(tokens.IN): return None self.next_token() iterator = self.parse_expression(LOWEST) if not self.expect_peek(tokens.RPAREN): return None if not self.expect_peek(tokens.LBRACE): return None body = self.parse_block_statement() return ast.ForExpression(token=cur_token, iterator=iterator, element=ident, body=body)
def parse_function_parameters(self): identifiers = [] if self.peek_token_is(TokenType.RParen): self.next_token() return identifiers self.next_token() ident = ast.Identifier(self.cur_token, self.cur_token.literal) identifiers.append(ident) while self.peek_token_is(TokenType.Comma): self.next_token() self.next_token() ident = ast.Identifier(self.cur_token, self.cur_token.literal) identifiers.append(ident) if not self.expect_peek(TokenType.RParen): return None return identifiers
def parse_function_parameters(self) -> List[ast.Identifier]: identifiers: List[ast.Identifier] = [] if self.peek_token_is(tokens.RPAREN): self.next_token() return identifiers self.next_token() ident: ast.Identifier = ast.Identifier(token=self.cur_token, value=self.cur_token.literal) identifiers.append(ident) while self.peek_token_is(tokens.COMMA): self.next_token() self.next_token() ident = ast.Identifier(token=self.cur_token, value=self.cur_token.literal) identifiers.append(ident) if not self.expect_peek(tokens.RPAREN): return None return identifiers
def parse_let_statement(self): tok = self.cur_token if not self.expect_peek(TokenType.Ident): return None name = ast.Identifier(self.cur_token, self.cur_token.literal) if not self.expect_peek(TokenType.Assign): return None self.next_token() value = self.parse_expression(Precedence.LOWEST) if self.peek_token_is(TokenType.Semicolon): self.next_token() return ast.LetStatement(tok, name, value)
def parse_let_statement(self): statement = ast.LetStatement(token=self.cur_token) if not self.expect_peek(tokens.IDENT): return None statement.name = ast.Identifier(token=self.cur_token, value=self.cur_token.literal) if not self.expect_peek(tokens.ASSIGN): return None self.next_token() statement.value = self.parse_expression(LOWEST) if self.peek_token_is(tokens.SEMICOLON): self.next_token() return statement
def parse_identifier(self) -> ast.Expression: return ast.Identifier(token=self.cur_token, value=self.cur_token.literal)
def parse_identifier(self): return ast.Identifier(self.cur_token, self.cur_token.literal)