From 3ec5557ddb693d571fc89726dfb64159e5418c95 Mon Sep 17 00:00:00 2001 From: Torsten Date: Sat, 7 Mar 2020 19:23:19 +0200 Subject: [PATCH] fix builder logic error and tests --- lib/risc/builder.rb | 12 ++++----- test/slot_machine/macro/helper.rb | 44 +++++++++++++++---------------- test/support/risc_assert.rb | 34 +++++++++++++++++------- 3 files changed, 52 insertions(+), 38 deletions(-) diff --git a/lib/risc/builder.rb b/lib/risc/builder.rb index 49e989ff..f082a52a 100644 --- a/lib/risc/builder.rb +++ b/lib/risc/builder.rb @@ -144,13 +144,13 @@ module Risc null = load_object Parfait.object_space.nil_object int = nil build do - null.op :- , factory[:next_object] - if_not_zero cont_label - factory[:next_object] << factory[:reserve] - call_get_more - add_code cont_label int = factory[:next_object].to_reg - factory[:next_object] << int[:next_integer] + null.op :- , int + if_not_zero cont_label + factory[:next_object] << factory[:reserve] + call_get_more + add_code cont_label + factory[:next_object] << factory[:next_object][:next_integer] end int end diff --git a/test/slot_machine/macro/helper.rb b/test/slot_machine/macro/helper.rb index 57da0763..aa1cf8a6 100644 --- a/test/slot_machine/macro/helper.rb +++ b/test/slot_machine/macro/helper.rb @@ -29,31 +29,29 @@ module SlotMachine assert_label at + 7 , "unreachable" end def assert_allocate - assert_load 1 , Parfait::Factory - assert_slot_to_reg 2 , :r2 , 2 , :r1 - assert_load 3 , Parfait::NilClass - assert_operator 4 , :- , :r3 , :r1 - assert_not_zero 5 , :label - assert risc(5).label.name.to_s.start_with?("cont_label") - assert_slot_to_reg 6 , :r2 , 3 , :r4 - assert_reg_to_slot 7 ,:r4 , :r2 , 2 - assert_load 8 , Parfait::CallableMethod - assert_slot_to_reg 9 , :r0 , 1 , :r6 - assert_reg_to_slot 10 , :r5 , :r6 , 7 - assert_load 11 , Parfait::Factory - assert_reg_to_slot 12 , :r7 , :r0 , 2 - assert_load 13 , Risc::Label - assert_slot_to_reg 14 , :r0 , 1 , :r9 - assert_slot_to_reg 14 ,:r0,1,:r9 - assert_reg_to_slot 15 ,:r8,:r9,4 - assert_slot_to_reg 16 ,:r0 , 1 , :r0 + assert_load 1 , Parfait::Factory , "id_factory_" + assert_load 2 , Parfait::NilClass , "id_nilclass_" + assert_slot_to_reg 3 , "id_factory_" , 2 , "id_factory_.next_object" + assert_operator 4 , :- , "id_nilclass_" , "id_factory_.next_object" + assert_zero 5 , "cont_label" + assert_slot_to_reg 6 , "id_factory_" , 2 , "id_factory_.next_object" + assert_reg_to_slot 7 , "id_factory_.next_object" , "id_factory_" , 2 + assert_load 8 , Parfait::Factory , "id_factory_" + assert_load 9 , Parfait::CallableMethod , "id_callablemethod" + assert_slot_to_reg 10 , :message , 1 , "message.next_message" + assert_reg_to_slot 11 , "id_callablemethod_" , "message.next_message" , 7 + assert_reg_to_slot 12 , "id_factory_" , :message , 2 + assert_load 13 , Risc::Label , "id_label" + assert_slot_to_reg 14 , :message , 1 , "message.next_message" + assert_reg_to_slot 15 , "id_label" , "message.next_message" , 4 + assert_slot_to_reg 16 ,:message , 1 , :message assert_equal Risc::FunctionCall, risc(17).class assert_equal :main, risc(17).method.name - assert_label 18 , "continue_" - assert_slot_to_reg 19 , :r2 , 2 , :r1 - assert_label 20 ,"cont_label_" - assert_slot_to_reg 21 , :r1 , 1 , :r4 - assert_reg_to_slot 22 , :r4 , :r2 , 2 + assert_label 18 , "after_main_" + assert_label 19 , "cont_label" + assert_slot_to_reg 20 , "id_factory_" , 2 , "id_factory_.next_object" + assert_slot_to_reg 21 , "id_factory_" , 2 , "id_factory_.next_object" + assert_reg_to_slot 22 , "id_factory_.next_object" , "id_factory_" , 2 end end end diff --git a/test/support/risc_assert.rb b/test/support/risc_assert.rb index 57949a82..02e91fd2 100644 --- a/test/support/risc_assert.rb +++ b/test/support/risc_assert.rb @@ -10,19 +10,27 @@ module Minitest else is_parts = pattern.split(".") reg_parts = register.symbol.to_s.split(".") - assert_equal reg_parts.length , is_parts.length , "wrong dot length for #{pattern} , at:#{at}" + assert_equal reg_parts.length , is_parts.length , "wrong dot #{kind} length for #{pattern} , at:#{at}" is_parts.each_with_index do |part , index| - assert reg_parts[index].start_with?(part) , "wrong #{kind}, part:#{part} register:#{register}, at:#{at}" + assert reg_parts[index].start_with?(part) , "wrong #{kind} part(#{reg_parts[index]}), at index #{index}:#{part}, ins:#{at}" end end end def assert_slot_to_reg( slot_i , array = nil, index = nil , register = nil) - assert_equal Integer , slot_i.class , "assert_slot_to_reg #{slot_i}" - slot = risc(slot_i) + if(slot_i.is_a? Integer) + slot = risc(slot_i) + else + slot = slot_i + slot_i = :unknown + end assert_equal Risc::SlotToReg , slot.class , "Class at #{slot_i}" assert_register( :source , array , slot.array , slot_i) + if(slot_i == :unknown) + assert_index( :source , slot , index) + else + assert_index( :source , slot_i , index) + end assert_register( :destination , register , slot.register , slot_i ) - assert_index( :source , slot_i , index) end def assert_reg_to_slot( slot_i , register = nil, array = nil, index = nil ) assert_equal Integer , slot_i.class, "assert_reg_to_slot #{slot_i}" @@ -34,8 +42,12 @@ module Minitest end def assert_index(kind , slot_i , index) return unless index - assert_equal Integer , slot_i.class , "assert_index #{slot_i}" - slot = risc(slot_i) + if(slot_i.is_a? Integer) + slot = risc(slot_i) + else + slot = slot_i + slot_i = :unknown + end if(slot.index.is_a?(Risc::RegisterValue)) assert_equal( Symbol , index.class, "wrong #{kind} index class, at:#{slot_i}") assert_equal( index , slot.index.symbol, "wrong #{kind} index, at#{slot_i}") @@ -83,8 +95,12 @@ module Minitest assert_label branch.label , label_name , "Label at #{branch_i}" end def assert_operator ins_i , op , left , right - assert_equal Integer , ins_i.class, "assert_operator #{ins_i}" - ins = risc(ins_i) + if(ins_i.is_a?(Integer)) + ins = risc(ins_i) + else + ins = ins_i + ins_i = :unknown + end assert_equal Risc::OperatorInstruction , ins.class , "Class at:#{ins_i}" assert_equal op , ins.operator assert_register :left , left , ins.left , ins_i