2015-10-07 14:22:47 +02:00
|
|
|
module Phisol
|
2015-09-19 17:56:18 +02:00
|
|
|
Compiler.class_eval do
|
2015-05-04 13:22:22 +02:00
|
|
|
|
2015-10-09 16:51:14 +02:00
|
|
|
def on_call statement
|
|
|
|
name , arguments , receiver = *statement
|
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-10-14 12:48:21 +02:00
|
|
|
reset_regs
|
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-10-14 12:48:21 +02:00
|
|
|
if @method.for_class.name == :Integer
|
|
|
|
type = :int
|
2015-10-07 09:05:34 +02:00
|
|
|
else
|
2015-10-14 12:48:21 +02:00
|
|
|
type = :ref
|
2015-10-07 09:05:34 +02:00
|
|
|
end
|
2015-10-14 12:48:21 +02:00
|
|
|
me = Register.self_reg type
|
2015-09-19 16:57:44 +02:00
|
|
|
end
|
2015-10-14 12:48:21 +02:00
|
|
|
#move the new message (that we need to populate to make a call) to std register
|
|
|
|
new_message = Register.resolve_to_register(:new_message)
|
|
|
|
@method.source.add_code Register.get_slot(@method, :message , :next_message , new_message )
|
|
|
|
# move our receiver there
|
|
|
|
@method.source.add_code Register.set_slot( statement , me , :new_message , :receiver)
|
|
|
|
# load method name and set to new message (for exceptions/debug)
|
|
|
|
name_tmp = use_reg(:ref)
|
|
|
|
@method.source.add_code Register::LoadConstant.new(statement, name , name_tmp)
|
|
|
|
@method.source.add_code Register.set_slot( statement , name_tmp , :new_message , :name)
|
|
|
|
# next arguments. reset tmp regs for each and load result into new_message
|
2015-09-19 15:28:41 +02:00
|
|
|
arguments.to_a.each_with_index do |arg , i|
|
2015-10-14 12:48:21 +02:00
|
|
|
reset_regs
|
|
|
|
# processing should return the register with the value
|
2015-09-19 17:56:18 +02:00
|
|
|
val = process( arg)
|
2015-10-14 12:48:21 +02:00
|
|
|
raise "Not register #{val}" unless val.is_a?(Register::RegisterValue)
|
|
|
|
# which we load int the new_message at the argument's index
|
|
|
|
@method.source.add_code Register.set_slot( statement , val , :new_message , i + 1)
|
2014-08-20 16:14:52 +02:00
|
|
|
end
|
2015-10-14 12:48:21 +02:00
|
|
|
|
|
|
|
# now we have to resolve the method name (+ receiver) into a callable method
|
2015-09-27 18:07:12 +02:00
|
|
|
method = nil
|
|
|
|
if(me.value)
|
2015-10-14 12:48:21 +02:00
|
|
|
raise "branch should only be optiisation, revit"
|
2015-09-27 18:07:12 +02:00
|
|
|
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 )
|
|
|
|
method = Virtual.machine.space.get_class_by_name(:Integer).get_instance_method(name)
|
2015-10-07 09:05:34 +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
|
|
|
|
raise "unimplemented: \n#{code} \nfor #{ref.inspect}"
|
|
|
|
end
|
|
|
|
else
|
2015-10-14 13:02:34 +02:00
|
|
|
if( me.type.is_a? Phisol::Integer)
|
2015-10-06 14:26:57 +02:00
|
|
|
name = :plus if name == :+
|
|
|
|
method = Virtual.machine.space.get_class_by_name(:Integer).get_instance_method(name)
|
|
|
|
puts Virtual.machine.space.get_class_by_name(:Integer).method_names.to_a
|
|
|
|
raise "Method not implemented Integer.#{name}" unless method
|
|
|
|
@method.source.add_code Virtual::MethodCall.new( method )
|
|
|
|
else
|
2015-10-07 09:05:34 +02:00
|
|
|
method = @clazz.get_instance_method(name)
|
2015-10-07 09:22:45 +02:00
|
|
|
raise "Method not implemented #{@clazz.name}.#{name}" unless method
|
2015-10-07 09:05:34 +02:00
|
|
|
@method.source.add_code Virtual::MethodCall.new( method )
|
2015-10-06 14:26:57 +02:00
|
|
|
end
|
2015-09-27 18:07:12 +02:00
|
|
|
end
|
|
|
|
raise "Method not implemented #{me.value}.#{name}" unless method
|
2015-10-14 12:48:21 +02:00
|
|
|
ret = use_reg( method.source.return_type )
|
2014-09-14 17:14:57 +02:00
|
|
|
# the effect of the method is that the NewMessage Return slot will be filled, return it
|
2015-10-14 12:48:21 +02:00
|
|
|
# but move it into a register too
|
|
|
|
@method.source.add_code Register.get_slot(@method, :message , :return_value , ret )
|
|
|
|
ret
|
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
|