2014-06-25 01:47:59 +02:00
|
|
|
module Ast
|
|
|
|
# assignment, like operators are really function calls
|
|
|
|
|
|
|
|
class CallSiteExpression < Expression
|
|
|
|
# attr_reader :name, :args , :receiver
|
|
|
|
@@counter = 0
|
2014-07-12 20:59:17 +02:00
|
|
|
def compile frame , method
|
2014-07-13 15:00:48 +02:00
|
|
|
with = args.collect{|a| a.compile(frame , method)}
|
|
|
|
frame.compile_send( method , @name , with )
|
|
|
|
end
|
|
|
|
|
|
|
|
def scratch
|
2014-06-25 01:47:59 +02:00
|
|
|
into = context.function
|
|
|
|
params = args.collect{ |a| a.compile(context) }
|
|
|
|
puts "compiling receiver #{receiver} (call #{name})"
|
|
|
|
if receiver.is_a? ModuleName
|
|
|
|
clazz = context.object_space.get_or_create_class receiver.name
|
|
|
|
value_receiver = clazz.meta_class
|
|
|
|
function = value_receiver.resolve_function name
|
|
|
|
elsif receiver.is_a?(StringExpression) or receiver.is_a?(IntegerExpression)
|
|
|
|
#TODO obviously the class is wrong, but you gotta start somewhere
|
|
|
|
clazz = context.object_space.get_or_create_class :Object
|
|
|
|
function = clazz.resolve_function name
|
|
|
|
value_receiver = receiver.compile(context)
|
|
|
|
elsif receiver.is_a?(NameExpression)
|
|
|
|
if(receiver.name == :self)
|
|
|
|
function = context.current_class.resolve_function(name)
|
2014-06-26 16:52:15 +02:00
|
|
|
value_receiver = Virtual::Integer.new(Virtual::RegisterMachine.instance.receiver_register)
|
2014-06-25 01:47:59 +02:00
|
|
|
else
|
|
|
|
value_receiver = receiver.compile(context)
|
|
|
|
# TODO HACK warning: should determine class dynamically
|
|
|
|
function = context.current_class.resolve_function(name)
|
|
|
|
end
|
|
|
|
elsif receiver.is_a? VariableExpression
|
|
|
|
value_receiver = receiver.compile(context)
|
|
|
|
function = context.current_class.resolve_function(name)
|
|
|
|
else
|
|
|
|
#This , how does one say nowadays, smells. Smells of unused polymorphism actually
|
|
|
|
raise "Not sure this is possible, but never good to leave elses open #{receiver} #{receiver.class}"
|
|
|
|
end
|
|
|
|
raise "No such method error #{inspect}" if (function.nil?)
|
|
|
|
raise "No receiver error #{inspect}:#{receiver}" if (value_receiver.nil?)
|
2014-06-26 16:52:15 +02:00
|
|
|
call = Virtual::CallSite.new( name , value_receiver , params , function)
|
2014-06-25 01:47:59 +02:00
|
|
|
current_function = context.function
|
|
|
|
into.push([]) unless current_function.nil?
|
|
|
|
call.load_args into
|
|
|
|
call.do_call into
|
|
|
|
|
|
|
|
after = into.new_block("#{name}#{@@counter+=1}")
|
|
|
|
into.insert_at after
|
|
|
|
into.pop([]) unless current_function.nil?
|
|
|
|
function.return_type
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|