예제 #1
0
def test_Multiplication_of_symbols_is_parsed_as_expression():
    assert (
        parsed("foo*bar") ==
        [
            OperationTree("*", SymbolTree("foo"), SymbolTree("bar"))
        ]
    )
예제 #2
0
def test_Trailing_comma_in_array_is_ignored():
    assert (
        parsed("[a, bb,]") ==
        [
            ArrayTree(
                [
                    SymbolTree("a"),
                    SymbolTree("bb"),
                ],
            )
        ]
    )
예제 #3
0
def test_Trailing_comma_in_arg_list_is_ignored():
    assert (
        parsed("{:(aa,bb,)}") ==
        [
            FunctionDefTree(
                [
                    SymbolTree("aa"),
                    SymbolTree("bb"),
                ],
                []
            )
        ]
    )
예제 #4
0
def test_Empty_function_definition_with_params_gets_parsed():
    assert (
        parsed("{:(aa,bb,cc,dd)}") ==
        [
            FunctionDefTree(
                [
                    SymbolTree("aa"),
                    SymbolTree("bb"),
                    SymbolTree("cc"),
                    SymbolTree("dd"),
                ],
                []
            )
        ]
    )
예제 #5
0
def test_Difference_of_symbol_and_number_is_parsed_as_expression():
    assert (
        parsed("foo-44") ==
        [
            OperationTree("-", SymbolTree("foo"), NumberTree("44"))
        ]
    )
예제 #6
0
def test_Variable_assignment_gets_parsed():
    assert (
        parsed("x=3") ==
        [
            AssignmentTree(SymbolTree("x"), NumberTree("3"))
        ]
    )
예제 #7
0
def test_Modify_symbol_is_parsed_as_expression():
    assert (
        parsed("foo-=44") ==
        [
            ModifyTree("-=", SymbolTree("foo"), NumberTree("44"))
        ]
    )
예제 #8
0
def test_Multiple_function_calls_with_various_args_get_parsed():
    assert (
        parsed("print('a',3,4/12)(512)()") ==
        [
            FunctionCallTree(
                FunctionCallTree(
                    FunctionCallTree(
                        SymbolTree("print"),
                        [
                            StringTree("a"),
                            NumberTree("3"),
                            OperationTree(
                                "/",
                                NumberTree("4"),
                                NumberTree("12")
                            )
                        ]
                    ),
                    [
                        NumberTree("512")
                    ]
                ),
                []
            )
        ]
    )
예제 #9
0
def test_Function_call_with_no_args_gets_parsed():
    assert (
        parsed("print()") ==
        [
            FunctionCallTree(SymbolTree("print"), [])
        ]
    )
예제 #10
0
def test_Multiple_function_calls_with_no_args_get_parsed():
    assert (
        parsed("print()()") ==
        [
            FunctionCallTree(FunctionCallTree(SymbolTree("print"), []), [])
        ]
    )
예제 #11
0
def test_Modify_by_negative_is_parsed_as_expression():
    assert (
        parsed("foo*=-44") ==
        [
            ModifyTree(
                "*=",
                SymbolTree("foo"), NegativeTree(NumberTree("44"))
            )
        ]
    )
예제 #12
0
def test_Variable_assignment_to_negative_gets_parsed():
    assert (
        parsed("x=--3") ==
        [
            AssignmentTree(
                SymbolTree("x"),
                NegativeTree(NegativeTree(NumberTree("3"))),
            )
        ]
    )
예제 #13
0
def test_Function_definition_with_params_and_commands_gets_parsed():
    assert (
        parsed('{:(x,yy)print(3-4) a="x" print(a)}') ==
        [
            FunctionDefTree(
                [
                    SymbolTree("x"),
                    SymbolTree("yy")
                ],
                [
                    FunctionCallTree(
                        SymbolTree("print"),
                        [
                            OperationTree(
                                '-',
                                NumberTree('3'),
                                NumberTree('4')
                            )
                        ]
                    ),
                    AssignmentTree(SymbolTree("a"), StringTree("x")),
                    FunctionCallTree(SymbolTree("print"), [SymbolTree("a")])
                ]
            )
        ]
    )
예제 #14
0
def test_Function_call_with_various_args_gets_parsed():
    assert (
        parsed("print('a',3,4/12)") ==
        [
            FunctionCallTree(
                SymbolTree("print"),
                [
                    StringTree("a"),
                    NumberTree("3"),
                    OperationTree("/", NumberTree("4"), NumberTree("12"))
                ]
            )
        ]
    )
예제 #15
0
def test_Spaces_are_allowed_where_unimportant():
    assert (
        parsed('''
        {:( x, y )
            x+y
            foo( 3 )
        }( 3, 4 )
        ''') ==
        [
            FunctionCallTree(
                FunctionDefTree(
                    [
                        SymbolTree("x"),
                        SymbolTree("y"),
                    ],
                    [
                        OperationTree(
                            '+',
                            SymbolTree("x"),
                            SymbolTree("y"),
                        ),
                        FunctionCallTree(
                            SymbolTree("foo"),
                            [
                                NumberTree("3"),
                            ]
                        ),
                    ]
                ),
                [
                    NumberTree("3"),
                    NumberTree("4"),
                ]
            )
        ]
    )
예제 #16
0
def test_Multiple_commands_parse_into_multiple_expressions():
    program = """
    x=3
    func={:(a)print(a)}
    func(x)
    """
    assert (
        parsed(program) ==
        [
            AssignmentTree(SymbolTree('x'), NumberTree('3')),
            AssignmentTree(
                SymbolTree('func'),
                FunctionDefTree(
                    [SymbolTree('a')],
                    [
                        FunctionCallTree(
                            SymbolTree('print'), [SymbolTree('a')])
                    ]
                )
            ),
            FunctionCallTree(SymbolTree('func'), [SymbolTree('x')])
        ]
    )
예제 #17
0
def test_Comparisons_are_parsed():
    assert (
        parsed("12<3") ==
        [OperationTree("<", NumberTree("12"), NumberTree("3"))]
    )
    assert (
        parsed("12>x") ==
        [OperationTree(">", NumberTree("12"), SymbolTree("x"))]
    )
    assert (
        parsed("x<=1") ==
        [OperationTree("<=", SymbolTree("x"), NumberTree("1"))]
    )
    assert (
        parsed("y>=x") ==
        [OperationTree(">=", SymbolTree("y"), SymbolTree("x"))]
    )
    assert (
        parsed("y==x") ==
        [OperationTree("==", SymbolTree("y"), SymbolTree("x"))]
    )