clean up function definition, now function statement
This commit is contained in:
parent
059fb38f08
commit
037fd722df
@ -4,7 +4,7 @@ module Typed
|
|||||||
|
|
||||||
CompilerModules = [ "assignment" , "basic_values" , "call_site", "class_field" ,
|
CompilerModules = [ "assignment" , "basic_values" , "call_site", "class_field" ,
|
||||||
"class_statement" , "collections" , "field_def" , "field_access",
|
"class_statement" , "collections" , "field_def" , "field_access",
|
||||||
"if_statement" , "name_expression"]
|
"function_statement" , "if_statement" , "name_expression"]
|
||||||
|
|
||||||
CompilerModules.each do |mod|
|
CompilerModules.each do |mod|
|
||||||
require_relative "compiler/" + mod
|
require_relative "compiler/" + mod
|
||||||
@ -190,7 +190,6 @@ module Typed
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
require_relative "compiler/function_definition"
|
|
||||||
require_relative "compiler/operator_value"
|
require_relative "compiler/operator_value"
|
||||||
require_relative "compiler/return_statement"
|
require_relative "compiler/return_statement"
|
||||||
require_relative "compiler/statement_list"
|
require_relative "compiler/statement_list"
|
||||||
|
@ -1,36 +1,25 @@
|
|||||||
module Typed
|
module Typed
|
||||||
Compiler.class_eval do
|
module FunctionStatement
|
||||||
|
|
||||||
def on_FunctionStatement statement
|
def on_FunctionStatement statement
|
||||||
#puts statement.inspect
|
# return_type , name , parameters, kids , receiver = *statement
|
||||||
# return_type , name , parameters, kids , receiver = *statement
|
|
||||||
name = statement.name
|
|
||||||
raise "Already in method #{@method}" if @method
|
raise "Already in method #{@method}" if @method
|
||||||
|
|
||||||
args = statement.parameters.collect do |p|
|
args = statement.parameters.collect do |p|
|
||||||
Parfait::Variable.new( *p )
|
Parfait::Variable.new( *p )
|
||||||
end
|
end
|
||||||
|
|
||||||
class_method = nil
|
class_method = handle_receiver( statement ) #nil for instance method
|
||||||
if(statement.receiver )
|
|
||||||
if( statement.receiver.first == :self) #class method
|
|
||||||
class_method = @clazz
|
|
||||||
@clazz = @clazz.meta
|
|
||||||
else
|
|
||||||
raise "Not covered #{statement.receiver}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@method = @clazz.get_instance_method( name )
|
@method = @clazz.get_instance_method( statement.name )
|
||||||
if(@method)
|
if(@method)
|
||||||
#puts "Warning, redefining method #{name}" unless name == :main
|
#puts "Warning, redefining method #{name}" unless name == :main
|
||||||
#TODO check args / type compatibility
|
#TODO check args / type compatibility
|
||||||
init_method
|
init_method
|
||||||
else
|
else
|
||||||
create_method_for(@clazz, name , args ).init_method
|
create_method_for(@clazz, statement.name , args ).init_method
|
||||||
end
|
end
|
||||||
@method.source = statement
|
@method.source = statement
|
||||||
#puts "compile method #{@method.name}"
|
|
||||||
|
|
||||||
process(statement.statements)
|
process(statement.statements)
|
||||||
|
|
||||||
@ -39,5 +28,15 @@ module Typed
|
|||||||
# function definition is a statement, does not return any value
|
# function definition is a statement, does not return any value
|
||||||
return nil
|
return nil
|
||||||
end
|
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
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user