rubyx/lib/soml/compiler/function_definition.rb

44 lines
1.2 KiB
Ruby
Raw Normal View History

2015-10-23 14:22:55 +03:00
module Soml
Compiler.class_eval do
def on_FunctionStatement statement
#puts statement.inspect
# return_type , name , parameters, kids , receiver = *statement
name = statement.name
raise "Already in method #{@method}" if @method
args = statement.parameters.collect do |p|
Parfait::Variable.new( *p )
2014-07-16 13:20:47 +03:00
end
class_method = nil
if(statement.receiver )
if( statement.receiver.first == :self) #class method
class_method = @clazz
2015-10-26 17:23:35 +02:00
@clazz = @clazz.meta
else
raise "Not covered #{statement.receiver}"
end
end
@method = @clazz.get_instance_method( name )
if(@method)
2015-10-18 17:20:25 +03:00
#puts "Warning, redefining method #{name}" unless name == :main
#TODO check args / type compatibility
2015-10-28 21:40:48 +02:00
init_method
else
2015-10-28 21:40:48 +02:00
create_method_for(@clazz, name , args ).init_method
end
2015-11-01 19:13:40 +02:00
@method.source = statement
2015-10-15 10:27:06 +03:00
#puts "compile method #{@method.name}"
2015-06-01 17:31:35 +03:00
process(statement.statements)
@clazz = class_method if class_method
@method = nil
2015-10-14 15:17:33 +03:00
# function definition is a statement, does not return any value
return nil
2014-07-14 11:29:38 +03:00
end
end
end