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:
parent
aba42a6836
commit
2eb9364283
@ -4,6 +4,7 @@ module Mom
|
||||
# and to instantiate the methods correctly.
|
||||
|
||||
class MethodCompiler < CallableCompiler
|
||||
include Util::CompilerList
|
||||
|
||||
def initialize( method )
|
||||
super(method)
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -21,7 +21,7 @@ module Vool
|
||||
def setup
|
||||
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)
|
||||
@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
|
||||
def test_array
|
||||
check_array [MessageSetup,ArgumentTransfer,SimpleCall,SlotLoad,
|
||||
|
@ -20,8 +20,8 @@ module Vool
|
||||
|
||||
def setup
|
||||
ret = RubyX::RubyXCompiler.new(RubyX.default_test_options).ruby_to_mom(class_main)
|
||||
@compiler = ret.compilers.find{|c|c.callable.name==:some_inst}
|
||||
@main = ret.compilers.find{|c|c.callable.name==:main}
|
||||
@compiler = ret.compilers.find_compiler{|c|c.callable.name==:some_inst}
|
||||
@main = ret.compilers.find_compiler{|c|c.callable.name==:main}
|
||||
@ins = @compiler.mom_instructions.next
|
||||
end
|
||||
def test_class_inst
|
||||
|
@ -23,7 +23,7 @@ module Vool
|
||||
def setup
|
||||
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)
|
||||
@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
|
||||
def test_array
|
||||
check_array [MessageSetup,ArgumentTransfer,SimpleCall,SlotLoad,
|
||||
|
@ -39,10 +39,10 @@ module Vool
|
||||
def test_mom_basic
|
||||
mom = as_mom
|
||||
assert_equal Mom::MomCollection , mom.class
|
||||
assert_equal Mom::MethodCompiler , mom.method_compilers.first.class
|
||||
assert_equal Mom::MethodCompiler , mom.method_compilers.class
|
||||
end
|
||||
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::IntOperator , mom_compiler.mom_instructions.next.class
|
||||
assert_equal Mom::SlotLoad , mom_compiler.mom_instructions.next(2).class
|
||||
|
Loading…
Reference in New Issue
Block a user