2018-06-28 19:15:24 +02:00
|
|
|
module Vool
|
|
|
|
|
|
|
|
class YieldStatement < Statement
|
|
|
|
attr_reader :arguments
|
|
|
|
|
2018-07-21 13:34:39 +02:00
|
|
|
def initialize(name , receiver , arguments)
|
2018-06-28 19:15:24 +02:00
|
|
|
@arguments = arguments
|
2018-07-21 13:34:39 +02:00
|
|
|
@receiver = receiver
|
|
|
|
@name = name
|
2018-06-28 19:15:24 +02:00
|
|
|
@arguments ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
2018-07-03 18:15:36 +02:00
|
|
|
"#{receiver}.#{name}(#{arguments.join(', ')})"
|
2018-06-28 19:15:24 +02:00
|
|
|
end
|
2018-07-24 10:35:49 +02:00
|
|
|
|
2018-06-28 19:15:24 +02:00
|
|
|
def each(&block)
|
|
|
|
block.call(self)
|
|
|
|
block.call(@receiver)
|
|
|
|
@arguments.each do |arg|
|
|
|
|
block.call(arg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-24 10:35:49 +02:00
|
|
|
# When used as right hand side, this tells what data to move to get the result into
|
|
|
|
# a varaible. It is (off course) the return value of the message
|
|
|
|
def slot_definition(compiler)
|
|
|
|
Mom::SlotDefinition.new(:message ,[ :return_value])
|
|
|
|
end
|
|
|
|
|
2018-06-28 19:15:24 +02:00
|
|
|
# A Send breaks down to 2 steps:
|
|
|
|
# - 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)
|
|
|
|
ok_label = Mom::Label.new("method_ok_#{self.object_id}")
|
|
|
|
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
|
|
|
|
#builder = compiler.code_builder("yield")
|
|
|
|
#Risc::Builtin::Object.exit_sequence(builder)
|
|
|
|
#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)
|
|
|
|
arg_index = compiler.get_method.arguments_type.get_length
|
|
|
|
setup = Mom::MessageSetup.new( arg_index )
|
2018-07-05 13:02:38 +02:00
|
|
|
mom_receive = @receiver.slot_definition(compiler)
|
2018-06-28 19:15:24 +02:00
|
|
|
arg_target = [:message , :next_message , :arguments]
|
|
|
|
args = []
|
|
|
|
@arguments.each_with_index do |arg , index| # +1 because of type
|
2018-07-05 13:02:38 +02:00
|
|
|
args << Mom::SlotLoad.new( arg_target + [index + 1] , arg.slot_definition(compiler))
|
2018-06-28 19:15:24 +02:00
|
|
|
end
|
|
|
|
setup << Mom::ArgumentTransfer.new( mom_receive , args )
|
2018-07-24 10:35:49 +02:00
|
|
|
setup << Mom::BlockYield.new( arg_index )
|
2018-06-28 19:15:24 +02:00
|
|
|
end
|
|
|
|
|
2018-07-24 10:35:49 +02:00
|
|
|
def to_s(depth = 0)
|
|
|
|
sen = "#{receiver}.#{name}(#{@arguments.collect{|a| a.to_s}.join(', ')})"
|
|
|
|
at_depth(depth , sen)
|
2018-06-28 19:15:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|