rubyx/lib/slot_machine/instructions/truth_check.rb

37 lines
1.0 KiB
Ruby
Raw Normal View History

2019-10-03 20:07:55 +02:00
module SlotMachine
2017-08-30 16:21:13 +02:00
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_label)
super(false_label)
2017-09-06 11:08:44 +02:00
@condition = condition
2020-02-17 08:26:50 +01:00
raise "condition must be slot_definition #{condition}" unless condition.is_a?(Slotted)
2017-08-30 16:21:13 +02:00
end
def to_s
"TruthCheck #{@condition} -> #{false_label}"
end
def to_risc(compiler)
false_label = @false_label.risc_label(compiler)
condition_reg = @condition.to_register(compiler,self)
compiler.build(self.to_s) do
object = load_object Parfait.object_space.false_object
object.op :- , condition_reg
if_zero false_label
object = load_object Parfait.object_space.nil_object
object.op :- , condition_reg
if_zero false_label
end
end
2017-08-30 16:21:13 +02:00
end
end