make mom:method_compiler a list

Compilers forms alist by Util::CompilerList
Change Collection to use the list instead of array
This commit is contained in:
Torsten Rüger 2019-09-28 12:41:38 +03:00
parent aba42a6836
commit 2eb9364283
16 changed files with 58 additions and 63 deletions

View File

@ -4,6 +4,7 @@ module Mom
# and to instantiate the methods correctly. # and to instantiate the methods correctly.
class MethodCompiler < CallableCompiler class MethodCompiler < CallableCompiler
include Util::CompilerList
def initialize( method ) def initialize( method )
super(method) super(method)

View File

@ -11,32 +11,46 @@ module Mom
# 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
# lazily instantiate the compiler for __init__ function and __method_missing__ # lazily instantiate the compiler for __init__ function and __method_missing__
def init_compiler def init_compilers
@init_compilers ||= [ return if @init_compilers
MomCollection.create_init_compiler , @init_compilers = true
MomCollection.create_mm_compiler , add_compiler MomCollection.create_init_compiler
] add_compiler MomCollection.create_mm_compiler
self
end end
# Return all compilers, namely the MethodCompilers passed in, plus the # Return all compilers, namely the MethodCompilers passed in, plus the
# boot_function's compilers (boot_compilers) # boot_function's compilers (boot_compilers)
def compilers def compilers
@method_compilers + init_compiler init_compilers
@method_compilers
end
def add_compiler(compiler)
if(@method_compilers)
@method_compilers.add_method_compiler(compiler)
else
@method_compilers = compiler
end
self
end end
# Append another MomCompilers method_compilers to this one. # Append another MomCompilers method_compilers to this one.
def append(mom_compiler) def append(collection)
@method_compilers += mom_compiler.method_compilers @method_compilers.add_method_compiler( collection.method_compilers)
self self
end end
def to_risc( ) def to_risc( )
riscs = compilers.collect do | mom_c | init_compilers
mom_c.to_risc riscs =[]
@method_compilers.each_compiler do | mom_c |
riscs << mom_c.to_risc
end end
# to_risc all compilers # to_risc all compilers
# for each suffling constnts and fist label, then all instructions (see below) # for each suffling constnts and fist label, then all instructions (see below)

View File

@ -5,7 +5,7 @@ module Util
attr_reader :next_compiler attr_reader :next_compiler
def add_method_compiler(comp) def add_method_compiler(comp)
raise "not compiler #{comp.class}" unless comp.is_a?(MethodCompiler) raise "not compiler #{comp.class}" unless comp.respond_to?(:find_compiler)
if(@next_compiler) if(@next_compiler)
@next_compiler.add_method_compiler(comp) @next_compiler.add_method_compiler(comp)
else else
@ -15,7 +15,7 @@ module Util
def each_compiler &block def each_compiler &block
block.yield(self) block.yield(self)
@next_compiler.each(&block) if @next_compiler @next_compiler.each_compiler(&block) if @next_compiler
end end
def find_compiler &block def find_compiler &block

View File

@ -8,7 +8,7 @@ module Mom
def get_compiler(clazz , name) def get_compiler(clazz , name)
compiler = RubyX::RubyXCompiler.new(RubyX.default_test_options) compiler = RubyX::RubyXCompiler.new(RubyX.default_test_options)
coll = compiler.ruby_to_mom( get_preload("Space.main;#{clazz}.#{name}") ) coll = compiler.ruby_to_mom( get_preload("Space.main;#{clazz}.#{name}") )
@method = coll.method_compilers.last @method = coll.method_compilers.last_compiler
@method @method
end end

View File

@ -12,17 +12,17 @@ module Vool
assert_equal Mom::MomCollection , @ret.class assert_equal Mom::MomCollection , @ret.class
end end
def test_has_method def test_has_method
assert_equal Parfait::CallableMethod , @ret.method_compilers.first.get_method.class assert_equal Parfait::CallableMethod , @ret.method_compilers.get_method.class
end end
def test_method_has_block def test_method_has_block
assert @ret.method_compilers.first.get_method.blocks , "No block created" assert @ret.method_compilers.get_method.blocks , "No block created"
end end
end end
class TestBlockLocal < MiniTest::Test class TestBlockLocal < MiniTest::Test
include MomCompile include MomCompile
def setup def setup
@ret = compile_mom( as_main("self.main {|elem| local = 5 } ")) @ret = compile_mom( as_main("self.main {|elem| local = 5 } "))
@block = @ret.method_compilers.first.get_method.blocks @block = @ret.method_compilers.get_method.blocks
end end
def test_block_arg_type def test_block_arg_type
assert_equal Parfait::Type, @block.arguments_type.class assert_equal Parfait::Type, @block.arguments_type.class

View File

@ -13,11 +13,11 @@ module Vool
assert_equal Mom::MomCollection , @ret.class assert_equal Mom::MomCollection , @ret.class
end end
def test_has_compilers def test_has_compilers
assert_equal Mom::MethodCompiler , @ret.method_compilers.first.class assert_equal Mom::MethodCompiler , @ret.method_compilers.class
end end
def test_constant def test_constant
assert @ret.method_compilers.first.add_constant( Parfait::Integer.new(5) ) assert @ret.method_compilers.add_constant( Parfait::Integer.new(5) )
end end
end end

View File

@ -20,15 +20,15 @@ module Mom
def test_method_mom_col def test_method_mom_col
mom = in_test_vool() mom = in_test_vool()
assert_equal Mom::MomCollection , mom.class assert_equal Mom::MomCollection , mom.class
assert_equal Mom::MethodCompiler , mom.compilers.first.class assert_equal Mom::MethodCompiler , mom.compilers.class
end end
def test_compiles_risc def test_compiles_risc
compiler = in_test_vool().compilers.first.to_risc compiler = in_test_vool().compilers.to_risc
assert_equal Risc::MethodCompiler , compiler.class assert_equal Risc::MethodCompiler , compiler.class
assert_equal Risc::Label , compiler.risc_instructions.class assert_equal Risc::Label , compiler.risc_instructions.class
end end
def test_compiles_all_risc def test_compiles_all_risc
compiler = in_test_vool().compilers.first.to_risc compiler = in_test_vool().compilers.to_risc
assert_equal Risc::LoadConstant , compiler.risc_instructions.next.class assert_equal Risc::LoadConstant , compiler.risc_instructions.next.class
assert_equal 16 , compiler.risc_instructions.length assert_equal 16 , compiler.risc_instructions.length
end end

View File

@ -12,23 +12,17 @@ module Mom
assert_equal MomCollection , @comp.class assert_equal MomCollection , @comp.class
end end
def test_compilers def test_compilers
assert_equal 3 , @comp.compilers.length assert_equal 3 , @comp.compilers.num_compilers
end end
def test_init_compilers def test_init_compilers
assert_equal Array , @comp.init_compiler.class assert_equal MomCollection , @comp.init_compilers.class
end
def test_init_compiler
assert_equal Mom::MethodCompiler , @comp.init_compiler.first.class
end end
def test_compilers_bare def test_compilers_bare
assert_equal 2 , MomCollection.new.compilers.length assert_equal 2 , MomCollection.new.compilers.num_compilers
end end
def test_append_class def test_append_class
assert_equal MomCollection, (@comp.append @comp).class assert_equal MomCollection, (@comp.append @comp).class
end end
def test_append_length
assert_equal 2 , @comp.append(@comp).method_compilers.length
end
end end
class TestMomCollectionToRisc < MiniTest::Test class TestMomCollectionToRisc < MiniTest::Test
include MomCompile include MomCompile

View File

@ -64,7 +64,7 @@ module Risc
def constant_setup(input) def constant_setup(input)
mom = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(in_Test(input)) mom = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(in_Test(input))
assert_equal Mom::MomCollection , mom.class assert_equal Mom::MomCollection , mom.class
compiler = mom.method_compilers.first compiler = mom.method_compilers
assert_equal Mom::MethodCompiler , compiler.class assert_equal Mom::MethodCompiler , compiler.class
compiler compiler
end end

View File

@ -5,12 +5,12 @@ module RubyX
def setup def setup
whole ="class Space;def main(arg);return;end;end;" + source whole ="class Space;def main(arg);return;end;end;" + source
@mom = RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(whole) @mom = RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(whole)
@mom.method_compilers.first @mom.method_compilers
assert_equal Mom::MomCollection , @mom.class assert_equal Mom::MomCollection , @mom.class
assert_equal Mom::MethodCompiler , compiler.class assert_equal Mom::MethodCompiler , compiler.class
end end
def compiler def compiler
@mom.method_compilers.last @mom.method_compilers.last_compiler
end end
end end

View File

@ -37,15 +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{|comp| comp.callable.name == :main} compiler = collection.compilers.find_compiler{|comp| comp.callable.name == :main}
assert compiler.is_a?(Mom::MethodCompiler)
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{|c| c.get_method.name.to_s.start_with?("main") } compiler = mom_col.method_compilers.find_compiler{|c| c.get_method.name.to_s.start_with?("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

@ -42,23 +42,10 @@ module Util
def test_find_two def test_find_two
assert_equal :two , @compiler.find_compiler{|c| c.name == :two}.name assert_equal :two , @compiler.find_compiler{|c| c.name == :two}.name
end end
def test_each
all = []
@compiler.each_compiler{|c| all << c.name}
assert_equal [:one, :two] , all
end
end end
end end

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{|c|c.callable.name==:main}.mom_instructions.next @ins = ret.compilers.find_compiler{|c|c.callable.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{|c|c.callable.name==:some_inst} @compiler = ret.compilers.find_compiler{|c|c.callable.name==:some_inst}
@main = ret.compilers.find{|c|c.callable.name==:main} @main = ret.compilers.find_compiler{|c|c.callable.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{|c|c.callable.name==:main}.mom_instructions.next @ins = ret.compilers.find_compiler{|c|c.callable.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

@ -39,10 +39,10 @@ module Vool
def test_mom_basic def test_mom_basic
mom = as_mom mom = as_mom
assert_equal Mom::MomCollection , mom.class assert_equal Mom::MomCollection , mom.class
assert_equal Mom::MethodCompiler , mom.method_compilers.first.class assert_equal Mom::MethodCompiler , mom.method_compilers.class
end end
def test_mom_instructions def test_mom_instructions
mom_compiler = as_mom.method_compilers.first mom_compiler = as_mom.method_compilers
assert_equal Mom::Label , mom_compiler.mom_instructions.class assert_equal Mom::Label , mom_compiler.mom_instructions.class
assert_equal Mom::IntOperator , mom_compiler.mom_instructions.next.class assert_equal Mom::IntOperator , mom_compiler.mom_instructions.next.class
assert_equal Mom::SlotLoad , mom_compiler.mom_instructions.next(2).class assert_equal Mom::SlotLoad , mom_compiler.mom_instructions.next(2).class