also giving word it's length
still hacked as char per word, but should be easy enough to fix when…
This commit is contained in:
parent
979ebb7612
commit
96f43dcfda
@ -3,7 +3,7 @@
|
|||||||
module Parfait
|
module Parfait
|
||||||
# A word is a a short sequence of characters
|
# A word is a a short sequence of characters
|
||||||
# Characters are not modeled as objects but as (small) integers
|
# Characters are not modeled as objects but as (small) integers
|
||||||
# The small means two of them have to fit into a machine word, iw utf16 or similar
|
# The small means two of them have to fit into a machine word, utf16 or similar
|
||||||
#
|
#
|
||||||
# Words are constant, maybe like js strings, ruby symbols
|
# Words are constant, maybe like js strings, ruby symbols
|
||||||
# Words are short, but may have spaces
|
# Words are short, but may have spaces
|
||||||
@ -14,14 +14,17 @@ module Parfait
|
|||||||
|
|
||||||
# big TODO , this has NO encoding, a char takes a machine word. Go fix.
|
# big TODO , this has NO encoding, a char takes a machine word. Go fix.
|
||||||
class Word < Object
|
class Word < Object
|
||||||
|
attribute :char_length
|
||||||
|
|
||||||
# initialize with length. For now we try to keep all non-parfait (including String) out
|
# initialize with length. For now we try to keep all non-parfait (including String) out
|
||||||
# String will contain spaces for non-zero length
|
# String will contain spaces for non-zero length
|
||||||
# Register provides methods to create Parfait objects from ruby
|
# Register provides methods to create Parfait objects from ruby
|
||||||
def initialize len
|
def initialize len
|
||||||
super()
|
super()
|
||||||
|
self.char_length = 0
|
||||||
raise "Must init with int, not #{len.class}" unless len.kind_of? Fixnum
|
raise "Must init with int, not #{len.class}" unless len.kind_of? Fixnum
|
||||||
raise "Must init with positive, not #{len}" if len < 0
|
raise "Must init with positive, not #{len}" if len < 0
|
||||||
set_length( len , 32 ) unless len == 0
|
set_length( len , 32 ) unless len == 0 #32 beeing ascii space
|
||||||
end
|
end
|
||||||
|
|
||||||
# return a copy of self
|
# return a copy of self
|
||||||
@ -37,7 +40,7 @@ module Parfait
|
|||||||
|
|
||||||
# return the number of characters
|
# return the number of characters
|
||||||
def length()
|
def length()
|
||||||
obj_len = internal_object_length - 1
|
obj_len = self.char_length
|
||||||
return obj_len
|
return obj_len
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -65,10 +68,11 @@ module Parfait
|
|||||||
#
|
#
|
||||||
def set_length(len , fill_char)
|
def set_length(len , fill_char)
|
||||||
return if len <= 0
|
return if len <= 0
|
||||||
counter = self.length()
|
old = self.char_length
|
||||||
return if counter >= len
|
return if old >= len
|
||||||
internal_object_grow( len + 1)
|
self.char_length = len
|
||||||
fill_from_with( counter + 1 , fill_char )
|
check_length
|
||||||
|
fill_from_with( old + 1 , fill_char )
|
||||||
end
|
end
|
||||||
|
|
||||||
# set the character at the given index to the given character
|
# set the character at the given index to the given character
|
||||||
@ -76,9 +80,9 @@ module Parfait
|
|||||||
# the index starts at one, but may be negative to count from the end
|
# the index starts at one, but may be negative to count from the end
|
||||||
# indexes out of range will raise an error
|
# indexes out of range will raise an error
|
||||||
def set_char at , char
|
def set_char at , char
|
||||||
raise "char not fixnum #{char}" unless char.kind_of? Fixnum
|
raise "char not fixnum #{char.class}" unless char.kind_of? Fixnum
|
||||||
index = range_correct_index(at)
|
index = range_correct_index(at)
|
||||||
internal_object_set( index + 1 , char )
|
internal_object_set( index + 2, char )
|
||||||
end
|
end
|
||||||
|
|
||||||
# get the character at the given index
|
# get the character at the given index
|
||||||
@ -87,7 +91,7 @@ module Parfait
|
|||||||
#the return "character" is an integer
|
#the return "character" is an integer
|
||||||
def get_char at
|
def get_char at
|
||||||
index = range_correct_index(at)
|
index = range_correct_index(at)
|
||||||
return internal_object_get(index + 1)
|
return internal_object_get(index + 2 )
|
||||||
end
|
end
|
||||||
|
|
||||||
# private method to calculate negative indexes into positives
|
# private method to calculate negative indexes into positives
|
||||||
@ -113,11 +117,6 @@ module Parfait
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
# this is a sof check if there are instance variables or "structure"
|
|
||||||
def is_value?
|
|
||||||
true
|
|
||||||
end
|
|
||||||
|
|
||||||
# as we answered is_value? with true, sof will create a basic node with this string
|
# as we answered is_value? with true, sof will create a basic node with this string
|
||||||
def to_sof
|
def to_sof
|
||||||
"'" + to_s + "'"
|
"'" + to_s + "'"
|
||||||
@ -126,5 +125,10 @@ module Parfait
|
|||||||
def word_length
|
def word_length
|
||||||
padded self.length
|
padded self.length
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def check_length
|
||||||
|
raise "Length out of bounds #{self.char_length}" if self.char_length > 32
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -113,7 +113,7 @@ module Register
|
|||||||
# unfortuantely that constant condenses every detail about the system, class names
|
# unfortuantely that constant condenses every detail about the system, class names
|
||||||
# and all instance variable names. Really have to find a better way
|
# and all instance variable names. Really have to find a better way
|
||||||
def layout_names
|
def layout_names
|
||||||
{ :Word => [] ,
|
{ :Word => [:char_length] ,
|
||||||
:List => [:indexed_length] ,
|
:List => [:indexed_length] ,
|
||||||
# Assumtion is that name is the last of message
|
# Assumtion is that name is the last of message
|
||||||
:Message => [:next_message , :receiver , :frame , :return_address , :return_value,
|
:Message => [:next_message , :receiver , :frame , :return_address , :return_value,
|
||||||
|
@ -22,8 +22,9 @@ module Register
|
|||||||
end
|
end
|
||||||
layout = object.get_layout
|
layout = object.get_layout
|
||||||
keep(layout , depth + 1)
|
keep(layout , depth + 1)
|
||||||
|
return if object.is_a? Symbol
|
||||||
layout.object_instance_names.each do |name|
|
layout.object_instance_names.each do |name|
|
||||||
#puts "Keep #{name}"
|
#puts "Keep #{name} for #{object.class}"
|
||||||
inst = object.get_instance_variable name
|
inst = object.get_instance_variable name
|
||||||
keep(inst , depth + 1)
|
keep(inst , depth + 1)
|
||||||
end
|
end
|
||||||
|
@ -12,6 +12,9 @@ class TestEmptyWord < MiniTest::Test
|
|||||||
def test_empty_is_zero
|
def test_empty_is_zero
|
||||||
assert_equal 0 , @word.length
|
assert_equal 0 , @word.length
|
||||||
end
|
end
|
||||||
|
def test_empty_is_zero_internal
|
||||||
|
assert_equal 0 , @word.char_length
|
||||||
|
end
|
||||||
def test_index_check_get
|
def test_index_check_get
|
||||||
assert_raises RuntimeError do
|
assert_raises RuntimeError do
|
||||||
@word.get_char(0)
|
@word.get_char(0)
|
||||||
@ -26,11 +29,15 @@ end
|
|||||||
class TestWord < MiniTest::Test
|
class TestWord < MiniTest::Test
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
|
Register.machine.boot unless Register.machine.booted
|
||||||
@word = ::Parfait::Word.new(5)
|
@word = ::Parfait::Word.new(5)
|
||||||
end
|
end
|
||||||
def test_len
|
def test_len
|
||||||
assert_equal 5 , @word.length
|
assert_equal 5 , @word.length
|
||||||
end
|
end
|
||||||
|
def test_len_internal
|
||||||
|
assert_equal 5 , @word.char_length
|
||||||
|
end
|
||||||
def test_first_char
|
def test_first_char
|
||||||
assert_equal 32 , @word.get_char(1)
|
assert_equal 32 , @word.get_char(1)
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user