From 8b29326957974cf73ea97eb349872c6eaa69a7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Sat, 15 Feb 2020 15:26:49 +0700 Subject: [PATCH] moving know_object out of the base class object is only common to constant and object slots (which should be unified) On the way to making the array recursive --- lib/slot_machine/constant_slot.rb | 38 +++++++++---------------- lib/slot_machine/message_slot.rb | 3 +- lib/slot_machine/object_slot.rb | 6 +++- lib/slot_machine/slot.rb | 7 ++--- test/slot_machine/helper.rb | 8 ++++++ test/slot_machine/instruction/helper.rb | 5 ---- 6 files changed, 32 insertions(+), 35 deletions(-) diff --git a/lib/slot_machine/constant_slot.rb b/lib/slot_machine/constant_slot.rb index e6e7da11..005f20f1 100644 --- a/lib/slot_machine/constant_slot.rb +++ b/lib/slot_machine/constant_slot.rb @@ -1,22 +1,15 @@ module SlotMachine class ConstantSlot < Slot - # get the right definition, depending on the object - def self.for(object , slots) - case object - when :message - MessageSlot.new(slots) - when Constant - ConstantSlot.new(object , slots) - else - Slot.new(object,slots) - end - end + + + attr_reader :known_object def initialize( object , slots) - super(object, slots) + super(slots) + @known_object = object + raise "Not known #{slots}" unless object end - def known_name known_object.class.short_name end @@ -31,18 +24,15 @@ module SlotMachine type = :Object end right = compiler.use_reg( type ) - case known_object - when Constant - parfait = known_object.to_parfait(compiler) - const = Risc.load_constant(source, parfait , right) - compiler.add_code const - if slots.length == 1 - raise "only type allowed for constants, not #{slots[0]}" unless slots[0] == :type - compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right) - end - raise "Can't have slots into Constants #{slots}" if slots.length > 1 + parfait = known_object.to_parfait(compiler) + const = Risc.load_constant(source, parfait , right) + compiler.add_code const + if slots.length == 1 + raise "only type allowed for constants, not #{slots[0]}" unless slots[0] == :type + compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right) end - return const.register + raise "Can't have slots into Constants #{slots}" if slots.length > 1 + return const.register end end diff --git a/lib/slot_machine/message_slot.rb b/lib/slot_machine/message_slot.rb index b83e9428..70d31499 100644 --- a/lib/slot_machine/message_slot.rb +++ b/lib/slot_machine/message_slot.rb @@ -2,12 +2,13 @@ module SlotMachine class MessageSlot < Slot def initialize(slots) - super(:message , slots) + super(slots) end def known_name :message end + alias :known_object :known_name # load the slots into a register # the code is added to compiler diff --git a/lib/slot_machine/object_slot.rb b/lib/slot_machine/object_slot.rb index 8e652106..feec5c0c 100644 --- a/lib/slot_machine/object_slot.rb +++ b/lib/slot_machine/object_slot.rb @@ -1,8 +1,12 @@ module SlotMachine class ObjectSlot < Slot + attr_reader :known_object + def initialize( object , slots) - super(object , slots ) + super(slots) + @known_object = object + raise "Not known #{slots}" unless object end def known_name diff --git a/lib/slot_machine/slot.rb b/lib/slot_machine/slot.rb index d8536d6e..96d30070 100644 --- a/lib/slot_machine/slot.rb +++ b/lib/slot_machine/slot.rb @@ -29,17 +29,16 @@ module SlotMachine end end - attr_reader :known_object , :slots + attr_reader :slots # is an array of symbols, that specifies the first the object, and then the Slot. # The first element is either a known type name (Capitalized symbol of the class name) , # or the symbol :message # And subsequent symbols must be instance variables on the previous type. # Examples: [:message , :receiver] or [:Space , :next_message] - def initialize( object , slots) + def initialize( slots) raise "No slots #{object}" unless slots slots = [slots] unless slots.is_a?(Array) - @known_object , @slots = object , slots - raise "Not known #{slots}" unless object + @slots = slots end def to_s diff --git a/test/slot_machine/helper.rb b/test/slot_machine/helper.rb index 3f91a123..6b075c4a 100644 --- a/test/slot_machine/helper.rb +++ b/test/slot_machine/helper.rb @@ -1 +1,9 @@ require_relative '../helper' + +module SlotMachine + class InstructionMock < Instruction + def initialize + super("mocking") + end + end +end diff --git a/test/slot_machine/instruction/helper.rb b/test/slot_machine/instruction/helper.rb index 38bd1f76..3e2fd1c1 100644 --- a/test/slot_machine/instruction/helper.rb +++ b/test/slot_machine/instruction/helper.rb @@ -1,11 +1,6 @@ require_relative '../helper' module SlotMachine - class InstructionMock < Instruction - def initialize - super("mocking") - end - end # Most SlotMachineInstructionTests test the risc instructions of the slot instruction # quite carefully, ie every instruction, every register.