Esempio n. 1
0
 def parse_call_expression(self, left: ast.Node) -> ast.Node:
     cur_token = self.cur_token
     return ast.CallExpression(
         token=cur_token,
         function=left,
         arguments=self.parse_expression_list(token.RPAREN),
     )
Esempio n. 2
0
 def parseCallExpression(
         self, function: ast.Expression) -> Optional[ast.Expression]:
     arguments = self.parseExpressionList(token.RPAREN)
     exp = ast.CallExpression(Token=self.curToken,
                              Function=function,
                              Arguments=arguments)
     return exp
Esempio n. 3
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))"
Esempio n. 4
0
 def parse_call_expression(self,
                           function: ast.Expression) -> ast.Expression:
     exp = ast.CallExpression(self.cur_token, function)
     exp.arguments = self.parse_expression_list(token.RPAREN)
     return exp