rubyx/lib/virtual/compiler/function_expression.rb

36 lines
1.2 KiB
Ruby
Raw Normal View History

module Virtual
module Compiler
# function attr_reader :name, :params, :body , :receiver
2015-05-06 14:14:47 +02:00
def self.compile_function expression, method
2015-05-05 14:11:09 +02:00
args = expression.params.collect do |p|
raise "error, argument must be a identifier, not #{p}" unless p.is_a? Ast::NameExpression
p.name
2014-07-16 12:20:47 +02:00
end
if expression.receiver
2015-07-19 12:31:13 +02:00
# compiler will always return slot. with known value or not
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
else
raise "unimplemented #{r}"
end
else
r = Self.new()
class_name = method.for_class.name
end
2015-07-03 19:13:03 +02:00
new_method = MethodSource.create_method(class_name, expression.name , args )
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
return_type = nil
2015-05-05 14:11:09 +02:00
expression.body.each do |ex|
2015-05-06 14:14:47 +02:00
return_type = Compiler.compile(ex,new_method )
raise return_type.inspect if return_type.is_a? Instruction
end
2015-07-03 19:13:03 +02:00
new_method.source.return_type = return_type
Return.new(return_type)
2014-07-14 10:29:38 +02:00
end
end
end