From 04bcfea8ceca4d865632468ebdc09c7f07fbd8c7 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 31 Jul 2018 18:00:42 +0300 Subject: [PATCH] fix scoping of blockcompiler Was accessing caller scope, but must use caller's caller as the yield itself is a call. --- lib/risc/block_compiler.rb | 4 +- test/mom/send/test_setup_simple.rb | 1 - .../interpreter/blocks/test_assign_outer.rb | 63 +++++++++++++++++++ ...k_assign_local.rb => test_block_return.rb} | 38 +++-------- test/support/risc_assert.rb | 8 +-- test/vool/blocks/test_assign.rb | 2 +- 6 files changed, 80 insertions(+), 36 deletions(-) create mode 100644 test/risc/interpreter/blocks/test_assign_outer.rb rename test/risc/interpreter/blocks/{test_block_assign_local.rb => test_block_return.rb} (64%) diff --git a/lib/risc/block_compiler.rb b/lib/risc/block_compiler.rb index 95840234..b480d15d 100644 --- a/lib/risc/block_compiler.rb +++ b/lib/risc/block_compiler.rb @@ -39,9 +39,9 @@ module Risc elsif @callable.frame_type.variable_index(name) slot_def = [:frame] elsif @method.arguments_type.variable_index(name) - slot_def = [:caller , :arguments ] + slot_def = [:caller , :caller ,:arguments ] elsif @method.frame_type.variable_index(name) - slot_def = [:caller , :frame ] + slot_def = [:caller ,:caller , :frame ] elsif raise "no variable #{name} , need to resolve at runtime" end diff --git a/test/mom/send/test_setup_simple.rb b/test/mom/send/test_setup_simple.rb index ba993977..426d8638 100644 --- a/test/mom/send/test_setup_simple.rb +++ b/test/mom/send/test_setup_simple.rb @@ -3,7 +3,6 @@ require_relative "../helper" module Risc class TestMessageSetupSimple < MiniTest::Test include Statements - include Assertions def setup super diff --git a/test/risc/interpreter/blocks/test_assign_outer.rb b/test/risc/interpreter/blocks/test_assign_outer.rb new file mode 100644 index 00000000..d66a6c3a --- /dev/null +++ b/test/risc/interpreter/blocks/test_assign_outer.rb @@ -0,0 +1,63 @@ +require_relative "../helper" + +module Risc + class BloclAssignOuter < MiniTest::Test + include Ticker + + def setup + @string_input = block_main("a = 15 ;yielder {a = 10 ; return 15} ; return a") + super + end + + def test_chain + #show_main_ticks # get output of what is + check_main_chain [LoadConstant, SlotToReg, RegToSlot, LoadConstant, SlotToReg, + RegToSlot, LoadConstant, LoadConstant, SlotToReg, RegToSlot, + RegToSlot, SlotToReg, SlotToReg, Branch, RegToSlot, + SlotToReg, SlotToReg, RegToSlot, RegToSlot, SlotToReg, + RegToSlot, SlotToReg, SlotToReg, RegToSlot, SlotToReg, + SlotToReg, SlotToReg, Branch, SlotToReg, RegToSlot, + LoadConstant, SlotToReg, RegToSlot, SlotToReg, FunctionCall, + LoadConstant, SlotToReg, OperatorInstruction, IsZero, SlotToReg, + SlotToReg, LoadConstant, SlotToReg, RegToSlot, RegToSlot, + SlotToReg, SlotToReg, RegToSlot, Branch, SlotToReg, + SlotToReg, RegToSlot, RegToSlot, SlotToReg, RegToSlot, + SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg, + RegToSlot, SlotToReg, Branch, SlotToReg, SlotToReg, + DynamicJump, LoadConstant, SlotToReg, SlotToReg, SlotToReg, + RegToSlot, LoadConstant, RegToSlot, SlotToReg, SlotToReg, + RegToSlot, SlotToReg, SlotToReg, SlotToReg, Branch, + FunctionReturn, SlotToReg, SlotToReg, RegToSlot, SlotToReg, + SlotToReg, RegToSlot, SlotToReg, SlotToReg, RegToSlot, + SlotToReg, Branch, SlotToReg, SlotToReg, FunctionReturn, + SlotToReg, SlotToReg, RegToSlot, SlotToReg, SlotToReg, + RegToSlot, Branch, SlotToReg, SlotToReg, SlotToReg, + FunctionReturn, Transfer, SlotToReg, SlotToReg, Branch, + Syscall, NilClass] + assert_equal 10 , get_return + end + + def test_block_jump + load_ins = main_ticks(66) + assert_equal DynamicJump , load_ins.class + assert_equal Parfait::Block , @interpreter.get_register(load_ins.register).class + end + def test_block_load + load_ins = main_ticks(67) + assert_load load_ins , Parfait::Integer , :r1 + assert_equal 10 , @interpreter.get_register(load_ins.register).value + end + def test_block_slot1 + assert_slot_to_reg main_ticks(68) ,:r0 , 6 , :r2 + end + def test_block_slot2 + assert_slot_to_reg main_ticks(69) ,:r2 , 6 , :r2 + end + def test_block_slot3 + assert_slot_to_reg main_ticks(70) ,:r2 , 3 , :r2 + end + def test_block_reg + assert_reg_to_slot main_ticks(71) ,:r1 , :r2 , 1 + end + end +end diff --git a/test/risc/interpreter/blocks/test_block_assign_local.rb b/test/risc/interpreter/blocks/test_block_return.rb similarity index 64% rename from test/risc/interpreter/blocks/test_block_assign_local.rb rename to test/risc/interpreter/blocks/test_block_return.rb index f1524515..8ed96460 100644 --- a/test/risc/interpreter/blocks/test_block_assign_local.rb +++ b/test/risc/interpreter/blocks/test_block_return.rb @@ -1,7 +1,7 @@ require_relative "../helper" module Risc - class BlockAssignLocal < MiniTest::Test + class BlockReturn < MiniTest::Test include Ticker def setup @@ -36,36 +36,18 @@ module Risc assert_equal 15 , get_return end - def est_call_main - call_ins = ticks(main_at) - assert_equal FunctionCall , call_ins.class - assert :main , call_ins.method.name - end - def est_load_yield - load_ins = main_ticks(4) + def test_load_return + load_ins = main_ticks(56) assert_equal LoadConstant , load_ins.class - assert_equal Parfait::CallableMethod , @interpreter.get_register(load_ins.register).class - assert_equal :yielder , @interpreter.get_register(load_ins.register).name - end - def est_load_space - load_ins = main_ticks(5) - assert_equal LoadConstant , load_ins.class - assert_equal Parfait::Space , @interpreter.get_register(load_ins.register).class - end - def est_op - op = main_ticks(35) - assert_equal OperatorInstruction , op.class - assert_equal :- , op.operator - end - def est_load_block - load_ins = main_ticks(39) - assert_equal LoadConstant , load_ins.class - assert_equal Parfait::Space , @interpreter.get_register(load_ins.register).class + assert_equal Parfait::ReturnAddress , @interpreter.get_register(load_ins.register).class end - def pest_sys - sys = main_ticks(18) - assert_equal Syscall , sys.class + def test_load_block + load_ins = main_ticks(63) + assert_equal DynamicJump , load_ins.class + assert_equal Parfait::Block , @interpreter.get_register(load_ins.register).class + assert_equal :main_block , @interpreter.get_register(load_ins.register).name end + end end diff --git a/test/support/risc_assert.rb b/test/support/risc_assert.rb index 7a1cc752..637b4882 100644 --- a/test/support/risc_assert.rb +++ b/test/support/risc_assert.rb @@ -1,19 +1,19 @@ -module Risc +module Minitest module Assertions def assert_slot_to_reg( slot , array = nil, index = nil , register = nil) - assert_equal SlotToReg , slot.class + assert_equal Risc::SlotToReg , slot.class assert_equal( array , slot.array.symbol , "wrong source register") if array assert_equal( index , slot.index, "wrong source index") if index assert_equal( register , slot.register.symbol, "wrong destination") if register end def assert_reg_to_slot( slot , register = nil, array = nil, index = nil ) - assert_equal RegToSlot , slot.class + assert_equal Risc::RegToSlot , slot.class assert_equal( register , slot.register.symbol, "wrong source register") if register assert_equal( array , slot.array.symbol, "wrong destination register") if array assert_equal( index , slot.index, "wrong destination index") if index end def assert_load(load , clazz = nil , register = nil) - assert_equal LoadConstant , load.class + assert_equal Risc::LoadConstant , load.class assert_equal( clazz , load.constant.class) if clazz assert_equal( register , load.register.symbol, "wrong destination register") if register end diff --git a/test/vool/blocks/test_assign.rb b/test/vool/blocks/test_assign.rb index 0f363061..7db7b0ab 100644 --- a/test/vool/blocks/test_assign.rb +++ b/test/vool/blocks/test_assign.rb @@ -61,7 +61,7 @@ module VoolBlocks assert @ins.left end def test_slots_left - assert_equal [:caller, :arguments, :arg] , @ins.left.slots + assert_equal [:caller,:caller, :arguments, :arg] , @ins.left.slots end end