2019-10-03 20:07:55 +02:00
|
|
|
module SlotMachine
|
2019-09-11 19:17:43 +02:00
|
|
|
class Comparison < Macro
|
2019-09-11 18:23:56 +02:00
|
|
|
attr_reader :operator
|
|
|
|
def initialize(name , operator)
|
|
|
|
super(name)
|
2020-03-03 21:35:23 +01:00
|
|
|
#TODO check operator to be in valid range
|
2019-09-11 18:23:56 +02:00
|
|
|
@operator = operator.value
|
|
|
|
end
|
2020-03-03 21:35:23 +01:00
|
|
|
|
|
|
|
# basically use subtract to subtract left from right (or right from left)
|
|
|
|
# and load true_object in the true branch and false_object in the false
|
|
|
|
# for the case of =, as in <= or >= we also do not check for zero
|
2019-09-11 18:23:56 +02:00
|
|
|
def to_risc(compiler)
|
|
|
|
builder = compiler.builder(compiler.source)
|
|
|
|
operator = @operator # make accessible in block
|
2020-03-06 20:26:23 +01:00
|
|
|
false_label = Risc.label("false" , "false_label_#{object_id}")
|
|
|
|
merge_label = Risc.label("merge" , "merge_label_#{object_id}")
|
2020-03-03 21:35:23 +01:00
|
|
|
result = Risc::RegisterValue.new(:result , :Object)
|
2019-09-11 18:23:56 +02:00
|
|
|
builder.build do
|
2020-03-11 21:54:20 +01:00
|
|
|
left = message[:receiver].reduce_int(false) #false == hack
|
|
|
|
right = message[:arg1].reduce_int(false)
|
2020-03-03 21:35:23 +01:00
|
|
|
|
|
|
|
if(operator.to_s.start_with?('<') )
|
|
|
|
right.op :- , left
|
|
|
|
else
|
|
|
|
left.op :- , right
|
|
|
|
end
|
2019-09-11 18:23:56 +02:00
|
|
|
if_minus false_label
|
2020-03-06 20:26:23 +01:00
|
|
|
if_not_zero( false_label ) if operator.to_s.length == 1
|
2020-03-03 21:35:23 +01:00
|
|
|
add_code Risc::LoadConstant.new(to_s , Parfait.object_space.true_object, result)
|
2019-09-11 18:23:56 +02:00
|
|
|
branch merge_label
|
|
|
|
add_code false_label
|
2020-03-03 21:35:23 +01:00
|
|
|
add_code Risc::LoadConstant.new(to_s , Parfait.object_space.false_object, result)
|
2019-09-11 18:23:56 +02:00
|
|
|
add_code merge_label
|
2020-03-03 21:35:23 +01:00
|
|
|
message[:return_value] << result
|
2019-09-11 18:23:56 +02:00
|
|
|
end
|
|
|
|
return compiler
|
|
|
|
end
|
2019-08-12 12:16:15 +02:00
|
|
|
end
|
|
|
|
end
|