Makes slots linked list

slots used to ba an array of symbols
Now we have an object for each slot, that holds the name and the next_slot
relatively easy change, though quite broad
This commit is contained in:
2020-02-17 14:29:45 +07:00
parent 93103d551f
commit c1679bd6ff
16 changed files with 69 additions and 49 deletions

View File

@ -18,11 +18,13 @@ module SlotMachine
attr_reader :name , :next_slot
# initialize with just the name of the slot. Add more to the chain with set_next
def initialize( name )
raise "No name" unless name
@name = name
@name = name
end
#set the next_slot , but always at the end of the chain
def set_next(slot)
if(@next_slot)
@next_slot.set_next(slot)
@ -31,12 +33,18 @@ module SlotMachine
end
end
# return the length of chain, ie 1 plus however many more next_slots there are
def length
return 1 unless @next_slot
1 + @next_slot.length
end
# name of all the slots, with dot syntax
def to_s
names = name.to_s
names += ".#{@next_slot}" if @next_slot
names
end
end
end

View File

@ -11,27 +11,41 @@ module SlotMachine
when Parfait::Object , Risc::Label
SlottedObject.new(object , slots)
else
raise "not supported type #{object}"
raise "not supported type #{object}:#{object.class}"
end
end
# The first in a possible chain of slots, that name instance variables in the
# previous object
attr_reader :slot
attr_reader :slots
def initialize( slots )
raise "No slots #{object}" unless slots
slots = [slots] unless slots.is_a?(Array)
first = slots.shift
@slot = Slot.new(first)
return unless first
@slots = Slot.new(first)
until(slots.empty?)
@slot.set_next( Slot.new( slots.shift ))
@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
end
end
def to_s
names = known_name.to_s
names += ".#{@slot}" if @slot
names += ".#{@slots}" if @slots
names
end

View File

@ -1,7 +1,6 @@
module SlotMachine
class SlottedConstant < Slotted
attr_reader :known_object
def initialize( object , slots)
@ -27,11 +26,11 @@ module SlotMachine
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
if slots_length == 2
raise "only type allowed for constants, not #{slots}" unless slots.name == :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
raise "Can't have slots into Constants #{slots}" if slots_length > 2
return const.register
end

View File

@ -1,7 +1,6 @@
module SlotMachine
class SlottedMessage < Slotted
def known_name
:message
end
@ -13,13 +12,13 @@ module SlotMachine
def to_register(compiler, source)
type = :Message
right = compiler.use_reg( type )
slots = @slot
slots = @slots
left = Risc.message_reg
left = left.resolve_and_add( slots.name , compiler)
reg = compiler.current.register
slots = slots.next_slot
while( slots )
left = left.resolve_and_add( slots , compiler)
left = left.resolve_and_add( slots.name , compiler)
slots = slots.next_slot
end
return reg
@ -32,15 +31,14 @@ module SlotMachine
# They are very similar (apart from the final reg_to_slot here) and should
# most likely be united
def reduce_and_load(const_reg , compiler , original_source )
left_slots = slots.dup
raise "Not Message #{object}" unless known_object == :message
left = Risc.message_reg
slot = left_slots.shift
while( !left_slots.empty? )
left = left.resolve_and_add( slot , compiler)
slot = left_slots.shift
slot = slots
while( slot.next_slot )
left = left.resolve_and_add( slot.name , compiler)
slot = slot.next_slot
end
compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, slot)
compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, slot.name)
end
end

View File

@ -21,16 +21,16 @@ module SlotMachine
right = compiler.use_reg( type )
const = Risc.load_constant(source, known_object , right)
compiler.add_code const
if slots.length > 0
if slots_length > 1
# desctructively replace the existing value to be loaded if more slots
compiler.add_code Risc.slot_to_reg( source , right ,slots[0], right)
compiler.add_code Risc.slot_to_reg( source , right ,slots.name, right)
end
if slots.length > 1
if slots_length > 2
# desctructively replace the existing value to be loaded if more slots
index = Risc.resolve_to_index(slots[0] , slots[1] ,compiler)
index = Risc.resolve_to_index(slots.name , slots.next_slot.name ,compiler)
compiler.add_code Risc::SlotToReg.new( source , right ,index, right)
if slots.length > 2
raise "3 slots only for type #{slots}" unless slots[2] == :type
if slots_length > 3
raise "3 slots only for type #{slots}" unless slots.next_slot.next_slot.name == :type
compiler.add_code Risc::SlotToReg.new( source , right , Parfait::TYPE_INDEX, right)
end
end
@ -44,7 +44,7 @@ module SlotMachine
raise "only cache" unless known_object.is_a?( Parfait::CacheEntry)
left = compiler.use_reg( :CacheEntry )
compiler.add_code Risc.load_constant(original_source, known_object , left)
compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, slots.first)
compiler.add_code Risc.reg_to_slot(original_source, const_reg , left, slots.name)
end
end
end