From 088017bc05944ed80f91d5d8100a546c7bfedb70 Mon Sep 17 00:00:00 2001 From: Torsten Date: Sat, 29 Feb 2020 17:19:53 +0200 Subject: [PATCH] SA for Slotted derivations Object and Constant similarity are coming more into focus. Will unite after the merge --- lib/slot_machine/slotted_constant.rb | 10 +++--- lib/slot_machine/slotted_message.rb | 11 ++----- lib/slot_machine/slotted_object.rb | 25 ++++++++------- test/slot_machine/test_slotted_constant.rb | 6 ++-- test/slot_machine/test_slotted_message2.rb | 4 +-- test/slot_machine/test_slotted_object.rb | 36 ++++++++++++++++++++-- 6 files changed, 59 insertions(+), 33 deletions(-) diff --git a/lib/slot_machine/slotted_constant.rb b/lib/slot_machine/slotted_constant.rb index b8ee9e65..94f89cbe 100644 --- a/lib/slot_machine/slotted_constant.rb +++ b/lib/slot_machine/slotted_constant.rb @@ -22,16 +22,16 @@ module SlotMachine else type = :Object end - right = compiler.use_reg( type ) parfait = known_object.to_parfait(compiler) - const = Risc.load_constant(source, parfait , right) - compiler.add_code const + last = Risc.load_constant(source, parfait ) + compiler.add_code(last) if slots_length == 2 raise "only type allowed for constants, not #{slots}" unless slots.name == :type - compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right) + last = Risc.slot_to_reg( source , last.register , Parfait::TYPE_INDEX ) + compiler.add_code(last) end raise "Can't have slots into Constants #{slots}" if slots_length > 2 - return const.register + return last.register end end diff --git a/lib/slot_machine/slotted_message.rb b/lib/slot_machine/slotted_message.rb index 2336f1d6..553dab64 100644 --- a/lib/slot_machine/slotted_message.rb +++ b/lib/slot_machine/slotted_message.rb @@ -14,18 +14,13 @@ module SlotMachine # the code is added to compiler # the register returned def to_register(compiler, source) - type = :Message - right = compiler.use_reg( type ) slots = @slots - left = Risc.message_reg - left = left.resolve_and_add( slots.name , compiler) - reg = compiler.current.register - slots = slots.next_slot + left = Risc.message_named_reg while( slots ) left = left.resolve_and_add( slots.name , compiler) slots = slots.next_slot end - return reg + return left end # load the data in const_reg into the slot that is named by slot symbols @@ -36,7 +31,7 @@ module SlotMachine # most likely be united def reduce_and_load(const_reg , compiler , original_source ) raise "Not Message #{object}" unless known_object == :message - left = Risc.message_reg + left = Risc.message_named_reg slot = slots while( slot.next_slot ) left = left.resolve_and_add( slot.name , compiler) diff --git a/lib/slot_machine/slotted_object.rb b/lib/slot_machine/slotted_object.rb index 890540ce..ad373e31 100644 --- a/lib/slot_machine/slotted_object.rb +++ b/lib/slot_machine/slotted_object.rb @@ -19,23 +19,22 @@ module SlotMachine def to_register(compiler, source) type = known_object.get_type raise "not sym for #{known_object}" if type.is_a?(String) - right = compiler.use_reg( type ) - const = Risc.load_constant(source, known_object , right) - compiler.add_code const + last = Risc.load_constant(source, known_object) + compiler.add_code last if slots_length > 1 - # desctructively replace the existing value to be loaded if more slots - compiler.add_code Risc.slot_to_reg( source , right ,slots.name, right) + last = Risc.slot_to_reg( source , last.register ,slots.name) + compiler.add_code(last) end if slots_length > 2 - # desctructively replace the existing value to be loaded if more slots - index = Risc.resolve_to_index(slots.name , slots.next_slot.name ,compiler) - compiler.add_code Risc::SlotToReg.new( source , right ,index, right) - if slots_length > 3 - raise "3 slots only for type #{slots}" unless slots.next_slot.next_slot.name == :type - compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right) - end + last = Risc.slot_to_reg( source , last.register , slots.next_slot.name ) + compiler.add_code(last) end - return const.register + if slots_length > 3 + raise "3 slots only for type #{slots}" unless slots.next_slot.next_slot.name == :type + last = Risc.slot_to_reg( source , last.register , slots.next_slot.name ) + compiler.add_code(last) + end + return last.register end # Note: this is the left hand case, the right hand being to_register diff --git a/test/slot_machine/test_slotted_constant.rb b/test/slot_machine/test_slotted_constant.rb index 529bd0f7..86601862 100644 --- a/test/slot_machine/test_slotted_constant.rb +++ b/test/slot_machine/test_slotted_constant.rb @@ -14,7 +14,7 @@ module SlotMachine assert_equal Risc::LoadConstant , @instruction.class end def test_def_register - assert_equal :r1 , @instruction.register.symbol + assert @instruction.register.is_object? end def test_def_const assert_equal "hi" , @instruction.constant.to_string @@ -32,7 +32,7 @@ module SlotMachine assert_equal Risc::LoadConstant , @instruction.class end def test_def_register - assert_equal :r1 , @instruction.register.symbol + assert @instruction.register.is_object? end def test_def_const assert_equal "hi" , @instruction.constant.to_string @@ -41,7 +41,7 @@ module SlotMachine assert_equal "StringConstant.type" , @slotted.to_s end def test_def_register2 - assert_equal :r1 , @instruction.next.register.symbol + assert @instruction.next.register.is_object? end def test_def_next_index assert_equal 0 , @instruction.next.index diff --git a/test/slot_machine/test_slotted_message2.rb b/test/slot_machine/test_slotted_message2.rb index 11720893..f033017b 100644 --- a/test/slot_machine/test_slotted_message2.rb +++ b/test/slot_machine/test_slotted_message2.rb @@ -16,10 +16,10 @@ module SlotMachine assert_equal NilClass , @instruction.next.class end def test_def_array #from message r0 - assert_equal :r0 , @instruction.array.symbol + assert_equal :message , @instruction.array.symbol end def test_def_register # to next free register r1 - assert_equal :r2 , @register.symbol + assert_equal :"message.caller" , @register.symbol end def test_def_index # at caller index 6 assert_equal 6 , @instruction.index diff --git a/test/slot_machine/test_slotted_object.rb b/test/slot_machine/test_slotted_object.rb index 28c8ebb0..63f40b85 100644 --- a/test/slot_machine/test_slotted_object.rb +++ b/test/slot_machine/test_slotted_object.rb @@ -14,7 +14,7 @@ module SlotMachine assert_equal Risc::LoadConstant , @instruction.class end def test_def_register - assert_equal :r1 , @instruction.register.symbol + assert @instruction.register.is_object? end def test_def_const assert_equal Parfait::Space , @instruction.constant.class @@ -23,10 +23,42 @@ module SlotMachine assert_equal "Space.type" , @slotted.to_s end def test_def_register2 - assert_equal :r1 , @instruction.next.register.symbol + reg = @instruction.next.register + assert reg.is_object? + assert reg.symbol.to_s.index(".") , reg.symbol.to_s end def test_def_next_index assert_equal 0 , @instruction.next.index end end + class TestSlottedObjectType2 < MiniTest::Test + def setup + Parfait.boot!(Parfait.default_test_options) + compiler = Risc.test_compiler + @slotted = Slotted.for(Parfait.object_space , [:type , :type]) + register = @slotted.to_register(compiler , InstructionMock.new) + @instruction = compiler.risc_instructions.next + end + def test_def_register2 + reg = @instruction.next.register + assert reg.is_object? + assert_equal "type", reg.symbol.to_s.split(".").last + assert_equal 2, reg.symbol.to_s.split(".").length + end + end + class TestSlottedObjectType3 < MiniTest::Test + def setup + Parfait.boot!(Parfait.default_test_options) + compiler = Risc.test_compiler + @slotted = Slotted.for(Parfait.object_space , [:type , :type , :type]) + register = @slotted.to_register(compiler , InstructionMock.new) + @instruction = compiler.risc_instructions.next + end + def test_def_register3 + reg = @instruction.next.next.register + assert reg.is_object? + assert_equal "type", reg.symbol.to_s.split(".").last + assert_equal 3, reg.symbol.to_s.split(".").length + end + end end