rubyx/lib/mom/instruction/truth_check.rb
Torsten Ruger de7e02b0b8 remove IsSame branch from risc
mixing up levels, IsSame is Mom
at risc this is a minus and Zero check
fix all tests
2018-03-24 18:54:36 +02:00

34 lines
1.1 KiB
Ruby

module Mom
# The funny thing about the ruby truth is that it is anything but false or nil
#
# 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
def initialize(condition , false_jump)
super(false_jump)
@condition = condition
raise "condition must be slot_definition #{condition}" unless condition.is_a?(SlotDefinition)
end
def to_risc(compiler)
false_label = @false_jump.to_risc(compiler)
left = @condition.to_register(compiler,self)
false_load = SlotDefinition.new( FalseConstant.new , [] ).to_register(compiler,self)
left << false_load
left << Risc.op( self , :- , left.register , false_load.register)
left << Risc::IsNotZero.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::IsNotZero.new( self, false_label)
left
end
end
end