rename instance_methods to just methods
This commit is contained in:
parent
631038dfbd
commit
ef872edd7a
@ -143,7 +143,7 @@ module Register
|
|||||||
:Space => {:classes => :Dictionary , :types => :Dictionary , :first_message => :Message},
|
:Space => {:classes => :Dictionary , :types => :Dictionary , :first_message => :Message},
|
||||||
:NamedList => {},
|
:NamedList => {},
|
||||||
:Type => {:names => :List , :types => :List ,
|
:Type => {:names => :List , :types => :List ,
|
||||||
:object_class => :Class, :instance_methods => :List } ,
|
:object_class => :Class, :methods => :List } ,
|
||||||
:Class => {:instance_methods => :List, :instance_type => :Type, :name => :Word,
|
:Class => {:instance_methods => :List, :instance_type => :Type, :name => :Word,
|
||||||
:super_class_name => :Word , :instance_names => :List },
|
:super_class_name => :Word , :instance_names => :List },
|
||||||
:Dictionary => {:keys => :List , :values => :List } ,
|
:Dictionary => {:keys => :List , :values => :List } ,
|
||||||
@ -163,26 +163,26 @@ module Register
|
|||||||
# have to define some dummies, just for the others to compile
|
# have to define some dummies, just for the others to compile
|
||||||
# TODO go through the virtual parfait layer and adjust function names to what they really are
|
# TODO go through the virtual parfait layer and adjust function names to what they really are
|
||||||
space = @space.get_class_by_name(:Space)
|
space = @space.get_class_by_name(:Space)
|
||||||
space.instance_type.add_instance_method Builtin::Space.send(:main, nil)
|
space.instance_type.add_method Builtin::Space.send(:main, nil)
|
||||||
|
|
||||||
obj = @space.get_class_by_name(:Object)
|
obj = @space.get_class_by_name(:Object)
|
||||||
[ :get_internal_word , :set_internal_word ].each do |f|
|
[ :get_internal_word , :set_internal_word ].each do |f|
|
||||||
obj.instance_type.add_instance_method Builtin::Object.send(f , nil)
|
obj.instance_type.add_method Builtin::Object.send(f , nil)
|
||||||
end
|
end
|
||||||
obj = @space.get_class_by_name(:Kernel)
|
obj = @space.get_class_by_name(:Kernel)
|
||||||
# create __init__ main first, __init__ calls it
|
# create __init__ main first, __init__ calls it
|
||||||
[:exit , :__init__ ].each do |f|
|
[:exit , :__init__ ].each do |f|
|
||||||
obj.instance_type.add_instance_method Builtin::Kernel.send(f , nil)
|
obj.instance_type.add_method Builtin::Kernel.send(f , nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
obj = @space.get_class_by_name(:Word)
|
obj = @space.get_class_by_name(:Word)
|
||||||
[:putstring , :get_internal_byte , :set_internal_byte ].each do |f|
|
[:putstring , :get_internal_byte , :set_internal_byte ].each do |f|
|
||||||
obj.instance_type.add_instance_method Builtin::Word.send(f , nil)
|
obj.instance_type.add_method Builtin::Word.send(f , nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
obj = @space.get_class_by_name(:Integer)
|
obj = @space.get_class_by_name(:Integer)
|
||||||
[ :putint, :mod4, :div10].each do |f| #mod4 is just a forward declaration
|
[ :putint, :mod4, :div10].each do |f| #mod4 is just a forward declaration
|
||||||
obj.instance_type.add_instance_method Builtin::Integer.send(f , nil)
|
obj.instance_type.add_method Builtin::Integer.send(f , nil)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -37,7 +37,7 @@ module Register
|
|||||||
def collect_methods
|
def collect_methods
|
||||||
methods = []
|
methods = []
|
||||||
self.space.types.each do |hash , t|
|
self.space.types.each do |hash , t|
|
||||||
t.instance_methods.each do |f|
|
t.methods.each do |f|
|
||||||
methods << f
|
methods << f
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -72,8 +72,8 @@ module Typed
|
|||||||
@type = method.for_type
|
@type = method.for_type
|
||||||
else
|
else
|
||||||
@type = Parfait::Space.object_space.get_type()
|
@type = Parfait::Space.object_space.get_type()
|
||||||
@method = @type.get_instance_method( :main )
|
@method = @type.get_method( :main )
|
||||||
@method = @type.create_instance_method( :main ,{}) unless @method
|
@method = @type.create_method( :main ,{}) unless @method
|
||||||
end
|
end
|
||||||
@current = @method.instructions
|
@current = @method.instructions
|
||||||
end
|
end
|
||||||
@ -123,7 +123,7 @@ module Typed
|
|||||||
raise "Args must be Hash #{args}" unless args.is_a?(Hash)
|
raise "Args must be Hash #{args}" unless args.is_a?(Hash)
|
||||||
raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a? Symbol
|
raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a? Symbol
|
||||||
arguments = Parfait::Type.for_hash( type.object_class , args )
|
arguments = Parfait::Type.for_hash( type.object_class , args )
|
||||||
@method = type.create_instance_method( method_name , arguments)
|
@method = type.create_method( method_name , arguments)
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ module Typed
|
|||||||
me = get_me( statement )
|
me = get_me( statement )
|
||||||
type = get_my_type(me)
|
type = get_my_type(me)
|
||||||
|
|
||||||
method = type.get_instance_method(statement.name)
|
method = type.get_method(statement.name)
|
||||||
raise "Method not implemented #{type.inspect}.#{statement.name}" unless method
|
raise "Method not implemented #{type.inspect}.#{statement.name}" unless method
|
||||||
|
|
||||||
# move our receiver there
|
# move our receiver there
|
||||||
|
@ -54,12 +54,12 @@ module Parfait
|
|||||||
|
|
||||||
def get_main
|
def get_main
|
||||||
kernel = get_class_by_name :Space
|
kernel = get_class_by_name :Space
|
||||||
kernel.instance_type.get_instance_method :main
|
kernel.instance_type.get_method :main
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_init
|
def get_init
|
||||||
kernel = get_class_by_name :Kernel
|
kernel = get_class_by_name :Kernel
|
||||||
kernel.instance_type.get_instance_method :__init__
|
kernel.instance_type.get_method :__init__
|
||||||
end
|
end
|
||||||
|
|
||||||
# get a class by name (symbol)
|
# get a class by name (symbol)
|
||||||
|
@ -35,9 +35,9 @@
|
|||||||
module Parfait
|
module Parfait
|
||||||
class Type < Object
|
class Type < Object
|
||||||
def self.attributes
|
def self.attributes
|
||||||
[:names , :types , :object_class , :instance_methods ]
|
[:names , :types , :object_class , :methods ]
|
||||||
end
|
end
|
||||||
attr_reader :object_class , :names , :types , :instance_methods
|
attr_reader :object_class , :names , :types , :methods
|
||||||
|
|
||||||
def self.for_hash( object_class , hash)
|
def self.for_hash( object_class , hash)
|
||||||
new_type = Type.new( object_class , hash)
|
new_type = Type.new( object_class , hash)
|
||||||
@ -82,7 +82,7 @@ module Parfait
|
|||||||
# this part of the init is seperate because at boot time we can not use normal new
|
# this part of the init is seperate because at boot time we can not use normal new
|
||||||
# new is overloaded to grab the type from space, and before boot, that is not set up
|
# new is overloaded to grab the type from space, and before boot, that is not set up
|
||||||
def init_lists(hash)
|
def init_lists(hash)
|
||||||
@instance_methods = List.new
|
@methods = List.new
|
||||||
@names = List.new
|
@names = List.new
|
||||||
@types = List.new
|
@types = List.new
|
||||||
private_add_instance_variable :type ,:Type
|
private_add_instance_variable :type ,:Type
|
||||||
@ -95,55 +95,47 @@ module Parfait
|
|||||||
"#{@object_class.name}:#{@names.inspect}"
|
"#{@object_class.name}:#{@names.inspect}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def methods
|
|
||||||
@instance_methods
|
|
||||||
end
|
|
||||||
|
|
||||||
def method_names
|
def method_names
|
||||||
names = List.new
|
names = List.new
|
||||||
self.methods.each do |method|
|
@methods.each do |method|
|
||||||
names.push method.name
|
names.push method.name
|
||||||
end
|
end
|
||||||
names
|
names
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_instance_method( method_name , arguments )
|
def create_method( method_name , arguments )
|
||||||
raise "create_instance_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol)
|
raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol)
|
||||||
#puts "Self: #{self.class} clazz: #{clazz.name}"
|
#puts "Self: #{self.class} clazz: #{clazz.name}"
|
||||||
type = arguments
|
type = arguments
|
||||||
type = Parfait::Type.for_hash( @object_class , arguments) if arguments.is_a?(Hash)
|
type = Parfait::Type.for_hash( @object_class , arguments) if arguments.is_a?(Hash)
|
||||||
add_instance_method TypedMethod.new( self , method_name , type )
|
add_method TypedMethod.new( self , method_name , type )
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_instance_method( method )
|
def add_method( method )
|
||||||
raise "not a method #{method.class} #{method.inspect}" unless method.is_a? TypedMethod
|
raise "not a method #{method.class} #{method.inspect}" unless method.is_a? TypedMethod
|
||||||
raise "syserr #{method.name.class}" unless method.name.is_a? Symbol
|
raise "syserr #{method.name.class}" unless method.name.is_a? Symbol
|
||||||
if self.is_a?(Class) and (method.for_type != self)
|
if self.is_a?(Class) and (method.for_type != self)
|
||||||
raise "Adding to wrong class, should be #{method.for_class}"
|
raise "Adding to wrong class, should be #{method.for_class}"
|
||||||
end
|
end
|
||||||
found = get_instance_method( method.name )
|
found = get_method( method.name )
|
||||||
if found
|
if found
|
||||||
self.methods.delete(found)
|
@methods.delete(found)
|
||||||
end
|
end
|
||||||
self.methods.push method
|
@methods.push method
|
||||||
#puts "#{self.name} add #{method.name}"
|
#puts "#{self.name} add #{method.name}"
|
||||||
method
|
method
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_instance_method method_name
|
def remove_method( method_name )
|
||||||
found = get_instance_method( method_name )
|
found = get_method( method_name )
|
||||||
if found
|
raise "No such method #{method_name} in #{self.name}" unless found
|
||||||
self.methods.delete(found)
|
@methods.delete(found)
|
||||||
else
|
|
||||||
raise "No such method #{method_name} in #{self.name}"
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_instance_method( fname )
|
def get_method( fname )
|
||||||
raise "get_instance_method #{fname}.#{fname.class}" unless fname.is_a?(Symbol)
|
raise "get_method #{fname}.#{fname.class}" unless fname.is_a?(Symbol)
|
||||||
#if we had a hash this would be easier. Detect or find would help too
|
#if we had a hash this would be easier. Detect or find would help too
|
||||||
self.methods.each do |m|
|
@methods.each do |m|
|
||||||
return m if(m.name == fname )
|
return m if(m.name == fname )
|
||||||
end
|
end
|
||||||
nil
|
nil
|
||||||
|
@ -21,7 +21,7 @@ class TypeMessages < MiniTest::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_type_methods
|
def test_type_methods
|
||||||
assert_equal 5 , @mess.get_type.get_type.variable_index(:instance_methods)
|
assert_equal 5 , @mess.get_type.get_type.variable_index(:methods)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -14,44 +14,44 @@ class TestMethodApi < MiniTest::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_new_methods
|
def test_new_methods
|
||||||
assert_equal @try_type.method_names.class, @try_type.instance_methods.class
|
assert_equal @try_type.method_names.class, @try_type.methods.class
|
||||||
assert_equal @try_type.method_names.get_length , @try_type.instance_methods.get_length
|
assert_equal @try_type.method_names.get_length , @try_type.methods.get_length
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_add_method
|
def test_add_method
|
||||||
before = @try_type.instance_methods.get_length
|
before = @try_type.methods.get_length
|
||||||
foo = foo_method
|
foo = foo_method
|
||||||
assert_equal foo , @try_type.add_instance_method(foo)
|
assert_equal foo , @try_type.add_method(foo)
|
||||||
assert_equal 1 , @try_type.instance_methods.get_length - before
|
assert_equal 1 , @try_type.methods.get_length - before
|
||||||
assert @try_type.method_names.inspect.include?(":foo")
|
assert @try_type.method_names.inspect.include?(":foo")
|
||||||
end
|
end
|
||||||
def test_remove_method
|
def test_remove_method
|
||||||
test_add_method
|
test_add_method
|
||||||
assert_equal true , @try_type.remove_instance_method(:foo)
|
assert_equal true , @try_type.remove_method(:foo)
|
||||||
end
|
end
|
||||||
def test_remove_nothere
|
def test_remove_nothere
|
||||||
assert_raises RuntimeError do
|
assert_raises RuntimeError do
|
||||||
@try_type.remove_instance_method(:foo)
|
@try_type.remove_method(:foo)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def test_create_method
|
def test_create_method
|
||||||
args = Parfait::Type.for_hash( @try_class , { bar: :Integer})
|
args = Parfait::Type.for_hash( @try_class , { bar: :Integer})
|
||||||
@try_type.create_instance_method :bar, args
|
@try_type.create_method :bar, args
|
||||||
assert @try_type.method_names.inspect.include?("bar")
|
assert @try_type.method_names.inspect.include?("bar")
|
||||||
end
|
end
|
||||||
def test_method_get
|
def test_method_get
|
||||||
test_add_method
|
test_add_method
|
||||||
assert_equal Parfait::TypedMethod , @try_type.get_instance_method(:foo).class
|
assert_equal Parfait::TypedMethod , @try_type.get_method(:foo).class
|
||||||
end
|
end
|
||||||
def test_method_get_nothere
|
def test_method_get_nothere
|
||||||
assert_nil @try_type.get_instance_method(:foo)
|
assert_nil @try_type.get_method(:foo)
|
||||||
test_remove_method
|
test_remove_method
|
||||||
assert_nil @try_type.get_instance_method(:foo)
|
assert_nil @try_type.get_method(:foo)
|
||||||
end
|
end
|
||||||
def test_get_instance
|
def test_get_instance
|
||||||
foo = foo_method :Object
|
foo = foo_method :Object
|
||||||
type = @space.get_class_by_name(:Object).instance_type
|
type = @space.get_class_by_name(:Object).instance_type
|
||||||
type.add_instance_method(foo)
|
type.add_method(foo)
|
||||||
assert_equal :foo , type.get_instance_method(:foo).name
|
assert_equal :foo , type.get_method(:foo).name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user