From d6108e7b3a1ece42669e12269376c2c3bf01bfed Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 8 Nov 2015 15:15:55 +0200 Subject: [PATCH] fix interpreter overflow issue flag set for bigness, result reduced tests for + and * fixed fibs tests --- lib/interpreter/interpreter.rb | 10 ++++-- stash/fibo_times.ruby | 3 +- test/compiler/fragments/test_while_fibo.rb | 18 +++++----- test/interpreter/helper.rb | 5 +++ test/interpreter/test_all.rb | 3 +- test/interpreter/test_if.rb | 2 +- .../interpreter/{test_ops.rb => test_mult.rb} | 30 +++++++--------- test/interpreter/test_plus.rb | 35 +++++++++++++++++++ 8 files changed, 73 insertions(+), 33 deletions(-) rename test/interpreter/{test_ops.rb => test_mult.rb} (52%) create mode 100644 test/interpreter/test_plus.rb diff --git a/lib/interpreter/interpreter.rb b/lib/interpreter/interpreter.rb index 891675fe..1f6d0e77 100644 --- a/lib/interpreter/interpreter.rb +++ b/lib/interpreter/interpreter.rb @@ -1,5 +1,6 @@ require_relative "eventable" +require 'bigdecimal' module Interpreter class Interpreter @@ -174,10 +175,10 @@ module Interpreter end def execute_OperatorInstruction - log.debug @instruction left = get_register(@instruction.left) rr = @instruction.right right = get_register(rr) + @flags[:overflow] = false case @instruction.operator.to_s when "+" result = left + right @@ -193,8 +194,11 @@ module Interpreter else raise "unimplemented '#{@instruction.operator}' #{@instruction}" end - ## result not over 2**62 => overflow - log.debug "#{@instruction} == #{result} (#{left}|#{right})" + if( result.is_a? Bignum) + @flags[:overflow] = true + result = result % 2**62 + end + log.debug "#{@instruction} == #{result}(#{result.class}) (#{left}|#{right})" right = set_register(@instruction.left , result) true end diff --git a/stash/fibo_times.ruby b/stash/fibo_times.ruby index f6ced695..b06f34c0 100644 --- a/stash/fibo_times.ruby +++ b/stash/fibo_times.ruby @@ -10,4 +10,5 @@ def fibonaccit(n) end #1000000.times {fibonaccit( 30 )} -puts fibonaccit 100 +#10.times {|i| puts fibonaccit(i+90).class} +puts fibonaccit 90 diff --git a/test/compiler/fragments/test_while_fibo.rb b/test/compiler/fragments/test_while_fibo.rb index aaba5442..8ff769fd 100644 --- a/test/compiler/fragments/test_while_fibo.rb +++ b/test/compiler/fragments/test_while_fibo.rb @@ -28,17 +28,17 @@ HERE @string_input.sub!( "100" , num.to_s ) end - def test_while_fibo100 - fibo 100 - @length = 2345 - #TODO bug, int max is 92 ruby converts to biginteger. - check_return 354224848179261915075 + def test_while_fibo94 + fibo 91 + @length = 2138 + # this is not the correct fibo, just what comes from wrapping (smaller than below) + check_return 48360591948142405 end - def test_while_fibo92 - fibo 92 - @length = 2161 - check_return 7540113804746346429 + def test_while_fibo90 + fibo 90 + @length = 2115 + check_return 2880067194370816120 end end diff --git a/test/interpreter/helper.rb b/test/interpreter/helper.rb index a17ce153..1d871037 100644 --- a/test/interpreter/helper.rb +++ b/test/interpreter/helper.rb @@ -21,6 +21,11 @@ module Ticker end end + def check_return val + assert_equal Parfait::Message , @interpreter.get_register(:r0).class + assert_equal val , @interpreter.get_register(:r0).return_value + end + def ticks num last = nil num.times do diff --git a/test/interpreter/test_all.rb b/test/interpreter/test_all.rb index 1e826c92..332f49ce 100644 --- a/test/interpreter/test_all.rb +++ b/test/interpreter/test_all.rb @@ -1,4 +1,5 @@ require_relative "test_add" require_relative "test_if" require_relative "test_puts" -require_relative "test_ops" +require_relative "test_plus" +require_relative "test_mult" diff --git a/test/interpreter/test_if.rb b/test/interpreter/test_if.rb index 0c19614a..7f0e1723 100644 --- a/test/interpreter/test_if.rb +++ b/test/interpreter/test_if.rb @@ -1,6 +1,6 @@ require_relative "helper" -class AddTest < MiniTest::Test +class IfTest < MiniTest::Test include Ticker def setup diff --git a/test/interpreter/test_ops.rb b/test/interpreter/test_mult.rb similarity index 52% rename from test/interpreter/test_ops.rb rename to test/interpreter/test_mult.rb index aec8aa9e..09f08bae 100644 --- a/test/interpreter/test_ops.rb +++ b/test/interpreter/test_mult.rb @@ -1,6 +1,6 @@ require_relative "helper" -class AddTest < MiniTest::Test +class MultTest < MiniTest::Test include Ticker include AST::Sexp @@ -8,35 +8,29 @@ class AddTest < MiniTest::Test @string_input = <