Redoing ruby block conversion
Since the block is actually a constant, it does not need assignment or special hoisting Just use the send and stick the lambda in as last arg
This commit is contained in:
parent
f87526f86f
commit
3ddf2e3837
@ -11,26 +11,13 @@ module Ruby
|
|||||||
case value
|
case value
|
||||||
when Variable , Constant
|
when Variable , Constant
|
||||||
return self.vool_brother.new(name,@value.to_vool)
|
return self.vool_brother.new(name,@value.to_vool)
|
||||||
when SendStatement , YieldStatement
|
when SendStatement , YieldStatement , RubyBlockStatement
|
||||||
return normalize_send
|
return normalize_send
|
||||||
when RubyBlockStatement
|
|
||||||
return normalize_block
|
|
||||||
else
|
else
|
||||||
raise "unsupported right #{value}"
|
raise "unsupported right #{value}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Ruby BlockStatements have the block and the send. Normalize the send
|
|
||||||
# and assign it (it is the last in the list)
|
|
||||||
def normalize_block
|
|
||||||
statements = value.to_vool
|
|
||||||
index = statements.length - 1
|
|
||||||
snd = statements.statements[index]
|
|
||||||
raise "Expecting Send #{snd.class}:#{snd}" unless snd.is_a?( Vool::SendStatement)
|
|
||||||
statements.statements[index] = assignment( snd )
|
|
||||||
statements
|
|
||||||
end
|
|
||||||
|
|
||||||
# sends may have complex args that get hoisted in vool:ing them
|
# sends may have complex args that get hoisted in vool:ing them
|
||||||
# in which case we have to assign the simplified, otherwise the
|
# in which case we have to assign the simplified, otherwise the
|
||||||
# plain send
|
# plain send
|
||||||
|
@ -39,9 +39,7 @@ module Ruby
|
|||||||
# rather than using a stack to do that at runtime
|
# rather than using a stack to do that at runtime
|
||||||
def normalize_arg(arg , statements)
|
def normalize_arg(arg , statements)
|
||||||
vool_arg = arg.to_vool
|
vool_arg = arg.to_vool
|
||||||
if arg.is_a?(Variable) || arg.is_a?(Constant)
|
return vool_arg if vool_arg.is_a?(Vool::Expression)
|
||||||
return vool_arg
|
|
||||||
end
|
|
||||||
if( vool_arg.is_a?(Vool::Statements))
|
if( vool_arg.is_a?(Vool::Statements))
|
||||||
while(vool_arg.length > 1)
|
while(vool_arg.length > 1)
|
||||||
statements << vool_arg.shift
|
statements << vool_arg.shift
|
||||||
|
@ -13,24 +13,17 @@ module Ruby
|
|||||||
raise "no bod" unless @body
|
raise "no bod" unless @body
|
||||||
end
|
end
|
||||||
|
|
||||||
# In Vool we "hoist" the block definition through a local assignment, much
|
# This resolves to a Vool SendStatement, in fact that is mostly what it is.
|
||||||
# as we would other complex args (bit like in the Normalizer)
|
#
|
||||||
# The block is then passed as a normal variable to the send. In other words, the
|
# The implicitly passed block (in ruby) gets converted to the constant it is, and
|
||||||
# BlockStatement resolves to a list of Statements, the last of which is the send
|
# is passed as the last argument.
|
||||||
#
|
#
|
||||||
def to_vool
|
def to_vool
|
||||||
block_name = "implicit_block_#{object_id}".to_sym
|
#block_name = "implicit_block_#{object_id}".to_sym
|
||||||
block = Vool::LambdaExpression.new( @args.dup , @body.to_vool)
|
lambda = Vool::LambdaExpression.new( @args.dup , @body.to_vool)
|
||||||
assign = Vool::LocalAssignment.new( block_name , block)
|
ret = @send.to_vool
|
||||||
sendd = @send.to_vool
|
sendd = ret.is_a?(Vool::Statements) ? ret.last : ret
|
||||||
if(sendd.is_a?(Vool::Statements))
|
sendd.arguments << lambda
|
||||||
ret = sendd
|
|
||||||
sendd = sendd.last
|
|
||||||
else
|
|
||||||
ret = Vool::Statements.new([sendd])
|
|
||||||
end
|
|
||||||
sendd.arguments << LocalVariable.new(block_name).to_vool
|
|
||||||
ret.prepend(assign)
|
|
||||||
ret
|
ret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -76,7 +76,6 @@ module Ruby
|
|||||||
end
|
end
|
||||||
|
|
||||||
def on_block(block_node)
|
def on_block(block_node)
|
||||||
puts block_node.to_s
|
|
||||||
sendd = process(block_node.children[0])
|
sendd = process(block_node.children[0])
|
||||||
args = process(block_node.children[1])
|
args = process(block_node.children[1])
|
||||||
body = process(block_node.children[2])
|
body = process(block_node.children[2])
|
||||||
|
@ -33,24 +33,17 @@ module Ruby
|
|||||||
@lst = compile( input ).to_vool
|
@lst = compile( input ).to_vool
|
||||||
end
|
end
|
||||||
def test_block
|
def test_block
|
||||||
assert_equal Vool::Statements , @lst.class
|
assert_equal Vool::SendStatement , @lst.class
|
||||||
end
|
end
|
||||||
def test_first
|
def test_send_name
|
||||||
assert_equal Vool::LocalAssignment , @lst.first.class
|
assert_equal :plus_one , @lst.name
|
||||||
assert @lst.first.name.to_s.start_with?("implicit_block_")
|
|
||||||
assert_equal Vool::LambdaExpression , @lst.first.value.class
|
|
||||||
end
|
|
||||||
def test_last_send
|
|
||||||
assert_equal 2 , @lst.length
|
|
||||||
assert_equal Vool::SendStatement , @lst.last.class
|
|
||||||
assert_equal :plus_one , @lst.last.name
|
|
||||||
end
|
end
|
||||||
def test_send_block_arg
|
def test_send_block_arg
|
||||||
assert_equal 1 , @lst.last.arguments.length
|
assert_equal 1 , @lst.arguments.length
|
||||||
assert @lst.last.arguments.first.name.to_s.start_with?("implicit_block_")
|
assert_equal Vool::LambdaExpression , @lst.arguments.first.class
|
||||||
end
|
end
|
||||||
def test_block_args
|
def test_block_args
|
||||||
assert_equal [:arg1] , @lst.first.value.args
|
assert_equal [:arg1] , @lst.arguments.first.args
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -33,26 +33,19 @@ module Ruby
|
|||||||
def test_scope
|
def test_scope
|
||||||
assert_equal Vool::ScopeStatement , @lst.class
|
assert_equal Vool::ScopeStatement , @lst.class
|
||||||
end
|
end
|
||||||
def test_first
|
def test_assign
|
||||||
assert_equal Vool::Statements , @lst.first.class
|
assert_equal Vool::LocalAssignment , @lst.first.class
|
||||||
|
assert_equal :a , @lst.first.name
|
||||||
end
|
end
|
||||||
def test_block_assign
|
def test_send
|
||||||
assert_equal Vool::LocalAssignment , @lst.first.first.class
|
assert_equal Vool::SendStatement , @lst.first.value.class
|
||||||
assert @lst.first.first.name.to_s.start_with?("implicit_block")
|
assert_equal :plus_one , @lst.first.value.name
|
||||||
end
|
end
|
||||||
def test_block_assign_right
|
def test_block_arg
|
||||||
assert_equal Vool::LambdaExpression , @lst.first.first.value.class
|
assert_equal Vool::LambdaExpression , @lst.first.value.arguments.first.class
|
||||||
end
|
|
||||||
def test_a_assign
|
|
||||||
assert_equal Vool::LocalAssignment , @lst.first.last.class
|
|
||||||
assert_equal :a , @lst.first.last.name
|
|
||||||
end
|
|
||||||
def test_a_assign_val
|
|
||||||
assert_equal Vool::SendStatement , @lst.first.last.value.class
|
|
||||||
assert_equal :plus_one , @lst.first.last.value.name
|
|
||||||
end
|
end
|
||||||
def test_ret
|
def test_ret
|
||||||
assert_equal Vool::ReturnStatement , @lst.last.class
|
assert_equal Vool::ReturnStatement , @lst[1].class
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -39,13 +39,14 @@ module Ruby
|
|||||||
end
|
end
|
||||||
class TestSendSuperVool < MiniTest::Test
|
class TestSendSuperVool < MiniTest::Test
|
||||||
include RubyTests
|
include RubyTests
|
||||||
def test_super0_receiver
|
|
||||||
lst = compile( "super").to_vool
|
|
||||||
assert_equal Vool::SuperExpression , lst.receiver.class
|
|
||||||
end
|
|
||||||
def test_super0
|
def test_super0
|
||||||
lst = compile( "super").to_vool
|
lst = compile( "super").to_vool
|
||||||
assert_equal Vool::SendStatement , lst.class
|
assert_equal Vool::Statements , lst.class
|
||||||
|
assert_equal Vool::SendStatement , lst.last.class
|
||||||
|
end
|
||||||
|
def test_super0_receiver
|
||||||
|
lst = compile( "super").to_vool
|
||||||
|
assert_equal Vool::SuperExpression , lst.first.value.class
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
class TestSendSuperArgsVool < MiniTest::Test
|
class TestSendSuperArgsVool < MiniTest::Test
|
||||||
@ -54,13 +55,14 @@ module Ruby
|
|||||||
@lst = compile( "super(1)").to_vool
|
@lst = compile( "super(1)").to_vool
|
||||||
end
|
end
|
||||||
def test_super_class
|
def test_super_class
|
||||||
assert_equal Vool::SendStatement , @lst.class
|
assert_equal Vool::Statements , @lst.class
|
||||||
|
assert_equal Vool::SendStatement , @lst.last.class
|
||||||
end
|
end
|
||||||
def test_super_receiver
|
def test_super_receiver
|
||||||
assert_equal Vool::SuperExpression , @lst.receiver.class
|
assert_equal Vool::SuperExpression , @lst.first.value.class
|
||||||
end
|
end
|
||||||
def test_super_name #is nil
|
def test_super_name
|
||||||
assert_nil @lst.name
|
assert @lst.first.name.to_s.start_with?("tmp")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
class TestSendReceiverTypeVool < MiniTest::Test
|
class TestSendReceiverTypeVool < MiniTest::Test
|
||||||
|
Loading…
Reference in New Issue
Block a user