From 63323376e4aafea693275a1818e6936cd4206c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20R=C3=BCger?= Date: Tue, 10 Sep 2019 12:33:57 +0300 Subject: [PATCH] use more instances in parfait and misc --- lib/parfait/behaviour.rb | 9 +++-- lib/parfait/callable.rb | 6 +-- lib/parfait/callable_method.rb | 2 +- lib/parfait/class.rb | 14 +++---- lib/parfait/type.rb | 45 +++++++++++---------- lib/parfait/vool_method.rb | 4 +- lib/risc/instructions/branch.rb | 4 ++ test/risc/interpreter/calling/test_minus.rb | 2 +- test/risc/test_collector.rb | 10 +++-- test/risc/test_interpreter.rb | 4 +- 10 files changed, 54 insertions(+), 46 deletions(-) diff --git a/lib/parfait/behaviour.rb b/lib/parfait/behaviour.rb index 09340553..c81d65c0 100644 --- a/lib/parfait/behaviour.rb +++ b/lib/parfait/behaviour.rb @@ -7,13 +7,13 @@ module Parfait def initialize super() - instance_methods = List.new + @instance_methods = List.new end def methods - m = instance_methods + m = @instance_methods return m if m - instance_methods = List.new + @instance_methods = List.new end def method_names @@ -26,12 +26,13 @@ module Parfait def add_instance_method( method ) raise "not implemented #{method.class} #{method.inspect}" unless method.is_a? VoolMethod + raise "HMM" method end def remove_instance_method( method_name ) found = get_instance_method( method_name ) - found ? @methods.delete(found) : false + found ? methods.delete(found) : false end def get_instance_method( fname ) diff --git a/lib/parfait/callable.rb b/lib/parfait/callable.rb index 21f9fbe1..9510201d 100644 --- a/lib/parfait/callable.rb +++ b/lib/parfait/callable.rb @@ -49,17 +49,17 @@ module Parfait # determine if method has a local variable or tmp (anonymous local) by given name def has_local( name ) raise "has_local #{name}.#{name.class}" unless name.is_a? Symbol - frame_type.variable_index( name ) + @frame_type.variable_index( name ) end def add_local( name , type ) index = has_local( name ) return index if index - @frame_type = frame_type.add_instance_variable(name,type) + @frame_type = @frame_type.add_instance_variable(name,type) end def each_binary( &block ) - bin = binary + bin = @binary while(bin) do block.call( bin ) bin = bin.next_code diff --git a/lib/parfait/callable_method.rb b/lib/parfait/callable_method.rb index 4fc01faa..f0dab840 100644 --- a/lib/parfait/callable_method.rb +++ b/lib/parfait/callable_method.rb @@ -28,7 +28,7 @@ module Parfait def each_method( &block ) block.call( self ) - next_callable.each_method( &block ) if next_callable + @next_callable.each_method( &block ) if @next_callable end def create_block(args , frame) diff --git a/lib/parfait/class.rb b/lib/parfait/class.rb index ab82817d..6a9ff06e 100644 --- a/lib/parfait/class.rb +++ b/lib/parfait/class.rb @@ -53,16 +53,16 @@ module Parfait def add_method(method) raise "Must be untyped method #{method}" unless method.is_a? Parfait::VoolMethod - instance_methods.push(method) + @instance_methods.push(method) end def get_method(name) - instance_methods.find{|m| m.name == name } + @instance_methods.find{|m| m.name == name } end # adding an instance changes the instance_type to include that variable def add_instance_variable( name , type) - @instance_type = instance_type.add_instance_variable( name , type ) + @instance_type = @instance_type.add_instance_variable( name , type ) end # setting the type generates all methods for this type @@ -76,9 +76,9 @@ module Parfait # or the super classs is nil. # Use only for non Object base class def super_class! - raise "No super_class for class #{name}" unless super_class_name + raise "No super_class for class #{@name}" unless @super_class_name s = super_class - raise "superclass not found for class #{name} (#{super_class_name})" unless s + raise "superclass not found for class #{@name} (#{@super_class_name})" unless s s end @@ -86,8 +86,8 @@ module Parfait # we only store the name, and so have to resolve. # Nil name means no superclass, and so nil is a valid return value def super_class - return nil unless super_class_name - Parfait.object_space.get_class_by_name(super_class_name) + return nil unless @super_class_name + Parfait.object_space.get_class_by_name(@super_class_name) end # ruby 2.1 list (just for reference, keep at bottom) diff --git a/lib/parfait/type.rb b/lib/parfait/type.rb index e4c5c073..f504a4d9 100644 --- a/lib/parfait/type.rb +++ b/lib/parfait/type.rb @@ -88,7 +88,7 @@ module Parfait def method_names names = List.new return names unless @methods - methods.each_method do |method| + @methods.each_method do |method| names.push method.name end names @@ -118,19 +118,19 @@ module Parfait if get_method( method.name ) remove_method(method.name) end - method.set_next( methods ) + method.set_next( @methods ) @methods = method #puts "#{self.name} add #{method.name}" method end def remove_method( method_name ) - raise "No such method #{method_name} in #{self.name}" unless methods - if( methods.name == method_name) - @methods = methods.next_callable + raise "No such method #{method_name} in #{self.name}" unless @methods + if( @methods.name == method_name) + @methods = @methods.next_callable return true end - method = methods + method = @methods while(method && method.next_callable) if( method.next_callable.name == method_name) method.set_next( method.next_callable.next_callable ) @@ -144,8 +144,8 @@ module Parfait def get_method( fname ) raise "get_method #{fname}.#{fname.class}" unless fname.is_a?(Symbol) - return nil unless methods - methods.each_method do |m| + return nil unless @methods + @methods.each_method do |m| return m if(m.name == fname ) end nil @@ -167,9 +167,9 @@ module Parfait end def methods_length - return 0 unless methods + return 0 unless @methods len = 0 - methods.each_method { len += 1} + @methods.each_method { len += 1} return len end @@ -194,29 +194,29 @@ module Parfait end def instance_length - names.get_length() + @names.get_length() end # index of the variable when using get_internal_word # (get_internal_word is 0 based and 0 is always the type) def variable_index( name ) - has = names.index_of(name) + has = @names.index_of(name) return nil unless has raise "internal error #{name}:#{has}" if has < 0 has end def get_length() - names.get_length() + @names.get_length() end def name_at( index ) - raise "No names #{index}" unless names - names.get(index) + raise "No names #{index}" unless @names + @names.get(index) end def type_at( index ) - types.get(index) + @types.get(index) end def type_for( name ) @@ -226,11 +226,11 @@ module Parfait end def inspect - "Type[#{names.inspect}]" + "Type[#{@names.inspect}]" end def rxf_reference_name - "#{object_class.name}_Type" + "#{@object_class.name}_Type" end alias :name :rxf_reference_name @@ -243,9 +243,10 @@ module Parfait end def each_method(&block) - return unless methods - methods.each_method(&block) + return unless @methods + @methods.each_method(&block) end + def to_hash hash = {} each do |name , type| @@ -284,8 +285,8 @@ module Parfait def private_add_instance_variable( name , type) raise "Name shouldn't be nil" unless name raise "Value Type shouldn't be nil" unless type - names.push(name) - types.push(type) + @names.push(name) + @types.push(type) end end diff --git a/lib/parfait/vool_method.rb b/lib/parfait/vool_method.rb index fb6d851d..18da906b 100644 --- a/lib/parfait/vool_method.rb +++ b/lib/parfait/vool_method.rb @@ -27,13 +27,13 @@ module Parfait raise "Empty bod" if(@source.is_a?(Vool::Statements) and @source.empty?) end - def create_callable_method( type ) + def create_callable_method_for( type ) raise "create_method #{type.inspect} is not a Type" unless type.is_a? Parfait::Type type.create_method( @name , @args_type , @frame_type) end def compiler_for(self_type) - callable_method = create_callable_method(self_type) + callable_method = create_callable_method_for(self_type) compiler = Mom::MethodCompiler.new( callable_method ) head = @source.to_mom( compiler ) compiler.add_code(head) diff --git a/lib/risc/instructions/branch.rb b/lib/risc/instructions/branch.rb index 30ca56bc..66e12420 100644 --- a/lib/risc/instructions/branch.rb +++ b/lib/risc/instructions/branch.rb @@ -43,6 +43,10 @@ module Risc @register = register end attr_reader :register + + def to_s + class_source( register.to_s) + end end # A Dynamic yield is very much like a DynamicJump, especially in it's idea diff --git a/test/risc/interpreter/calling/test_minus.rb b/test/risc/interpreter/calling/test_minus.rb index 4c117c67..2b3da3d4 100644 --- a/test/risc/interpreter/calling/test_minus.rb +++ b/test/risc/interpreter/calling/test_minus.rb @@ -37,7 +37,7 @@ module Risc ret = main_ticks(46) assert_equal FunctionReturn , ret.class assert_equal :r3 , ret.register.symbol - assert_equal 40220 , @interpreter.get_register(ret.register) + assert_equal 23404 , @interpreter.get_register(ret.register) end end end diff --git a/test/risc/test_collector.rb b/test/risc/test_collector.rb index e56b1675..2adaa3ac 100644 --- a/test/risc/test_collector.rb +++ b/test/risc/test_collector.rb @@ -12,7 +12,7 @@ module Risc def test_simple_collect objects = Collector.collect_space(@linker) - assert_equal 1564 , objects.length , objects.length.to_s + assert_equal 564 , objects.length , objects.length.to_s end def test_collect_all_types @@ -38,8 +38,10 @@ module Risc def test_integer_positions objects = Collector.collect_space(@linker) int = Parfait.object_space.get_next_for(:Integer) + count = 0 while(int) - assert Position.set?(int) , "INt #{int.object_id}" + count += 1 + assert Position.set?(int) , "INT #{int.object_id.to_s(16)} , count #{count}" int = int.next_integer end end @@ -57,7 +59,7 @@ module Risc def test_simple_collect objects = Collector.collect_space(@linker) - assert_equal 1564, objects.length , objects.length.to_s + assert_equal 564, objects.length , objects.length.to_s end def test_integer_positions @@ -66,7 +68,7 @@ module Risc count = 0 while(int) count += 1 - assert Position.set?(int) , "INT #{int.object_id} , count #{count}" + assert Position.set?(int) , "INT #{int.object_id.to_s(16)} , count #{count}" int = int.next_integer end end diff --git a/test/risc/test_interpreter.rb b/test/risc/test_interpreter.rb index 5e8f7816..0101bc5c 100644 --- a/test/risc/test_interpreter.rb +++ b/test/risc/test_interpreter.rb @@ -55,12 +55,12 @@ module Risc end def test_pc1 @interpreter.tick - assert_equal 40168 , @interpreter.pc + assert_equal 23320 , @interpreter.pc end def test_pc2 @interpreter.tick @interpreter.tick - assert_equal 40172 , @interpreter.pc + assert_equal 23324 , @interpreter.pc end def test_tick2 @interpreter.tick