rubyx/lib/sol/return_statement.rb

39 lines
1.1 KiB
Ruby
Raw Normal View History

module Sol
2017-04-01 20:28:57 +02:00
class ReturnStatement < Statement
2018-03-16 14:56:27 +01:00
attr_reader :return_value
2017-04-04 09:42:20 +02:00
def initialize(value)
@return_value = value
end
2017-04-08 11:10:42 +02:00
2018-03-16 14:56:27 +01:00
def each(&block)
block.call(self)
@return_value.each(&block)
2017-04-08 11:10:42 +02:00
end
2017-04-14 09:52:23 +02:00
2018-03-16 14:56:27 +01:00
# Since the return is normalized to only allow simple values it is simple.
2019-10-03 20:07:55 +02:00
# To return form a method in slot_machine instructions we only need to do two things:
2018-03-16 14:56:27 +01:00
# - store the given return value, this is a SlotMove
# - activate return sequence (reinstantiate old message and jump to return address)
def to_slot( compiler )
if @return_value.is_a?(CallStatement)
ret = @return_value.to_slot(compiler)
ret << slot_load(compiler)
else
ret = slot_load(compiler)
end
ret << SlotMachine::ReturnJump.new(self , compiler.return_label )
2017-04-14 09:52:23 +02:00
end
2018-07-03 21:18:19 +02:00
def to_s(depth = 0)
at_depth(depth , "return #{@return_value.to_s}")
end
def slot_load(compiler)
SlotMachine::SlotLoad.new( self , [:message , :return_value] ,
@return_value.to_slotted(compiler) )
end
2017-04-01 20:28:57 +02:00
end
end