ff22c17784
new compile module does just that: compile Dispatch with visitor pattern no more patching into ast
27 lines
1.3 KiB
Ruby
27 lines
1.3 KiB
Ruby
module Compiler
|
|
# operators are really function calls
|
|
|
|
# call_site - attr_reader :name, :args , :receiver
|
|
|
|
def compile_call_site expession , method , message
|
|
me = expession.receiver.compile( method, message )
|
|
method.add_code Virtual::NewMessage.new
|
|
method.add_code Virtual::Set.new(Virtual::NewSelf.new(me.type), me)
|
|
method.add_code Virtual::Set.new(Virtual::NewName.new(), Virtual::StringConstant.new(expession.name))
|
|
compiled_args = []
|
|
expession.args.each_with_index do |arg , i|
|
|
#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
|
|
to = Virtual::NewMessageSlot.new(i ,val.type , val)
|
|
# (doing this immediately, not after the loop, so if it's a return it won't get overwritten)
|
|
method.add_code Virtual::Set.new(to , val )
|
|
compiled_args << to
|
|
end
|
|
method.add_code Virtual::MessageSend.new(expession.name , me , compiled_args) #and pass control
|
|
# 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 )
|
|
end
|
|
end
|