Slotted constructor cleanup

This commit is contained in:
Torsten Rüger 2020-02-17 14:45:54 +07:00
parent c1679bd6ff
commit 2d11078a37
13 changed files with 26 additions and 22 deletions

View File

@ -26,7 +26,7 @@ module SlotLanguage
end end
def to_slot(compiler) def to_slot(compiler)
SlotMachine::Slotted.for(:message , name) SlotMachine::Slotted.for(:message , [name])
end end
def to_s def to_s

View File

@ -2,7 +2,7 @@ module SlotMachine
class Slotted class Slotted
def self.for(object , slots) def self.for(object , slots = nil)
case object case object
when :message when :message
SlottedMessage.new(slots) SlottedMessage.new(slots)
@ -19,11 +19,11 @@ module SlotMachine
# previous object # previous object
attr_reader :slots attr_reader :slots
def initialize( slots ) def initialize( slots = nil )
raise "No slots #{object}" unless slots return unless slots
slots = [slots] unless slots.is_a?(Array) raise "stopped" unless slots.is_a?(Array)
first = slots.shift first = slots.shift
return unless first raise "ended" unless first
@slots = Slot.new(first) @slots = Slot.new(first)
until(slots.empty?) until(slots.empty?)
@slots.set_next( Slot.new( slots.shift )) @slots.set_next( Slot.new( slots.shift ))

View File

@ -6,6 +6,10 @@ module SlotMachine
end end
alias :known_object :known_name 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 # load the slots into a register
# the code is added to compiler # the code is added to compiler
# the register returned # the register returned

View File

@ -10,7 +10,7 @@ module Sol
@value = value @value = value
end end
def to_slotted(_) def to_slotted(_)
return SlotMachine::Slotted.for(SlotMachine::IntegerConstant.new(@value) , []) return SlotMachine::Slotted.for(SlotMachine::IntegerConstant.new(@value) )
end end
def ct_type def ct_type
Parfait.object_space.get_type_by_class_name(:Integer) Parfait.object_space.get_type_by_class_name(:Integer)
@ -38,7 +38,7 @@ module Sol
Parfait.object_space.get_type_by_class_name(:True) Parfait.object_space.get_type_by_class_name(:True)
end end
def to_slotted(_) def to_slotted(_)
return SlotMachine::Slotted.for(Parfait.object_space.true_object , []) return SlotMachine::Slotted.for(Parfait.object_space.true_object )
end end
def to_s(depth = 0) def to_s(depth = 0)
"true" "true"
@ -50,7 +50,7 @@ module Sol
Parfait.object_space.get_type_by_class_name(:False) Parfait.object_space.get_type_by_class_name(:False)
end end
def to_slotted(_) def to_slotted(_)
return SlotMachine::Slotted.for(Parfait.object_space.false_object , []) return SlotMachine::Slotted.for(Parfait.object_space.false_object )
end end
def to_s(depth = 0) def to_s(depth = 0)
"false" "false"
@ -62,7 +62,7 @@ module Sol
Parfait.object_space.get_type_by_class_name(:Nil) Parfait.object_space.get_type_by_class_name(:Nil)
end end
def to_slotted(_) def to_slotted(_)
return SlotMachine::Slotted.for(Parfait.object_space.nil_object , []) return SlotMachine::Slotted.for(Parfait.object_space.nil_object )
end end
def to_s(depth = 0) def to_s(depth = 0)
"nil" "nil"
@ -92,7 +92,7 @@ module Sol
@value = value @value = value
end end
def to_slotted(_) def to_slotted(_)
return SlotMachine::Slotted.for(SlotMachine::StringConstant.new(@value),[]) return SlotMachine::Slotted.for(SlotMachine::StringConstant.new(@value))
end end
def ct_type def ct_type
Parfait.object_space.get_type_by_class_name(:Word) Parfait.object_space.get_type_by_class_name(:Word)

View File

@ -15,7 +15,7 @@ module Sol
# fact never called) # fact never called)
def to_slotted(compiler) def to_slotted(compiler)
compile(compiler) unless @parfait_block 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 end
# create a block, a compiler for it, compile the block and add the compiler(code) # create a block, a compiler for it, compile the block and add the compiler(code)

View File

@ -52,7 +52,7 @@ module Sol
get_named_class.single_class.instance_type get_named_class.single_class.instance_type
end end
def to_slotted(_) def to_slotted(_)
return SlotMachine::Slotted.for( get_named_class, []) return SlotMachine::Slotted.for( get_named_class)
end end
def get_named_class def get_named_class
Parfait.object_space.get_class_by_name(self.name) Parfait.object_space.get_class_by_name(self.name)

View File

@ -34,7 +34,7 @@ module Sol
# we brace ourselves with the check, and exit (later raise) if . . . # we brace ourselves with the check, and exit (later raise) if . . .
def method_check(compiler) def method_check(compiler)
ok_label = SlotMachine::Label.new(self,"method_ok_#{self.object_id}") 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] ) runtime_method = SlotMachine::Slotted.for( :message , [ :method] )
check = SlotMachine::NotSameCheck.new(compile_method , runtime_method, ok_label) check = SlotMachine::NotSameCheck.new(compile_method , runtime_method, ok_label)
# TODO? Maybe create slot instructions for this # TODO? Maybe create slot instructions for this

View File

@ -3,7 +3,7 @@ require_relative "helper"
module SlotMachine module SlotMachine
class TestNotSameCheck < SlotMachineInstructionTest class TestNotSameCheck < SlotMachineInstructionTest
def instruction def instruction
target = Slotted.for(:message , :caller) target = Slotted.for(:message , [:caller])
NotSameCheck.new(target , target , Label.new("ok" , "target")) NotSameCheck.new(target , target , Label.new("ok" , "target"))
end end
def test_len def test_len

View File

@ -3,7 +3,7 @@ require_relative "helper"
module SlotMachine module SlotMachine
class TestSameCheck < SlotMachineInstructionTest class TestSameCheck < SlotMachineInstructionTest
def instruction def instruction
target = SlottedMessage.new( :caller) target = SlottedMessage.new( [:caller])
TruthCheck.new(target , Label.new("ok" , "target")) TruthCheck.new(target , Label.new("ok" , "target"))
end end
def test_len def test_len

View File

@ -3,13 +3,13 @@ require_relative "helper"
module SlotMachine module SlotMachine
class TestSlotted < MiniTest::Test class TestSlotted < MiniTest::Test
def test_to_s def test_to_s
assert_equal "message.caller" , SlottedMessage.new(:caller).to_s assert_equal "message.caller" , SlottedMessage.new([:caller]).to_s
end end
def test_for_mess def test_for_mess
assert_equal 2 , SlottedMessage.new(:caller).slots_length assert_equal 2 , SlottedMessage.new([:caller]).slots_length
end end
def test_for_const def test_for_const
slotted = Slotted.for(StringConstant.new("hi") , []) slotted = Slotted.for(StringConstant.new("hi") , nil)
assert_equal "StringConstant" , slotted.to_s assert_equal "StringConstant" , slotted.to_s
end end
end end

View File

@ -6,7 +6,7 @@ module SlotMachine
def setup def setup
Parfait.boot!(Parfait.default_test_options) Parfait.boot!(Parfait.default_test_options)
compiler = Risc::FakeCompiler.new compiler = Risc::FakeCompiler.new
@slotted = Slotted.for(StringConstant.new("hi") , []) @slotted = Slotted.for(StringConstant.new("hi") , nil)
register = @slotted.to_register(compiler , InstructionMock.new) register = @slotted.to_register(compiler , InstructionMock.new)
@instruction = compiler.instructions.first @instruction = compiler.instructions.first
end end

View File

@ -3,7 +3,7 @@ require_relative "helper"
module SlotMachine module SlotMachine
class TestSlottedMessage < MiniTest::Test class TestSlottedMessage < MiniTest::Test
def slotted(slot = :caller) def slotted(slot = [:caller])
SlottedMessage.new(slot) SlottedMessage.new(slot)
end end
def test_create_ok1 def test_create_ok1

View File

@ -5,7 +5,7 @@ module SlotMachine
def setup def setup
Parfait.boot!(Parfait.default_test_options) Parfait.boot!(Parfait.default_test_options)
compiler = Risc::FakeCompiler.new compiler = Risc::FakeCompiler.new
slotted = SlottedMessage.new(:caller) slotted = SlottedMessage.new([:caller])
@register = slotted.to_register(compiler , "fake source") @register = slotted.to_register(compiler , "fake source")
@instruction = compiler.instructions.first @instruction = compiler.instructions.first
end end