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