2014-05-13 20:15:02 +02:00
|
|
|
module Ast
|
|
|
|
# assignment, like operators are really function calls
|
|
|
|
|
|
|
|
class CallSiteExpression < Expression
|
2014-05-31 16:58:26 +02:00
|
|
|
attr_reader :name, :args , :receiver
|
|
|
|
|
|
|
|
def initialize name, args , receiver = Ast::NameExpression.new("self")
|
|
|
|
@name , @args , @receiver = name.to_sym , args , receiver
|
2014-05-13 20:15:02 +02:00
|
|
|
end
|
2014-05-31 16:58:26 +02:00
|
|
|
|
2014-05-13 20:15:02 +02:00
|
|
|
def compile context , into
|
|
|
|
params = args.collect{ |a| a.compile(context, into) }
|
2014-05-31 15:19:44 +02:00
|
|
|
#TOOD, this needs dynamic resolution
|
2014-05-31 13:35:33 +02:00
|
|
|
function = context.current_class.get_or_create_function(name)
|
2014-05-13 20:15:02 +02:00
|
|
|
raise "Forward declaration not implemented (#{name}) #{inspect}" if function == nil
|
|
|
|
call = Vm::CallSite.new( name , params , function)
|
2014-05-22 20:55:17 +02:00
|
|
|
current_function = context.function
|
|
|
|
current_function.save_locals(context , into) if current_function
|
2014-05-13 20:15:02 +02:00
|
|
|
call.load_args into
|
|
|
|
call.do_call into
|
2014-05-22 20:55:17 +02:00
|
|
|
current_function.restore_locals(context , into) if current_function
|
2014-05-25 10:35:45 +02:00
|
|
|
puts "compile call #{function.return_type}"
|
2014-05-19 20:28:18 +02:00
|
|
|
function.return_type
|
2014-05-13 20:15:02 +02:00
|
|
|
end
|
2014-05-19 10:29:18 +02:00
|
|
|
|
2014-05-13 20:15:02 +02:00
|
|
|
def inspect
|
|
|
|
self.class.name + ".new(" + name.inspect + ", ["+
|
2014-05-31 16:58:26 +02:00
|
|
|
args.collect{|m| m.inspect }.join( ",") + "] ," + receiver.inspect + ")"
|
2014-05-13 20:15:02 +02:00
|
|
|
end
|
|
|
|
def to_s
|
|
|
|
"#{name}(" + args.join(",") + ")"
|
|
|
|
end
|
|
|
|
def attributes
|
2014-05-31 16:58:26 +02:00
|
|
|
[:name , :args , :receiver]
|
2014-05-13 20:15:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-01 13:24:54 +02:00
|
|
|
class VariableExpression < CallSiteExpression
|
|
|
|
|
|
|
|
def initialize name
|
|
|
|
super( :_get_instance_variable , [StringExpression.new(name)] )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-13 20:15:02 +02:00
|
|
|
end
|