From ef872edd7a2c8cd15eaae31cabb6c635ed0977f2 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Fri, 30 Dec 2016 13:33:07 +0200 Subject: [PATCH] rename instance_methods to just methods --- lib/register/boot.rb | 12 +++---- lib/register/machine.rb | 2 +- lib/typed/method_compiler.rb | 6 ++-- lib/typed/method_compiler/call_site.rb | 4 +-- lib/typed/parfait/space.rb | 6 ++-- lib/typed/parfait/type.rb | 44 +++++++++++--------------- test/typed/type/test_message.rb | 2 +- test/typed/type/test_method_api.rb | 26 +++++++-------- 8 files changed, 47 insertions(+), 55 deletions(-) diff --git a/lib/register/boot.rb b/lib/register/boot.rb index 47172618..4e4a12fd 100644 --- a/lib/register/boot.rb +++ b/lib/register/boot.rb @@ -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 diff --git a/lib/register/machine.rb b/lib/register/machine.rb index ccc0a812..22f9dae6 100644 --- a/lib/register/machine.rb +++ b/lib/register/machine.rb @@ -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 diff --git a/lib/typed/method_compiler.rb b/lib/typed/method_compiler.rb index c1beacda..cc3f983a 100644 --- a/lib/typed/method_compiler.rb +++ b/lib/typed/method_compiler.rb @@ -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 diff --git a/lib/typed/method_compiler/call_site.rb b/lib/typed/method_compiler/call_site.rb index 0157d4a6..2a00a7b0 100644 --- a/lib/typed/method_compiler/call_site.rb +++ b/lib/typed/method_compiler/call_site.rb @@ -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 diff --git a/lib/typed/parfait/space.rb b/lib/typed/parfait/space.rb index 8785f162..390f984d 100644 --- a/lib/typed/parfait/space.rb +++ b/lib/typed/parfait/space.rb @@ -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) diff --git a/lib/typed/parfait/type.rb b/lib/typed/parfait/type.rb index 3281af7f..5a4efd6a 100644 --- a/lib/typed/parfait/type.rb +++ b/lib/typed/parfait/type.rb @@ -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 diff --git a/test/typed/type/test_message.rb b/test/typed/type/test_message.rb index 73bf8ac8..023a209b 100644 --- a/test/typed/type/test_message.rb +++ b/test/typed/type/test_message.rb @@ -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 diff --git a/test/typed/type/test_method_api.rb b/test/typed/type/test_method_api.rb index 964b302b..b74c6453 100644 --- a/test/typed/type/test_method_api.rb +++ b/test/typed/type/test_method_api.rb @@ -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