rename instance_methods to just methods

This commit is contained in:
Torsten Ruger 2016-12-30 13:33:07 +02:00
parent 631038dfbd
commit ef872edd7a
8 changed files with 47 additions and 55 deletions

View File

@ -143,7 +143,7 @@ module Register
:Space => {:classes => :Dictionary , :types => :Dictionary , :first_message => :Message},
:NamedList => {},
: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,
:super_class_name => :Word , :instance_names => :List },
:Dictionary => {:keys => :List , :values => :List } ,
@ -163,26 +163,26 @@ module Register
# 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
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)
[ :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
obj = @space.get_class_by_name(:Kernel)
# create __init__ main first, __init__ calls it
[: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
obj = @space.get_class_by_name(:Word)
[: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
obj = @space.get_class_by_name(:Integer)
[ :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

View File

@ -37,7 +37,7 @@ module Register
def collect_methods
methods = []
self.space.types.each do |hash , t|
t.instance_methods.each do |f|
t.methods.each do |f|
methods << f
end
end

View File

@ -72,8 +72,8 @@ module Typed
@type = method.for_type
else
@type = Parfait::Space.object_space.get_type()
@method = @type.get_instance_method( :main )
@method = @type.create_instance_method( :main ,{}) unless @method
@method = @type.get_method( :main )
@method = @type.create_method( :main ,{}) unless @method
end
@current = @method.instructions
end
@ -123,7 +123,7 @@ module Typed
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
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
end

View File

@ -11,7 +11,7 @@ module Typed
me = get_me( statement )
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
# move our receiver there
@ -85,7 +85,7 @@ module Typed
store_arg_no(arguments , arg_type , arg , i + 1) #+1 for ruby(0 based)
end
end
def store_arg_no(arguments , arg_type , arg , i )
reset_regs
i = i + 1 # disregarding type field

View File

@ -54,12 +54,12 @@ module Parfait
def get_main
kernel = get_class_by_name :Space
kernel.instance_type.get_instance_method :main
kernel.instance_type.get_method :main
end
def get_init
kernel = get_class_by_name :Kernel
kernel.instance_type.get_instance_method :__init__
kernel.instance_type.get_method :__init__
end
# get a class by name (symbol)
@ -77,7 +77,7 @@ module Parfait
def get_class_by_name! name
c = get_class_by_name(name)
return c if c
create_class name
create_class name
end
# this is the way to instantiate classes (not Parfait::Class.new)

View File

@ -35,9 +35,9 @@
module Parfait
class Type < Object
def self.attributes
[:names , :types , :object_class , :instance_methods ]
[:names , :types , :object_class , :methods ]
end
attr_reader :object_class , :names , :types , :instance_methods
attr_reader :object_class , :names , :types , :methods
def self.for_hash( 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
# new is overloaded to grab the type from space, and before boot, that is not set up
def init_lists(hash)
@instance_methods = List.new
@methods = List.new
@names = List.new
@types = List.new
private_add_instance_variable :type ,:Type
@ -95,55 +95,47 @@ module Parfait
"#{@object_class.name}:#{@names.inspect}"
end
def methods
@instance_methods
end
def method_names
names = List.new
self.methods.each do |method|
@methods.each do |method|
names.push method.name
end
names
end
def create_instance_method( method_name , arguments )
raise "create_instance_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol)
def create_method( method_name , arguments )
raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol)
#puts "Self: #{self.class} clazz: #{clazz.name}"
type = arguments
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
def add_instance_method( method )
def add_method( method )
raise "not a method #{method.class} #{method.inspect}" unless method.is_a? TypedMethod
raise "syserr #{method.name.class}" unless method.name.is_a? Symbol
if self.is_a?(Class) and (method.for_type != self)
raise "Adding to wrong class, should be #{method.for_class}"
end
found = get_instance_method( method.name )
found = get_method( method.name )
if found
self.methods.delete(found)
@methods.delete(found)
end
self.methods.push method
@methods.push method
#puts "#{self.name} add #{method.name}"
method
end
def remove_instance_method method_name
found = get_instance_method( method_name )
if found
self.methods.delete(found)
else
raise "No such method #{method_name} in #{self.name}"
end
return true
def remove_method( method_name )
found = get_method( method_name )
raise "No such method #{method_name} in #{self.name}" unless found
@methods.delete(found)
end
def get_instance_method( fname )
raise "get_instance_method #{fname}.#{fname.class}" unless fname.is_a?(Symbol)
def get_method( fname )
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
self.methods.each do |m|
@methods.each do |m|
return m if(m.name == fname )
end
nil

View File

@ -21,7 +21,7 @@ class TypeMessages < MiniTest::Test
end
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

View File

@ -14,44 +14,44 @@ class TestMethodApi < MiniTest::Test
end
def test_new_methods
assert_equal @try_type.method_names.class, @try_type.instance_methods.class
assert_equal @try_type.method_names.get_length , @try_type.instance_methods.get_length
assert_equal @try_type.method_names.class, @try_type.methods.class
assert_equal @try_type.method_names.get_length , @try_type.methods.get_length
end
def test_add_method
before = @try_type.instance_methods.get_length
before = @try_type.methods.get_length
foo = foo_method
assert_equal foo , @try_type.add_instance_method(foo)
assert_equal 1 , @try_type.instance_methods.get_length - before
assert_equal foo , @try_type.add_method(foo)
assert_equal 1 , @try_type.methods.get_length - before
assert @try_type.method_names.inspect.include?(":foo")
end
def test_remove_method
test_add_method
assert_equal true , @try_type.remove_instance_method(:foo)
assert_equal true , @try_type.remove_method(:foo)
end
def test_remove_nothere
assert_raises RuntimeError do
@try_type.remove_instance_method(:foo)
@try_type.remove_method(:foo)
end
end
def test_create_method
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")
end
def test_method_get
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
def test_method_get_nothere
assert_nil @try_type.get_instance_method(:foo)
assert_nil @try_type.get_method(:foo)
test_remove_method
assert_nil @try_type.get_instance_method(:foo)
assert_nil @try_type.get_method(:foo)
end
def test_get_instance
foo = foo_method :Object
type = @space.get_class_by_name(:Object).instance_type
type.add_instance_method(foo)
assert_equal :foo , type.get_instance_method(:foo).name
type.add_method(foo)
assert_equal :foo , type.get_method(:foo).name
end
end