2014-05-13 20:15:02 +02:00
|
|
|
module Ast
|
|
|
|
# assignment, like operators are really function calls
|
|
|
|
|
|
|
|
class CallSiteExpression < Expression
|
2014-06-04 21:03:45 +02:00
|
|
|
# attr_reader :name, :args , :receiver
|
2014-06-07 23:55:18 +02:00
|
|
|
@@counter = 0
|
2014-06-10 22:57:56 +02:00
|
|
|
def compile context
|
|
|
|
into = context.function
|
|
|
|
params = args.collect{ |a| a.compile(context) }
|
2014-06-13 22:41:45 +02:00
|
|
|
puts "compiling receiver #{receiver} (call #{name})"
|
|
|
|
if receiver.is_a? ModuleName
|
|
|
|
clazz = context.object_space.get_or_create_class receiver.name
|
|
|
|
function = clazz.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
|
2014-06-10 22:57:56 +02:00
|
|
|
value_receiver = receiver.compile(context)
|
2014-06-13 22:41:45 +02:00
|
|
|
elsif receiver.is_a?(NameExpression)
|
|
|
|
if(receiver.name == :self)
|
|
|
|
function = context.current_class.resolve_function(name)
|
|
|
|
value_receiver = Vm::Integer.new(Vm::RegisterMachine.instance.receiver_register)
|
|
|
|
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
|
2014-06-07 22:22:32 +02:00
|
|
|
raise "not implemented instance var:#{receiver}"
|
2014-06-13 22:41:45 +02:00
|
|
|
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}"
|
|
|
|
# this lot below should go, since the compile should handle all
|
2014-06-07 22:22:32 +02:00
|
|
|
end
|
2014-06-13 22:41:45 +02:00
|
|
|
raise "No such method error #{inspect}" if (function.nil?)
|
|
|
|
raise "No receiver error #{inspect}:#{receiver}" if (value_receiver.nil?)
|
2014-06-07 22:22:32 +02:00
|
|
|
call = Vm::CallSite.new( name , value_receiver , params , function)
|
2014-05-22 20:55:17 +02:00
|
|
|
current_function = context.function
|
2014-06-09 18:24:09 +02:00
|
|
|
into.push([]) unless current_function.nil?
|
2014-05-13 20:15:02 +02:00
|
|
|
call.load_args into
|
|
|
|
call.do_call into
|
2014-06-10 22:57:56 +02:00
|
|
|
|
2014-06-12 20:27:47 +02:00
|
|
|
after = into.new_block("#{name}#{@@counter+=1}")
|
2014-06-07 23:55:18 +02:00
|
|
|
into.insert_at after
|
2014-06-10 22:57:56 +02:00
|
|
|
into.pop([]) unless current_function.nil?
|
2014-05-19 20:28:18 +02:00
|
|
|
function.return_type
|
2014-05-13 20:15:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-01 13:24:54 +02:00
|
|
|
class VariableExpression < CallSiteExpression
|
2014-06-04 21:03:45 +02:00
|
|
|
# super( :_get_instance_variable , [StringExpression.new(name)] )
|
2014-06-01 13:24:54 +02:00
|
|
|
end
|
|
|
|
|
2014-05-13 20:15:02 +02:00
|
|
|
end
|