2017-08-30 16:21:13 +02:00
|
|
|
module Mom
|
|
|
|
|
2017-09-06 11:08:44 +02:00
|
|
|
# A base class for conditions in MOM
|
2018-03-16 08:03:11 +01:00
|
|
|
# Checks (if in code, compare in assm) jump or not, depending
|
|
|
|
# The logic we choose is closer to the code logic (the asm is reversed)
|
|
|
|
# When we write an if, the true is the next code, so the Check logic is
|
|
|
|
# that if the check passes, no jump happens
|
|
|
|
# This means you need to pass the false label, where to jump to if the
|
|
|
|
# check does not pass
|
|
|
|
# Note: In assembler a branch on 0 does just that, it branches if the condition
|
|
|
|
# is met. This means that the asm implementation is somewhat the reverse
|
|
|
|
# of the Mom names. But it's easier to understand (imho)
|
2017-09-06 11:08:44 +02:00
|
|
|
class Check < Instruction
|
2018-03-16 08:03:11 +01:00
|
|
|
attr_reader :false_jump
|
|
|
|
def initialize(false_jump)
|
|
|
|
@false_jump = false_jump
|
|
|
|
raise "Jump target must be a label #{false_jump}" unless false_jump.is_a?(Label)
|
|
|
|
end
|
2017-09-06 11:08:44 +02:00
|
|
|
end
|
|
|
|
|
2017-08-30 16:21:13 +02:00
|
|
|
# The funny thing about the ruby truth is that is 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
|
|
|
|
#
|
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-14 13:07:27 +01:00
|
|
|
Risc::Label.new(self,"TruthCheck")
|
2018-03-13 11:46:06 +01:00
|
|
|
end
|
|
|
|
|
2017-08-30 16:21:13 +02:00
|
|
|
end
|
|
|
|
end
|