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
|
|
|
|
#
|
2017-09-04 20:00:08 +02:00
|
|
|
class TruthCheck < Check
|
|
|
|
attr_reader :condition
|
2017-08-30 16:21:13 +02:00
|
|
|
|
2018-03-16 08:03:11 +01:00
|
|
|
def initialize(condition , false_jump)
|
|
|
|
super(false_jump)
|
2017-09-06 11:08:44 +02:00
|
|
|
@condition = condition
|
2018-03-16 08:03:11 +01:00
|
|
|
raise "condition must be slot_definition #{condition}" unless condition.is_a?(SlotDefinition)
|
2017-08-30 16:21:13 +02:00
|
|
|
end
|
2018-03-13 11:46:06 +01:00
|
|
|
|
|
|
|
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)
|
2018-03-22 18:14:22 +01:00
|
|
|
false_load = SlotDefinition.new( FalseConstant.new , [] ).to_register(compiler,self)
|
2018-03-19 16:49:26 +01:00
|
|
|
left << false_load
|
2018-03-24 17:54:36 +01:00
|
|
|
left << Risc.op( self , :- , left.register , false_load.register)
|
|
|
|
left << Risc::IsNotZero.new( self, false_label)
|
2018-03-22 18:14:22 +01:00
|
|
|
nil_load = SlotDefinition.new( NilConstant.new , [] ).to_register(compiler,self)
|
2018-03-20 09:00:38 +01:00
|
|
|
left << nil_load
|
2018-03-24 17:54:36 +01:00
|
|
|
left << Risc.op( self , :- , left.register , nil_load.register)
|
|
|
|
left << Risc::IsNotZero.new( self, false_label)
|
|
|
|
|
2018-03-19 16:49:26 +01:00
|
|
|
left
|
2018-03-13 11:46:06 +01:00
|
|
|
end
|
|
|
|
|
2017-08-30 16:21:13 +02:00
|
|
|
end
|
|
|
|
end
|