fix interpreter overflow issue
flag set for bigness, result reduced tests for + and * fixed fibs tests
This commit is contained in:
parent
6ea698d397
commit
d6108e7b3a
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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"
|
||||
|
@ -1,6 +1,6 @@
|
||||
require_relative "helper"
|
||||
|
||||
class AddTest < MiniTest::Test
|
||||
class IfTest < MiniTest::Test
|
||||
include Ticker
|
||||
|
||||
def setup
|
||||
|
@ -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 = <<HERE
|
||||
class Object
|
||||
int main()
|
||||
return 5 + 10
|
||||
return #{2**31} * #{2**31}
|
||||
end
|
||||
end
|
||||
HERE
|
||||
super
|
||||
end
|
||||
|
||||
def test_add
|
||||
#show_ticks # get output of what is
|
||||
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
|
||||
"LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
|
||||
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
|
||||
"RegisterTransfer","Syscall","NilClass"]
|
||||
end
|
||||
|
||||
def test_mult
|
||||
@string_input = <<HERE
|
||||
class Object
|
||||
int main()
|
||||
return 5 * 10
|
||||
end
|
||||
end
|
||||
HERE
|
||||
setup
|
||||
#show_ticks # get output of what is
|
||||
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
|
||||
"LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
|
||||
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
|
||||
"RegisterTransfer","Syscall","NilClass"]
|
||||
check_return 0
|
||||
end
|
||||
def test_overflow
|
||||
ticks( 12 )
|
||||
assert @interpreter.flags[:overflow]
|
||||
end
|
||||
|
||||
def test_zero
|
||||
ticks( 12 )
|
||||
assert @interpreter.flags[:zero]
|
||||
end
|
||||
|
||||
end
|
35
test/interpreter/test_plus.rb
Normal file
35
test/interpreter/test_plus.rb
Normal file
@ -0,0 +1,35 @@
|
||||
require_relative "helper"
|
||||
|
||||
class PlusTest < MiniTest::Test
|
||||
include Ticker
|
||||
|
||||
def setup
|
||||
@string_input = <<HERE
|
||||
class Object
|
||||
int main()
|
||||
return #{2**62 - 1} + 1
|
||||
end
|
||||
end
|
||||
HERE
|
||||
super
|
||||
end
|
||||
|
||||
def test_add
|
||||
#show_ticks # get output of what is
|
||||
check_chain ["Branch","Label","LoadConstant","GetSlot","SetSlot",
|
||||
"LoadConstant","SetSlot","FunctionCall","Label","LoadConstant",
|
||||
"LoadConstant","OperatorInstruction","SetSlot","Label","FunctionReturn",
|
||||
"RegisterTransfer","Syscall","NilClass"]
|
||||
check_return 0
|
||||
end
|
||||
|
||||
def test_overflow
|
||||
ticks( 12 )
|
||||
assert @interpreter.flags[:overflow]
|
||||
end
|
||||
|
||||
def test_zero
|
||||
ticks( 12 )
|
||||
assert @interpreter.flags[:zero]
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user