def test_static_to_member_of_indexed():
    assert compile_snippet('lst[0].inner1 = 5') == [
        OpcodePushStatic(STATIC_START),  # Push index 0
        OpcodePushStatic(STATIC_START + 1),  # Push constant 5
        OpcodePushLocal(LST_ID),  # Push the list
        internal_call('list.get'),
        OpcodePopDereferenced(INNER1_ID)
    ]
예제 #2
0
def test_simple_continue():
    assert compile_snippet({'while true': ['Console.write("")', 'continue', 'Console.write("")']}) == [
        OpcodePushStatic(STATIC_START),
        OpcodeJumpConditional(28),
        OpcodePushStatic(STATIC_START + 1),
        internal_call('Console.write'),
        OpcodeJump(20),
        OpcodePushStatic(STATIC_START + 2),
        internal_call('Console.write'),
        OpcodeJump(20)
    ]
예제 #3
0
def test_simple_break():
    assert compile_snippet({'while true': ['Console.write("")', 'break', 'Console.write("")']}) == [
        OpcodePushStatic(STATIC_START),
        OpcodeJumpConditional(28),
        OpcodePushStatic(STATIC_START + 1),
        internal_call('Console.write'),
        OpcodeJump(28), # TODO: optimize case where `if something: break`
        OpcodePushStatic(STATIC_START + 2),
        internal_call('Console.write'),
        OpcodeJump(20)
    ]
def test_static_to_indexed():
    assert compile_snippet('lst[0] = Container()') == [
        OpcodePushStatic(STATIC_START),  # Push index 0
        OpcodeCallStatic(CONTAINER, 0),  # Push constant 5
        OpcodePushLocal(LST_ID),  # Push the list
        internal_call('list.set')
    ]
def test_static_to_local():
    assert compile_snippet('a = 5') == [OpcodeAssignStatic(A_ID, STATIC_START)]
    assert compile_snippet('a = "hello"') == [
        OpcodePushStatic(STATIC_START),
        internal_call('text.as number'),
        OpcodePopLocal(A_ID)
    ]
def test_access_in_method_args():
    assert compile_snippet('self.action(lst[123])') == [
        OpcodePushStatic(STATIC_START),
        OpcodePushLocal(LST_ID),
        internal_call('list.get'),
        OpcodePushLocal(SELF_ID),
        OpcodeCallVirtual(1)
    ]

    assert compile_snippet('self.action(lst[self.val1])') == [
        OpcodePushMember(SELF_ID, VAL1_ID),
        OpcodePushLocal(LST_ID),
        internal_call('list.get'),
        OpcodePushLocal(SELF_ID),
        OpcodeCallVirtual(1)
    ]
예제 #7
0
def test_local_list_immediate_index():
    assert compile_snippet('lst[123]') == [
        OpcodePushStatic(STATIC_START),
        OpcodePushLocal(LST_ID),
        internal_call('list.get')
    ]
예제 #8
0
 def compile(self, context):
     ref = context.append_static(self.serialize())
     context.append(OpcodePushStatic(ref), self.source_ref)
     return self
예제 #9
0
def test_inlining_second_call_simple():
    assert compile_base(INLINING_TEST_PROGRAM.format('self.external_call("Hello")'), trim=2) == [
        OpcodePushStatic(2),
        internal_call('File.__constructor__')
    ]
예제 #10
0
def test_inlining_binary_op_mixed():
    assert compile_base(INLINING_TEST_PROGRAM.format('self.add(2, n1)'), trim=2) == [
        OpcodePushLocal(1),
        OpcodePushStatic(2),
        internal_call('number.__addition__')
    ]
예제 #11
0
def test_inlining_binary_op_constants():
    assert compile_base(INLINING_TEST_PROGRAM.format('self.add(1, 2)'), trim=2) == [
        OpcodePushStatic(2),
        OpcodePushStatic(3),
        internal_call('number.__addition__')
    ]
예제 #12
0
def test_simple_conditional():
    assert compile_snippet({'if "dog" == "dog"': ['Console.write("executing")']}) == PREFIX + [
        OpcodeJumpConditional(26),
        OpcodePushStatic(STATIC_START + 2),
        internal_call('Console.write')
    ]
예제 #13
0
from tests.compiler import compile_snippet, STATIC_START, internal_call
from thinglang.compiler.opcodes import OpcodePushStatic, OpcodeJumpConditional, OpcodeJump

PREFIX = [
    OpcodePushStatic(STATIC_START),
    OpcodePushStatic(STATIC_START + 1),
    internal_call('text.__equals__'),
]

def test_simple_conditional():
    assert compile_snippet({'if "dog" == "dog"': ['Console.write("executing")']}) == PREFIX + [
        OpcodeJumpConditional(26),
        OpcodePushStatic(STATIC_START + 2),
        internal_call('Console.write')
    ]


def test_empty_conditional():
    assert compile_snippet({'if "dog" == "dog"': ['pass']}) == PREFIX + [
        OpcodeJumpConditional(24)
    ]