rubyx/lib/bosl/compiler/function_expression.rb

48 lines
1.5 KiB
Ruby
Raw Normal View History

module Bosl
Compiler.class_eval do
# function attr_reader :name, :params, :body , :receiver
def on_function expression
2015-09-27 15:06:48 +02:00
#puts expression.inspect
2015-09-20 16:33:05 +02:00
return_type , name , parameters, kids , receiver = *expression
name = name.to_a.first
args = parameters.to_a.collect do |p|
2015-09-20 15:30:07 +02:00
raise "error, argument must be a identifier, not #{p}" unless p.type == :parameter
2015-09-27 15:06:48 +02:00
Parfait::Variable.new( *p)
2014-07-16 12:20:47 +02:00
end
2015-09-20 16:33:05 +02:00
if receiver
2015-07-19 12:31:13 +02:00
# compiler will always return slot. with known value or not
2015-09-20 16:33:05 +02:00
r = receiver.first
if( r.is_a? Parfait::Class )
2015-07-19 12:31:13 +02:00
class_name = r.value.name
else
2015-09-20 16:33:05 +02:00
if( r != :self)
raise "unimplemented case in function #{r}"
else
r = Virtual::Self.new()
class_name = method.for_class.name
end
end
else
2015-09-23 17:35:37 +02:00
r = Virtual::Self.new(:int)
class_name = method.for_class.name
end
2015-09-23 17:35:37 +02:00
new_method = Virtual::MethodSource.create_method(class_name, return_type, name , args )
2015-07-03 19:13:03 +02:00
new_method.source.receiver = r
2015-05-24 12:53:49 +02:00
new_method.for_class.add_instance_method new_method
2015-06-01 16:31:35 +02:00
old_method = @method
@method = new_method
2014-07-24 20:56:31 +02:00
#frame = frame.new_frame
kids.to_a.each do |ex|
return_type = process(ex)
raise return_type.inspect if return_type.is_a? Virtual::Instruction
end
@method = old_method
2015-07-03 19:13:03 +02:00
new_method.source.return_type = return_type
Virtual::Return.new(return_type)
2014-07-14 10:29:38 +02:00
end
end
end