From b242f9e223372fa9b5f31f02d3ec158f98a84418 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 21 Dec 2016 11:30:35 +0200 Subject: [PATCH] bringing the locals into the ruby method --- lib/melon/compiler/method_collector.rb | 13 ++++++++++++- lib/melon/compiler/ruby_method.rb | 8 ++++---- test/melon/compiler/test_method_collector.rb | 7 ++++++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/melon/compiler/method_collector.rb b/lib/melon/compiler/method_collector.rb index e8c7b4fd..991d88d9 100644 --- a/lib/melon/compiler/method_collector.rb +++ b/lib/melon/compiler/method_collector.rb @@ -14,9 +14,12 @@ module Melon def on_def(statement) name , args , body = *statement args_type = make_type(args) - @methods << RubyMethod.new(name , args_type , body ) + locals_type = make_locals(body) + @methods << RubyMethod.new(name , args_type , locals_type , body ) end + private + def make_type( statement ) type = Parfait::Space.object_space.get_class_by_name(:Message ).instance_type statement.children.each do |arg| @@ -25,5 +28,13 @@ module Melon type end + def make_locals(body) + locals = LocalsCollector.new.collect(body) + type = Parfait::Space.object_space.get_class_by_name(:Frame ).instance_type + locals.each do |name , local_type | + type = type.add_instance_variable( name , local_type ) + end + type + end end end diff --git a/lib/melon/compiler/ruby_method.rb b/lib/melon/compiler/ruby_method.rb index 14efe12b..f4dd8905 100644 --- a/lib/melon/compiler/ruby_method.rb +++ b/lib/melon/compiler/ruby_method.rb @@ -2,10 +2,10 @@ module Melon class RubyMethod - attr_reader :name , :args_type , :body - - def initialize(name , args_type , body ) - @name , @args_type , @body = name , args_type , body + attr_reader :name , :args_type , :locals_type , :body + + def initialize(name , args_type , locals_type , body ) + @name , @args_type , @locals_type , @body = name , args_type, locals_type , body end end diff --git a/test/melon/compiler/test_method_collector.rb b/test/melon/compiler/test_method_collector.rb index daf29070..cffaa7cf 100644 --- a/test/melon/compiler/test_method_collector.rb +++ b/test/melon/compiler/test_method_collector.rb @@ -23,10 +23,15 @@ module Melon assert method.args_type.variable_index(:arg1) , method.args_type.inspect end - def test_ivar_operator_assign + def test_three_args method = parse_collect("def meth3(yksi,kaksi,kolme); 1;end").first assert method.args_type.variable_index(:kolme) , method.args_type.inspect end + def test_one_local + method = parse_collect("def meth2(arg1); foo = 2 ;end").first + assert method.locals_type.variable_index(:foo) , method.locals_type.inspect + end + end end