2019-10-03 20:07:55 +02:00
|
|
|
module SlotMachine
|
2020-02-17 08:26:50 +01:00
|
|
|
# A Slot defines a slot in a slotted. A bit like a variable name but for objects.
|
2018-05-15 18:29:06 +02:00
|
|
|
#
|
2020-02-10 12:12:39 +01:00
|
|
|
# PS: for the interested: A "development" of Smalltalk was the
|
2018-05-15 18:29:06 +02:00
|
|
|
# prototype based language (read: JavaScript equivalent)
|
|
|
|
# called Self https://en.wikipedia.org/wiki/Self_(programming_language)
|
|
|
|
#
|
2020-02-11 10:19:52 +01:00
|
|
|
# Slots are the instance names of objects. But since the language is dynamic
|
2018-05-15 18:29:06 +02:00
|
|
|
# what is it that we can say about instance names at runtime?
|
2020-02-17 08:26:50 +01:00
|
|
|
# Start with a Slotted, like the Message (in register one), we know all it's
|
2018-05-15 18:29:06 +02:00
|
|
|
# variables. But there is a Message in there, and for that we know the instances
|
|
|
|
# too. And off course for _all_ objects we know where the type is.
|
|
|
|
#
|
|
|
|
# The definiion is an array of symbols that we can resolve to SlotLoad
|
|
|
|
# Instructions. Or in the case of constants to ConstantLoad
|
|
|
|
#
|
2020-02-11 10:19:52 +01:00
|
|
|
class Slot
|
2020-02-17 08:26:50 +01:00
|
|
|
|
|
|
|
attr_reader :name , :next_slot
|
|
|
|
|
2020-02-17 08:29:45 +01:00
|
|
|
# initialize with just the name of the slot. Add more to the chain with set_next
|
2020-02-17 08:26:50 +01:00
|
|
|
def initialize( name )
|
|
|
|
raise "No name" unless name
|
2020-02-17 08:29:45 +01:00
|
|
|
@name = name
|
2020-02-10 12:12:39 +01:00
|
|
|
end
|
|
|
|
|
2020-02-17 08:29:45 +01:00
|
|
|
#set the next_slot , but always at the end of the chain
|
2020-02-17 08:26:50 +01:00
|
|
|
def set_next(slot)
|
|
|
|
if(@next_slot)
|
|
|
|
@next_slot.set_next(slot)
|
|
|
|
else
|
|
|
|
@next_slot = slot
|
|
|
|
end
|
2018-05-15 18:29:06 +02:00
|
|
|
end
|
|
|
|
|
2020-02-17 08:29:45 +01:00
|
|
|
# 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
|
2018-05-15 18:29:06 +02:00
|
|
|
def to_s
|
2020-02-17 08:26:50 +01:00
|
|
|
names = name.to_s
|
|
|
|
names += ".#{@next_slot}" if @next_slot
|
|
|
|
names
|
2018-05-15 18:29:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|