2019-10-03 20:07:55 +02:00
|
|
|
module SlotMachine
|
2018-04-08 17:55:17 +02:00
|
|
|
|
|
|
|
# Dynamic method resolution is at the heart of a dynamic language, and here
|
2019-10-03 20:07:55 +02:00
|
|
|
# is the SlotMachine level instruction to do it.
|
2018-04-08 17:55:17 +02:00
|
|
|
#
|
|
|
|
# When the static type can not be determined a CacheEntry is used to store
|
|
|
|
# type and method of the resolved method. The CacheEntry is shared with
|
|
|
|
# DynamicCall instruction who is responsible for calling the method in the entry.
|
|
|
|
#
|
|
|
|
# This instruction resolves the method, in case the types don't match (and
|
|
|
|
# at least on first encouter)
|
|
|
|
#
|
|
|
|
# This used to be a method, but we don't really need the method setup etc
|
|
|
|
#
|
|
|
|
class ResolveMethod < Instruction
|
|
|
|
attr :cache_entry , :name
|
|
|
|
|
2019-10-03 23:36:49 +02:00
|
|
|
# pass in source (SolStatement)
|
2019-08-10 20:59:31 +02:00
|
|
|
# name of the method (don't knwow the actaual method)
|
|
|
|
# and the cache_entry
|
|
|
|
def initialize(source , name , cache_entry)
|
|
|
|
super(source)
|
2018-04-08 17:55:17 +02:00
|
|
|
@name = name
|
|
|
|
@cache_entry = cache_entry
|
|
|
|
end
|
|
|
|
|
2018-04-19 09:00:55 +02:00
|
|
|
def to_s
|
|
|
|
"ResolveMethod #{name}"
|
|
|
|
end
|
2018-04-08 21:30:18 +02:00
|
|
|
|
|
|
|
# When the method is resolved, a cache_entry is used to hold the result.
|
|
|
|
# That cache_entry (holding type and method) is checked before, and
|
|
|
|
# needs to be updated by this instruction.
|
|
|
|
#
|
|
|
|
# We use the type stored in the cache_entry to check the methods if any of it's
|
|
|
|
# names are the same as the given @name
|
|
|
|
#
|
|
|
|
# currently a fail results in sys exit
|
2018-04-08 17:55:17 +02:00
|
|
|
def to_risc( compiler )
|
2018-08-19 12:18:25 +02:00
|
|
|
builder = compiler.builder(self)
|
2020-03-08 17:48:39 +01:00
|
|
|
word = builder.load_object(Parfait.new_word(@name.to_s))
|
|
|
|
entry = builder.load_object(@cache_entry)
|
|
|
|
while_start_label = Risc.label(to_s, "while_start_label_#{object_id}")
|
|
|
|
ok_label = Risc.label(to_s, "ok_label_#{object_id}")
|
|
|
|
exit_label = Risc.label(to_s, "exit_label_#{object_id}")
|
2018-04-08 17:55:17 +02:00
|
|
|
builder.build do
|
2020-03-08 17:48:39 +01:00
|
|
|
callable_method = entry[:cached_type][:methods].to_reg
|
2018-04-08 17:55:17 +02:00
|
|
|
|
2018-04-08 21:30:18 +02:00
|
|
|
add_code while_start_label
|
|
|
|
|
2020-03-08 17:48:39 +01:00
|
|
|
object = load_object Parfait.object_space.nil_object
|
|
|
|
object.op :- , callable_method
|
2018-04-08 17:55:17 +02:00
|
|
|
if_zero exit_label
|
|
|
|
|
2020-03-08 17:48:39 +01:00
|
|
|
name = callable_method[:name].to_reg
|
|
|
|
name.op :- , word
|
2018-04-08 17:55:17 +02:00
|
|
|
|
2018-04-08 21:30:18 +02:00
|
|
|
if_zero ok_label
|
2018-04-08 17:55:17 +02:00
|
|
|
|
2020-03-08 17:48:39 +01:00
|
|
|
callable_method = callable_method[:next_callable].to_reg
|
2018-04-08 17:55:17 +02:00
|
|
|
branch while_start_label
|
|
|
|
|
2018-04-08 21:30:18 +02:00
|
|
|
add_code exit_label
|
2019-09-17 19:18:00 +02:00
|
|
|
MethodMissing.new(compiler.source_name , word.symbol).to_risc(compiler)
|
2018-04-08 21:30:18 +02:00
|
|
|
|
|
|
|
add_code ok_label
|
2020-03-08 17:48:39 +01:00
|
|
|
entry[:cached_method] << callable_method
|
2018-04-08 17:55:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|