diff --git a/lib/typed/compiler.rb b/lib/typed/compiler.rb index c52792df..e6186fbd 100644 --- a/lib/typed/compiler.rb +++ b/lib/typed/compiler.rb @@ -4,7 +4,7 @@ module Typed CompilerModules = [ "assignment" , "basic_values" , "call_site", "class_field" , "class_statement" , "collections" , "field_def" , "field_access", - "if_statement" , "name_expression"] + "function_statement" , "if_statement" , "name_expression"] CompilerModules.each do |mod| require_relative "compiler/" + mod @@ -190,7 +190,6 @@ module Typed end end -require_relative "compiler/function_definition" require_relative "compiler/operator_value" require_relative "compiler/return_statement" require_relative "compiler/statement_list" diff --git a/lib/typed/compiler/function_definition.rb b/lib/typed/compiler/function_statement.rb similarity index 51% rename from lib/typed/compiler/function_definition.rb rename to lib/typed/compiler/function_statement.rb index e3526d08..39876e88 100644 --- a/lib/typed/compiler/function_definition.rb +++ b/lib/typed/compiler/function_statement.rb @@ -1,36 +1,25 @@ module Typed - Compiler.class_eval do + module FunctionStatement def on_FunctionStatement statement - #puts statement.inspect -# return_type , name , parameters, kids , receiver = *statement - name = statement.name + # return_type , name , parameters, kids , receiver = *statement raise "Already in method #{@method}" if @method args = statement.parameters.collect do |p| Parfait::Variable.new( *p ) end - class_method = nil - if(statement.receiver ) - if( statement.receiver.first == :self) #class method - class_method = @clazz - @clazz = @clazz.meta - else - raise "Not covered #{statement.receiver}" - end - end + class_method = handle_receiver( statement ) #nil for instance method - @method = @clazz.get_instance_method( name ) + @method = @clazz.get_instance_method( statement.name ) if(@method) #puts "Warning, redefining method #{name}" unless name == :main #TODO check args / type compatibility init_method else - create_method_for(@clazz, name , args ).init_method + create_method_for(@clazz, statement.name , args ).init_method end @method.source = statement - #puts "compile method #{@method.name}" process(statement.statements) @@ -39,5 +28,15 @@ module Typed # function definition is a statement, does not return any value return nil end + + private + + def handle_receiver( statement ) + return nil unless statement.receiver + raise "Not covered #{statement.receiver}" unless ( statement.receiver.first == :self) + class_method = @clazz + @clazz = @clazz.meta + class_method + end end end