2019-10-03 20:07:55 +02:00
|
|
|
module SlotMachine
|
2020-02-11 10:19:52 +01:00
|
|
|
# A Slot defines a slot. 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?
|
|
|
|
# Start with a known object like the Message (in register one), we know all it's
|
|
|
|
# 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-10 12:12:39 +01:00
|
|
|
# get the right definition, depending on the object
|
|
|
|
def self.for(object , slots)
|
|
|
|
case object
|
|
|
|
when :message
|
2020-02-15 15:02:03 +01:00
|
|
|
SlottedMessage.new(slots)
|
2020-02-10 12:58:48 +01:00
|
|
|
when Constant
|
2020-02-15 15:02:03 +01:00
|
|
|
SlottedConstant.new(object , slots)
|
2020-02-11 10:03:51 +01:00
|
|
|
when Parfait::Object , Risc::Label
|
2020-02-15 15:02:03 +01:00
|
|
|
SlottedObject.new(object , slots)
|
2020-02-10 12:12:39 +01:00
|
|
|
else
|
2020-02-11 10:03:51 +01:00
|
|
|
raise "not supported type #{object}"
|
2020-02-10 12:12:39 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-15 09:26:49 +01:00
|
|
|
attr_reader :slots
|
2018-05-15 18:29:06 +02:00
|
|
|
# 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]
|
2020-02-15 09:26:49 +01:00
|
|
|
def initialize( slots)
|
2018-05-15 18:29:06 +02:00
|
|
|
raise "No slots #{object}" unless slots
|
|
|
|
slots = [slots] unless slots.is_a?(Array)
|
2020-02-15 09:26:49 +01:00
|
|
|
@slots = slots
|
2018-05-15 18:29:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
names = [known_name] + @slots
|
2018-07-03 18:15:36 +02:00
|
|
|
"[#{names.join(', ')}]"
|
2018-05-15 18:29:06 +02:00
|
|
|
end
|
|
|
|
|
2018-07-14 21:39:00 +02:00
|
|
|
|
2018-05-15 18:29:06 +02:00
|
|
|
end
|
|
|
|
end
|