fix TruthCheck

mixing up the false and true, such a basic human mistake
This commit is contained in:
Torsten Ruger 2018-04-19 10:34:15 +03:00
parent 3a50b7dd0e
commit 1849522a54
6 changed files with 55 additions and 23 deletions

View File

@ -20,11 +20,11 @@ module Mom
false_load = SlotDefinition.new( FalseConstant.new , [] ).to_register(compiler,self) false_load = SlotDefinition.new( FalseConstant.new , [] ).to_register(compiler,self)
left << false_load left << false_load
left << Risc.op( self , :- , left.register , false_load.register) left << Risc.op( self , :- , left.register , false_load.register)
left << Risc::IsNotZero.new( self, false_label) left << Risc::IsZero.new( self, false_label)
nil_load = SlotDefinition.new( NilConstant.new , [] ).to_register(compiler,self) nil_load = SlotDefinition.new( NilConstant.new , [] ).to_register(compiler,self)
left << nil_load left << nil_load
left << Risc.op( self , :- , left.register , nil_load.register) left << Risc.op( self , :- , left.register , nil_load.register)
left << Risc::IsNotZero.new( self, false_label) left << Risc::IsZero.new( self, false_label)
left left
end end

View File

@ -21,6 +21,20 @@ module Vool
end end
def to_mom( method ) def to_mom( method )
if_false ? full_if(method) : simple_if(method)
end
def simple_if(method)
true_label = Mom::Label.new( "true_label_#{object_id}")
merge_label = Mom::Label.new( "merge_label_#{object_id}")
head = Mom::TruthCheck.new(condition.slot_definition(method) , merge_label)
head << true_label
head << if_true.to_mom(method)
head << merge_label
end
def full_if(method)
true_label = Mom::Label.new( "true_label_#{object_id}") true_label = Mom::Label.new( "true_label_#{object_id}")
false_label = Mom::Label.new( "false_label_#{object_id}") false_label = Mom::Label.new( "false_label_#{object_id}")
merge_label = Mom::Label.new( "merge_label_#{object_id}") merge_label = Mom::Label.new( "merge_label_#{object_id}")
@ -28,13 +42,10 @@ module Vool
head = Mom::TruthCheck.new(condition.slot_definition(method) , false_label) head = Mom::TruthCheck.new(condition.slot_definition(method) , false_label)
head << true_label head << true_label
head << if_true.to_mom(method) head << if_true.to_mom(method)
head << Mom::Jump.new(merge_label) if if_false head << Mom::Jump.new(merge_label)
head << false_label head << false_label
if if_false head << if_false.to_mom(method)
head << if_false.to_mom(method) head << merge_label
head << merge_label
end
head
end end
def each(&block) def each(&block)

View File

@ -7,9 +7,9 @@ module Risc
def setup def setup
super super
@input = "if(@a) ; arg = 5 ; else; arg = 6; end" @input = "if(@a) ; arg = 5 ; else; arg = 6; end"
@expect = [SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, IsNotZero , @expect = [SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, IsZero,
LoadConstant, OperatorInstruction, IsNotZero, Label, LoadConstant , LoadConstant, OperatorInstruction, IsZero, Label, LoadConstant,
SlotToReg, RegToSlot, Branch, Label, LoadConstant , SlotToReg, RegToSlot, Branch, Label, LoadConstant,
SlotToReg, RegToSlot, Label] SlotToReg, RegToSlot, Label]
end end

View File

@ -7,8 +7,8 @@ module Risc
def setup def setup
super super
@input = "if(@a) ; arg = 5 ; end" @input = "if(@a) ; arg = 5 ; end"
@expect = [SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, IsNotZero , @expect = [SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, IsZero,
LoadConstant, OperatorInstruction, IsNotZero, Label, LoadConstant , LoadConstant, OperatorInstruction, IsZero, Label, LoadConstant,
SlotToReg, RegToSlot, Label] SlotToReg, RegToSlot, Label]
end end
@ -22,8 +22,9 @@ module Risc
end end
def test_isnotzero def test_isnotzero
produced = produce_body produced = produce_body
assert_equal IsNotZero , produced.next(4).class check = produced.next(4)
assert produced.next(4).label.name.start_with?("false_label") assert_equal IsZero , check.class
assert check.label.name.start_with?("merge_label") , check.label.name
end end
def test_false_label def test_false_label
produced = produce_body produced = produce_body

View File

@ -7,8 +7,8 @@ module Risc
def setup def setup
super super
@input = "while(@a) ; arg = 5 end" @input = "while(@a) ; arg = 5 end"
@expect = [Label, SlotToReg, SlotToReg, LoadConstant, OperatorInstruction , @expect = [Label, SlotToReg, SlotToReg, LoadConstant, OperatorInstruction,
IsNotZero, LoadConstant, OperatorInstruction, IsNotZero, LoadConstant , IsZero, LoadConstant, OperatorInstruction, IsZero, LoadConstant,
SlotToReg, RegToSlot, Branch, Label] SlotToReg, RegToSlot, Branch, Label]
end end

View File

@ -11,15 +11,35 @@ module Risc
def test_if def test_if
#show_main_ticks # get output of what is in main #show_main_ticks # get output of what is in main
check_main_chain [Label, LoadConstant, LoadConstant, OperatorInstruction, IsNotZero, check_main_chain [Label, LoadConstant, LoadConstant, OperatorInstruction, IsZero,
Label, LoadConstant, RegToSlot, SlotToReg, SlotToReg, LoadConstant, OperatorInstruction, IsZero, Label, LoadConstant,
RegToSlot, SlotToReg, SlotToReg, FunctionReturn, Transfer, RegToSlot, SlotToReg, SlotToReg, RegToSlot, SlotToReg,
Syscall, NilClass] SlotToReg, FunctionReturn, Transfer, Syscall, NilClass]
assert_equal Parfait::Word , get_return.class assert_equal Parfait::Word , get_return.class
assert_equal "else" , get_return.to_string assert_equal "then" , get_return.to_string
end
def test_load_10
load = main_ticks(2)
assert_equal LoadConstant , load.class
assert_equal 10 , load.constant.value
end
def test_load_false
load = main_ticks(3)
assert_equal LoadConstant , load.class
assert_equal Parfait::FalseClass , load.constant.class
end
def test_compare
op = main_ticks(4)
assert_equal OperatorInstruction , op.class
assert_equal :- , op.operator
end
def test_not_zero
check = main_ticks(5)
assert_equal IsZero , check.class
assert check.label.name.start_with?("false_label") , check.label.name
end end
def test_exit def test_exit
done = main_ticks(16) done = main_ticks(19)
assert_equal Syscall , done.class assert_equal Syscall , done.class
end end
end end