From 2d11078a37a109a26126ecf5da5e47714d3f42d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Mon, 17 Feb 2020 14:45:54 +0700 Subject: [PATCH] Slotted constructor cleanup --- lib/slot_language/variable.rb | 2 +- lib/slot_machine/slotted.rb | 10 +++++----- lib/slot_machine/slotted_message.rb | 4 ++++ lib/sol/basic_values.rb | 10 +++++----- lib/sol/lambda_expression.rb | 2 +- lib/sol/variables.rb | 2 +- lib/sol/yield_statement.rb | 2 +- test/slot_machine/instruction/test_not_same_check.rb | 2 +- test/slot_machine/instruction/test_truth_check.rb | 2 +- test/slot_machine/test_slotted.rb | 6 +++--- test/slot_machine/test_slotted_constant.rb | 2 +- test/slot_machine/test_slotted_message.rb | 2 +- test/slot_machine/test_slotted_message2.rb | 2 +- 13 files changed, 26 insertions(+), 22 deletions(-) diff --git a/lib/slot_language/variable.rb b/lib/slot_language/variable.rb index 1c8358d2..b92a4d5f 100644 --- a/lib/slot_language/variable.rb +++ b/lib/slot_language/variable.rb @@ -26,7 +26,7 @@ module SlotLanguage end def to_slot(compiler) - SlotMachine::Slotted.for(:message , name) + SlotMachine::Slotted.for(:message , [name]) end def to_s diff --git a/lib/slot_machine/slotted.rb b/lib/slot_machine/slotted.rb index fb59b06f..b17cfce6 100644 --- a/lib/slot_machine/slotted.rb +++ b/lib/slot_machine/slotted.rb @@ -2,7 +2,7 @@ module SlotMachine class Slotted - def self.for(object , slots) + def self.for(object , slots = nil) case object when :message SlottedMessage.new(slots) @@ -19,11 +19,11 @@ module SlotMachine # previous object attr_reader :slots - def initialize( slots ) - raise "No slots #{object}" unless slots - slots = [slots] unless slots.is_a?(Array) + def initialize( slots = nil ) + return unless slots + raise "stopped" unless slots.is_a?(Array) first = slots.shift - return unless first + raise "ended" unless first @slots = Slot.new(first) until(slots.empty?) @slots.set_next( Slot.new( slots.shift )) diff --git a/lib/slot_machine/slotted_message.rb b/lib/slot_machine/slotted_message.rb index 1407635d..2336f1d6 100644 --- a/lib/slot_machine/slotted_message.rb +++ b/lib/slot_machine/slotted_message.rb @@ -6,6 +6,10 @@ module SlotMachine end alias :known_object :known_name + def initialize(slots) + super(slots) + raise "Message must have slots, but none given" unless slots + end # load the slots into a register # the code is added to compiler # the register returned diff --git a/lib/sol/basic_values.rb b/lib/sol/basic_values.rb index 0f710eb3..2f8238bf 100644 --- a/lib/sol/basic_values.rb +++ b/lib/sol/basic_values.rb @@ -10,7 +10,7 @@ module Sol @value = value end def to_slotted(_) - return SlotMachine::Slotted.for(SlotMachine::IntegerConstant.new(@value) , []) + return SlotMachine::Slotted.for(SlotMachine::IntegerConstant.new(@value) ) end def ct_type Parfait.object_space.get_type_by_class_name(:Integer) @@ -38,7 +38,7 @@ module Sol Parfait.object_space.get_type_by_class_name(:True) end def to_slotted(_) - return SlotMachine::Slotted.for(Parfait.object_space.true_object , []) + return SlotMachine::Slotted.for(Parfait.object_space.true_object ) end def to_s(depth = 0) "true" @@ -50,7 +50,7 @@ module Sol Parfait.object_space.get_type_by_class_name(:False) end def to_slotted(_) - return SlotMachine::Slotted.for(Parfait.object_space.false_object , []) + return SlotMachine::Slotted.for(Parfait.object_space.false_object ) end def to_s(depth = 0) "false" @@ -62,7 +62,7 @@ module Sol Parfait.object_space.get_type_by_class_name(:Nil) end def to_slotted(_) - return SlotMachine::Slotted.for(Parfait.object_space.nil_object , []) + return SlotMachine::Slotted.for(Parfait.object_space.nil_object ) end def to_s(depth = 0) "nil" @@ -92,7 +92,7 @@ module Sol @value = value end def to_slotted(_) - return SlotMachine::Slotted.for(SlotMachine::StringConstant.new(@value),[]) + return SlotMachine::Slotted.for(SlotMachine::StringConstant.new(@value)) end def ct_type Parfait.object_space.get_type_by_class_name(:Word) diff --git a/lib/sol/lambda_expression.rb b/lib/sol/lambda_expression.rb index de57a9de..467c74e7 100644 --- a/lib/sol/lambda_expression.rb +++ b/lib/sol/lambda_expression.rb @@ -15,7 +15,7 @@ module Sol # fact never called) def to_slotted(compiler) compile(compiler) unless @parfait_block - return SlotMachine::Slotted.for(SlotMachine::LambdaConstant.new(parfait_block(compiler)) , []) + return SlotMachine::Slotted.for(SlotMachine::LambdaConstant.new(parfait_block(compiler)) ) end # create a block, a compiler for it, compile the block and add the compiler(code) diff --git a/lib/sol/variables.rb b/lib/sol/variables.rb index 351271a7..153982b8 100644 --- a/lib/sol/variables.rb +++ b/lib/sol/variables.rb @@ -52,7 +52,7 @@ module Sol get_named_class.single_class.instance_type end def to_slotted(_) - return SlotMachine::Slotted.for( get_named_class, []) + return SlotMachine::Slotted.for( get_named_class) end def get_named_class Parfait.object_space.get_class_by_name(self.name) diff --git a/lib/sol/yield_statement.rb b/lib/sol/yield_statement.rb index 23b6fae9..df49fb67 100644 --- a/lib/sol/yield_statement.rb +++ b/lib/sol/yield_statement.rb @@ -34,7 +34,7 @@ module Sol # we brace ourselves with the check, and exit (later raise) if . . . def method_check(compiler) ok_label = SlotMachine::Label.new(self,"method_ok_#{self.object_id}") - compile_method = SlotMachine::Slotted.for( compiler.get_method , []) + compile_method = SlotMachine::Slotted.for( compiler.get_method ) runtime_method = SlotMachine::Slotted.for( :message , [ :method] ) check = SlotMachine::NotSameCheck.new(compile_method , runtime_method, ok_label) # TODO? Maybe create slot instructions for this diff --git a/test/slot_machine/instruction/test_not_same_check.rb b/test/slot_machine/instruction/test_not_same_check.rb index 0a2e4f98..8325d3bc 100644 --- a/test/slot_machine/instruction/test_not_same_check.rb +++ b/test/slot_machine/instruction/test_not_same_check.rb @@ -3,7 +3,7 @@ require_relative "helper" module SlotMachine class TestNotSameCheck < SlotMachineInstructionTest def instruction - target = Slotted.for(:message , :caller) + target = Slotted.for(:message , [:caller]) NotSameCheck.new(target , target , Label.new("ok" , "target")) end def test_len diff --git a/test/slot_machine/instruction/test_truth_check.rb b/test/slot_machine/instruction/test_truth_check.rb index d36b644b..bf9eb438 100644 --- a/test/slot_machine/instruction/test_truth_check.rb +++ b/test/slot_machine/instruction/test_truth_check.rb @@ -3,7 +3,7 @@ require_relative "helper" module SlotMachine class TestSameCheck < SlotMachineInstructionTest def instruction - target = SlottedMessage.new( :caller) + target = SlottedMessage.new( [:caller]) TruthCheck.new(target , Label.new("ok" , "target")) end def test_len diff --git a/test/slot_machine/test_slotted.rb b/test/slot_machine/test_slotted.rb index c7d83f97..d79ea871 100644 --- a/test/slot_machine/test_slotted.rb +++ b/test/slot_machine/test_slotted.rb @@ -3,13 +3,13 @@ require_relative "helper" module SlotMachine class TestSlotted < MiniTest::Test def test_to_s - assert_equal "message.caller" , SlottedMessage.new(:caller).to_s + assert_equal "message.caller" , SlottedMessage.new([:caller]).to_s end def test_for_mess - assert_equal 2 , SlottedMessage.new(:caller).slots_length + assert_equal 2 , SlottedMessage.new([:caller]).slots_length end def test_for_const - slotted = Slotted.for(StringConstant.new("hi") , []) + slotted = Slotted.for(StringConstant.new("hi") , nil) assert_equal "StringConstant" , slotted.to_s end end diff --git a/test/slot_machine/test_slotted_constant.rb b/test/slot_machine/test_slotted_constant.rb index 26c9f837..2cb71e5e 100644 --- a/test/slot_machine/test_slotted_constant.rb +++ b/test/slot_machine/test_slotted_constant.rb @@ -6,7 +6,7 @@ module SlotMachine def setup Parfait.boot!(Parfait.default_test_options) compiler = Risc::FakeCompiler.new - @slotted = Slotted.for(StringConstant.new("hi") , []) + @slotted = Slotted.for(StringConstant.new("hi") , nil) register = @slotted.to_register(compiler , InstructionMock.new) @instruction = compiler.instructions.first end diff --git a/test/slot_machine/test_slotted_message.rb b/test/slot_machine/test_slotted_message.rb index 124c78c7..dddb44a5 100644 --- a/test/slot_machine/test_slotted_message.rb +++ b/test/slot_machine/test_slotted_message.rb @@ -3,7 +3,7 @@ require_relative "helper" module SlotMachine class TestSlottedMessage < MiniTest::Test - def slotted(slot = :caller) + def slotted(slot = [:caller]) SlottedMessage.new(slot) end def test_create_ok1 diff --git a/test/slot_machine/test_slotted_message2.rb b/test/slot_machine/test_slotted_message2.rb index 6e497c80..8e2ed2c6 100644 --- a/test/slot_machine/test_slotted_message2.rb +++ b/test/slot_machine/test_slotted_message2.rb @@ -5,7 +5,7 @@ module SlotMachine def setup Parfait.boot!(Parfait.default_test_options) compiler = Risc::FakeCompiler.new - slotted = SlottedMessage.new(:caller) + slotted = SlottedMessage.new([:caller]) @register = slotted.to_register(compiler , "fake source") @instruction = compiler.instructions.first end