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
This commit is contained in:
Torsten Rüger 2020-02-15 15:26:49 +07:00
parent 6aa6b32c50
commit 8b29326957
6 changed files with 32 additions and 35 deletions

View File

@ -1,22 +1,15 @@
module SlotMachine module SlotMachine
class ConstantSlot < Slot class ConstantSlot < Slot
# get the right definition, depending on the object
def self.for(object , slots)
case object attr_reader :known_object
when :message
MessageSlot.new(slots)
when Constant
ConstantSlot.new(object , slots)
else
Slot.new(object,slots)
end
end
def initialize( object , slots) def initialize( object , slots)
super(object, slots) super(slots)
@known_object = object
raise "Not known #{slots}" unless object
end end
def known_name def known_name
known_object.class.short_name known_object.class.short_name
end end
@ -31,8 +24,6 @@ module SlotMachine
type = :Object type = :Object
end end
right = compiler.use_reg( type ) right = compiler.use_reg( type )
case known_object
when Constant
parfait = known_object.to_parfait(compiler) parfait = known_object.to_parfait(compiler)
const = Risc.load_constant(source, parfait , right) const = Risc.load_constant(source, parfait , right)
compiler.add_code const compiler.add_code const
@ -41,7 +32,6 @@ module SlotMachine
compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right) compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right)
end end
raise "Can't have slots into Constants #{slots}" if slots.length > 1 raise "Can't have slots into Constants #{slots}" if slots.length > 1
end
return const.register return const.register
end end

View File

@ -2,12 +2,13 @@ module SlotMachine
class MessageSlot < Slot class MessageSlot < Slot
def initialize(slots) def initialize(slots)
super(:message , slots) super(slots)
end end
def known_name def known_name
:message :message
end end
alias :known_object :known_name
# load the slots into a register # load the slots into a register
# the code is added to compiler # the code is added to compiler

View File

@ -1,8 +1,12 @@
module SlotMachine module SlotMachine
class ObjectSlot < Slot class ObjectSlot < Slot
attr_reader :known_object
def initialize( object , slots) def initialize( object , slots)
super(object , slots ) super(slots)
@known_object = object
raise "Not known #{slots}" unless object
end end
def known_name def known_name

View File

@ -29,17 +29,16 @@ module SlotMachine
end end
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. # 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) , # The first element is either a known type name (Capitalized symbol of the class name) ,
# or the symbol :message # or the symbol :message
# And subsequent symbols must be instance variables on the previous type. # And subsequent symbols must be instance variables on the previous type.
# Examples: [:message , :receiver] or [:Space , :next_message] # Examples: [:message , :receiver] or [:Space , :next_message]
def initialize( object , slots) def initialize( slots)
raise "No slots #{object}" unless slots raise "No slots #{object}" unless slots
slots = [slots] unless slots.is_a?(Array) slots = [slots] unless slots.is_a?(Array)
@known_object , @slots = object , slots @slots = slots
raise "Not known #{slots}" unless object
end end
def to_s def to_s

View File

@ -1 +1,9 @@
require_relative '../helper' require_relative '../helper'
module SlotMachine
class InstructionMock < Instruction
def initialize
super("mocking")
end
end
end

View File

@ -1,11 +1,6 @@
require_relative '../helper' require_relative '../helper'
module SlotMachine module SlotMachine
class InstructionMock < Instruction
def initialize
super("mocking")
end
end
# Most SlotMachineInstructionTests test the risc instructions of the slot instruction # Most SlotMachineInstructionTests test the risc instructions of the slot instruction
# quite carefully, ie every instruction, every register. # quite carefully, ie every instruction, every register.