From 4b303977a782a941dc53b8bebfaf1daf7a68b521 Mon Sep 17 00:00:00 2001 From: Torsten Date: Sun, 1 Mar 2020 18:07:42 +0200 Subject: [PATCH] Fix first Slot instruction that uses builder now without method_missing and names but still with instance_eval, hmm. Tried without, makes code much less readable --- lib/risc/builder.rb | 3 +++ lib/risc/callable_compiler.rb | 1 + lib/slot_machine/instruction/truth_check.rb | 8 ++++---- .../instruction/test_truth_check.rb | 17 +++++++++-------- test/support/risc_assert.rb | 6 +++++- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/risc/builder.rb b/lib/risc/builder.rb index cc1c62c9..f039df67 100644 --- a/lib/risc/builder.rb +++ b/lib/risc/builder.rb @@ -142,6 +142,9 @@ module Risc return ins end + def load_object(object) + @compiler.load_object(object) + end # for some methods that return an integer it is beneficial to pre allocate the # integer and store it in the return value. That is what this function does. # diff --git a/lib/risc/callable_compiler.rb b/lib/risc/callable_compiler.rb index dcc833db..4343fbb7 100644 --- a/lib/risc/callable_compiler.rb +++ b/lib/risc/callable_compiler.rb @@ -96,6 +96,7 @@ module Risc def load_object( object ) raise "must be Parfait, not #{object.class}" unless object.is_a?(Parfait::Object) ins = Risc.load_constant("load to #{object.type}" , object) + ins.register.set_compiler(self) add_code ins # todo for constants (not objects) # add_constant(right) if compiler diff --git a/lib/slot_machine/instruction/truth_check.rb b/lib/slot_machine/instruction/truth_check.rb index 61672053..339ddcff 100644 --- a/lib/slot_machine/instruction/truth_check.rb +++ b/lib/slot_machine/instruction/truth_check.rb @@ -20,13 +20,13 @@ module SlotMachine def to_risc(compiler) false_label = @false_label.risc_label(compiler) - builder = compiler.builder("TruthCheck") condition_reg = @condition.to_register(compiler,self) - builder.build do - object! << Parfait.object_space.false_object + + compiler.build(self.to_s) do + object = load_object Parfait.object_space.false_object object.op :- , condition_reg if_zero false_label - object << Parfait.object_space.nil_object + object = load_object Parfait.object_space.nil_object object.op :- , condition_reg if_zero false_label end diff --git a/test/slot_machine/instruction/test_truth_check.rb b/test/slot_machine/instruction/test_truth_check.rb index bf9eb438..11dc2555 100644 --- a/test/slot_machine/instruction/test_truth_check.rb +++ b/test/slot_machine/instruction/test_truth_check.rb @@ -7,18 +7,19 @@ module SlotMachine TruthCheck.new(target , Label.new("ok" , "target")) end def test_len - assert_equal 8 , all.length , all_str + assert_equal 8 , all.length + assert_equal Risc::Label , all.first.class end def test_1_slot - assert_slot_to_reg risc(1) ,:r0 , 6 , :r2 + assert_slot_to_reg risc(1) ,:message , 6 , :"message.caller" end def test_2_load - assert_load risc(2) , Parfait::FalseClass , :r3 + assert_load risc(2) , Parfait::FalseClass end def test_3_op assert_equal Risc::OperatorInstruction , risc(3).class - assert_equal :r3 , risc(3).left.symbol - assert_equal :r2 , risc(3).right.symbol + assert risc(3).left.is_object? + assert_equal :"message.caller" , risc(3).right.symbol assert_equal :- , risc(3).operator end def test_4_zero @@ -26,12 +27,12 @@ module SlotMachine assert_label risc(4).label , "target" end def test_5_load - assert_load risc(5) , Parfait::NilClass , :r3 + assert_load risc(5) , Parfait::NilClass end def test_6_op assert_equal Risc::OperatorInstruction , risc(6).class - assert_equal :r3 , risc(6).left.symbol - assert_equal :r2 , risc(6).right.symbol + assert risc(6).left.is_object? + assert_equal :"message.caller" , risc(6).right.symbol assert_equal :- , risc(6).operator end def test_7_zero diff --git a/test/support/risc_assert.rb b/test/support/risc_assert.rb index 4fae6868..bdc2a7b9 100644 --- a/test/support/risc_assert.rb +++ b/test/support/risc_assert.rb @@ -18,7 +18,11 @@ module Minitest def assert_load(load , clazz = nil , register = nil) 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 + if register + assert_equal( register , load.register.symbol, "wrong destination register") if register + else + assert load.register.is_object? , "reg #{load.register.symbol} is not object (ie no id_xx)" + end end def assert_transfer( transfer , from , to) assert_equal Risc::Transfer , transfer.class