2015-10-23 13:22:55 +02:00
|
|
|
module Soml
|
2015-09-19 17:56:18 +02:00
|
|
|
Compiler.class_eval do
|
2015-10-05 23:27:13 +02:00
|
|
|
|
2015-10-09 16:51:14 +02:00
|
|
|
def on_function statement
|
|
|
|
#puts statement.inspect
|
|
|
|
return_type , name , parameters, kids , receiver = *statement
|
2015-09-19 15:28:41 +02:00
|
|
|
name = name.to_a.first
|
2015-10-25 14:40:12 +01:00
|
|
|
raise "Already in method #{@method}" if @method
|
|
|
|
|
2015-09-19 15:28:41 +02:00
|
|
|
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-19 15:28:41 +02:00
|
|
|
|
2015-10-25 14:40:12 +01:00
|
|
|
class_method = nil
|
|
|
|
if(receiver )
|
|
|
|
if( receiver.first == :self) #class method
|
|
|
|
class_method = @clazz
|
2015-10-26 16:23:35 +01:00
|
|
|
@clazz = @clazz.meta
|
2015-05-20 16:11:13 +02:00
|
|
|
else
|
2015-10-25 14:40:12 +01:00
|
|
|
raise "Not covered #{receiver}"
|
2015-05-20 16:11:13 +02:00
|
|
|
end
|
2015-10-05 23:27:13 +02:00
|
|
|
end
|
2015-10-25 14:40:12 +01:00
|
|
|
|
2015-10-05 23:27:13 +02:00
|
|
|
@method = @clazz.get_instance_method( name )
|
|
|
|
if(@method)
|
2015-10-18 16:20:25 +02:00
|
|
|
#puts "Warning, redefining method #{name}" unless name == :main
|
2015-10-05 23:27:13 +02:00
|
|
|
#TODO check args / type compatibility
|
2015-10-28 20:40:48 +01:00
|
|
|
init_method
|
2015-10-05 23:27:13 +02:00
|
|
|
else
|
2015-10-28 20:40:48 +01:00
|
|
|
create_method_for(@clazz, name , args ).init_method
|
2015-05-20 16:11:13 +02:00
|
|
|
end
|
2015-11-01 18:13:40 +01:00
|
|
|
@method.source = statement
|
2015-10-15 09:27:06 +02:00
|
|
|
#puts "compile method #{@method.name}"
|
2015-06-01 16:31:35 +02:00
|
|
|
|
2015-09-19 15:28:41 +02:00
|
|
|
kids.to_a.each do |ex|
|
2015-10-25 14:40:12 +01:00
|
|
|
process(ex)
|
2014-07-14 13:29:33 +02:00
|
|
|
end
|
2015-10-25 14:40:12 +01:00
|
|
|
@clazz = class_method if class_method
|
2015-10-05 23:27:13 +02:00
|
|
|
@method = nil
|
2015-10-14 14:17:33 +02:00
|
|
|
# function definition is a statement, does not return any value
|
|
|
|
return nil
|
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
|