extracting methods for the compiler to handle
This commit is contained in:
parent
a70d20d63e
commit
8507bef08b
@ -1,4 +1,6 @@
|
|||||||
require_relative "type_collector"
|
require_relative "compiler/type_collector"
|
||||||
|
require_relative "compiler/method_collector"
|
||||||
|
require_relative "compiler/ruby_method"
|
||||||
|
|
||||||
|
|
||||||
module Melon
|
module Melon
|
||||||
@ -19,7 +21,17 @@ module Melon
|
|||||||
name , sup , body = *statement
|
name , sup , body = *statement
|
||||||
clazz = Parfait::Space.object_space.create_class(get_name(name) , get_name(sup) )
|
clazz = Parfait::Space.object_space.create_class(get_name(name) , get_name(sup) )
|
||||||
ivar_hash = TypeCollector.new.collect(body)
|
ivar_hash = TypeCollector.new.collect(body)
|
||||||
clazz.set_instance_type( Parfait::Type.for_hash( clazz , ivar_hash ) )
|
clazz.set_instance_type( Parfait::Type.for_hash( clazz , ivar_hash ) )
|
||||||
|
|
||||||
|
MethodCollector.new.collect(body)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handler_missing(node)
|
||||||
|
node.children.each do |kid |
|
||||||
|
process(kid) if kid.is_a?(AST::Node)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
35
lib/melon/compiler/method_collector.rb
Normal file
35
lib/melon/compiler/method_collector.rb
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
module Melon
|
||||||
|
|
||||||
|
class MethodCollector < AST::Processor
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@methods = []
|
||||||
|
end
|
||||||
|
|
||||||
|
def collect(statement)
|
||||||
|
process statement
|
||||||
|
@methods
|
||||||
|
end
|
||||||
|
|
||||||
|
def on_def(statement)
|
||||||
|
name , args , body = *statement
|
||||||
|
args_type = make_type(args)
|
||||||
|
@methods << RubyMethod.new(name , args_type , body )
|
||||||
|
end
|
||||||
|
|
||||||
|
def make_type( statement )
|
||||||
|
type = Parfait::Space.object_space.get_class_by_name(:Message ).instance_type
|
||||||
|
statement.children.each do |arg|
|
||||||
|
type = type.add_instance_variable( arg.children[0] , :Object )
|
||||||
|
end
|
||||||
|
type
|
||||||
|
end
|
||||||
|
|
||||||
|
def handler_missing(node)
|
||||||
|
node.children.each do |kid |
|
||||||
|
process(kid) if kid.is_a?(AST::Node)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
32
test/melon/compiler/test_method_collector.rb
Normal file
32
test/melon/compiler/test_method_collector.rb
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
require_relative "helper"
|
||||||
|
|
||||||
|
module Melon
|
||||||
|
class TestMethodCollector < MiniTest::Test
|
||||||
|
|
||||||
|
def setup
|
||||||
|
Register.machine.boot unless Register.machine.booted
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_collect( input )
|
||||||
|
ast = Parser::Ruby22.parse input
|
||||||
|
MethodCollector.new.collect(ast)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_no_args
|
||||||
|
methods = parse_collect "def meth; @ivar;end"
|
||||||
|
assert methods.find{|m| m.name == :meth }
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_one_arg
|
||||||
|
method = parse_collect("def meth2(arg1); 1;end").first
|
||||||
|
assert method.name == :meth2
|
||||||
|
assert method.args_type.variable_index(:arg1) , method.args_type.inspect
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_ivar_operator_assign
|
||||||
|
method = parse_collect("def meth3(yksi,kaksi,kolme); 1;end").first
|
||||||
|
assert method.args_type.variable_index(:kolme) , method.args_type.inspect
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user