2015-05-04 13:22:22 +02:00
|
|
|
module Compiler
|
2014-08-20 16:14:52 +02:00
|
|
|
# operators are really function calls
|
2014-07-25 10:48:06 +02:00
|
|
|
|
2015-05-04 13:22:22 +02:00
|
|
|
# call_site - attr_reader :name, :args , :receiver
|
|
|
|
|
2015-05-04 22:03:52 +02:00
|
|
|
def self.compile_call_site expession , method , message
|
2015-05-04 13:22:22 +02:00
|
|
|
me = expession.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)
|
2015-05-04 13:22:22 +02:00
|
|
|
method.add_code Virtual::Set.new(Virtual::NewName.new(), Virtual::StringConstant.new(expession.name))
|
2014-08-21 21:57:20 +02:00
|
|
|
compiled_args = []
|
2015-05-04 13:22:22 +02:00
|
|
|
expession.args.each_with_index do |arg , i|
|
2014-09-14 17:14:57 +02:00
|
|
|
#compile in the running method, ie before passing control
|
2015-05-04 13:22:22 +02:00
|
|
|
val = arg.compile( method, message)
|
2014-09-14 17:14:57 +02:00
|
|
|
# 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
|
2015-05-04 13:22:22 +02:00
|
|
|
method.add_code Virtual::MessageSend.new(expession.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
|
2015-05-04 13:22:22 +02:00
|
|
|
end
|