コード例 #1
0
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')
    ]
コード例 #2
0
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)
    ]
コード例 #3
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)
    ]
コード例 #4
0
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')
    ]
コード例 #5
0
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)
    ]
コード例 #6
0
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)

    ]
コード例 #7
0
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()
    ]
コード例 #8
0
ファイル: buffer.py プロジェクト: ofples/thinglang
    def push_ref(self, ref: Reference,
                 source_ref: SourceReference) -> Reference:
        """
        Push down a reference object into the program stack
        """
        if isinstance(ref, LocalReference):
            self.append(OpcodePushLocal.from_reference(ref), source_ref)
        elif isinstance(ref, ElementReference):
            self.append(OpcodePushMember.from_reference(ref), source_ref)
        elif isinstance(ref, Reference):
            raise CalledInstanceMethodOnClass(ref, source_ref)
        else:
            raise Exception('Cannot push down {}'.format(ref))

        return ref
コード例 #9
0
def test_local_list_immediate_index():
    assert compile_snippet('lst[123]') == [
        OpcodePushStatic(STATIC_START),
        OpcodePushLocal(LST_ID),
        internal_call('list.get')
    ]
コード例 #10
0
ファイル: test_inlining.py プロジェクト: ytanay/thinglang
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
ファイル: test_inlining.py プロジェクト: ytanay/thinglang
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__')
    ]