Simple compiler list module to make compilers a list

This commit is contained in:
Torsten Rüger 2019-09-28 09:38:34 +03:00
parent 701890f625
commit aba42a6836
4 changed files with 104 additions and 2 deletions

View File

@ -1,7 +1,6 @@
require "rx-file"
require "util/logging"
require "util/list"
require_relative "util"
require_relative "elf/object_writer"
require_relative "risc"
require_relative "mom/mom"

3
lib/util.rb Normal file
View File

@ -0,0 +1,3 @@
require_relative "util/logging"
require_relative "util/list"
require_relative "util/compiler_list"

36
lib/util/compiler_list.rb Normal file
View File

@ -0,0 +1,36 @@
module Util
module CompilerList
attr_reader :next_compiler
def add_method_compiler(comp)
raise "not compiler #{comp.class}" unless comp.is_a?(MethodCompiler)
if(@next_compiler)
@next_compiler.add_method_compiler(comp)
else
@next_compiler = comp
end
end
def each_compiler &block
block.yield(self)
@next_compiler.each(&block) if @next_compiler
end
def find_compiler &block
return self if block.yield(self)
@next_compiler.find_compiler(&block) if @next_compiler
end
def last_compiler
return self unless @next_compiler
@next_compiler.last_compiler
end
def num_compilers
1 + (@next_compiler ? @next_compiler.num_compilers : 0)
end
end
end

View File

@ -0,0 +1,64 @@
require_relative "../helper"
module Util
class MethodCompiler
include Util::CompilerList
attr_reader :name
def initialize(name)
@name = name
end
end
class TestComplierListOne < Minitest::Test
def setup
@compiler = MethodCompiler.new(:one)
end
def add_two
@compiler.add_method_compiler MethodCompiler.new(:two)
end
def test_basic_length
assert_equal 1 , @compiler.num_compilers
end
def test_last_empty
assert_equal :one , @compiler.last_compiler.name
end
def test_find_one
assert_equal :one , @compiler.find_compiler{|c| c.name == :one}.name
end
end
class TestComplierListTwo < Minitest::Test
def setup
@compiler = MethodCompiler.new(:one)
@compiler.add_method_compiler MethodCompiler.new(:two)
end
def test_can_add
assert_equal 2 , @compiler.num_compilers
end
def test_last_two
assert_equal :two , @compiler.last_compiler.name
end
def test_find_two
assert_equal :two , @compiler.find_compiler{|c| c.name == :two}.name
end
end
end