def test_decr_withlabel():
    code = "decr mylabel"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert type(ast[0]) == parser.DecrOp
    assert ast[0].label_name == "mylabel"
def test_assign_from_tile():
    code = "emp = 3"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert ast[0].src == "3"
    assert ast[0].dst == "emp"
def test_assign_to_addressof_tilenumber():
    code = "*3 = emp"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert ast[0].src == "emp"
    assert ast[0].dst == parser.AddressOf("3")
def test_assign_to_tilenumber():
    code = "3 = emp"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert ast[0].src == "emp"
    assert ast[0].dst == "3"
def test_compat_inbox():
    code = "inbox"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

        assert ast[0].src == "inbox"
        assert ast[0].dst == "emp"
def test_assign_from_alias():
    code = "emp = myTile"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert ast[0].src == "myTile"
    assert ast[0].dst == "emp"
def test_assign_from_addressof_alias():
    code = "emp = *myTile"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert ast[0].src == parser.AddressOf("myTile")
    assert ast[0].dst == "emp"
def test_assign_from_addressof_tile():
    code = "emp = *3"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert ast[0].src == parser.AddressOf("3")
    assert ast[0].dst == "emp"
def test_aliases():
    code = "alias 5 test"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert ast[0].symbolic_name == "test"
    assert ast[0].tile_no == 5
def test_decr_withnumber_address():
    code = "decr *4"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert type(ast[0]) == parser.DecrOp
    assert ast[0].label_name == parser.AddressOf("4")
def test_compat_decr_withnumber():
    code = "bump- 0"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert type(ast[0]) == parser.DecrOp
    assert ast[0].label_name == "0"
def test_incr_withnumber():
    code = "bump+ 0"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert type(ast[0]) == parser.IncrOp
    assert ast[0].label_name == "0"
def test_compat_incr_withlabel():
    code = "bump+ mylabel"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert type(ast[0]) == parser.IncrOp
    assert ast[0].label_name == "mylabel"
def test_incr_withlabel_address():
    code = "incr *mylabel"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert type(ast[0]) == parser.IncrOp
    assert ast[0].label_name == parser.AddressOf("mylabel")
def test_compat_copyfrom_tilenumber():
    code = "copyfrom 0"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert type(ast[0]) == parser.AssignOp
    assert ast[0].src == "0"
    assert ast[0].dst == "emp"
def test_compat_copyto_addressof_tilenumber():
    code = "copyto [0]"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert type(ast[0]) == parser.AssignOp
    assert ast[0].src == "emp"
    assert ast[0].dst == parser.AddressOf("0")
def test_compat_copyto_addressof_aliased():
    code = "copyto *aliased"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert type(ast[0]) == parser.AssignOp
    assert ast[0].src == "emp"
    assert ast[0].dst == parser.AddressOf("aliased")
def test_compat_copyfrom_aliased():
    code = "copyfrom aliased"
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert type(ast[0]) == parser.AssignOp
    assert ast[0].src == "aliased"
    assert ast[0].dst == "emp"
def test_jump_to_undefined_label_condjmp():
    code = """
    notwhatyouwant:
    jez whatiwant
    """
    with StringIO(code) as f:
        ast = parser.parse_it(f)
        with pytest.raises(ValueError):
            semantic_check.check_undefined_label_jump(ast)
def test_singlelabel():
    code = """
    test:
    jmp test
    """
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert semantic_check.check_multiple_labels(ast)
    assert semantic_check.check_undefined_label_jump(ast)
def test_if_noelse():
    code = """
    if ez then
        outbox
    endif
    """
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    if_op = ast[0]
    assert if_op.false_branch == []
def test_if_nz():
    code = """
    if nz then
        outbox
    endif
    """
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    if_op = ast[0]
    assert type(if_op) == parser.IfOp
    assert if_op.condition == "nz"
def test_double_defined_label():
    code = """
    test:
    test:
    jmp test
    """
    with StringIO(code) as f:
        ast = parser.parse_it(f)

        with pytest.raises(ValueError):
            semantic_check.check_multiple_labels(ast)

        assert semantic_check.check_undefined_label_jump(ast)
def test_if_single_op():
    code = """
    emp = inbox
    if ez then
        emp = inbox
    endif
    """
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    assert type(ast[1]) == parser.IfOp
    assert ast[1].condition == "ez"
    assert ast[1].true_branch == [parser.AssignOp("inbox", "emp")]
def test_if_nested():
    code = """
    if ez then
        if nz then
        endif
    endif
    """
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    if_op = ast[0]
    nested_if = if_op.true_branch[0]
    assert if_op.condition == "ez"
    assert nested_if.condition == "nz"
    assert nested_if.true_branch == nested_if.false_branch
    assert if_op.false_branch == []
def test_if_multiple_ops():
    code = """
    start:
    emp = inbox
    if ez then
        outbox
        jmp start
    endif
    """
    with StringIO(code) as f:
        ast = parser.parse_it(f)

    if_op = ast[2]
    assert type(if_op) == parser.IfOp
    assert if_op.condition == "ez"
    assert if_op.true_branch == [parser.OutboxOp(), parser.JumpOp("start")]
Exemplo n.º 27
0
def calculate_optimized_ast(fhandle, args):
    result_ast = p.parse_it(fhandle)

    checker.perform_label_checks(result_ast)
    checker.perform_variable_checks(result_ast)

    result_ast = conversion.convert_ifnz_to_ifez(result_ast)
    result_ast = conversion.convert_iftojump(result_ast)
    if not args.no_jump_compression:
        result_ast = conversion.compress_jumps(result_ast)
    if not args.no_unreachable:
        result_ast = conversion.remove_unreachable_code(result_ast)
    if not args.no_jmp_then_label:
        unchanged = False
        while not unchanged:
            new_ast = conversion.fix_jmp_then_label(result_ast)
            unchanged = new_ast == result_ast
            result_ast = new_ast

    return result_ast
def test_assign_to_outbox():
    code = "outbox = emp"
    with StringIO(code) as f:
        with pytest.raises(ValueError):
            ast = parser.parse_it(f)
def test_compat_copyto_emp():
    code = "copyto emp"
    with StringIO(code) as f:
        with pytest.raises(ValueError):
            parser.parse_it(f)
def test_assign_from_outbox():
    code = "emp = outbox"
    with StringIO(code) as f:
        with pytest.raises(ValueError):
            ast = parser.parse_it(f)