rubyx/lib/mom/instruction/truth_check.rb

34 lines
1.1 KiB
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 , [] ).to_register(compiler,self)
2018-03-19 16:49:26 +01:00
left << false_load
left << Risc.op( self , :- , left.register , false_load.register)
left << Risc::IsZero.new( self, false_label)
nil_load = SlotDefinition.new( NilConstant.new , [] ).to_register(compiler,self)
left << nil_load
left << Risc.op( self , :- , left.register , nil_load.register)
left << Risc::IsZero.new( self, false_label)
2018-03-19 16:49:26 +01:00
left
end
2017-08-30 16:21:13 +02:00
end
end