Exemplo n.º 1
0
 def test_string(self):
     let = ast.LetStatement(
         token.Token(token.LET, "let"),
         ast.Identifier(token.Token(token.IDENT, "myVar"), "myVar"),
         ast.Identifier(token.Token(token.IDENT, "anotherVar"),
                        "anotherVar"))
     program = ast.Program([let])
     self.assertEqual(str(program), "let myVar = anotherVar;")
Exemplo n.º 2
0
    def test_string(self):
        program = ast.Program(Statements=[
            ast.LetStatement(Token=token.Token(Type=token.LET, Literal='let'),
                             Name=ast.Identifier(Token=token.Token(
                                 Type=token.IDENT, Literal='myVar'),
                                                 Value='myVar'),
                             Value=ast.Identifier(Token=token.Token(
                                 Type=token.IDENT, Literal='anotherVar'),
                                                  Value='anotherVar'))
        ])

        if program.String() != 'let myVar = anotherVar;':
            self.fail('program.String() wrong. got=\'%s\'' % program.String())
Exemplo n.º 3
0
def test_string():

    program = ast.Program()
    statements = [
        ast.LetStatement(
            token=token.Token(token.LET, "let"),
            name=ast.Identifier(token=token.Token(token.IDENT, "myVar"),
                                value="myVar"),
            value=ast.Identifier(token=token.Token(token.IDENT, "anotherVar"),
                                 value="anotherVar"),
        )
    ]
    program.statements = statements
    assert str(program) == "let myVar = anotherVar"
Exemplo n.º 4
0
def test_string():
    program = ast.Program([
        ast.LetStatement(
            token.Token(token.LET, 'let'),
            ast.Identifier(
                token.Token(token.IDENT, 'myVar'),
                'myVar'
            ),
            ast.Identifier(
                token.Token(token.IDENT, 'anotherVar'),
                'anotherVar'
            )
        )
    ])

    assert program.string() == 'let myVar = anotherVar;', "program.string() wrong. got='{}'".format(program.string())
Exemplo n.º 5
0
 def parse_function_parameters(self):
     identifiers = []
     # empty case
     if self.peek_token_is(token.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(token.COMMA):
         self.next_token()  # consume last ident
         self.next_token()  # consume the comma
         ident = ast.Identifier(self.cur_token, self.cur_token.Literal)
         identifiers.append(ident)
     if not self.expect_peek(token.RPAREN):
         return None
     return identifiers
Exemplo n.º 6
0
 def parse_let_statement(self):
     stmt = ast.LetStatement(self.cur_token)
     if not self.expect_peek(token.IDENT):
         return None
     stmt.name = ast.Identifier(self.cur_token, self.cur_token.Literal)
     if not self.expect_peek(token.ASSIGN):
         return None
     self.next_token()
     stmt.value = self.parse_expression(Precedence.LOWEST.value)
     if self.peek_token_is(token.SEMICOLON):
         self.next_token()
     return stmt
Exemplo n.º 7
0
def test_call_expression_parsing():
    source = "add(1, 2 * 3, 4 + 5);"

    l = Lexer(source)
    p = Parser(l)

    program = p.parse_program()
    assert p.errors == []

    assert len(program.statements) == 1

    stmt = program.statements[0]

    assert isinstance(stmt, ast.ExpressionStatement)

    assert isinstance(stmt.expression, ast.CallExpression)

    assert stmt.expression.function.value == "add"

    assert len(stmt.expression.arguments) == 3

    assert stmt.expression == ast.CallExpression(
        token=Token(tok_type="(", literal="("),
        function=ast.Identifier(token=Token(tok_type="IDENT", literal="add"),
                                value="add"),
        arguments=[
            ast.IntegerLiteral(token=Token(tok_type="INT", literal="1"),
                               value=1),
            ast.InfixExpression(
                token=Token(tok_type="*", literal="*"),
                left=ast.IntegerLiteral(token=Token(tok_type="INT",
                                                    literal="2"),
                                        value=2),
                operator="*",
                right=ast.IntegerLiteral(token=Token(tok_type="INT",
                                                     literal="3"),
                                         value=3),
            ),
            ast.InfixExpression(
                token=Token(tok_type="+", literal="+"),
                left=ast.IntegerLiteral(token=Token(tok_type="INT",
                                                    literal="4"),
                                        value=4),
                operator="+",
                right=ast.IntegerLiteral(token=Token(tok_type="INT",
                                                     literal="5"),
                                         value=5),
            ),
        ],
    )

    assert str(stmt.expression) == "add(1, (2 * 3), (4 + 5))"
Exemplo n.º 8
0
    def parse_function_parameters(self) -> Union[List[ast.Identifier], None]:
        identifiers: List[ast.Identifier] = []

        if self.peek_token_is(token.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(token.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(token.RPAREN):
            return None

        return identifiers
Exemplo n.º 9
0
    def parseFunctionParameters(self) -> List[ast.Identifier]:
        identifiers: List[ast.Identifier] = []

        if self.peekTokenIs(token.RPAREN):
            self.nextToken()
            return identifiers

        self.nextToken()

        ident = ast.Identifier(Token=self.curToken,
                               Value=self.curToken.Literal)
        identifiers.append(ident)

        while self.peekTokenIs(token.COMMA):
            self.nextToken()
            self.nextToken()
            ident = ast.Identifier(Token=self.curToken,
                                   Value=self.curToken.Literal)
            identifiers.append(ident)

        if not self.expectPeek(token.RPAREN):
            return identifiers

        return identifiers
Exemplo n.º 10
0
    def parse_let_statement(self) -> Optional[ast.LetStatement]:
        stmt_token = self.cur_token

        if not self.expect_peek(token.IDENT):
            return None

        name = ast.Identifier(token=self.cur_token,
                              value=self.cur_token.literal)

        if not self.expect_peek(token.ASSIGN):
            return None

        self.next_token()

        value = self.parse_expression(OperatorPrecedence.LOWEST)

        if self.peek_token_is(token.SEMICOLON):
            self.next_token()

        return ast.LetStatement(token=stmt_token, name=name, value=value)
Exemplo n.º 11
0
    def parseLetStatement(self) -> Optional[ast.LetStatement]:
        curToken = self.curToken

        if not self.expectPeek(token.IDENT):
            return None

        name = ast.Identifier(Token=self.curToken, Value=self.curToken.Literal)

        if not self.expectPeek(token.ASSIGN):
            return None

        self.nextToken()

        value = self.parseExpression(LOWEST)
        if not value:
            return None

        stmt = ast.LetStatement(Token=curToken, Name=name, Value=value)

        if self.peekTokenIs(token.SEMICOLON):
            self.nextToken()

        return stmt
Exemplo n.º 12
0
 def parse_identifier(self) -> ast.Expression:
     return ast.Identifier(self.cur_token, self.cur_token.literal)
Exemplo n.º 13
0
 def parse_identifier(self) -> ast.Node:
     return ast.Identifier(token=self.cur_token,
                           value=self.cur_token.literal)
Exemplo n.º 14
0
 def parseIdentifier(self) -> ast.Expression:
     return ast.Identifier(Token=self.curToken, Value=self.curToken.Literal)
Exemplo n.º 15
0
 def parse_identifer(self):
     # self.tracer.untrace(self.tracer.trace('parse_identifer'))
     return ast.Identifier(self.cur_token, self.cur_token.Literal)