def test_local_list_non_immediate_index(): assert compile_snippet('lst[a]') == [ OpcodePushLocal(A_ID), OpcodePushLocal(LST_ID), internal_call('list.get') ] assert compile_snippet('lst[self.val1]') == [ OpcodePushMember(SELF_ID, VAL1_ID), OpcodePushLocal(LST_ID), internal_call('list.get') ]
def test_member_access_via_method_call(): assert compile_snippet('a_inst.me().a1') == [ OpcodePushLocal(A_INST), OpcodeCallVirtual(1), OpcodeDereference(0) ] assert compile_snippet('a_inst.me().me().a1') == [ OpcodePushLocal(A_INST), OpcodeCallVirtual(1), OpcodeCallVirtual(1), OpcodeDereference(0) ]
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) ]
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_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) ]
def test_iteration_loop(): assert compile_snippet('for Container x in lst') == [ OpcodePushLocal(LST_ID), internal_call('list.iterator'), # Create iterator OpcodePopLocal(IMPLICIT_ITERATOR_ID), # Insert it into the frame OpcodePushLocal(IMPLICIT_ITERATOR_ID), # TODO: is this optimal? internal_call('iterator.has_next'), OpcodeJumpConditional(30), # Jump outside if not OpcodePushLocal(IMPLICIT_ITERATOR_ID), internal_call('iterator.next'), # Call next OpcodePopLocal(IMPLICIT_ITERATION_ID), # Insert into frame OpcodeJump(23) ]
def test_constructor_compilation(): assert compile_template(thing_id=A_THING) == [ OpcodeInstantiate(2, 0), # Type ID 2, implicit constructor (0-arg) OpcodeReturn() ] assert compile_template(thing_id=B_THING) == [ OpcodeInstantiate(3, 1), # Type ID 3, explicit constructor (1-arg) OpcodePushLocal(1), # Push the first argument OpcodePopMember(0, 1), # Set it as the second member of self OpcodeReturn() ]
def test_local_list_immediate_index(): assert compile_snippet('lst[123]') == [ OpcodePushStatic(STATIC_START), OpcodePushLocal(LST_ID), internal_call('list.get') ]
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__') ]
def test_inlining_binary_op_variables(): assert compile_base(INLINING_TEST_PROGRAM.format('self.add(n1, n2)'), trim=2) == [ OpcodePushLocal(2), OpcodePushLocal(1), internal_call('number.__addition__') ]