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)
This commit is contained in:
Torsten Rüger 2019-09-28 15:07:20 +03:00
parent 2eb9364283
commit 9f81d78767
10 changed files with 49 additions and 29 deletions

View File

@ -4,6 +4,7 @@ module Risc
# and to instantiate the methods correctly. # and to instantiate the methods correctly.
class MethodCompiler < CallableCompiler class MethodCompiler < CallableCompiler
include Util::CompilerList
# Methods starts with a Label, both in risc and mom. # Methods starts with a Label, both in risc and mom.
# Pass in the callable(method) and the mom label that the method starts with # Pass in the callable(method) and the mom label that the method starts with

View File

@ -7,17 +7,28 @@ module Risc
# Initialize with an array of risc MethodCompilers # Initialize with an array of risc MethodCompilers
def initialize(compilers = []) def initialize(compilers = [])
@method_compilers = compilers @method_compilers = nil
compilers.each{|c| add_compiler(c)}
end end
# collects constants from all compilers into one array # collects constants from all compilers into one array
def constants def constants
method_compilers.inject([]){|sum ,comp| sum + comp.constants } all = []
method_compilers.each_compiler{|comp| all += comp.constants }
all
end end
# Append another MomCompilers method_compilers to this one. def append(collection)
def append(mom_compiler) @method_compilers.add_method_compiler( collection.method_compilers)
@method_compilers += mom_compiler.method_compilers self
end
def add_compiler(compiler)
if(@method_compilers)
@method_compilers.add_method_compiler(compiler)
else
@method_compilers = compiler
end
self self
end end
@ -35,21 +46,22 @@ module Risc
# go through all methods and translate them to cpu, given the translator # go through all methods and translate them to cpu, given the translator
def translate_methods(translator) def translate_methods(translator)
method_compilers.collect do |compiler| collection = []
method_compilers.each_compiler do |compiler|
#puts "Translate method #{compiler.callable.name}" #puts "Translate method #{compiler.callable.name}"
translate_method(compiler , translator) translate_method(compiler , translator , collection)
end.flatten end
collection
end end
# translate one method, which means the method itself and all blocks inside it # translate one method, which means the method itself and all blocks inside it
# returns an array of assemblers # returns an array of assemblers
def translate_method( method_compiler , translator) def translate_method( method_compiler , translator , collection)
all = [] collection << translate_cpu( method_compiler , translator )
all << translate_cpu( method_compiler , translator )
method_compiler.block_compilers.each do |block_compiler| method_compiler.block_compilers.each do |block_compiler|
all << translate_cpu(block_compiler , translator) collection << translate_cpu(block_compiler , translator)
end end
all collection
end end
# compile the callable (method or block) to cpu # compile the callable (method or block) to cpu

View File

@ -6,6 +6,7 @@ module Util
def add_method_compiler(comp) def add_method_compiler(comp)
raise "not compiler #{comp.class}" unless comp.respond_to?(:find_compiler) raise "not compiler #{comp.class}" unless comp.respond_to?(:find_compiler)
raise "nil compiler #{self}" unless comp
if(@next_compiler) if(@next_compiler)
@next_compiler.add_method_compiler(comp) @next_compiler.add_method_compiler(comp)
else else
@ -18,6 +19,12 @@ module Util
@next_compiler.each_compiler(&block) if @next_compiler @next_compiler.each_compiler(&block) if @next_compiler
end 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 def find_compiler &block
return self if block.yield(self) return self if block.yield(self)
@next_compiler.find_compiler(&block) if @next_compiler @next_compiler.find_compiler(&block) if @next_compiler

View File

@ -13,11 +13,11 @@ module Mom
assert_equal Risc::RiscCollection, @risc.class assert_equal Risc::RiscCollection, @risc.class
end end
def test_main_compiler def test_main_compiler
assert_equal :main , @risc.method_compilers.first.callable.name assert_equal :main , @risc.method_compilers.callable.name
end end
def test_main_block_compiler def test_main_block_compiler
assert_equal :main , @risc.method_compilers.first.block_compilers.first.in_method.name assert_equal :main , @risc.method_compilers.block_compilers.first.in_method.name
assert_equal :main_block , @risc.method_compilers.first.block_compilers.first.callable.name assert_equal :main_block , @risc.method_compilers.block_compilers.first.callable.name
end end
end end
class TestBlockCompiler2 < MiniTest::Test class TestBlockCompiler2 < MiniTest::Test
@ -32,11 +32,11 @@ module Mom
assert_equal Risc::RiscCollection, @risc.class assert_equal Risc::RiscCollection, @risc.class
end end
def test_main_compiler def test_main_compiler
assert_equal :main , @risc.method_compilers.first.callable.name assert_equal :main , @risc.method_compilers.callable.name
end end
def test_main_block_compiler def test_main_block_compiler
assert_equal :main , @risc.method_compilers.first.block_compilers.first.in_method.name assert_equal :main , @risc.method_compilers.block_compilers.first.in_method.name
assert_equal :main_block , @risc.method_compilers.first.block_compilers.first.callable.name assert_equal :main_block , @risc.method_compilers.block_compilers.first.callable.name
end end
end end
end end

View File

@ -32,14 +32,14 @@ module Mom
@collection = @comp.to_risc() @collection = @comp.to_risc()
end end
def compiler def compiler
@collection.method_compilers.first @collection.method_compilers
end end
def test_has_to_risc def test_has_to_risc
assert_equal Risc::RiscCollection, @collection.class assert_equal Risc::RiscCollection, @collection.class
end end
def test_has_risc_compiler def test_has_risc_compiler
assert_equal Risc::MethodCompiler, compiler.class assert_equal Risc::MethodCompiler, compiler.class
assert_equal 3, @collection.method_compilers.length assert_equal 3, @collection.method_compilers.num_compilers
end end
def test_has_risc_instructions def test_has_risc_instructions
assert_equal Risc::Label, compiler.risc_instructions.class assert_equal Risc::Label, compiler.risc_instructions.class

View File

@ -37,14 +37,14 @@ module VoolCompile
input = get_preload(preload) + as_main( input ) input = get_preload(preload) + as_main( input )
collection = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(input) collection = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(input)
assert collection.is_a?(Mom::MomCollection) , collection.class.name 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 assert_equal Mom::MethodCompiler , compiler.class
compiler compiler
end end
def compile_main_block( block_input , method_input = "main_local = 5" , preload = nil) 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}}") 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 ) 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 block = compiler.block_compilers.first
assert block assert block
block.mom_instructions.next block.mom_instructions.next

View File

@ -9,7 +9,7 @@ module Util
end end
end end
class TestComplierListOne < Minitest::Test class TestCompilerListOne < Minitest::Test
def setup def setup
@compiler = MethodCompiler.new(:one) @compiler = MethodCompiler.new(:one)
@ -27,7 +27,7 @@ module Util
assert_equal :one , @compiler.find_compiler{|c| c.name == :one}.name assert_equal :one , @compiler.find_compiler{|c| c.name == :one}.name
end end
end end
class TestComplierListTwo < Minitest::Test class TestCompilerListTwo < Minitest::Test
def setup def setup
@compiler = MethodCompiler.new(:one) @compiler = MethodCompiler.new(:one)

View File

@ -21,7 +21,7 @@ module Vool
def setup def setup
source = "class Integer<Data4;def +(other);X.int_operator(:+);end;end;" + class_main source = "class Integer<Data4;def +(other);X.int_operator(:+);end;end;" + class_main
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(source) ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(source)
@ins = ret.compilers.find_compiler{|c|c.callable.name==:main}.mom_instructions.next @ins = ret.compilers.find_compiler_name(:main).mom_instructions.next
end end
def test_array def test_array
check_array [MessageSetup,ArgumentTransfer,SimpleCall,SlotLoad, check_array [MessageSetup,ArgumentTransfer,SimpleCall,SlotLoad,

View File

@ -20,8 +20,8 @@ module Vool
def setup def setup
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(class_main) ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(class_main)
@compiler = ret.compilers.find_compiler{|c|c.callable.name==:some_inst} @compiler = ret.compilers.find_compiler_name(:some_inst)
@main = ret.compilers.find_compiler{|c|c.callable.name==:main} @main = ret.compilers.find_compiler_name(:main)
@ins = @compiler.mom_instructions.next @ins = @compiler.mom_instructions.next
end end
def test_class_inst def test_class_inst

View File

@ -23,7 +23,7 @@ module Vool
def setup def setup
source = "class Integer < Data4;def +(other);X.int_operator(:+);end;end;" + class_main source = "class Integer < Data4;def +(other);X.int_operator(:+);end;end;" + class_main
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(source) ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(source)
@ins = ret.compilers.find_compiler{|c|c.callable.name==:main}.mom_instructions.next @ins = ret.compilers.find_compiler_name(:main).mom_instructions.next
end end
def test_array def test_array
check_array [MessageSetup,ArgumentTransfer,SimpleCall,SlotLoad, check_array [MessageSetup,ArgumentTransfer,SimpleCall,SlotLoad,