diff --git a/lib/mom/method_compiler.rb b/lib/mom/method_compiler.rb index bc8f5a15..1af2b68c 100644 --- a/lib/mom/method_compiler.rb +++ b/lib/mom/method_compiler.rb @@ -4,7 +4,8 @@ module Mom # and to instantiate the methods correctly. class MethodCompiler < CallableCompiler - + include Util::CompilerList + def initialize( method ) super(method) end diff --git a/lib/mom/mom_collection.rb b/lib/mom/mom_collection.rb index 97b4f93c..81e9aa07 100644 --- a/lib/mom/mom_collection.rb +++ b/lib/mom/mom_collection.rb @@ -11,32 +11,46 @@ module Mom # Initialize with an array of risc MethodCompilers def initialize(compilers = []) - @method_compilers = compilers + @method_compilers = nil + compilers.each{|c| add_compiler(c)} end # lazily instantiate the compiler for __init__ function and __method_missing__ - def init_compiler - @init_compilers ||= [ - MomCollection.create_init_compiler , - MomCollection.create_mm_compiler , - ] + def init_compilers + return if @init_compilers + @init_compilers = true + add_compiler MomCollection.create_init_compiler + add_compiler MomCollection.create_mm_compiler + self end # Return all compilers, namely the MethodCompilers passed in, plus the # boot_function's compilers (boot_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 # 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 to_risc( ) - riscs = compilers.collect do | mom_c | - mom_c.to_risc + init_compilers + riscs =[] + @method_compilers.each_compiler do | mom_c | + riscs << mom_c.to_risc end # to_risc all compilers # for each suffling constnts and fist label, then all instructions (see below) diff --git a/lib/util/compiler_list.rb b/lib/util/compiler_list.rb index 4af6896c..17d31296 100644 --- a/lib/util/compiler_list.rb +++ b/lib/util/compiler_list.rb @@ -5,7 +5,7 @@ module Util attr_reader :next_compiler 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) @next_compiler.add_method_compiler(comp) else @@ -15,7 +15,7 @@ module Util def each_compiler &block block.yield(self) - @next_compiler.each(&block) if @next_compiler + @next_compiler.each_compiler(&block) if @next_compiler end def find_compiler &block diff --git a/test/mom/macro/helper.rb b/test/mom/macro/helper.rb index b6831a91..7a2dd9a4 100644 --- a/test/mom/macro/helper.rb +++ b/test/mom/macro/helper.rb @@ -8,7 +8,7 @@ module Mom def get_compiler(clazz , name) compiler = RubyX::RubyXCompiler.new(RubyX.default_test_options) coll = compiler.ruby_to_mom( get_preload("Space.main;#{clazz}.#{name}") ) - @method = coll.method_compilers.last + @method = coll.method_compilers.last_compiler @method end diff --git a/test/mom/test_block_statement.rb b/test/mom/test_block_statement.rb index 21097869..cd9659ef 100644 --- a/test/mom/test_block_statement.rb +++ b/test/mom/test_block_statement.rb @@ -12,17 +12,17 @@ module Vool assert_equal Mom::MomCollection , @ret.class end 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 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 class TestBlockLocal < MiniTest::Test include MomCompile def setup @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 def test_block_arg_type assert_equal Parfait::Type, @block.arguments_type.class diff --git a/test/mom/test_class_statement.rb b/test/mom/test_class_statement.rb index 3da0f66c..40fce4c5 100644 --- a/test/mom/test_class_statement.rb +++ b/test/mom/test_class_statement.rb @@ -13,11 +13,11 @@ module Vool assert_equal Mom::MomCollection , @ret.class end def test_has_compilers - assert_equal Mom::MethodCompiler , @ret.method_compilers.first.class + assert_equal Mom::MethodCompiler , @ret.method_compilers.class end 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 diff --git a/test/mom/test_method_compiler.rb b/test/mom/test_method_compiler.rb index fc5cbf2b..2b93c789 100644 --- a/test/mom/test_method_compiler.rb +++ b/test/mom/test_method_compiler.rb @@ -20,15 +20,15 @@ module Mom def test_method_mom_col mom = in_test_vool() assert_equal Mom::MomCollection , mom.class - assert_equal Mom::MethodCompiler , mom.compilers.first.class + assert_equal Mom::MethodCompiler , mom.compilers.class end 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::Label , compiler.risc_instructions.class end 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 16 , compiler.risc_instructions.length end diff --git a/test/mom/test_mom_collection.rb b/test/mom/test_mom_collection.rb index 87b75b3e..fa50ccf1 100644 --- a/test/mom/test_mom_collection.rb +++ b/test/mom/test_mom_collection.rb @@ -12,23 +12,17 @@ module Mom assert_equal MomCollection , @comp.class end def test_compilers - assert_equal 3 , @comp.compilers.length + assert_equal 3 , @comp.compilers.num_compilers end def test_init_compilers - assert_equal Array , @comp.init_compiler.class - end - def test_init_compiler - assert_equal Mom::MethodCompiler , @comp.init_compiler.first.class + assert_equal MomCollection , @comp.init_compilers.class end def test_compilers_bare - assert_equal 2 , MomCollection.new.compilers.length + assert_equal 2 , MomCollection.new.compilers.num_compilers end def test_append_class assert_equal MomCollection, (@comp.append @comp).class end - def test_append_length - assert_equal 2 , @comp.append(@comp).method_compilers.length - end end class TestMomCollectionToRisc < MiniTest::Test include MomCompile diff --git a/test/risc/test_method_compiler.rb b/test/risc/test_method_compiler.rb index c61b01c0..94484a76 100644 --- a/test/risc/test_method_compiler.rb +++ b/test/risc/test_method_compiler.rb @@ -64,7 +64,7 @@ module Risc def constant_setup(input) mom = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(in_Test(input)) assert_equal Mom::MomCollection , mom.class - compiler = mom.method_compilers.first + compiler = mom.method_compilers assert_equal Mom::MethodCompiler , compiler.class compiler end diff --git a/test/rubyx/macro/helper.rb b/test/rubyx/macro/helper.rb index 968816a0..12518024 100644 --- a/test/rubyx/macro/helper.rb +++ b/test/rubyx/macro/helper.rb @@ -5,12 +5,12 @@ module RubyX def setup whole ="class Space;def main(arg);return;end;end;" + source @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::MethodCompiler , compiler.class end def compiler - @mom.method_compilers.last + @mom.method_compilers.last_compiler end end diff --git a/test/support/compiling.rb b/test/support/compiling.rb index 82079f4f..81220dbe 100644 --- a/test/support/compiling.rb +++ b/test/support/compiling.rb @@ -37,15 +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{|comp| comp.callable.name == :main} - assert compiler.is_a?(Mom::MethodCompiler) + compiler = collection.compilers.find_compiler{|comp| comp.callable.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{|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 assert block block.mom_instructions.next diff --git a/test/util/test_compiler_list.rb b/test/util/test_compiler_list.rb index 62e3f88c..4d600011 100644 --- a/test/util/test_compiler_list.rb +++ b/test/util/test_compiler_list.rb @@ -24,7 +24,7 @@ module Util assert_equal :one , @compiler.last_compiler.name end def test_find_one - assert_equal :one , @compiler.find_compiler{|c| c.name == :one}.name + assert_equal :one , @compiler.find_compiler{|c| c.name == :one}.name end end class TestComplierListTwo < Minitest::Test @@ -42,23 +42,10 @@ module Util def test_find_two assert_equal :two , @compiler.find_compiler{|c| c.name == :two}.name end + def test_each + all = [] + @compiler.each_compiler{|c| all << c.name} + assert_equal [:one, :two] , all + end end - - - - - - - - - - - - - - - - - - end diff --git a/test/vool/class_send/test_class_def.rb b/test/vool/class_send/test_class_def.rb index 01066b06..f1ea1431 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