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:
@ -11,26 +11,13 @@ module Ruby
|
||||
case value
|
||||
when Variable , Constant
|
||||
return self.vool_brother.new(name,@value.to_vool)
|
||||
when SendStatement , YieldStatement
|
||||
when SendStatement , YieldStatement , RubyBlockStatement
|
||||
return normalize_send
|
||||
when RubyBlockStatement
|
||||
return normalize_block
|
||||
else
|
||||
raise "unsupported right #{value}"
|
||||
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
|
||||
# in which case we have to assign the simplified, otherwise the
|
||||
# plain send
|
||||
|
@ -39,9 +39,7 @@ module Ruby
|
||||
# rather than using a stack to do that at runtime
|
||||
def normalize_arg(arg , statements)
|
||||
vool_arg = arg.to_vool
|
||||
if arg.is_a?(Variable) || arg.is_a?(Constant)
|
||||
return vool_arg
|
||||
end
|
||||
return vool_arg if vool_arg.is_a?(Vool::Expression)
|
||||
if( vool_arg.is_a?(Vool::Statements))
|
||||
while(vool_arg.length > 1)
|
||||
statements << vool_arg.shift
|
||||
|
@ -13,24 +13,17 @@ module Ruby
|
||||
raise "no bod" unless @body
|
||||
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
|
||||
# This resolves to a Vool SendStatement, in fact that is mostly what it is.
|
||||
#
|
||||
# The implicitly passed block (in ruby) gets converted to the constant it is, and
|
||||
# is passed as the last argument.
|
||||
#
|
||||
def to_vool
|
||||
block_name = "implicit_block_#{object_id}".to_sym
|
||||
block = Vool::LambdaExpression.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)
|
||||
#block_name = "implicit_block_#{object_id}".to_sym
|
||||
lambda = Vool::LambdaExpression.new( @args.dup , @body.to_vool)
|
||||
ret = @send.to_vool
|
||||
sendd = ret.is_a?(Vool::Statements) ? ret.last : ret
|
||||
sendd.arguments << lambda
|
||||
ret
|
||||
end
|
||||
|
||||
|
@ -76,7 +76,6 @@ module Ruby
|
||||
end
|
||||
|
||||
def on_block(block_node)
|
||||
puts block_node.to_s
|
||||
sendd = process(block_node.children[0])
|
||||
args = process(block_node.children[1])
|
||||
body = process(block_node.children[2])
|
||||
|
Reference in New Issue
Block a user