rubyx/lib/slot_machine/slotted.rb

55 lines
1.1 KiB
Ruby
Raw Normal View History

module SlotMachine
class Slotted
2020-02-17 08:45:54 +01:00
def self.for(object , slots = nil)
case object
when :message
SlottedMessage.new(slots)
when Constant
SlottedConstant.new(object , slots)
when Parfait::Object , Risc::Label
SlottedObject.new(object , slots)
else
raise "not supported type #{object}:#{object.class}"
end
end
2020-02-17 08:26:50 +01:00
# The first in a possible chain of slots, that name instance variables in the
# previous object
attr_reader :slots
2020-02-17 08:26:50 +01:00
2020-02-17 08:45:54 +01:00
def initialize( slots = nil )
return unless slots
raise "stopped #{slots.class}" unless slots.is_a?(Array)
2020-02-17 08:26:50 +01:00
first = slots.shift
2020-02-17 08:45:54 +01:00
raise "ended" unless first
@slots = Slot.new(first)
2020-02-17 08:26:50 +01:00
until(slots.empty?)
@slots.set_next( Slot.new( slots.shift ))
end
end
def slots_length
return 0 unless @slots
1 + @slots.length
end
def set_next(slot)
if(@slots)
@slots.set_next(slot)
else
@slots = slot
2020-02-17 08:26:50 +01:00
end
end
def to_s
2020-02-17 08:26:50 +01:00
names = known_name.to_s
names += ".#{@slots}" if @slots
2020-02-17 08:26:50 +01:00
names
end
end
end