コード例 #1
0
def test_for():
    source = textwrap.dedent('''\
        for x in range(10):
            y = x + x
        z = 1
        ''')

    expected = as_masm('''\
        set x 0
        jump 5 greaterThanEq x 10
        op add y x x
        op add x x 1
        jump 2 lessThan x 10
        set z 1
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected

    source = textwrap.dedent('''\
        for x in range(5, 10):
            y = x + x
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert 'set x 5' in masm

    source = textwrap.dedent('''\
        for x in range(0, 10, 3):
            y = x + x
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert 'op add x x 3' in masm
コード例 #2
0
def test_err_invalid_def():
    with pytest.raises(pyndustric.CompilerError, match=pyndustric.ERR_INVALID_DEF):
        pyndustric.Compiler().compile('def foo(a=None): pass')

    with pytest.raises(pyndustric.CompilerError, match=pyndustric.ERR_INVALID_DEF):
        pyndustric.Compiler().compile('def foo(*, a): pass')

    with pytest.raises(pyndustric.CompilerError, match=pyndustric.ERR_INVALID_DEF):
        pyndustric.Compiler().compile('def foo(a, /, b): pass')
コード例 #3
0
def test_err_complex_value():
    with pytest.raises(pyndustric.CompilerError, match=pyndustric.ERR_COMPLEX_VALUE):
        pyndustric.Compiler().compile('a = 1 + (2 + 3)')

    with pytest.raises(pyndustric.CompilerError, match=pyndustric.ERR_COMPLEX_VALUE):
        pyndustric.Compiler().compile('a += 1 + 2')

    with pytest.raises(pyndustric.CompilerError, match=pyndustric.ERR_COMPLEX_VALUE):
        pyndustric.Compiler().compile('a = 1 + 2j')
コード例 #4
0
def test_for_functions():
    # The test is set for default inline functions, but uncommenting either @function will switch that
    def source():
        #@function
        def square(i): return i*i
        #@function
        def hyper(n):
            a = 2
            for i in range(n):
                a = square(a)
            return a
        b = hyper(4)

    expected = as_masm('''\
        set a 2
        set i 0
        jump 6 greaterThanEq i 4
        op mul a a a
        op add i i 1
        jump 3 lessThan i 4
        set b a
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert expected == masm
コード例 #5
0
ファイル: __main__.py プロジェクト: o7-Fire/pyndustric
def main():
    args = parse_args()
    complete_masm = ""
    for file in args.files:
        print(f"# reading {file}...", file=sys.stderr)
        if file == "-":
            source = sys.stdin.read()
        else:
            with open(file, encoding="utf-8") as fd:
                source = fd.read()

        print(f"# compiling {file}...", file=sys.stderr)
        start = time.time()
        masm = pyndustric.Compiler().compile(source)
        complete_masm += masm
        took = time.time() - start
        print(masm)
        print(
            f"# compiled {file} with pyndustric {pyndustric.__version__} in {took:.2f}s",
            file=sys.stderr,
        )

    if args.clipboard:
        import ait

        ait.copy(complete_masm)
コード例 #6
0
def test_while_or():
    # this tests both negated-or (entering loop) and affirmative-or (test to loop back up)
    source = textwrap.dedent('''\
        x = 0
        y = 1
        while x or y:
            x = y
            y = y - x
        z = 1
        ''')

    expected = as_masm('''\
        set x 0
        set y 1
        jump 4 notEqual x 0 
        jump 8 Equal y 0 
        set x y
        op sub y y x
        jump 4 notEqual x 0 
        jump 4 notEqual y 0 
        set z 1
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #7
0
def test_env():
    # TODO this seems not to work
    source = textwrap.dedent('''\
        this = Env.this()
        x = Env.x()
        y = Env.y()
        pc = Env.counter()
        links = Env.link_count()
        time = Env.time()
        width = Env.width()
        height = Env.height()

        for link in Env.links():
            pass
        ''')

    expected = as_masm('''\
        set this @this
        set x @thisx
        set y @thisy
        set pc @counter
        set links @links
        set time @time
        set width @mapw
        set height @maph
        set __pyc_it_10_12 0
        jump 14 greaterThanEq __pyc_it_10_12 @links
        getlink link __pyc_it_10_12
        op add __pyc_it_10_12 __pyc_it_10_12 1
        jump 10 always
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #8
0
def test_while_and():
    # this tests both negated-and (entering loop) and affirmative-and (test to loop back up)
    source = textwrap.dedent('''\
        x = 10
        y = 5
        while x and y:
            x -= 1
            y -= 1
        z = 1
        ''')

    expected = as_masm('''\
        set x 10
        set y 5
        jump 8 equal x 0 
        jump 8 equal y 0 
        op sub x x 1
        op sub y y 1
        jump 8 equal x 0 
        jump 4 notEqual y 0 
        set z 1
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #9
0
def test_draw():
    source = textwrap.dedent('''\
        from pyndustri import *

        Screen.clear(255, 0, 0)
        Screen.color(0, 255, 255)
        Screen.stroke(2)
        Screen.line(0, 0, 80, 80)
        Screen.rect(0, 0, 20, 20)
        Screen.hollow_rect(0, 0, 40, 40)
        Screen.poly(60, 60, 10, 3)
        Screen.hollow_poly(60, 60, 20, 5)
        Screen.triangle(70, 80, 80, 80, 80, 70)
        Screen.flush()
        ''')

    expected = as_masm('''\
        draw clear 255 0 0
        draw color 0 255 255 255
        draw stroke 2
        draw line 0 0 80 80
        draw rect 0 0 20 20
        draw lineRect 0 0 40 40
        draw poly 60 60 3 10 0
        draw linePoly 60 60 5 20 0
        draw triangle 70 80 80 80 80 70
        drawflush display1
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #10
0
def test_if_elif_else():
    source = textwrap.dedent('''\
        x = 1
        if x == 0:
            y = 1
        elif x == 1:
            y = 2
        else:
            y = 3
        ''')

    # TODO detect jump-to-jump and rewrite to follow the chain
    #     Telos: I think my re-implementation of this probably fixed whatever you'd been worried about???
    expected = as_masm('''\
        set x 1
        jump 5 notEqual x 0
        set y 1
        jump 9 always
        jump 8 notEqual x 1
        set y 2
        jump 9 always
        set y 3
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #11
0
def test_def_call_as_call_arg():
    # the inline version is two instructions (which makes sense), and the out-of-line function is 9
    def source():
        @inline
        def isquare(n): return n**2

        @function
        def fsquare(n): return n**2

        r = isquare(isquare(2))
        s = fsquare(fsquare(2))

    expected = as_masm('''\
        op pow __temp_0 2 2
        op pow r __temp_0 2
        set __fsquare_n 2
        op add __fsquare_RETURN_LINE @counter 1
        jump 10 always
        set __fsquare_n __fsquare_RETURN_VALUE
        op add __fsquare_RETURN_LINE @counter 1
        jump 10 always
        set s __fsquare_RETURN_VALUE
        jump 0 always
        op pow __fsquare_RETURN_VALUE __fsquare_n 2
        set @counter __fsquare_RETURN_LINE
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #12
0
def test_inline_def_shielding():
    """This embeds a copy of the function each place it is "called", substituting the given values in place of its
       args.  As a consequence, this inline function adjusts the value of the given arg y, much like C's ++y.
       The second call shields y from being further modified with the 0+'s."""
    def source():
        @inline
        def inc(x):
            x = x+1
            return x
        y = 1
        z = inc(y) * inc(y)
        w = inc(0+y) * inc(0+y)

    expected = as_masm('''\
        set y 1
        op add y y 1
        set __temp_0 y
        op add y y 1
        set __temp_1 y
        op mul z __temp_0 __temp_1
        op add __temp_3 0 y
        op add __temp_3 __temp_3 1
        set __temp_2 __temp_3
        op add __temp_5 0 y
        op add __temp_5 __temp_5 1
        set __temp_4 __temp_5
        op mul w __temp_2 __temp_4
        ''')
    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #13
0
def test_compile_function():
    def source():
        """Skip me!"""
        x = 1

    def source2():
        x = 1

    expected = as_masm("""\
        set x 1
        """)

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected

    masm = pyndustric.Compiler().compile(source2)
    assert masm == expected
コード例 #14
0
def test_object_attribute():
    def source(container1):
        pf = container1.phase_fabric

    expected = as_masm('''\
        sensor pf container1 @phase-fabric
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #15
0
def test_no_compile_method():
    class Foo:
        def bar(self):
            pass

    foo = Foo()

    with pytest.raises(pyndustric.CompilerError,
                       match=pyndustric.ERR_INVALID_SOURCE):
        pyndustric.Compiler().compile(foo.bar)
コード例 #16
0
def test_sensor():
    source = textwrap.dedent('''\
        pf = Sensor.phase_fabric(container1)
        ''')

    expected = as_masm('''\
        sensor pf container1 @phase-fabric
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #17
0
def test_multi_assignment():
    def source():
        x = y = z = 0

    expected = as_masm('''\
        set x 0
        set y x
        set z x
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #18
0
def test_function_ops():
    def source():
        a = abs( 1 )
        lo = min( 1, 2 )

    expected = as_masm('''\
        op abs a 1
        op min lo 1 2
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #19
0
def test_compile_path():
    expected = as_masm("""\
        set x 1
        """)

    with tempfile.TemporaryDirectory() as folder:
        file = pathlib.Path(folder) / "test_compile_path.py"
        with file.open("w") as fd:
            fd.write("x = 1\n")

        masm = pyndustric.Compiler().compile(file)
        assert masm == expected
コード例 #20
0
def test_radar():
    def source(ripple1):
        u1 = Unit.radar( enemy, flying, order = max, key = distance)
        u2 = ripple1.radar( ally, key = health )

    expected = as_masm('''\
        uradar enemy flying any distance @unit 0 u1
        radar ally any any health ripple1 1 u2
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #21
0
def test_aug_assignments():
    source = textwrap.dedent('''\
        x = 1
        x += 1
        ''')

    expected = as_masm('''\
        set x 1
        op add x x 1
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #22
0
def test_inspect_function_source():
    """Test that compile can accept a function as its argument.  That function's body will be compiled.
       Note: the function must be one whose source code can be found by inspect.getsource, which looks for
       at the current version of the file in which that function was originally defined."""
    def source():
        """Doc string can be included for human readers, but will be ignored by compiler."""
        x = 1

    expected = as_masm('''\
        set x 1
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #23
0
def test_assignments():
    source = textwrap.dedent('''\
        x = 1
        y = x + 2
        z = x == y
        ''')

    expected = as_masm('''\
        set x 1
        op add y x 2
        op equal z x y
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #24
0
def test_complex_if():
    source = textwrap.dedent('''\
        x = 1
        if x < 10:
            y = 1
        ''')

    expected = as_masm('''\
        set x 1
        jump 4 greaterThanEq x 10
        set y 1
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #25
0
def test_control():
    source = textwrap.dedent('''\
        Control.enabled(reactor, False)
        Control.shoot(duo1, 10, 20)
        Control.ceasefire(scatter1)
        ''')

    expected = as_masm('''\
        control enabled reactor false
        control shoot duo1 10 20 1
        control shoot scatter1 0 0 0
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #26
0
def test_complex_assign():
    def source():
        a = 2*x + 1
        d = sqrt(x*x + y*y)

    expected = as_masm('''\
        op mul __temp_0 2 x
        op add a __temp_0 1
        op mul __temp_2 x x
        op mul __temp_3 y y
        op add __temp_1 __temp_2 __temp_3
        op sqrt d __temp_1
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #27
0
ファイル: __main__.py プロジェクト: TelosTelos/pyndustric
def main():
    for file in sys.argv[1:]:
        print(f'# reading {file}...', file=sys.stderr)
        if file == '-':
            source = sys.stdin.read()
        else:
            with open(file, encoding='utf-8') as fd:
                source = fd.read()

        print(f'# compiling {file}...', file=sys.stderr)
        start = time.time()
        masm = pyndustric.Compiler().compile(source)
        took = time.time() - start
        print(masm)
        print(
            f'# compiled {file} with pyndustric {pyndustric.__version__} in {took:.2f}s',
            file=sys.stderr)
コード例 #28
0
def test_types():
    source = textwrap.dedent('''\
        x = True
        y = 1
        z = 0.1
        a = "string"
        ''')

    expected = as_masm('''\
        set x true
        set y 1
        set z 0.1
        set a "string"
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #29
0
def test_while():
    source = textwrap.dedent('''\
        x = 10
        while x:
            x = x - 1
        z = 1
        ''')

    expected = as_masm('''\
        set x 10
        jump 5 equal x 0
        op sub x x 1
        jump 3 notEqual x 0
        set z 1
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected
コード例 #30
0
def test_complex_while():
    source = textwrap.dedent('''\
        x = 10
        while x>5:
            x -= 1
        z = 1
        ''')

    expected = as_masm('''\
        set x 10
        jump 5 lessThanEq x 5
        op sub x x 1
        jump 3 greaterThan x 5
        set z 1
        ''')

    masm = pyndustric.Compiler().compile(source)
    assert masm == expected