From ec5a7f8a022cd44f65966d1a4afbf87d903c6877 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Tue, 24 Apr 2018 19:45:58 +0300 Subject: [PATCH] implement larger/smaller or equal --- lib/risc/boot.rb | 2 +- lib/risc/builtin/integer.rb | 12 +++- test/risc/builtin/test_int_cmp.rb | 58 +++++++++++++++++++ .../{test_simple_int.rb => test_int_math.rb} | 26 +-------- 4 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 test/risc/builtin/test_int_cmp.rb rename test/risc/builtin/{test_simple_int.rb => test_int_math.rb} (54%) diff --git a/lib/risc/boot.rb b/lib/risc/boot.rb index e6b6561e..0fea24a4 100644 --- a/lib/risc/boot.rb +++ b/lib/risc/boot.rb @@ -184,7 +184,7 @@ module Risc Risc.operators.each do |op| obj.instance_type.add_method Builtin::Integer.operator_method(op) end - [:putint, :div4, :div10 , :<, :>].each do |f| #div4 is just a forward declaration + [:putint, :div4, :div10 , :<,:<= , :>=, :>].each do |f| #div4 is just a forward declaration obj.instance_type.add_method Builtin::Integer.send(f , nil) end end diff --git a/lib/risc/builtin/integer.rb b/lib/risc/builtin/integer.rb index a86f48de..e7d1cf0a 100644 --- a/lib/risc/builtin/integer.rb +++ b/lib/risc/builtin/integer.rb @@ -25,6 +25,12 @@ module Risc def <( context ) comparison( :< ) end + def <=( context ) + comparison( :<= ) + end + def >=( context ) + comparison( :>= ) + end def comparison( operator ) compiler = compiler_for(:Integer, operator ,{other: :Integer}) builder = compiler.builder(true, compiler.method) @@ -33,12 +39,14 @@ module Risc merge_label = Risc.label(compiler.method , "merge_label_#{builder.object_id}") builder.reduce_int( "#{operator} fix me", me ) builder.reduce_int( "#{operator} fix arg", other ) - if(operator == :<) + if(operator.to_s.start_with?('<') ) me , other = other , me end builder.add_code Risc.op( "#{operator} operator", :- , me , other) builder.add_code IsMinus.new( "#{operator} if", false_label) - builder.add_code IsZero.new( "#{operator} if", false_label) + if(operator.to_s.length == 1) + builder.add_code IsZero.new( "#{operator} if", false_label) + end builder.add_load_constant("#{operator} new int", Parfait.object_space.true_object , other) builder.add_code Risc::Branch.new("jump over false", merge_label) builder.add_code false_label diff --git a/test/risc/builtin/test_int_cmp.rb b/test/risc/builtin/test_int_cmp.rb new file mode 100644 index 00000000..6a984f8c --- /dev/null +++ b/test/risc/builtin/test_int_cmp.rb @@ -0,0 +1,58 @@ +require_relative "helper" + +module Risc + module Builtin + class IntCmp < BuiltinTest + + def test_smaller_true + run_input "4 < 5" + assert_equal Parfait::TrueClass , get_return.class + end + def test_smaller_false + run_input "6 < 5" + assert_equal Parfait::FalseClass , get_return.class + end + def test_smaller_false_same + run_input "5 < 5" + assert_equal Parfait::FalseClass , get_return.class + end + def test_larger_true + run_input "5 > 4" + assert_equal Parfait::TrueClass , get_return.class + end + def test_larger_false + run_input "5 > 6" + assert_equal Parfait::FalseClass , get_return.class + end + def test_larger_false_same + run_input "5 > 5" + assert_equal Parfait::FalseClass , get_return.class + end + + def test_smaller_or_true + run_input "4 <= 5" + assert_equal Parfait::TrueClass , get_return.class + end + def test_smaller_or_false + run_input "6 <= 5" + assert_equal Parfait::FalseClass , get_return.class + end + def test_smaller_or_same + run_input "5 <= 5" + assert_equal Parfait::TrueClass , get_return.class + end + def test_larger_or_true + run_input "5 >= 4" + assert_equal Parfait::TrueClass , get_return.class + end + def test_larger_or_false + run_input "5 >= 6" + assert_equal Parfait::FalseClass , get_return.class + end + def test_larger_or_same + run_input "5 >= 5" + assert_equal Parfait::TrueClass , get_return.class + end + end + end +end diff --git a/test/risc/builtin/test_simple_int.rb b/test/risc/builtin/test_int_math.rb similarity index 54% rename from test/risc/builtin/test_simple_int.rb rename to test/risc/builtin/test_int_math.rb index d58f94e9..4cd6a46a 100644 --- a/test/risc/builtin/test_simple_int.rb +++ b/test/risc/builtin/test_int_math.rb @@ -2,7 +2,7 @@ require_relative "helper" module Risc module Builtin - class SimpleInt < BuiltinTest + class IntMath < BuiltinTest def test_add run_input "5 + 5" @@ -37,30 +37,6 @@ module Risc run_input "4 * 4" assert_equal 16 , get_return.value end - def test_smaller_true - run_input "4 < 5" - assert_equal Parfait::TrueClass , get_return.class - end - def test_smaller_false - run_input "6 < 5" - assert_equal Parfait::FalseClass , get_return.class - end - def test_smaller_false_same - run_input "5 < 5" - assert_equal Parfait::FalseClass , get_return.class - end - def test_larger_true - run_input "5 > 4" - assert_equal Parfait::TrueClass , get_return.class - end - def test_larger_false - run_input "5 > 6" - assert_equal Parfait::FalseClass , get_return.class - end - def test_larger_false_same - run_input "5 > 5" - assert_equal Parfait::FalseClass , get_return.class - end end end end