From c885888f4a4af5129e89f02a3097fa5bd581613e Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 14 Apr 2017 10:52:23 +0300 Subject: [PATCH] start implementing return for constant --- lib/vool/statements/return_statement.rb | 9 +++++++ test/vool/to_mom/test_local.rb | 2 +- test/vool/to_mom/test_return.rb | 31 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/vool/to_mom/test_return.rb diff --git a/lib/vool/statements/return_statement.rb b/lib/vool/statements/return_statement.rb index 75bc5e89..5f33895e 100644 --- a/lib/vool/statements/return_statement.rb +++ b/lib/vool/statements/return_statement.rb @@ -10,5 +10,14 @@ module Vool @return_value.collect(arr) super end + + # To return form a method in mom instructions we need to do three things: + # - store the given return value, this is a SlotMove / SlotConstant + # - restore the previous message + # - jump to the return address + def to_mom( method ) + [Mom::SlotConstant.new([:message , :return_value] , @return_value)] + end + end end diff --git a/test/vool/to_mom/test_local.rb b/test/vool/to_mom/test_local.rb index 599afb3f..46d06801 100644 --- a/test/vool/to_mom/test_local.rb +++ b/test/vool/to_mom/test_local.rb @@ -18,7 +18,7 @@ module Vool def test_slot_starts_at_message assert_equal :message , @stats.first.left[0] end - def test_slot_gets_self + def test_slot_gets_frame assert_equal :frame , @stats.first.left[1] end def test_slot_assigns_to_local diff --git a/test/vool/to_mom/test_return.rb b/test/vool/to_mom/test_return.rb new file mode 100644 index 00000000..27b07756 --- /dev/null +++ b/test/vool/to_mom/test_return.rb @@ -0,0 +1,31 @@ +require_relative "helper" + +module Vool + class TestReturnMom < MiniTest::Test + include MomCompile + + def setup + Risc.machine.boot + @stats = compile_first_method( "return 5").first + end + + def test_class_compiles + assert_equal Mom::SlotConstant , @stats.first.class , @stats + end + def test_slot_is_set + assert @stats.first.left + end + def test_slot_starts_at_message + assert_equal :message , @stats.first.left[0] + end + def test_slot_gets_self + assert_equal :return_value , @stats.first.left[1] + end + def test_slot_assigns_something + assert @stats.first.right + end + def test_slot_assigns_int + assert_equal IntegerStatement , @stats.first.right.class + end + end +end