Fixing ruby send with arguments

When send has complex args, mostly more sends, we hoist those out and pass created temporary variables
This commit is contained in:
2019-08-15 21:30:36 +03:00
parent 31ae0a9670
commit 84b9811e55
8 changed files with 124 additions and 12 deletions

View File

@ -17,6 +17,7 @@ module Ruby
@arguments ||= []
end
# we "normalize" or flatten any complex argument expressions into a list
def to_vool
statements = Vool::Statements.new([])
arguments = []
@ -31,12 +32,23 @@ module Ruby
end
end
# this is called for each arg and if the arg is not constant or Variable
# we create a tmp variable and assign to that, hoising all the calls.
# the effect is of walking the call tree now,
# rather than using a stack to do that at runtime
def normalize_arg(arg , arguments , statements)
if arg.is_a?(Constant) and !arg.is_a?(CallStatement)
arguments << arg.to_vool
vool_arg = arg.to_vool
if arg.is_a?(Constant)
arguments << vool_arg
return
end
assign = Vool::LocalAssignment.new( "tmp_#{arg.object_id}".to_sym, arg.to_vool)
if( vool_arg.is_a?(Vool::Statements))
while(vool_arg.length > 1)
statements << vool_arg.shift
end
vool_arg = vool_arg.shift
end
assign = Vool::LocalAssignment.new( "tmp_#{arg.object_id}".to_sym, vool_arg)
statements << assign
arguments << Vool::LocalVariable.new(assign.name)
end

View File

@ -20,13 +20,16 @@ module Ruby
def length
@statements.length
end
def shift
@statements.shift
end
def [](i)
@statements[i]
end
def <<(o)
@statements << o
self
end
end
def to_vool
if( single? )
first.to_vool

View File

@ -5,6 +5,8 @@ module Vool
def initialize(name , value )
raise "Name nil #{self}" unless name
raise "Value nil #{self}" unless value
raise "Value cant be Assignment #{value}" if value.is_a?(Assignment)
raise "Value cant be Statements #{value}" if value.is_a?(Statements)
@name , @value = name , value
end
@ -19,6 +21,7 @@ module Vool
def chain_assign(assign , compiler)
return assign unless @value.is_a?(CallStatement)
raise "Move me to ruby layer"
@value.to_mom(compiler) << assign
end
end

View File

@ -30,6 +30,9 @@ module Vool
def prepend(o)
@statements = [o] + @statements
end
def shift
@statements.shift
end
# to_mom all the statements. Append subsequent ones to the first, and return the
# first.