fix ruby return statement

assignment and normalizer on the way
This commit is contained in:
Torsten Ruger 2018-07-20 09:07:09 +03:00
parent a5168ef818
commit 8cd9818f64
4 changed files with 30 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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