diff --git a/lib/mom/instruction/truth_check.rb b/lib/mom/instruction/truth_check.rb index e3e81f6b..72b818f4 100644 --- a/lib/mom/instruction/truth_check.rb +++ b/lib/mom/instruction/truth_check.rb @@ -20,6 +20,9 @@ module Mom false_load = SlotDefinition.new( FalseConstant.new , nil ).to_register(compiler,self) left << false_load left << Risc::IsSame.new(self , left.register , false_load.register , false_label) + nil_load = SlotDefinition.new( NilConstant.new , nil ).to_register(compiler,self) + left << nil_load + left << Risc::IsSame.new(self , left.register , nil_load.register , false_label) left end diff --git a/test/mom/test_if_else.rb b/test/mom/test_if_else.rb new file mode 100644 index 00000000..55edfbcb --- /dev/null +++ b/test/mom/test_if_else.rb @@ -0,0 +1,51 @@ +require_relative "helper" + +module Risc + class TestIfElse < MiniTest::Test + include Statements + + def setup + super + @input = "if(@a) ; arg = 5 ; else; arg = 6; end" + @expect = [SlotToReg, SlotToReg, LoadConstant, IsSame, LoadConstant, IsSame , + Label, LoadConstant, SlotToReg, RegToSlot, Label, Label , + LoadConstant, SlotToReg, RegToSlot, Label] + end + + def test_if_instructions + assert_nil msg = check_nil , msg + end + + def test_false_load + produced = produce_body + assert_equal Mom::FalseConstant , produced.next(2).constant.known_object.class + end + def test_false_check + produced = produce_body + assert_equal produced.next(11) , produced.next(3).label + end + def test_nil_load + produced = produce_body + assert_equal Mom::NilConstant , produced.next(4).constant.known_object.class + end + def test_nil_check + produced = produce_body + assert_equal produced.next(11) , produced.next(5).label + end + + def test_true_label + produced = produce_body + assert produced.next(6).name.start_with?("true_label") + end + + def test_merge_label + produced = produce_body + assert produced.next(15).name.start_with?("merge_label") + end + + def test_true_jump # should jumpp to merge label + produced = produce_body + assert_equal "Jump" , produced.next(10).name + end + end +end diff --git a/test/mom/test_if_no_else.rb b/test/mom/test_if_no_else.rb index 9b995856..74526cd1 100644 --- a/test/mom/test_if_no_else.rb +++ b/test/mom/test_if_no_else.rb @@ -7,17 +7,35 @@ module Risc def setup super @input = "if(@a) ; arg = 5 ; end" - @expect = [SlotToReg, SlotToReg, LoadConstant, IsSame, Label, LoadConstant , - SlotToReg, RegToSlot, Label] + @expect = [SlotToReg, SlotToReg, LoadConstant, IsSame, LoadConstant, IsSame , + Label, LoadConstant, SlotToReg, RegToSlot, Label] end def test_if_instructions assert_nil msg = check_nil , msg end - def ttest_if_instructions + def test_false_load produced = produce_body - assert_equal 5 , produced.class , produced + assert_equal Mom::FalseConstant , produced.next(2).constant.known_object.class end + def test_false_check + produced = produce_body + assert_equal produced.next(10) , produced.next(3).label + end + def test_nil_load + produced = produce_body + assert_equal Mom::NilConstant , produced.next(4).constant.known_object.class + end + def test_nil_check + produced = produce_body + assert_equal produced.next(10) , produced.next(5).label + end + + def test_true_label + produced = produce_body + assert produced.next(6).name.start_with?("true_label") + end + end end diff --git a/test/parfait/type/test_type_api.rb b/test/parfait/type/test_type_api.rb index 24f916ad..fbe043b7 100644 --- a/test/parfait/type/test_type_api.rb +++ b/test/parfait/type/test_type_api.rb @@ -27,7 +27,7 @@ class TypeApi < MiniTest::Test assert_equal Parfait::Space , space.class type = space.get_type assert_equal Parfait::Type , type.class - assert_equal 4 , type.names.get_length + assert_equal 7 , type.names.get_length assert_equal type.object_class.class , Parfait::Class assert_equal type.object_class.name , :Space end