From 8ef1a471a43f6e781bcc229b9aab9ce37e27b7d8 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Mon, 28 May 2018 15:45:29 +0300 Subject: [PATCH] finally change List to derive from data object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit can’t derive from data16 as some lists are longer have to get the delegation to work first --- lib/parfait/data_object.rb | 9 ++++-- lib/parfait/list.rb | 57 ++++++++++++------------------------- lib/parfait/type.rb | 1 + lib/parfait/word.rb | 8 ++---- lib/risc/boot.rb | 2 +- lib/risc/parfait_adapter.rb | 6 ++-- test/parfait/test_list.rb | 2 +- 7 files changed, 33 insertions(+), 52 deletions(-) diff --git a/lib/parfait/data_object.rb b/lib/parfait/data_object.rb index e9bcaacc..4d09a596 100644 --- a/lib/parfait/data_object.rb +++ b/lib/parfait/data_object.rb @@ -30,8 +30,8 @@ module Parfait def data_length raise "called #{self}" end - def data_start - return get_type.get_length + def self.type_length + raise "called #{self}" end end @@ -69,4 +69,9 @@ module Parfait 16 * 4 end end + class Data32 < DataObject + def self.memory_size + 32 + end + end end diff --git a/lib/parfait/list.rb b/lib/parfait/list.rb index 88148b38..ac50baa2 100644 --- a/lib/parfait/list.rb +++ b/lib/parfait/list.rb @@ -12,22 +12,18 @@ # so all incoming/outgoing indexes have to be shifted one up/down module Parfait - class List < Object + class List < Data32 + attr_reader :indexed_length + + def self.type_length + 2 # 0 type , 1 length + end def self.get_length_index - return 2 - end - def self.get_indexed(index) - return index + 2 - end - def length_index - 1 - end - def get_offset - return 2 + type_length - 1 end def get_length - r = get_internal_word( length_index ) #one for type + r = @indexed_length r.nil? ? 0 : r end @@ -39,7 +35,7 @@ module Parfait grow_to(index + 1) end # start one higher than offset, which is where the length is - set_internal_word( index + get_offset, value) + set_internal_word( index + self.class.type_length, value) end # set the value at index. @@ -49,36 +45,30 @@ module Parfait ret = nil if(index < get_length) # start one higher than offset, which is where the length is - ret = get_internal_word(index + get_offset ) + ret = get_internal_word(index + self.class.type_length ) end ret end - + alias :[] :get + def grow_to( len ) raise "Only positive lenths, #{len}" if len < 0 old_length = get_length return if old_length >= len - # raise "bounds error at #{len}" if( len + offset > 16 ) - # be nice to use the indexed_length , but that relies on booted space - set_internal_word( length_index , len) #one for type + internal_set_length( len) end def shrink_to( len ) raise "Only positive lenths, #{len}" if len < 0 old_length = get_length return if old_length <= len - set_internal_word( length_index , len) + internal_set_length( len) end def indexed_length get_length() end - def initialize( ) - super() - @memory = [] - end - # include? means non nil index def include?( item ) return index_of(item) != nil @@ -236,20 +226,6 @@ module Parfait ret end - # 0 -based index - def get_internal_word(index) - @memory[index] - end - - # 0 -based index - def set_internal_word(index , value) - raise "Word[#{index}] = " if((self.class == Parfait::Word) and value.nil? ) - @memory[index] = value - value - end - - alias :[] :get - def to_rxf_node(writer , level , ref ) Sof.array_to_rxf_node(self , writer , level , ref ) end @@ -271,6 +247,10 @@ module Parfait end array end + private + def internal_set_length( i ) + @indexed_length = i + end end # new list from ruby array to be precise @@ -284,5 +264,4 @@ module Parfait end list end - end diff --git a/lib/parfait/type.rb b/lib/parfait/type.rb index 3cf14d25..bf28a266 100644 --- a/lib/parfait/type.rb +++ b/lib/parfait/type.rb @@ -192,6 +192,7 @@ module Parfait end def name_at( index ) + raise "No names #{index}" unless @names @names.get(index) end diff --git a/lib/parfait/word.rb b/lib/parfait/word.rb index 8b1575ec..d871c751 100644 --- a/lib/parfait/word.rb +++ b/lib/parfait/word.rb @@ -15,15 +15,11 @@ module Parfait class Word < Data8 attr_reader :char_length - #semi "indexed" methods for interpreter - def self.get_length_index - 2 # 2 is the amount of attributes, type and char_length. the offset after which chars start - end def self.type_length 2 # 0 type , 1 char_length end - def self.get_indexed( i ) - i + get_length_index * 4 + def self.get_length_index + type_length - 1 end # initialize with length. For now we try to keep all non-parfait (including String) out # String will contain spaces for non-zero length diff --git a/lib/risc/boot.rb b/lib/risc/boot.rb index 5dcfbb01..7a534843 100644 --- a/lib/risc/boot.rb +++ b/lib/risc/boot.rb @@ -119,7 +119,7 @@ module Risc def super_class_names { Data4: :DataObject , Data8: :DataObject ,Data16: :DataObject , BinaryCode: :Data16 , Integer: :Data4, Word: :Data8 , - Object: :BasicObject} + Object: :BasicObject , List: :Data16} end # the function really just returns a constant (just avoiding the constant) diff --git a/lib/risc/parfait_adapter.rb b/lib/risc/parfait_adapter.rb index a308dcff..2e5199c9 100644 --- a/lib/risc/parfait_adapter.rb +++ b/lib/risc/parfait_adapter.rb @@ -11,14 +11,14 @@ module Parfait # 0 -based index def get_internal_word(index) - return super if index < data_start + return super if index < self.class.type_length @memory[index] end # 1 -based index def set_internal_word(index , value) - return super if index < data_start - raise "Word[#{index}] = nil" if( value.nil? ) + return super if index < self.class.type_length + raise "Word[#{index}] = nil" if( value.nil? and self.class != List) @memory[index] = value value end diff --git a/test/parfait/test_list.rb b/test/parfait/test_list.rb index d682cd29..98694f12 100644 --- a/test/parfait/test_list.rb +++ b/test/parfait/test_list.rb @@ -29,7 +29,7 @@ module Parfait assert_equal 0, @list.indexed_length end def test_offset - assert_equal 2 , @list.get_offset + assert_equal 2 , @list.class.type_length end def test_indexed_index # 0 type , 1 indexed_length