From 9f81d787676a3a86fb03e6ad6e1f0d3bd651b6e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Sat, 28 Sep 2019 15:07:20 +0300 Subject: [PATCH] Also make risc compilers a linked lists also via util::compiler_ist leave collection as much in place as possible (though collections and seperate block_compilers are about to go) --- lib/risc/method_compiler.rb | 1 + lib/risc/risc_collection.rb | 38 ++++++++++++------- lib/util/compiler_list.rb | 7 ++++ test/mom/test_block_compiler.rb | 12 +++--- test/mom/test_mom_collection.rb | 4 +- test/support/compiling.rb | 4 +- test/util/test_compiler_list.rb | 4 +- test/vool/class_send/test_class_def.rb | 2 +- test/vool/class_send/test_class_instance.rb | 4 +- .../class_send/test_class_send_inherited.rb | 2 +- 10 files changed, 49 insertions(+), 29 deletions(-) diff --git a/lib/risc/method_compiler.rb b/lib/risc/method_compiler.rb index a764b93b..41b5c8a3 100644 --- a/lib/risc/method_compiler.rb +++ b/lib/risc/method_compiler.rb @@ -4,6 +4,7 @@ module Risc # and to instantiate the methods correctly. class MethodCompiler < CallableCompiler + include Util::CompilerList # Methods starts with a Label, both in risc and mom. # Pass in the callable(method) and the mom label that the method starts with diff --git a/lib/risc/risc_collection.rb b/lib/risc/risc_collection.rb index 7179d1ea..076916df 100644 --- a/lib/risc/risc_collection.rb +++ b/lib/risc/risc_collection.rb @@ -7,17 +7,28 @@ module Risc # Initialize with an array of risc MethodCompilers def initialize(compilers = []) - @method_compilers = compilers + @method_compilers = nil + compilers.each{|c| add_compiler(c)} end # collects constants from all compilers into one array def constants - method_compilers.inject([]){|sum ,comp| sum + comp.constants } + all = [] + method_compilers.each_compiler{|comp| all += comp.constants } + all end - # Append another MomCompilers method_compilers to this one. - def append(mom_compiler) - @method_compilers += mom_compiler.method_compilers + def append(collection) + @method_compilers.add_method_compiler( collection.method_compilers) + self + end + + def add_compiler(compiler) + if(@method_compilers) + @method_compilers.add_method_compiler(compiler) + else + @method_compilers = compiler + end self end @@ -35,21 +46,22 @@ module Risc # go through all methods and translate them to cpu, given the translator def translate_methods(translator) - method_compilers.collect do |compiler| + collection = [] + method_compilers.each_compiler do |compiler| #puts "Translate method #{compiler.callable.name}" - translate_method(compiler , translator) - end.flatten + translate_method(compiler , translator , collection) + end + collection end # translate one method, which means the method itself and all blocks inside it # returns an array of assemblers - def translate_method( method_compiler , translator) - all = [] - all << translate_cpu( method_compiler , translator ) + def translate_method( method_compiler , translator , collection) + collection << translate_cpu( method_compiler , translator ) method_compiler.block_compilers.each do |block_compiler| - all << translate_cpu(block_compiler , translator) + collection << translate_cpu(block_compiler , translator) end - all + collection end # compile the callable (method or block) to cpu diff --git a/lib/util/compiler_list.rb b/lib/util/compiler_list.rb index 17d31296..e4c238ec 100644 --- a/lib/util/compiler_list.rb +++ b/lib/util/compiler_list.rb @@ -6,6 +6,7 @@ module Util def add_method_compiler(comp) raise "not compiler #{comp.class}" unless comp.respond_to?(:find_compiler) + raise "nil compiler #{self}" unless comp if(@next_compiler) @next_compiler.add_method_compiler(comp) else @@ -18,6 +19,12 @@ module Util @next_compiler.each_compiler(&block) if @next_compiler end + def find_compiler_name name + return self if @callable.name == name + return nil unless @next_compiler + @next_compiler.find_compiler_name(name) + end + def find_compiler &block return self if block.yield(self) @next_compiler.find_compiler(&block) if @next_compiler diff --git a/test/mom/test_block_compiler.rb b/test/mom/test_block_compiler.rb index 70b4f3d7..c7c09cee 100644 --- a/test/mom/test_block_compiler.rb +++ b/test/mom/test_block_compiler.rb @@ -13,11 +13,11 @@ module Mom assert_equal Risc::RiscCollection, @risc.class end def test_main_compiler - assert_equal :main , @risc.method_compilers.first.callable.name + assert_equal :main , @risc.method_compilers.callable.name end def test_main_block_compiler - assert_equal :main , @risc.method_compilers.first.block_compilers.first.in_method.name - assert_equal :main_block , @risc.method_compilers.first.block_compilers.first.callable.name + assert_equal :main , @risc.method_compilers.block_compilers.first.in_method.name + assert_equal :main_block , @risc.method_compilers.block_compilers.first.callable.name end end class TestBlockCompiler2 < MiniTest::Test @@ -32,11 +32,11 @@ module Mom assert_equal Risc::RiscCollection, @risc.class end def test_main_compiler - assert_equal :main , @risc.method_compilers.first.callable.name + assert_equal :main , @risc.method_compilers.callable.name end def test_main_block_compiler - assert_equal :main , @risc.method_compilers.first.block_compilers.first.in_method.name - assert_equal :main_block , @risc.method_compilers.first.block_compilers.first.callable.name + assert_equal :main , @risc.method_compilers.block_compilers.first.in_method.name + assert_equal :main_block , @risc.method_compilers.block_compilers.first.callable.name end end end diff --git a/test/mom/test_mom_collection.rb b/test/mom/test_mom_collection.rb index fa50ccf1..292b8099 100644 --- a/test/mom/test_mom_collection.rb +++ b/test/mom/test_mom_collection.rb @@ -32,14 +32,14 @@ module Mom @collection = @comp.to_risc() end def compiler - @collection.method_compilers.first + @collection.method_compilers end def test_has_to_risc assert_equal Risc::RiscCollection, @collection.class end def test_has_risc_compiler assert_equal Risc::MethodCompiler, compiler.class - assert_equal 3, @collection.method_compilers.length + assert_equal 3, @collection.method_compilers.num_compilers end def test_has_risc_instructions assert_equal Risc::Label, compiler.risc_instructions.class diff --git a/test/support/compiling.rb b/test/support/compiling.rb index 81220dbe..15f92ad8 100644 --- a/test/support/compiling.rb +++ b/test/support/compiling.rb @@ -37,14 +37,14 @@ module VoolCompile input = get_preload(preload) + as_main( input ) collection = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(input) assert collection.is_a?(Mom::MomCollection) , collection.class.name - compiler = collection.compilers.find_compiler{|comp| comp.callable.name == :main} + compiler = collection.compilers.find_compiler_name(:main) assert_equal Mom::MethodCompiler , compiler.class compiler end def compile_main_block( block_input , method_input = "main_local = 5" , preload = nil) source = get_preload(preload) + as_main("#{method_input} ; self.main{|val| #{block_input}}") mom_col = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom( source ) - compiler = mom_col.method_compilers.find_compiler{|c| c.get_method.name.to_s.start_with?("main") } + compiler = mom_col.method_compilers.find_compiler_name(:main) block = compiler.block_compilers.first assert block block.mom_instructions.next diff --git a/test/util/test_compiler_list.rb b/test/util/test_compiler_list.rb index 4d600011..c8461ee9 100644 --- a/test/util/test_compiler_list.rb +++ b/test/util/test_compiler_list.rb @@ -9,7 +9,7 @@ module Util end end - class TestComplierListOne < Minitest::Test + class TestCompilerListOne < Minitest::Test def setup @compiler = MethodCompiler.new(:one) @@ -27,7 +27,7 @@ module Util assert_equal :one , @compiler.find_compiler{|c| c.name == :one}.name end end - class TestComplierListTwo < Minitest::Test + class TestCompilerListTwo < Minitest::Test def setup @compiler = MethodCompiler.new(:one) diff --git a/test/vool/class_send/test_class_def.rb b/test/vool/class_send/test_class_def.rb index f1ea1431..fbb1486f 100644 --- a/test/vool/class_send/test_class_def.rb +++ b/test/vool/class_send/test_class_def.rb @@ -21,7 +21,7 @@ module Vool def setup source = "class Integer