From 5b92b6b785c3600df85e46d95c587450cb0b7a70 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 31 Mar 2018 19:37:24 +0300 Subject: [PATCH] get plus (+) working alas, new integer is not created yet --- lib/parfait/integer.rb | 5 +++ lib/risc/builtin/integer.rb | 4 +- test/risc/interpreter/test_plus.rb | 68 ++++++++++++++++++------------ 3 files changed, 48 insertions(+), 29 deletions(-) diff --git a/lib/parfait/integer.rb b/lib/parfait/integer.rb index 3a566437..4a9d50d2 100644 --- a/lib/parfait/integer.rb +++ b/lib/parfait/integer.rb @@ -19,6 +19,11 @@ module Parfait def self.integer_index 3 # 1 type, 2 next_i end + + def get_internal_word( index ) + return super(index) unless index == Integer.integer_index + return @value + end # :integer?, :odd?, :even?, :upto, :downto, :times, :succ, :next, :pred, :chr, :ord, :to_i, :to_int, :floor, # :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator, :to_r, :rationalize, # :singleton_method_added, :coerce, :i, :+@, :-@, :fdiv, :div, :divmod, :%, :modulo, :remainder, :abs, :magnitude, diff --git a/lib/risc/builtin/integer.rb b/lib/risc/builtin/integer.rb index 20d28366..a1e966e3 100644 --- a/lib/risc/builtin/integer.rb +++ b/lib/risc/builtin/integer.rb @@ -20,8 +20,8 @@ module Risc compiler = compiler_for(:Integer,:+ ,{other: :Integer}) me , other = self_and_int_arg(compiler,source + "1") # reduce me and other to integers - compiler.add_slot_to_reg( source + "2" , me , 2 , me) - compiler.add_slot_to_reg( source + "3", other , 2 , other) + compiler.add_slot_to_reg( source + "2" , me , Parfait::Integer.integer_index , me) + compiler.add_slot_to_reg( source + "3", other , Parfait::Integer.integer_index , other) compiler.add_code Risc.op( source + "4", :+ , me , other) #TODO must get an Integer and put the value there then return the integer (object not value) # and put it back into the return value diff --git a/test/risc/interpreter/test_plus.rb b/test/risc/interpreter/test_plus.rb index c7c8d75c..230756c2 100644 --- a/test/risc/interpreter/test_plus.rb +++ b/test/risc/interpreter/test_plus.rb @@ -5,12 +5,12 @@ module Risc include Ticker def setup - @string_input = as_main("a = 5 + 5") + @string_input = as_main("a = 5 + 5;return a") super end def est_add - show_ticks # get output of what is + #show_ticks # get output of what is check_chain [Branch, Label, LoadConstant, SlotToReg, LoadConstant, SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg, SlotToReg, SlotToReg, SlotToReg, RegToSlot, LoadConstant, @@ -20,40 +20,54 @@ module Risc LoadConstant, SlotToReg, SlotToReg, SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg, SlotToReg, SlotToReg, SlotToReg, RegToSlot, LoadConstant, SlotToReg, RegToSlot, - LoadConstant, SlotToReg, SlotToReg] - #assert_equal 10 , get_return + LoadConstant, SlotToReg, SlotToReg, RegToSlot, LoadConstant, + SlotToReg, RegToSlot, SlotToReg, LoadConstant, FunctionCall, + Label, SlotToReg, SlotToReg, SlotToReg, SlotToReg, + SlotToReg, OperatorInstruction, RegToSlot, Label, NilClass] + assert_equal 10 , get_return end - def est_slot3 - sl = ticks( 49 ) - assert_equal SlotToReg , sl.class - assert_equal :r2 , sl.array.symbol #load from message - assert_equal 9 , sl.index - assert_equal :r3 , sl.register.symbol + def test_load_5 + lod = ticks( 46 ) + assert_equal LoadConstant , lod.class + assert_equal Parfait::Integer , lod.constant.class + assert_equal 5 , lod.constant.value end - def est_slot2 #load arg from args - sl = ticks( 48 ) - assert_equal SlotToReg , sl.class - assert_equal :r2 , sl.array.symbol #load from message - assert_equal 9 , sl.index - assert_equal :r3 , sl.register.symbol - end - def est_slot1 #load args from message - sl = ticks( 47 ) + def test_slot_receiver #load receiver from message + sl = ticks( 57 ) assert_equal SlotToReg , sl.class assert_equal :r0 , sl.array.symbol #load from message + assert_equal 3 , sl.index + assert_equal :r1 , sl.register.symbol + end + def test_slot_args #load args from message + sl = ticks( 58 ) + assert_equal SlotToReg , sl.class + assert_equal :r0 , sl.array.symbol #load from message + assert_equal 9 , sl.index + assert_equal :r2 , sl.register.symbol + end + def test_slot_arg #load arg 1, destructively from args + sl = ticks( 59 ) + assert_equal SlotToReg , sl.class + assert_equal :r2 , sl.array.symbol #load from message assert_equal 2 , sl.index assert_equal :r2 , sl.register.symbol end - def est_load_2 - lod = ticks( 46 ) - assert_equal LoadConstant , lod.class - assert_equal 5 , lod.constant.value + def test_slot_int1 #load int from object + sl = ticks( 60 ) + assert_equal SlotToReg , sl.class + assert_equal :r1 , sl.array.symbol #load from message + assert_equal 3 , sl.index + assert_equal :r1 , sl.register.symbol end - - def pest_zero - ticks( 12 ) - assert @interpreter.flags[:zero] + def test_op + op = ticks(62) + assert_equal OperatorInstruction , op.class + assert_equal :r1 , op.left.symbol + assert_equal :r2 , op.right.symbol + assert_equal 5 , @interpreter.get_register(:r2) + assert_equal 10 , @interpreter.get_register(:r1) end end end