Esempio n. 1
0
def test_can_calculate_factorial():
    vm = quaff.VirtualMachine()
    vm.run(
        [
            # n:
            quaff.inst_const(6),  # [6]
            # n!:
            quaff.inst_const(1),  # [T1, 6]
            quaff.inst_swap(1),  # [6, T1]
            quaff.inst_dup(),  # [6, 6, T1]
            quaff.inst_const(0),  # [0, 6, 6, T1]
            quaff.inst_cmp(),  # [1, 6, T1]
            quaff.inst_jle(7),  # [6, T1]
            quaff.inst_dup(),  # [6, 6, T1]
            quaff.inst_swap(2),  # [T1, 6, 6]
            quaff.inst_mul(),  # [T6, 6]
            quaff.inst_swap(1),  # [6, T6]
            quaff.inst_const(-1),  # [-1, 6, T6]
            quaff.inst_add(),  # [5, T6]
            quaff.inst_jmp(-11),
            quaff.inst_pop(),
        ]
    )
    assert_stack_elements(vm, [720])
Esempio n. 2
0
def test_const_then_pop():
    vm = quaff.VirtualMachine()
    vm.run([quaff.inst_const(42), quaff.inst_const(64), quaff.inst_pop()])
    assert_stack_elements(vm, [42])