generalize get_main and get_init to get_method
to get at those know methods that really __must__ exists, hence the bang, raise if don't about to add method missing and raise to the list
This commit is contained in:
parent
b36ba42990
commit
7ee57f2b08
@ -2,6 +2,8 @@ module Mom
|
|||||||
class Init < Macro
|
class Init < Macro
|
||||||
def to_risc(compiler)
|
def to_risc(compiler)
|
||||||
builder = compiler.builder(compiler.source)
|
builder = compiler.builder(compiler.source)
|
||||||
|
main = Parfait.object_space.get_method!(:Space, :main)
|
||||||
|
|
||||||
builder.build do
|
builder.build do
|
||||||
factory! << Parfait.object_space.get_factory_for(:Message)
|
factory! << Parfait.object_space.get_factory_for(:Message)
|
||||||
message << factory[:next_object]
|
message << factory[:next_object]
|
||||||
@ -9,7 +11,7 @@ module Mom
|
|||||||
factory[:next_object] << next_message
|
factory[:next_object] << next_message
|
||||||
end
|
end
|
||||||
builder.reset_names
|
builder.reset_names
|
||||||
Mom::MessageSetup.new(Parfait.object_space.get_main).build_with( builder )
|
Mom::MessageSetup.new(main).build_with( builder )
|
||||||
|
|
||||||
builder.build do
|
builder.build do
|
||||||
message << message[:next_message]
|
message << message[:next_message]
|
||||||
@ -22,7 +24,7 @@ module Mom
|
|||||||
builder.build do
|
builder.build do
|
||||||
ret_tmp << exit_label
|
ret_tmp << exit_label
|
||||||
message[:return_address] << ret_tmp
|
message[:return_address] << ret_tmp
|
||||||
add_code Risc.function_call( "__init__ issue call" , Parfait.object_space.get_main)
|
add_code Risc.function_call( "__init__ issue call" , main)
|
||||||
add_code exit_label
|
add_code exit_label
|
||||||
end
|
end
|
||||||
compiler.reset_regs
|
compiler.reset_regs
|
||||||
|
@ -113,16 +113,15 @@ module Parfait
|
|||||||
methods
|
methods
|
||||||
end
|
end
|
||||||
|
|
||||||
# shortcut to get the main method. main is defined on Space
|
# shortcut to get at known methods that are used in the compiler
|
||||||
def get_main
|
# arguments are class and method names
|
||||||
space = get_class_by_name :Space
|
# returns method or raises (!)
|
||||||
space.instance_type.get_method :main
|
def get_method!( clazz_name , method_name )
|
||||||
end
|
clazz = get_class_by_name( clazz_name )
|
||||||
|
raise "No such class #{clazz_name}" unless clazz
|
||||||
# shortcut to get the __init__ method, which is defined on Object
|
method = clazz.instance_type.get_method(method_name)
|
||||||
def get_init
|
raise "No such Method #{method_name}, in #{clazz_name}" unless method
|
||||||
object = get_class_by_name :Object
|
method
|
||||||
object.instance_type.get_method :__init__
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# get the current instance_typ of the class with the given name
|
# get the current instance_typ of the class with the given name
|
||||||
|
@ -19,6 +19,7 @@ module Risc
|
|||||||
# call build with a block to build
|
# call build with a block to build
|
||||||
def initialize(compiler, for_source)
|
def initialize(compiler, for_source)
|
||||||
raise "no compiler" unless compiler
|
raise "no compiler" unless compiler
|
||||||
|
raise "no source" unless for_source
|
||||||
@compiler = compiler
|
@compiler = compiler
|
||||||
@source = for_source
|
@source = for_source
|
||||||
@source_used = false
|
@source_used = false
|
||||||
@ -207,7 +208,7 @@ module Risc
|
|||||||
def call_get_more
|
def call_get_more
|
||||||
factory = Parfait.object_space.get_factory_for( :Integer )
|
factory = Parfait.object_space.get_factory_for( :Integer )
|
||||||
calling = factory.get_type.get_method( :get_more )
|
calling = factory.get_type.get_method( :get_more )
|
||||||
calling = Parfait.object_space.get_main #until we actually parse Factory
|
calling = Parfait.object_space.get_method!(:Space,:main) #until we actually parse Factory
|
||||||
raise "no main defined" unless calling
|
raise "no main defined" unless calling
|
||||||
Mom::MessageSetup.new( calling ).build_with( self )
|
Mom::MessageSetup.new( calling ).build_with( self )
|
||||||
self.build do
|
self.build do
|
||||||
|
@ -14,6 +14,7 @@ module Risc
|
|||||||
# Must pass the callable (method/block)
|
# Must pass the callable (method/block)
|
||||||
# Also start instuction, usually a label is mandatory
|
# Also start instuction, usually a label is mandatory
|
||||||
def initialize( callable , mom_label)
|
def initialize( callable , mom_label)
|
||||||
|
raise "No method" unless callable
|
||||||
@callable = callable
|
@callable = callable
|
||||||
@regs = []
|
@regs = []
|
||||||
@constants = []
|
@constants = []
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
require_relative "helper"
|
require_relative "helper"
|
||||||
class FakeCallable
|
|
||||||
end
|
|
||||||
module Mom
|
module Mom
|
||||||
class FakeCallableCompiler < CallableCompiler
|
class FakeCallableCompiler < CallableCompiler
|
||||||
def source_name
|
def source_name
|
||||||
|
@ -45,28 +45,6 @@ module Parfait
|
|||||||
def test_all_classes
|
def test_all_classes
|
||||||
assert_equal classes.length , @space.classes.length , @space.classes.keys.inspect
|
assert_equal classes.length , @space.classes.length , @space.classes.keys.inspect
|
||||||
end
|
end
|
||||||
def test_types
|
|
||||||
assert @space.types.is_a? Parfait::Dictionary
|
|
||||||
end
|
|
||||||
def test_types_attr
|
|
||||||
assert @space.types.is_a? Parfait::Dictionary
|
|
||||||
end
|
|
||||||
def test_types_each
|
|
||||||
@space.each_type do |type|
|
|
||||||
assert type.is_a?(Parfait::Type)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
def test_types_hashes
|
|
||||||
types = @space.types
|
|
||||||
types.each do |has , type|
|
|
||||||
assert has.is_a?(::Integer) , has.inspect
|
|
||||||
end
|
|
||||||
end
|
|
||||||
def test_classes_types_in_space_types
|
|
||||||
@space.classes do |name , clazz|
|
|
||||||
assert_equal clazz.instance_type , @space.get_type_for(clazz.instance_type.hash) , clazz.name
|
|
||||||
end
|
|
||||||
end
|
|
||||||
def test_word_class
|
def test_word_class
|
||||||
word = @space.classes[:Word]
|
word = @space.classes[:Word]
|
||||||
assert word.instance_type
|
assert word.instance_type
|
||||||
@ -149,39 +127,6 @@ module Parfait
|
|||||||
assert @space.get_class_by_name(:NewerClass)
|
assert @space.get_class_by_name(:NewerClass)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_class_types_are_stored
|
|
||||||
@space.classes.each do |name,clazz|
|
|
||||||
assert @space.get_type_for(clazz.instance_type.hash)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_class_types_are_identical
|
|
||||||
@space.classes.each do |name , clazz|
|
|
||||||
cl_type = @space.get_type_for(clazz.instance_type.hash)
|
|
||||||
assert_equal cl_type.object_id , clazz.instance_type.object_id
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_remove_methods
|
|
||||||
@space.each_type do | type |
|
|
||||||
type.method_names.each do |method|
|
|
||||||
type.remove_method(method)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
assert_equal 0 , @space.get_all_methods.length
|
|
||||||
end
|
|
||||||
def test_no_methods_in_types
|
|
||||||
test_remove_methods
|
|
||||||
@space.each_type do |type|
|
|
||||||
assert_equal 0 , type.methods_length , "name #{type.name}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
def test_no_methods_in_classes
|
|
||||||
test_remove_methods
|
|
||||||
@space.classes.each do |name , cl|
|
|
||||||
assert_equal 0 , cl.instance_type.methods_length , "name #{cl.name}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
class BigTestSpace < BigParfaitTest
|
class BigTestSpace < BigParfaitTest
|
||||||
def test_address_count
|
def test_address_count
|
||||||
|
71
test/parfait/test_space2.rb
Normal file
71
test/parfait/test_space2.rb
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
require_relative "helper"
|
||||||
|
|
||||||
|
module Parfait
|
||||||
|
# Most type and method related stuff
|
||||||
|
class TestSpaceMethod < ParfaitTest
|
||||||
|
|
||||||
|
def test_types
|
||||||
|
assert @space.types.is_a? Parfait::Dictionary
|
||||||
|
end
|
||||||
|
def test_types_attr
|
||||||
|
assert @space.types.is_a? Parfait::Dictionary
|
||||||
|
end
|
||||||
|
def test_types_each
|
||||||
|
@space.each_type do |type|
|
||||||
|
assert type.is_a?(Parfait::Type)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def test_types_hashes
|
||||||
|
types = @space.types
|
||||||
|
types.each do |has , type|
|
||||||
|
assert has.is_a?(::Integer) , has.inspect
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def test_classes_types_in_space_types
|
||||||
|
@space.classes do |name , clazz|
|
||||||
|
assert_equal clazz.instance_type , @space.get_type_for(clazz.instance_type.hash) , clazz.name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_class_types_are_stored
|
||||||
|
@space.classes.each do |name,clazz|
|
||||||
|
assert @space.get_type_for(clazz.instance_type.hash)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_class_types_are_identical
|
||||||
|
@space.classes.each do |name , clazz|
|
||||||
|
cl_type = @space.get_type_for(clazz.instance_type.hash)
|
||||||
|
assert_equal cl_type.object_id , clazz.instance_type.object_id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_remove_methods
|
||||||
|
@space.each_type do | type |
|
||||||
|
type.method_names.each do |method|
|
||||||
|
type.remove_method(method)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal 0 , @space.get_all_methods.length
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_no_methods_in_types
|
||||||
|
test_remove_methods
|
||||||
|
@space.each_type do |type|
|
||||||
|
assert_equal 0 , type.methods_length , "name #{type.name}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_no_methods_in_classes
|
||||||
|
test_remove_methods
|
||||||
|
@space.classes.each do |name , cl|
|
||||||
|
assert_equal 0 , cl.instance_type.methods_length , "name #{cl.name}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_get_method_raises
|
||||||
|
assert_raises(RuntimeError){ @space.get_method!(:Space,:main)}
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
@ -63,6 +63,10 @@ module Parfait
|
|||||||
type.add_method(foo)
|
type.add_method(foo)
|
||||||
assert_equal :foo , type.get_method(:foo).name
|
assert_equal :foo , type.get_method(:foo).name
|
||||||
end
|
end
|
||||||
|
def test_space_get_method
|
||||||
|
test_get_instance
|
||||||
|
assert_equal :foo , @space.get_method!(:Object , :foo).name
|
||||||
|
end
|
||||||
def test_resolve_on_object
|
def test_resolve_on_object
|
||||||
add_foo_to :Object
|
add_foo_to :Object
|
||||||
assert_equal :foo , object_type.resolve_method( :foo ).name
|
assert_equal :foo , object_type.resolve_method( :foo ).name
|
||||||
|
@ -6,8 +6,8 @@ module Risc
|
|||||||
def setup
|
def setup
|
||||||
Parfait.boot!(Parfait.default_test_options)
|
Parfait.boot!(Parfait.default_test_options)
|
||||||
Risc.boot!
|
Risc.boot!
|
||||||
init = Parfait.object_space.get_init
|
label = Mom::Label.new( "source_name", "return_label")
|
||||||
@builder = Risc::MethodCompiler.new( init ,Mom::Label.new( "source_name", "return_label")).builder(init)
|
@builder = Risc::MethodCompiler.new( FakeCallable.new ,label).builder("source")
|
||||||
@label = Risc.label("source","name")
|
@label = Risc.label("source","name")
|
||||||
@start = @builder.compiler.current
|
@start = @builder.compiler.current
|
||||||
end
|
end
|
||||||
|
@ -6,9 +6,9 @@ module Risc
|
|||||||
def setup
|
def setup
|
||||||
Parfait.boot!(Parfait.default_test_options)
|
Parfait.boot!(Parfait.default_test_options)
|
||||||
Risc.boot!
|
Risc.boot!
|
||||||
@init = Parfait.object_space.get_init
|
method = FakeCallable.new
|
||||||
@compiler = Risc::MethodCompiler.new( @init, Mom::Label.new( "source_name", "return_label") )
|
@compiler = Risc::MethodCompiler.new( method, Mom::Label.new( "source_name", "return_label") )
|
||||||
@builder = @compiler.builder(@init)
|
@builder = @compiler.builder(method)
|
||||||
end
|
end
|
||||||
def test_list
|
def test_list
|
||||||
assert_equal :List , @builder.infer_type(:list).class_name
|
assert_equal :List , @builder.infer_type(:list).class_name
|
||||||
|
@ -30,11 +30,11 @@ module Risc
|
|||||||
assert_equal Position , Position.get(@linker.cpu_init.first).class
|
assert_equal Position , Position.get(@linker.cpu_init.first).class
|
||||||
end
|
end
|
||||||
def test_first_binary_jump
|
def test_first_binary_jump
|
||||||
bin = Parfait.object_space.get_init.binary
|
bin = Parfait.object_space.get_method!(:Object,:__init__).binary
|
||||||
assert_equal 116 , bin.total_byte_length
|
assert_equal 116 , bin.total_byte_length
|
||||||
end
|
end
|
||||||
def test_second_binary_first
|
def test_second_binary_first
|
||||||
bin = Parfait.object_space.get_init.binary
|
bin = Parfait.object_space.get_method!(:Object,:__init__).binary
|
||||||
assert 0 != bin.get_word(0) , "index 0 is 0 #{bin.inspect}"
|
assert 0 != bin.get_word(0) , "index 0 is 0 #{bin.inspect}"
|
||||||
end
|
end
|
||||||
def test_positions_set
|
def test_positions_set
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
class FakeCallable
|
||||||
|
end
|
||||||
module Risc
|
module Risc
|
||||||
class FakeCompiler
|
class FakeCompiler
|
||||||
attr_reader :instructions
|
attr_reader :instructions
|
||||||
|
Loading…
x
Reference in New Issue
Block a user