From 9ec9ccb9b636a9762e9aea46bd1f0d5efe862675 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 21 Dec 2016 11:20:36 +0200 Subject: [PATCH] start on collector for local variables --- lib/melon/compiler.rb | 1 + lib/melon/compiler/locals_collector.rb | 24 ++++++++++++++ test/melon/compiler/test_all.rb | 1 + test/melon/compiler/test_locals_collector.rb | 35 ++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 lib/melon/compiler/locals_collector.rb create mode 100644 test/melon/compiler/test_locals_collector.rb diff --git a/lib/melon/compiler.rb b/lib/melon/compiler.rb index 1ece116c..39def3b1 100644 --- a/lib/melon/compiler.rb +++ b/lib/melon/compiler.rb @@ -1,6 +1,7 @@ require_relative "compiler/total_processor" require_relative "compiler/type_collector" require_relative "compiler/method_collector" +require_relative "compiler/locals_collector" require_relative "compiler/ruby_method" diff --git a/lib/melon/compiler/locals_collector.rb b/lib/melon/compiler/locals_collector.rb new file mode 100644 index 00000000..49c63978 --- /dev/null +++ b/lib/melon/compiler/locals_collector.rb @@ -0,0 +1,24 @@ +module Melon + + class LocalsCollector < TotalProcessor + + def initialize + @locals = {} + end + + def collect(statement) + process statement + @locals + end + + def on_lvasgn statement + add_local( statement ) + end + + def add_local(statement) + var = statement.children[0] + @locals[var] = :Object #guess, can maybe guess better + end + + end +end diff --git a/test/melon/compiler/test_all.rb b/test/melon/compiler/test_all.rb index 068f85b3..e0604a89 100644 --- a/test/melon/compiler/test_all.rb +++ b/test/melon/compiler/test_all.rb @@ -1,2 +1,3 @@ require_relative "test_type_collector" require_relative "test_method_collector" +require_relative "test_locals_collector" diff --git a/test/melon/compiler/test_locals_collector.rb b/test/melon/compiler/test_locals_collector.rb new file mode 100644 index 00000000..775a511f --- /dev/null +++ b/test/melon/compiler/test_locals_collector.rb @@ -0,0 +1,35 @@ +require_relative "helper" + +module Melon + class TestLocalsCollector < MiniTest::Test + + def setup + Register.machine.boot unless Register.machine.booted + end + + def parse_collect( input ) + ast = Parser::Ruby22.parse input + LocalsCollector.new.collect(ast) + end + + def test_no_locals + locals = parse_collect "def meth; end" + assert locals.empty? + end + + def test_method_is_not_local + locals = parse_collect("def meth2(arg1); foo ;end") + assert locals.empty? + end + + def test_local_assign_one + locals = parse_collect("def meth2(arg1); foo = bar ;end") + assert locals[:foo] , locals.inspect + end + def test_local_assign_two + locals = parse_collect("def meth2(arg1); foo = bar ; groove = 1 + 2 ;end") + assert locals.length == 2 , locals.inspect + end + + end +end