2015-10-23 14:22:55 +03:00
|
|
|
module Soml
|
2015-09-19 18:56:18 +03:00
|
|
|
Compiler.class_eval do
|
2015-10-06 00:27:13 +03:00
|
|
|
|
2015-10-09 17:51:14 +03:00
|
|
|
def on_function statement
|
|
|
|
#puts statement.inspect
|
|
|
|
return_type , name , parameters, kids , receiver = *statement
|
2015-09-19 16:28:41 +03:00
|
|
|
name = name.to_a.first
|
2015-10-25 15:40:12 +02:00
|
|
|
raise "Already in method #{@method}" if @method
|
|
|
|
|
2015-09-19 16:28:41 +03:00
|
|
|
args = parameters.to_a.collect do |p|
|
2015-09-20 16:30:07 +03:00
|
|
|
raise "error, argument must be a identifier, not #{p}" unless p.type == :parameter
|
2015-09-27 16:06:48 +03:00
|
|
|
Parfait::Variable.new( *p)
|
2014-07-16 13:20:47 +03:00
|
|
|
end
|
2015-09-19 16:28:41 +03:00
|
|
|
|
2015-10-25 15:40:12 +02:00
|
|
|
class_method = nil
|
|
|
|
if(receiver )
|
|
|
|
if( receiver.first == :self) #class method
|
|
|
|
class_method = @clazz
|
2015-10-26 17:23:35 +02:00
|
|
|
@clazz = @clazz.meta
|
2015-05-20 17:11:13 +03:00
|
|
|
else
|
2015-10-25 15:40:12 +02:00
|
|
|
raise "Not covered #{receiver}"
|
2015-05-20 17:11:13 +03:00
|
|
|
end
|
2015-10-06 00:27:13 +03:00
|
|
|
end
|
2015-10-25 15:40:12 +02:00
|
|
|
r = @clazz
|
|
|
|
class_name = @clazz.name
|
|
|
|
|
2015-10-06 00:27:13 +03:00
|
|
|
@method = @clazz.get_instance_method( name )
|
|
|
|
if(@method)
|
2015-10-18 17:20:25 +03:00
|
|
|
#puts "Warning, redefining method #{name}" unless name == :main
|
2015-10-06 00:27:13 +03:00
|
|
|
#TODO check args / type compatibility
|
|
|
|
@method.source.init @method
|
|
|
|
else
|
2015-10-25 15:40:12 +02:00
|
|
|
@method = Register::MethodSource.create_method_for(@clazz, return_type, name , args )
|
|
|
|
@clazz.add_instance_method @method
|
2015-05-20 17:11:13 +03:00
|
|
|
end
|
2015-10-06 00:27:13 +03:00
|
|
|
@method.source.receiver = r
|
2015-10-15 10:27:06 +03:00
|
|
|
#puts "compile method #{@method.name}"
|
2015-06-01 17:31:35 +03:00
|
|
|
|
2015-09-19 16:28:41 +03:00
|
|
|
kids.to_a.each do |ex|
|
2015-10-25 15:40:12 +02:00
|
|
|
process(ex)
|
2014-07-14 14:29:33 +03:00
|
|
|
end
|
2015-10-25 15:40:12 +02:00
|
|
|
@clazz = class_method if class_method
|
2015-10-06 00:27:13 +03:00
|
|
|
@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
|
2015-05-08 15:10:30 +03:00
|
|
|
end
|
2015-05-04 14:22:22 +03:00
|
|
|
end
|