2015-09-19 15:28:41 +02:00
|
|
|
module Bosl
|
2015-09-19 17:56:18 +02:00
|
|
|
Compiler.class_eval do
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2015-09-20 15:30:39 +02:00
|
|
|
def on_call expression
|
|
|
|
name , arguments , receiver = *expression
|
2015-09-19 15:28:41 +02:00
|
|
|
name = name.to_a.first
|
2015-10-05 23:27:13 +02:00
|
|
|
raise "not inside method " unless @method
|
2015-09-19 16:57:44 +02:00
|
|
|
if receiver
|
2015-09-19 17:56:18 +02:00
|
|
|
me = process( receiver.to_a.first )
|
2015-09-19 16:57:44 +02:00
|
|
|
else
|
2015-09-23 17:35:37 +02:00
|
|
|
me = Virtual::Self.new :int
|
2015-09-19 16:57:44 +02:00
|
|
|
end
|
2015-07-04 13:34:51 +02:00
|
|
|
## need two step process, compile and save to frame
|
|
|
|
# then move from frame to new message
|
2015-10-05 23:27:13 +02:00
|
|
|
@method.source.add_code Virtual::NewMessage.new
|
|
|
|
@method.source.add_code Virtual::Set.new( me , Virtual::NewSelf.new(me.type))
|
|
|
|
@method.source.add_code Virtual::Set.new( name.to_sym , Virtual::NewMessageName.new(:int))
|
2014-08-21 21:57:20 +02:00
|
|
|
compiled_args = []
|
2015-09-19 15:28:41 +02:00
|
|
|
arguments.to_a.each_with_index do |arg , i|
|
2014-09-14 17:14:57 +02:00
|
|
|
#compile in the running method, ie before passing control
|
2015-09-19 17:56:18 +02:00
|
|
|
val = process( arg)
|
2014-09-14 17:14:57 +02:00
|
|
|
# move the compiled value to it's slot in the new message
|
2015-07-01 08:48:20 +02:00
|
|
|
# + 1 as this is a ruby 0-start , but 0 is the last message ivar.
|
2015-07-02 08:48:41 +02:00
|
|
|
# so the next free is +1
|
2015-09-19 15:28:41 +02:00
|
|
|
to = Virtual::NewArgSlot.new(i + 1 ,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)
|
2015-10-05 23:27:13 +02:00
|
|
|
@method.source.add_code Virtual::Set.new( val , to )
|
2014-09-23 18:06:10 +02:00
|
|
|
compiled_args << to
|
2014-08-20 16:14:52 +02:00
|
|
|
end
|
2015-09-27 18:07:12 +02:00
|
|
|
#method.source.add_code Virtual::MessageSend.new(name , me , compiled_args) #and pass control
|
|
|
|
method = nil
|
|
|
|
if(me.value)
|
|
|
|
me = me.value
|
|
|
|
if( me.is_a? Parfait::Class )
|
|
|
|
raise "unimplemented #{code} me is #{me}"
|
|
|
|
elsif( me.is_a? Symbol )
|
|
|
|
# get the function from my class. easy peasy
|
|
|
|
method = Virtual.machine.space.get_class_by_name(:Word).get_instance_method(name)
|
|
|
|
raise "Method not implemented #{me.class}.#{code.name}" unless method
|
|
|
|
@method.source.add_code Virtual::MethodCall.new( method )
|
|
|
|
elsif( me.is_a? Fixnum )
|
|
|
|
name = :plus if name == :+
|
|
|
|
method = Virtual.machine.space.get_class_by_name(:Integer).get_instance_method(name)
|
2015-10-05 23:27:13 +02:00
|
|
|
puts Virtual.machine.space.get_class_by_name(:Integer).method_names.to_a
|
2015-09-27 18:07:12 +02:00
|
|
|
raise "Method not implemented Integer.#{name}" unless method
|
|
|
|
@method.source.add_code Virtual::MethodCall.new( method )
|
|
|
|
else
|
|
|
|
# note: this is the current view: call internal send, even the method name says else
|
|
|
|
# but send is "special" and accesses the internal method name and resolves.
|
|
|
|
kernel = Virtual.machine.space.get_class_by_name(:Kernel)
|
|
|
|
method = kernel.get_instance_method(:__send)
|
|
|
|
@method.source.add_code Virtual::MethodCall.new( method )
|
|
|
|
raise "unimplemented: \n#{code} \nfor #{ref.inspect}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
method = @method
|
|
|
|
@method.source.add_code Virtual::MethodCall.new( @method )
|
|
|
|
end
|
|
|
|
raise "Method not implemented #{me.value}.#{name}" unless method
|
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))
|
2015-09-19 15:28:41 +02:00
|
|
|
Virtual::Return.new( method.source.return_type )
|
2014-07-13 15:00:48 +02:00
|
|
|
end
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
2015-05-04 13:22:22 +02:00
|
|
|
end
|