first run at ruby block to_vool
leaving the parser structure in the ruby layer, but adopting the constant approach in vool
This commit is contained in:
parent
0238874c20
commit
235853ab2d
@ -1,5 +1,10 @@
|
|||||||
module Ruby
|
module Ruby
|
||||||
|
# The way the ruby parser presents a call with a block is by wrapping the
|
||||||
|
# whole thing in a :block scope. It includes the send and the block definition.
|
||||||
|
#
|
||||||
|
# A block is in essence quite like a method, so the block definition is like a
|
||||||
|
# method definition, except it is not bound to the class direcly, but the enclosing
|
||||||
|
# method. The enclosing method also provides the scope.
|
||||||
class BlockStatement < Statement
|
class BlockStatement < Statement
|
||||||
attr_reader :send , :args , :body
|
attr_reader :send , :args , :body
|
||||||
|
|
||||||
@ -8,8 +13,25 @@ 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
|
||||||
|
# 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
|
||||||
|
# BlockStatement resolves to a list of Statements, the last of which is the send
|
||||||
|
#
|
||||||
def to_vool
|
def to_vool
|
||||||
BlockStatement.new( @args , @body.normalize)
|
block_name = "implicit_block_#{object_id}".to_sym
|
||||||
|
block = Vool::BlockStatement.new( @args.dup , @body.to_vool)
|
||||||
|
assign = Vool::LocalAssignment.new( block_name , block)
|
||||||
|
sendd = @send.to_vool
|
||||||
|
if(sendd.is_a?(Vool::Statements))
|
||||||
|
ret = sendd
|
||||||
|
sendd = sendd.last
|
||||||
|
else
|
||||||
|
ret = Vool::Statements.new([sendd])
|
||||||
|
end
|
||||||
|
sendd.arguments << LocalVariable.new(block_name).to_vool
|
||||||
|
ret.prepend(assign)
|
||||||
|
ret
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -25,4 +25,32 @@ module Ruby
|
|||||||
assert_equal 1 , @lst.body.arguments.length
|
assert_equal 1 , @lst.body.arguments.length
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
class TestBlockStatementVool < MiniTest::Test
|
||||||
|
include RubyTests
|
||||||
|
|
||||||
|
def setup()
|
||||||
|
input = "plus_one{|arg1| arg1 + 1 } "
|
||||||
|
@lst = compile( input ).to_vool
|
||||||
|
end
|
||||||
|
def test_block
|
||||||
|
assert_equal Vool::Statements , @lst.class
|
||||||
|
end
|
||||||
|
def test_first
|
||||||
|
assert_equal Vool::LocalAssignment , @lst.first.class
|
||||||
|
assert @lst.first.name.to_s.start_with?("implicit_block_")
|
||||||
|
assert_equal Vool::BlockStatement , @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
|
||||||
|
def test_send_block_arg
|
||||||
|
assert_equal 1 , @lst.last.arguments.length
|
||||||
|
assert @lst.last.arguments.first.name.to_s.start_with?("implicit_block_")
|
||||||
|
end
|
||||||
|
def test_block_args
|
||||||
|
assert_equal [:arg1] , @lst.first.value.args
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user