Exemplo n.º 1
0
def test_first_power_of_x_l():

    # x ^ 1 = x

    example = Tree("suc", [
        Tree("pow", [Token("WORD", "x"),
                     Tree("suc", [Token("NUMBER", "0")])])
    ])

    example = label_parents(example)

    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = first_power_of_x_l(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of not root
    # suc
    #   pow
    #     x
    #     suc	0

    # Expected results
    # suc	x

    assert repr(entire) == "Tree(suc, [Token(WORD, 'x')])"

    example = Tree("pow",
                   [Token("WORD", "x"),
                    Tree("suc", [Token("NUMBER", "0")])])

    example = label_parents(example)

    print("b")
    print(example.pretty())

    cursor, entire = first_power_of_x_l(example, example)

    print("b")
    print(entire)
    print(repr(entire))

    # Case of root
    # pow
    #   x
    #   suc	0

    # Expected result
    # x

    assert repr(entire) == "Token(WORD, 'x')"
Exemplo n.º 2
0
def test_power_of_one_l():

    # 1 ^ x = 1

    example = Tree("suc", [
        Tree("pow", [Tree("suc", [Token("NUMBER", "0")]),
                     Token("WORD", "x")])
    ])
    example = label_parents(example)

    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = power_of_one_l(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of not root
    # suc
    #   pow
    #     suc	0
    #     x

    # Expected result
    # suc
    #   suc	0

    assert repr(entire) == "Tree(suc, [Tree(suc, [Token(NUMBER, '0')])])"

    example = Tree("pow",
                   [Tree("suc", [Token("NUMBER", "0")]),
                    Token("WORD", "x")])

    example = label_parents(example)

    print("b")
    print(example.pretty())

    cursor, entire = power_of_one_l(example, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of root
    # pow
    #   suc	0
    #   x

    # Expected result
    # suc	0

    assert repr(entire) == "Tree(suc, [Token(NUMBER, '0')])"
Exemplo n.º 3
0
def test_times_identity_l():

    # x * 1 = x
    example = Tree("suc", [
        Tree("mul", [Token("WORD", "x"),
                     Tree("suc", [Token("NUMBER", "0")])])
    ])
    example = label_parents(example)
    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = times_identity_l(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of not root
    # suc
    #   mul <----
    #     x
    #     suc	0

    # Expected result
    # suc	x

    assert repr(entire) == "Tree(suc, [Token(WORD, 'x')])"

    example = Tree("mul",
                   [Token("WORD", "x"),
                    Tree("suc", [Token("NUMBER", "0")])])

    example = label_parents(example)

    print("b")
    print(example.pretty())

    cursor, entire = times_identity_l(example, example)

    print("b")
    print(entire)
    print(repr(entire))

    # case of root
    # mul
    #   x
    #   suc	0

    # Expected result
    # x

    assert repr(entire) == "Token(WORD, 'x')"
Exemplo n.º 4
0
def test_first_power_of_x_r():

    # x = x ^ 1

    example = Tree("suc", [Token("WORD", "x")])
    example = label_parents(example)

    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = first_power_of_x_r(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of not root
    # suc	x

    # Expected result
    # suc
    #   pow
    #     x
    #     suc	0

    assert (
        repr(entire) ==
        "Tree(suc, [Tree(pow, [Token(WORD, 'x'), Tree(suc, [Token(NUMBER, '0')])])])"
    )

    example = Token("WORD", "x")
    example = label_parents(example)

    print("b")
    print(example)

    cursor, entire = first_power_of_x_r(example, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of root:
    # x

    # Expected result
    # pow
    #   x
    #   suc	0

    assert (repr(entire) ==
            "Tree(pow, [Token(WORD, 'x'), Tree(suc, [Token(NUMBER, '0')])])")
Exemplo n.º 5
0
def test_times_identity_r():

    # x = x * 1

    example = Tree("suc", [Token("WORD", "x")])
    example = label_parents(example)

    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = times_identity_r(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of not root
    # suc	x

    # Expected result
    # suc
    #   mul
    #     x
    #     suc	0

    assert (
        repr(entire) ==
        "Tree(suc, [Tree(mul, [Token(WORD, 'x'), Tree(suc, [Token(NUMBER, '0')])])])"
    )

    example = Token("WORD", "x")
    example = label_parents(example)

    print("b")
    print(example)

    cursor, entire = times_identity_r(example, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of root
    # x

    # Expected result
    # mul
    #   x
    #   suc	0

    assert (repr(entire) ==
            "Tree(mul, [Token(WORD, 'x'), Tree(suc, [Token(NUMBER, '0')])])")
Exemplo n.º 6
0
def _debug_log_tree(tree: Tree):
    if Logger.isEnabledFor(DEBUG):
        debug_log_text(tree.pretty(), "[Compile] Intermediate Tree:")
Exemplo n.º 7
0
def test_distributivity_power_times_l():

    # (x * y) ^ z = x ^ z * y ^ z

    example = Tree(
        "suc",
        [
            Tree(
                "pow",
                [
                    Tree("mul", [Token("WORD", "x"),
                                 Token("WORD", "y")]),
                    Token("WORD", "z"),
                ],
            )
        ],
    )
    example = label_parents(example)

    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = distributivity_power_times_l(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of  not root
    # suc
    #   pow <----
    #     mul
    #       x
    #       y
    #     z

    # Expected result
    # suc
    #   mul
    #     pow
    #       x
    #       z
    #     pow
    #       y
    #       z

    assert (
        repr(entire) ==
        "Tree(suc, [Tree(mul, [Tree(pow, [Token(WORD, 'x'), Token(WORD, 'z')]), Tree(pow, [Token(WORD, 'y'), Token(WORD, 'z')])])])"
    )

    example = Tree(
        "pow",
        [
            Tree("mul",
                 [Token("WORD", "x"), Token("WORD", "y")]),
            Token("WORD", "z"),
        ],
    )

    example = label_parents(example)

    print("b")
    print(example.pretty())

    cursor, entire = distributivity_power_times_l(example, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of root
    # pow
    #   mul
    #     x
    #     y
    #   z

    # Expected result
    # mul
    #   pow
    #     x
    #     z
    #   pow
    #     y
    #     z

    assert (
        repr(entire) ==
        "Tree(mul, [Tree(pow, [Token(WORD, 'x'), Token(WORD, 'z')]), Tree(pow, [Token(WORD, 'y'), Token(WORD, 'z')])])"
    )
Exemplo n.º 8
0
def test_distributivity_power_plus_r():
    # x ^ y * x ^ z = x ^ (y + z)

    example = Tree(
        "suc",
        [
            Tree(
                "mul",
                [
                    Tree("pow", [Token("WORD", "x"),
                                 Token("WORD", "y")]),
                    Tree("pow", [Token("WORD", "x"),
                                 Token("WORD", "z")]),
                ],
            )
        ],
    )
    example = label_parents(example)

    print("b")
    print(example.pretty())

    op = example.children[0]

    cursor, entire = distributivity_power_plus_r(op, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # Case of not root
    # suc
    #   mul <-----
    #     pow
    #       x
    #       y
    #     pow
    #       x
    #       z

    # Expected result
    # suc
    #   pow
    #     x
    #     add
    #       y
    #       z

    assert (
        repr(entire) ==
        "Tree(suc, [Tree(pow, [Token(WORD, 'x'), Tree(add, [Token(WORD, 'y'), Token(WORD, 'z')])])])"
    )

    example = Tree(
        "mul",
        [
            Tree("pow",
                 [Token("WORD", "x"), Token("WORD", "y")]),
            Tree("pow",
                 [Token("WORD", "x"), Token("WORD", "z")]),
        ],
    )

    example = label_parents(example)

    print("b")
    print(example.pretty())

    cursor, entire = distributivity_power_plus_r(example, example)

    print("b")
    print(entire.pretty())
    print(repr(entire))

    # case of root
    # mul
    #   pow
    #     x
    #     y
    #   pow
    #     x
    #     z

    # Expected result
    # pow
    #   x
    #   add
    #     y
    #     z

    assert (
        repr(entire) ==
        "Tree(pow, [Token(WORD, 'x'), Tree(add, [Token(WORD, 'y'), Token(WORD, 'z')])])"
    )
Exemplo n.º 9
0
 def format(klass: Type['Parser'], parsed: lark.Tree) -> tree.Node:
     return parsed.pretty()