2014-06-25 01:47:59 +02:00
|
|
|
module Ast
|
2014-08-20 16:14:52 +02:00
|
|
|
# operators are really function calls
|
2014-06-25 01:47:59 +02:00
|
|
|
|
|
|
|
class CallSiteExpression < Expression
|
|
|
|
# attr_reader :name, :args , :receiver
|
2014-07-25 10:48:06 +02:00
|
|
|
|
2014-07-25 09:49:34 +02:00
|
|
|
def compile method , message
|
|
|
|
me = receiver.compile( method, message )
|
2014-09-23 18:06:10 +02:00
|
|
|
method.add_code Virtual::NewMessage.new
|
2014-08-21 16:46:12 +02:00
|
|
|
method.add_code Virtual::Set.new(Virtual::NewSelf.new(me.type), me)
|
2014-09-14 20:26:30 +02:00
|
|
|
method.add_code Virtual::Set.new(Virtual::NewName.new(), Virtual::StringConstant.new(name))
|
2014-08-21 21:57:20 +02:00
|
|
|
compiled_args = []
|
2014-08-20 16:14:52 +02:00
|
|
|
args.each_with_index do |arg , i|
|
2014-09-14 17:14:57 +02:00
|
|
|
#compile in the running method, ie before passing control
|
|
|
|
val = arg.compile( method, message)
|
|
|
|
# move the compiled value to it's slot in the new message
|
2014-09-23 18:06:10 +02:00
|
|
|
to = Virtual::NewMessageSlot.new(i ,val.type , val)
|
2014-09-14 17:14:57 +02:00
|
|
|
# (doing this immediately, not after the loop, so if it's a return it won't get overwritten)
|
2014-09-23 18:06:10 +02:00
|
|
|
method.add_code Virtual::Set.new(to , val )
|
|
|
|
compiled_args << to
|
2014-08-20 16:14:52 +02:00
|
|
|
end
|
2014-08-21 21:57:20 +02:00
|
|
|
method.add_code Virtual::MessageSend.new(name , me , compiled_args) #and pass control
|
2014-09-14 17:14:57 +02:00
|
|
|
# the effect of the method is that the NewMessage Return slot will be filled, return it
|
|
|
|
# (this is what is moved _inside_ above loop for such expressions that are calls (or constants))
|
|
|
|
Virtual::NewReturn.new( method.return_type )
|
2014-07-13 15:00:48 +02:00
|
|
|
end
|
2014-09-14 17:14:57 +02:00
|
|
|
end
|
2014-06-25 01:47:59 +02:00
|
|
|
end
|