コード例 #1
0
ファイル: ast_test.py プロジェクト: Maximophone/monkey
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}'"
コード例 #2
0
    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)
コード例 #3
0
 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
コード例 #4
0
    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
コード例 #5
0
    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)
コード例 #6
0
    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
コード例 #7
0
 def parse_identifier(self) -> ast.Expression:
     return ast.Identifier(token=self.cur_token,
                           value=self.cur_token.literal)
コード例 #8
0
 def parse_identifier(self):
     return ast.Identifier(self.cur_token, self.cur_token.literal)