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
|
|
|
|
#
|
|
|
|
# Using the "vool_brother" we can create the right Vool class for it.
|
|
|
|
# Arguments in vool must be simple, so any complex expressions get
|
|
|
|
# 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
|
2018-07-21 13:34:39 +02:00
|
|
|
def to_vool
|
|
|
|
statements = Vool::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-08-16 15:05:45 +02:00
|
|
|
return vool_brother.new(@name, receiver , arguments)
|
2018-07-21 13:34:39 +02:00
|
|
|
else
|
2019-08-16 15:05:45 +02:00
|
|
|
statements << vool_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-08-15 20:30:36 +02:00
|
|
|
vool_arg = arg.to_vool
|
2019-08-19 13:23:55 +02:00
|
|
|
return vool_arg if vool_arg.is_a?(Vool::Expression)
|
2019-08-15 20:30:36 +02:00
|
|
|
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)
|
2018-07-21 13:34:39 +02:00
|
|
|
statements << assign
|
2019-08-16 15:05:45 +02:00
|
|
|
return Vool::LocalVariable.new(assign.name)
|
2018-07-21 13:34:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|