2018-06-28 19:15:24 +02:00
|
|
|
module Vool
|
|
|
|
|
2018-07-30 13:45:37 +02:00
|
|
|
# A Yield is a lot like a Send, which is why they share the base class CallStatement
|
|
|
|
# That means it has a receiver (self), arguments and an (implicitly assigned) name
|
|
|
|
#
|
|
|
|
# On the ruby side, normalisation works pretty much the same too.
|
|
|
|
#
|
|
|
|
# On the way down to Mom, small differences become abvious, as the block that is
|
|
|
|
# yielded to is an argument. Whereas in a send it is either statically known
|
|
|
|
# or resolved and cached. Here it is dynamic, but sort of known dynamic.
|
2018-07-30 19:11:52 +02:00
|
|
|
# All we do before calling it is check that it is the right type.
|
2018-07-30 13:45:37 +02:00
|
|
|
class YieldStatement < CallStatement
|
|
|
|
|
|
|
|
# A Yield breaks down to 2 steps:
|
2018-06-28 19:15:24 +02:00
|
|
|
# - Setting up the next message, with receiver, arguments, and (importantly) return address
|
|
|
|
# - a SimpleCall,
|
2018-07-05 13:02:38 +02:00
|
|
|
def to_mom( compiler )
|
|
|
|
@parfait_block = @block.to_mom(compiler) if @block
|
2018-07-21 13:34:39 +02:00
|
|
|
@receiver = SelfExpression.new(compiler.receiver_type) if @receiver.is_a?(SelfExpression)
|
2018-07-24 10:35:49 +02:00
|
|
|
yield_call(compiler)
|
2018-06-28 19:15:24 +02:00
|
|
|
end
|
|
|
|
|
2018-07-24 10:35:49 +02:00
|
|
|
# this breaks into two parts:
|
|
|
|
# - check the calling method and break to a (not implemented) dynamic version
|
|
|
|
# - call the block, that is the last argument of the method
|
|
|
|
def yield_call(compiler)
|
|
|
|
method_check(compiler) << yield_arg_block(compiler)
|
|
|
|
end
|
|
|
|
|
|
|
|
# check that the calling method is the method that the block was created in.
|
|
|
|
# In that case variable resolution is reasy and we can prceed to yield
|
|
|
|
# Note: the else case is not implemented (ie passing lambdas around)
|
|
|
|
# this needs run-time variable resolution, which is just not done.
|
|
|
|
# we brace ourselves with the check, and exit (later raise) if . . .
|
|
|
|
def method_check(compiler)
|
2019-08-07 14:08:45 +02:00
|
|
|
ok_label = Mom::Label.new(self,"method_ok_#{self.object_id}")
|
2018-07-24 10:35:49 +02:00
|
|
|
compile_method = Mom::SlotDefinition.new( compiler.get_method , [])
|
|
|
|
runtime_method = Mom::SlotDefinition.new( :message , [ :method] )
|
|
|
|
check = Mom::NotSameCheck.new(compile_method , runtime_method, ok_label)
|
|
|
|
# TODO? Maybe create mom instructions for this
|
2018-08-19 12:16:07 +02:00
|
|
|
#builder = compiler.builder("yield")
|
2019-08-11 19:36:10 +02:00
|
|
|
#Risc::Builtin.exit_sequence(builder)
|
2018-07-24 10:35:49 +02:00
|
|
|
#check << builder.built
|
|
|
|
check << ok_label
|
2018-06-28 19:15:24 +02:00
|
|
|
end
|
|
|
|
|
2018-07-24 10:35:49 +02:00
|
|
|
# to call the block (that we know now to be the last arg),
|
|
|
|
# we do a message setup, arg transfer and the a arg_yield (which is similar to dynamic_call)
|
|
|
|
def yield_arg_block(compiler)
|
2018-07-27 11:16:06 +02:00
|
|
|
arg_index = compiler.get_method.arguments_type.get_length - 1
|
2018-07-24 10:35:49 +02:00
|
|
|
setup = Mom::MessageSetup.new( arg_index )
|
2019-08-19 10:33:12 +02:00
|
|
|
mom_receive = @receiver.to_slot(compiler)
|
2019-08-22 20:24:02 +02:00
|
|
|
arg_target = [:message , :next_message ]
|
2018-06-28 19:15:24 +02:00
|
|
|
args = []
|
|
|
|
@arguments.each_with_index do |arg , index| # +1 because of type
|
2019-08-22 20:24:02 +02:00
|
|
|
args << Mom::SlotLoad.new(self, arg_target + ["arg#{index+1}".to_sym] , arg.to_slot(compiler))
|
2018-06-28 19:15:24 +02:00
|
|
|
end
|
2019-08-10 20:59:31 +02:00
|
|
|
setup << Mom::ArgumentTransfer.new( self , mom_receive , args )
|
|
|
|
setup << Mom::BlockYield.new( self , arg_index )
|
2018-06-28 19:15:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|