switch to 0 based indexing

the world rocked for a moment (and more fixes to follow)
also the crumbling of idealism was heard
This commit is contained in:
Torsten Ruger
2018-05-14 11:55:01 +03:00
parent 4856b9891d
commit ab01fa3862
19 changed files with 205 additions and 235 deletions

View File

@ -27,7 +27,7 @@ module Parfait
set_instance_type( instance_type )
end
def sof_reference_name
def rxf_reference_name
name
end

View File

@ -32,15 +32,15 @@ module Parfait
return get_type.get_length
end
# 1 -based index
# 0 -based index
def get_internal_word(index)
return super if index <= data_start
return super if index < data_start
@memory[index]
end
# 1 -based index
def set_internal_word(index , value)
return super if index <= data_start
return super if index < data_start
raise "Word[#{index}] = nil" if( value.nil? )
@memory[index] = value
value
@ -49,7 +49,7 @@ module Parfait
class Data4 < DataObject
def data_length
3
4
end
def padded_length
2 * 4
@ -58,7 +58,7 @@ module Parfait
class Data8 < DataObject
def data_length
7
8
end
def padded_length
8 * 4
@ -66,7 +66,7 @@ module Parfait
end
class Data16 < DataObject
def data_length
15
16
end
def padded_length
16 * 4

View File

@ -75,8 +75,8 @@ module Parfait
# yield to each key value pair
def each
index = 1
while index <= @keys.get_length
index = 0
while index < @keys.get_length
key = @keys.get(index)
value = @values.get(index)
yield key , value
@ -93,8 +93,8 @@ module Parfait
string + "}"
end
def to_sof_node(writer , level , ref)
Sof.hash_to_sof_node( self , writer , level , ref)
def to_rxf_node(writer , level , ref)
Sof.hash_to_rxf_node( self , writer , level , ref)
end
end
end

View File

@ -14,58 +14,60 @@
module Parfait
class List < Object
def self.get_length_index
2
return 2
end
def self.get_indexed(index)
index + 2
return index + 2
end
def length_index
1
end
def get_offset
2
return 2
end
def get_length
r = get_internal_word( 2 ) #one for type
r = get_internal_word( length_index ) #one for type
r.nil? ? 0 : r
end
# set the value at index.
# Lists start from index 1
# Lists start from index 0
def set( index , value)
raise "Only positive indexes #{index}" if index <= 0
if index > get_length
grow_to(index)
raise "Only positive indexes #{index}" if index < 0
if index >= get_length
grow_to(index + 1)
end
# start one higher than offset, which is where the length is
set_internal_word( index + 2, value)
set_internal_word( index + get_offset, value)
end
# set the value at index.
# Lists start from index 1
# Lists start from index 0
def get( index )
raise "Only positive indexes, #{index}" if index <= 0
raise "Only positive indexes, #{index}" if index < 0
ret = nil
if(index <= get_length)
if(index < get_length)
# start one higher than offset, which is where the length is
ret = get_internal_word(index + 2 )
ret = get_internal_word(index + get_offset )
end
ret
end
def grow_to( len)
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( 2 , len) #one for type
set_internal_word( length_index , len) #one for type
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( 2 , len)
set_internal_word( length_index , len)
end
def indexed_length
@ -82,13 +84,13 @@ module Parfait
return index_of(item) != nil
end
# index of item, remeber first item has index 1
# index of item
# return nil if no such item
def index_of( item )
max = self.get_length
#puts "length #{max} #{max.class}"
counter = 1
while( counter <= max )
counter = 0
while( counter < max )
if( get(counter) == item)
return counter
end
@ -101,14 +103,14 @@ module Parfait
def next_value(val)
index = index_of(val)
return nil unless index
return nil if index == get_length
return nil if index == (get_length - 1)
return get(index + 1)
end
# push means add to the end
# this automatically grows the List
def push( value )
to = self.get_length + 1
to = self.get_length
set( to , value)
to
end
@ -131,12 +133,12 @@ module Parfait
def first
return nil if empty?
get(1)
get(0)
end
def last
return nil if empty?
get(get_length())
get(get_length() - 1)
end
def empty?
@ -148,7 +150,7 @@ module Parfait
return false if other.class != self.class
return false if other.get_length != self.get_length
index = self.get_length
while(index > 0)
while(index >= 0)
return false if other.get(index) != self.get(index)
index = index - 1
end
@ -158,7 +160,7 @@ module Parfait
# above, correct, implementation causes problems in the machine object space
# because when a second empty (newly created) list is added, it is not actually
# added as it exists already. TODO, but hack with below identity function
def == other
def ==( other )
self.object_id == other.object_id
end
@ -172,8 +174,8 @@ module Parfait
end
def each
index = 1
while index <= self.get_length
index = 0
while index < self.get_length
item = get(index)
yield item
index = index + 1
@ -182,8 +184,8 @@ module Parfait
end
def each_with_index
index = 1
while index <= self.get_length
index = 0
while index < self.get_length
item = get(index)
yield item , index
index = index + 1
@ -192,8 +194,8 @@ module Parfait
end
def each_pair
index = 1
while index <= self.get_length
index = 0
while index < self.get_length
key = get( index )
value = get(index + 1)
yield key , value
@ -203,8 +205,8 @@ module Parfait
end
def find
index = 1
while index <= self.get_length
index = 0
while index < self.get_length
item = get(index)
return item if yield item
index = index + 1
@ -212,34 +214,34 @@ module Parfait
return nil
end
def set_length len
def set_length( len )
was = self.get_length
return if was == len
if(was < len)
grow_to len
grow_to( len )
else
shrink_to len
shrink_to( len )
end
end
def inspect
index = 1
index = 0
ret = ""
while index <= self.get_length
while index < self.get_length
item = get(index)
ret += item.inspect
ret += "," unless index == self.get_length
ret += "," unless index == (self.get_length - 1)
index = index + 1
end
ret
end
# 1 -based index
# 0 -based index
def get_internal_word(index)
@memory[index]
end
# 1 -based index
# 0 -based index
def set_internal_word(index , value)
raise "Word[#{index}] = " if((self.class == Parfait::Word) and value.nil? )
@memory[index] = value
@ -248,8 +250,8 @@ module Parfait
alias :[] :get
def to_sof_node(writer , level , ref )
Sof.array_to_sof_node(self , writer , level , ref )
def to_rxf_node(writer , level , ref )
Sof.array_to_rxf_node(self , writer , level , ref )
end
def dup
@ -262,8 +264,8 @@ module Parfait
def to_a
array = []
index = 1
while( index <= self.get_length)
index = 0
while( index < self.get_length)
array[index - 1] = get(index)
index = index + 1
end
@ -275,9 +277,9 @@ module Parfait
def self.new_list array
list = Parfait::List.new
list.set_length array.length
index = 1
while index <= array.length do
list.set(index , array[index - 1])
index = 0
while index < array.length do
list.set(index , array[index])
index = index + 1
end
list

View File

@ -7,13 +7,13 @@
# Objects are arranged or layed out (in memory) according to their Type
# every object has a Type. Type objects are immutalbe and may be reused for a group/class
# off objects.
# of objects.
# The Type of an object may change, but then a new Type is created
# The Type also defines the class of the object
# The Type is **always** the first entry (index 1) in an object
# The Type is **always** the first entry (index 0) in an object
module Parfait
TYPE_INDEX = 1
TYPE_INDEX = 0
class Object
@ -30,16 +30,16 @@ module Parfait
object
end
# 1 -based index
# 0 -based index
def get_internal_word(index)
name = get_type().name_at(index)
return nil unless name
instance_variable_get("@#{name}".to_sym)
end
# 1 -based index
# 0 -based index
def set_internal_word(index , value)
return set_type(value) if( index == 1)
return set_type(value) if( index == 0)
raise "not type #{@type.class}" unless @type.is_a?(Type)
name = @type.name_at(index)
#return value unless name
@ -111,7 +111,7 @@ module Parfait
end
# parfait versions are deliberately called different, so we "relay"
# have to put the "@" on the names for sof to take them off again
# have to put the "@" on the names for rfx to take them off again
def instance_variables
get_instance_variables.to_a.collect{ |n| "@#{n}".to_sym }
end

View File

@ -121,7 +121,7 @@ module Parfait
@classes[name] = c
end
def sof_reference_name
def rxf_reference_name
"space"
end

View File

@ -1,3 +1,5 @@
module Parfait
# An Object is really a hash like structure. It is dynamic and
# you want to store values by name (instance variable names).
#
@ -32,7 +34,6 @@
# there is only one instance of every type. Hash and equality are defined on type
# for this to work.
module Parfait
class Type < Object
attr_reader :object_class , :names , :types , :methods
@ -178,11 +179,11 @@ module Parfait
end
# index of the variable when using get_internal_word
# (get_internal_word is 1 based and 1 is always the type)
# (get_internal_word is 0 based and 0 is always the type)
def variable_index( name )
has = @names.index_of(name)
return nil unless has
raise "internal error #{name}:#{has}" if has < 1
raise "internal error #{name}:#{has}" if has < 0
has
end
@ -202,14 +203,14 @@ module Parfait
"Type[#{names.inspect}]"
end
def sof_reference_name
def rxf_reference_name
"#{@object_class.name}_Type"
end
alias :name :sof_reference_name
alias :name :rxf_reference_name
def each
index = 1
while( index <= get_length() )
index = 0
while( index < get_length() )
yield( name_at(index) , type_at(index) )
index += 1
end
@ -222,6 +223,8 @@ module Parfait
def to_hash
hash = {}
each do |name , type|
raise "Name nil #{type}" unless name
raise "Type nil #{name}" unless type
hash[name] = type
end
hash

View File

@ -109,7 +109,7 @@ module Parfait
frame_type.types.get(index + 1)
end
def sof_reference_name
def rxf_reference_name
"Method: " + @name.to_s
end

View File

@ -25,12 +25,12 @@ module Parfait
# initialize with length. For now we try to keep all non-parfait (including String) out
# String will contain spaces for non-zero length
# Risc provides methods to create Parfait objects from ruby
def initialize len
def initialize( len )
super()
@char_length = 0
raise "Must init with int, not #{len.class}" unless len.kind_of? Fixnum
raise "Must init with positive, not #{len}" if len < 0
set_length( len , 32 ) unless len == 0 #32 beeing ascii space
set_length( len , 32 ) unless len == 0 #32 being ascii space
#puts "type #{self.get_type} #{self.object_id.to_s(16)}"
end
@ -38,8 +38,8 @@ module Parfait
# return a copy of self
def copy
cop = Word.new( self.length )
index = 1
while( index <= self.length )
index = 0
while( index < self.length )
cop.set_char(index , self.get_char(index))
index = index + 1
end
@ -53,14 +53,14 @@ module Parfait
end
# make every char equal the given one
def fill_with char
def fill_with( char )
fill_from_with(0 , char)
end
def fill_from_with from , char
def fill_from_with( from , char )
len = self.length()
return if from <= 0
while( from <= len)
return if from < 0
while( from < len)
set_char( from , char)
from = from + 1
end
@ -134,12 +134,12 @@ module Parfait
return ret
end
# private method to calculate negative indexes into positives
def range_correct_index at
# private method to account for
def range_correct_index( at )
index = at
# index = self.length + at if at < 0
raise "index must be positive , not #{at}" if (index <= 0)
raise "index too large #{at} > #{self.length}" if (index > self.length )
raise "index must be positive , not #{at}" if (index < 0)
raise "index too large #{at} > #{self.length}" if (index >= self.length )
return index + 11
end
@ -149,8 +149,8 @@ module Parfait
def compare( other )
return false if other.class != self.class
return false if other.length != self.length
len = self.length
while(len > 0)
len = self.length - 1
while(len >= 0)
return false if self.get_char(len) != other.get_char(len)
len = len - 1
end
@ -168,8 +168,8 @@ module Parfait
def to_string
string = ""
index = 1
while( index <= @char_length)
index = 0
while( index < @char_length)
char = get_char(index)
string += char ? char.chr : "*"
index = index + 1
@ -177,8 +177,8 @@ module Parfait
string
end
# as we answered is_value? with true, sof will create a basic node with this string
def to_sof
# as we answered is_value? with true, rfx will create a basic node with this string
def to_rfx
"'" + to_s + "'"
end
@ -197,7 +197,7 @@ module Parfait
string = string.to_s if string.is_a? Symbol
word = Word.new( string.length )
string.codepoints.each_with_index do |code , index |
word.set_char(index + 1 , code)
word.set_char(index , code)
end
word
end