rubyx/lib/mom/instruction/truth_check.rb

28 lines
855 B
Ruby
Raw Normal View History

2017-08-30 16:21:13 +02:00
module Mom
2018-03-19 16:49:26 +01:00
# The funny thing about the ruby truth is that it is anything but false or nil
2017-08-30 16:21:13 +02:00
#
# To implement the normal ruby logic, we check for false or nil and jump
# to the false branch. true_block follows implicitly
#
class TruthCheck < Check
attr_reader :condition
2017-08-30 16:21:13 +02:00
def initialize(condition , false_jump)
super(false_jump)
2017-09-06 11:08:44 +02:00
@condition = condition
raise "condition must be slot_definition #{condition}" unless condition.is_a?(SlotDefinition)
2017-08-30 16:21:13 +02:00
end
def to_risc(compiler)
2018-03-19 16:49:26 +01:00
false_label = @false_jump.to_risc(compiler)
left = @condition.to_register(compiler,self)
false_load = SlotDefinition.new( FalseConstant.new , nil ).to_register(compiler,self)
left << false_load
left << Risc::IsSame.new(self , left.register , false_load.register , false_label)
left
end
2017-08-30 16:21:13 +02:00
end
end