2015-09-19 15:28:41 +02:00
|
|
|
module Bosl
|
2015-05-08 14:10:30 +02:00
|
|
|
module Compiler
|
2015-05-04 13:22:22 +02:00
|
|
|
# function attr_reader :name, :params, :body , :receiver
|
2015-05-06 14:14:47 +02:00
|
|
|
def self.compile_function expression, method
|
2015-09-19 15:28:41 +02:00
|
|
|
return_type , name , parameters, kids = *expression
|
|
|
|
name = name.to_a.first
|
|
|
|
args = parameters.to_a.collect do |p|
|
|
|
|
raise "error, argument must be a identifier, not #{p}" unless p.type == :field
|
|
|
|
p[2]
|
2014-07-16 12:20:47 +02:00
|
|
|
end
|
2015-09-19 15:28:41 +02:00
|
|
|
|
|
|
|
if expression[:receiver]
|
2015-07-19 12:31:13 +02:00
|
|
|
# compiler will always return slot. with known value or not
|
2015-05-20 16:11:13 +02:00
|
|
|
r = Compiler.compile(expression.receiver, method )
|
2015-07-19 12:31:13 +02:00
|
|
|
if( r.value.is_a? Parfait::Class )
|
|
|
|
class_name = r.value.name
|
2015-05-20 16:11:13 +02:00
|
|
|
else
|
2015-08-04 20:46:33 +02:00
|
|
|
raise "unimplemented case in function #{r}"
|
2015-05-20 16:11:13 +02:00
|
|
|
end
|
|
|
|
else
|
2015-09-19 15:28:41 +02:00
|
|
|
r = Virtual::Self.new()
|
2015-05-20 16:11:13 +02:00
|
|
|
class_name = method.for_class.name
|
|
|
|
end
|
2015-09-19 15:28:41 +02:00
|
|
|
new_method = Virtual::MethodSource.create_method(class_name, 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
|
|
|
|
2014-07-24 20:56:31 +02:00
|
|
|
#frame = frame.new_frame
|
2015-09-19 15:28:41 +02:00
|
|
|
kids.to_a.each do |ex|
|
2015-05-06 14:14:47 +02:00
|
|
|
return_type = Compiler.compile(ex,new_method )
|
2015-09-19 15:28:41 +02:00
|
|
|
raise return_type.inspect if return_type.is_a? Virtual::Instruction
|
2014-07-14 13:29:33 +02:00
|
|
|
end
|
2015-07-03 19:13:03 +02:00
|
|
|
new_method.source.return_type = return_type
|
2015-09-19 15:28:41 +02:00
|
|
|
Virtual::Return.new(return_type)
|
2014-07-14 10:29:38 +02:00
|
|
|
end
|
2015-05-08 14:10:30 +02:00
|
|
|
end
|
2015-05-04 13:22:22 +02:00
|
|
|
end
|