From c30e4613859578b6fe3dbaeb81a24078e119a6b4 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sun, 8 Apr 2018 01:01:24 +0300 Subject: [PATCH] add operator to builder just minus for now, easily extended --- lib/risc/builtin/word.rb | 4 ++-- lib/risc/risc_value.rb | 7 +++++++ test/risc/test_builder.rb | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/risc/builtin/word.rb b/lib/risc/builtin/word.rb index 066af51a..f6de295f 100644 --- a/lib/risc/builtin/word.rb +++ b/lib/risc/builtin/word.rb @@ -69,11 +69,11 @@ module Risc space << Parfait.object_space space << space[:nil_object] - add Risc.op(source + "if method is nil", :- , space , typed_method ) + space - typed_method if_zero exit_label name << typed_method[:name] - add Risc.op(source + " compare name with me", :- , name , word ) + name - word if_not_zero false_label typed_method << typed_method[:binary] diff --git a/lib/risc/risc_value.rb b/lib/risc/risc_value.rb index 8c8c9ba5..8e9edd60 100644 --- a/lib/risc/risc_value.rb +++ b/lib/risc/risc_value.rb @@ -77,6 +77,13 @@ module Risc return ins end + def -( right ) + raise "operators only on registers, not #{right.class}" unless right.is_a? RiscValue + op = Risc.op("#{self.type} - #{right.type}", :- , self , right ) + builder.add(op) if builder + op + end + # just capture the values in an intermediary object (RValue) # The RValue then gets used in a RegToSlot ot SlotToReg, where # the values are unpacked to call Risc.reg_to_slot or Risc.slot_to_reg diff --git a/test/risc/test_builder.rb b/test/risc/test_builder.rb index 990a9dc0..f3e0248d 100644 --- a/test/risc/test_builder.rb +++ b/test/risc/test_builder.rb @@ -90,5 +90,11 @@ module Risc assert_equal Branch , ret.class assert_equal @label , ret.label end + def test_minus + op = @builder.build {space - typed_method} + assert_equal OperatorInstruction , op.class + assert_equal :- , op.operator + assert_equal :Space , op.left.type + end end end