diff --git a/lib/mom/block_compiler.rb b/lib/mom/block_compiler.rb index ee305245..0fdabd7d 100644 --- a/lib/mom/block_compiler.rb +++ b/lib/mom/block_compiler.rb @@ -16,8 +16,8 @@ module Mom "#{@method.self_type.name}.init" end - def to_risc(in_method) - risc_compiler = Risc::BlockCompiler.new(@callable , in_method , mom_instructions) + def to_risc + risc_compiler = Risc::BlockCompiler.new(@callable , @method , mom_instructions) instructions_to_risc(risc_compiler) #recursive blocks not done risc_compiler diff --git a/lib/mom/callable_compiler.rb b/lib/mom/callable_compiler.rb index bdfb0553..76df5ee4 100644 --- a/lib/mom/callable_compiler.rb +++ b/lib/mom/callable_compiler.rb @@ -7,11 +7,11 @@ module Mom # Instructions derive from class Instruction and form a linked list class CallableCompiler + include Util::CompilerList def initialize( callable ) @callable = callable @constants = [] - @block_compilers = [] @mom_instructions = Label.new(source_name, source_name) @current = start = @mom_instructions add_code Label.new( source_name, "return_label") @@ -19,7 +19,7 @@ module Mom add_code Label.new( source_name, "unreachable") @current = start end - attr_reader :mom_instructions , :constants , :block_compilers , :callable , :current + attr_reader :mom_instructions , :constants , :callable , :current def return_label @mom_instructions.each do |ins| diff --git a/lib/mom/method_compiler.rb b/lib/mom/method_compiler.rb index 1af2b68c..90deb321 100644 --- a/lib/mom/method_compiler.rb +++ b/lib/mom/method_compiler.rb @@ -4,8 +4,7 @@ module Mom # and to instantiate the methods correctly. class MethodCompiler < CallableCompiler - include Util::CompilerList - + def initialize( method ) super(method) end @@ -28,9 +27,6 @@ module Mom def to_risc risc_compiler = Risc::MethodCompiler.new(@callable , mom_instructions) instructions_to_risc(risc_compiler) - block_compilers.each do |m_comp| - risc_compiler.block_compilers << m_comp.to_risc(@callable) - end risc_compiler end @@ -77,10 +73,6 @@ module Mom return ["local#{index}".to_sym] end - def add_block_compiler(compiler) - @block_compilers << compiler - end - # return true or false if the given name is in scope (arg/local) def in_scope?(name) ret = true if @callable.arguments_type.variable_index(name) diff --git a/lib/risc/block_compiler.rb b/lib/risc/block_compiler.rb index 1bbf2d19..83c21a4b 100644 --- a/lib/risc/block_compiler.rb +++ b/lib/risc/block_compiler.rb @@ -1,6 +1,7 @@ module Risc - # A BlockCompiler is much like a Mehtodcompiler, exept for blocks + # A BlockCompiler is much like a MethodCompiler, exept for it's for blocks + # This only changes scoping for variables, lsee slot_type # class BlockCompiler < CallableCompiler diff --git a/lib/risc/callable_compiler.rb b/lib/risc/callable_compiler.rb index 31677f54..acd8338e 100644 --- a/lib/risc/callable_compiler.rb +++ b/lib/risc/callable_compiler.rb @@ -10,6 +10,7 @@ module Risc # - current instruction is where addidion happens # class CallableCompiler + include Util::CompilerList # Must pass the callable (method/block) # Also start instuction, usually a label is mandatory @@ -18,11 +19,10 @@ module Risc @callable = callable @regs = [] @constants = [] - @block_compilers = [] @current = @risc_instructions = mom_label.risc_label(self) reset_regs end - attr_reader :risc_instructions , :constants , :block_compilers , :callable , :current + attr_reader :risc_instructions , :constants , :callable , :current def return_label @risc_instructions.each do |ins| @@ -138,5 +138,12 @@ module Risc Risc::Assembler.new(@callable , cpu_instructions ) end + # translate this method, which means the method itself and all blocks inside it + # returns the array (of assemblers) that you pass in as collection + def translate_method( translator , collection) + collection << translate_cpu( translator ) + collection + end + end end diff --git a/lib/risc/method_compiler.rb b/lib/risc/method_compiler.rb index 6c7fbaa1..d4fe8010 100644 --- a/lib/risc/method_compiler.rb +++ b/lib/risc/method_compiler.rb @@ -4,7 +4,6 @@ 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 @@ -12,11 +11,6 @@ module Risc super(method , mom_label) end - #include block_compilers constants - def constants - block_compilers.inject(@constants.dup){|all, compiler| all += compiler.constants} - end - def source_name "#{@callable.self_type.name}.#{@callable.name}" end @@ -26,18 +20,5 @@ module Risc @callable end - def add_block_compiler(compiler) - @block_compilers << compiler - end - - # translate this method, which means the method itself and all blocks inside it - # returns the array (of assemblers) that you pass in as collection - def translate_method( translator , collection) - collection << translate_cpu( translator ) - @block_compilers.each do |block_compiler| - collection << block_compiler.translate_cpu(translator) - end - collection - end end end diff --git a/lib/vool/lambda_expression.rb b/lib/vool/lambda_expression.rb index 51499eb4..edef2376 100644 --- a/lib/vool/lambda_expression.rb +++ b/lib/vool/lambda_expression.rb @@ -23,10 +23,10 @@ module Vool def compile( compiler ) parfait_block = self.parfait_block(compiler) block_compiler = Mom::BlockCompiler.new( parfait_block , compiler.get_method ) - compiler.add_block_compiler(block_compiler) head = body.to_mom( block_compiler ) block_compiler.add_code(head) - block_compiler + compiler.add_method_compiler(block_compiler) + nil end def each(&block) diff --git a/test/mom/test_block_compiler.rb b/test/mom/test_block_compiler.rb index c7c09cee..d008e025 100644 --- a/test/mom/test_block_compiler.rb +++ b/test/mom/test_block_compiler.rb @@ -16,8 +16,9 @@ module Mom assert_equal :main , @risc.method_compilers.callable.name end def test_main_block_compiler - assert_equal :main , @risc.method_compilers.block_compilers.first.in_method.name - assert_equal :main_block , @risc.method_compilers.block_compilers.first.callable.name + main_block = @risc.method_compilers.find_compiler_name(:main_block) + assert_equal :main_block , main_block.callable.name + assert_equal :main , main_block.in_method.name end end class TestBlockCompiler2 < MiniTest::Test @@ -35,8 +36,9 @@ module Mom assert_equal :main , @risc.method_compilers.callable.name end def test_main_block_compiler - assert_equal :main , @risc.method_compilers.block_compilers.first.in_method.name - assert_equal :main_block , @risc.method_compilers.block_compilers.first.callable.name + main_block = @risc.method_compilers.find_compiler_name(:main_block) + assert_equal :main_block , main_block.callable.name + assert_equal :main , main_block.in_method.name end end end diff --git a/test/support/compiling.rb b/test/support/compiling.rb index 15f92ad8..44b51753 100644 --- a/test/support/compiling.rb +++ b/test/support/compiling.rb @@ -45,7 +45,7 @@ module VoolCompile 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_name(:main) - block = compiler.block_compilers.first + block = mom_col.method_compilers.find_compiler_name(:main_block) assert block block.mom_instructions.next end