2018-07-21 13:34:39 +02:00
|
|
|
module Ruby
|
|
|
|
|
2018-09-01 14:54:25 +02:00
|
|
|
# A CallStatement is the abstraction of Send and Yield. The two are really
|
|
|
|
# much more similar than different.
|
|
|
|
#
|
|
|
|
# A CallStatement has a name, receiver and arguments
|
|
|
|
#
|
2019-10-03 23:36:49 +02:00
|
|
|
# Using the "sol_brother" we can create the right Sol class for it.
|
|
|
|
# Arguments in sol must be simple, so any complex expressions get
|
2018-09-01 14:54:25 +02:00
|
|
|
# hoisted and assigned to temporary variables.
|
|
|
|
#
|
2018-07-30 13:44:14 +02:00
|
|
|
class CallStatement < Statement
|
2018-07-21 13:34:39 +02:00
|
|
|
attr_reader :name , :receiver , :arguments
|
|
|
|
|
|
|
|
def initialize(name , receiver , arguments )
|
|
|
|
@name , @receiver , @arguments = name , receiver , arguments
|
|
|
|
@arguments ||= []
|
|
|
|
end
|
|
|
|
|
2019-08-15 20:30:36 +02:00
|
|
|
# we "normalize" or flatten any complex argument expressions into a list
|
2019-10-03 23:36:49 +02:00
|
|
|
def to_sol
|
|
|
|
statements = Sol::Statements.new([])
|
2019-08-16 15:05:45 +02:00
|
|
|
receiver = normalize_arg(@receiver , statements)
|
2018-07-21 13:34:39 +02:00
|
|
|
arguments = []
|
|
|
|
@arguments.each_with_index do |arg , index |
|
2019-08-16 15:05:45 +02:00
|
|
|
arguments << normalize_arg(arg , statements)
|
2018-07-21 13:34:39 +02:00
|
|
|
end
|
|
|
|
if statements.empty?
|
2019-10-03 23:36:49 +02:00
|
|
|
return sol_brother.new(@name, receiver , arguments)
|
2018-07-21 13:34:39 +02:00
|
|
|
else
|
2019-10-03 23:36:49 +02:00
|
|
|
statements << sol_brother.new(@name, receiver , arguments)
|
2018-07-21 13:34:39 +02:00
|
|
|
return statements
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-15 20:30:36 +02:00
|
|
|
# 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
|
2019-08-16 15:05:45 +02:00
|
|
|
def normalize_arg(arg , statements)
|
2019-10-03 23:36:49 +02:00
|
|
|
sol_arg = arg.to_sol
|
|
|
|
return sol_arg if sol_arg.is_a?(Sol::Expression)
|
|
|
|
if( sol_arg.is_a?(Sol::Statements))
|
|
|
|
while(sol_arg.length > 1)
|
|
|
|
statements << sol_arg.shift
|
2019-08-15 20:30:36 +02:00
|
|
|
end
|
2019-10-03 23:36:49 +02:00
|
|
|
sol_arg = sol_arg.shift
|
2019-08-15 20:30:36 +02:00
|
|
|
end
|
2019-10-03 23:36:49 +02:00
|
|
|
assign = Sol::LocalAssignment.new( "tmp_#{arg.object_id}".to_sym, sol_arg)
|
2018-07-21 13:34:39 +02:00
|
|
|
statements << assign
|
2019-10-03 23:36:49 +02:00
|
|
|
return Sol::LocalVariable.new(assign.name)
|
2018-07-21 13:34:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|