From 8cd9818f643d3369138b09b0359dbaf6537db029 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 20 Jul 2018 09:07:09 +0300 Subject: [PATCH] fix ruby return statement assignment and normalizer on the way --- lib/ruby/assignment_statement.rb | 6 +++--- lib/ruby/normalizer.rb | 8 ++++---- lib/ruby/return_statement.rb | 2 +- test/ruby/test_return_statement.rb | 22 ++++++++++++++++++++++ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/ruby/assignment_statement.rb b/lib/ruby/assignment_statement.rb index 2aa01e2b..05e4ec93 100644 --- a/lib/ruby/assignment_statement.rb +++ b/lib/ruby/assignment_statement.rb @@ -20,12 +20,12 @@ module Ruby def copy(value = nil) value ||= @value - self.class.new(name,value) + self.vool_brother.new(name,value) end def to_vool_send statements = value.normalize() - return copy( statements ) if statements.is_a?(SendStatement) + return copy( statements ) if statements.is_a?(Vool::SendStatement) assign = statements.statements.pop statements << copy(assign) statements @@ -41,7 +41,7 @@ module Ruby def to_vool() super() - return IvarAssignment.new(@name , @value) + return Vool::IvarAssignment.new(@name , @value) end end diff --git a/lib/ruby/normalizer.rb b/lib/ruby/normalizer.rb index bf148c0f..0aa98462 100644 --- a/lib/ruby/normalizer.rb +++ b/lib/ruby/normalizer.rb @@ -8,15 +8,15 @@ module Ruby # but if(tmp_123) is with tmp_123 = @var % 5 hoisted above the if # # also constants count, though they may not be so useful in ifs, but returns - def to_vool_name( condition ) + def normalize_name( condition ) if( condition.is_a?(ScopeStatement) and condition.single?) condition = condition.first end return [condition] if condition.is_a?(Named) or condition.is_a?(Constant) - condition = condition.normalize + condition = condition.to_vool local = "tmp_#{object_id}".to_sym - assign = Statements.new [LocalAssignment.new( local , condition)] - [LocalVariable.new(local) , assign] + assign = Vool::Statements.new [Vool::LocalAssignment.new( local , condition)] + [Vool::LocalVariable.new(local) , assign] end end end diff --git a/lib/ruby/return_statement.rb b/lib/ruby/return_statement.rb index 138246f2..f76343cc 100644 --- a/lib/ruby/return_statement.rb +++ b/lib/ruby/return_statement.rb @@ -10,7 +10,7 @@ module Ruby def to_vool val , rest = *normalize_name(@return_value) - me = ReturnStatement.new(val) + me = Vool::ReturnStatement.new(val) return me unless rest rest << me rest diff --git a/test/ruby/test_return_statement.rb b/test/ruby/test_return_statement.rb index d9f0fa3e..7d0e5af6 100644 --- a/test/ruby/test_return_statement.rb +++ b/test/ruby/test_return_statement.rb @@ -1,6 +1,28 @@ require_relative "helper" module Ruby + class TestReturnStatementVool < MiniTest::Test + include RubyTests + + def test_return_const + lst = compile( "return 1" ).to_vool + assert_equal Vool::ReturnStatement , lst.class + end + def test_return_value + lst = compile( "return 1" ).to_vool + assert_equal 1 , lst.return_value.value + end + + def test_return_send + lst = compile( "return foo" ).to_vool + assert_equal Vool::LocalAssignment , lst.first.class + end + def test_return_send + lst = compile( "return foo" ).to_vool + assert lst.last.return_value.name.to_s.start_with?("tmp_") + assert_equal 2, lst.length + end + end class TestReturnStatement < MiniTest::Test include RubyTests