fix word implementation and tests

much better, though wasteful implementation
breaks a few tests, but ploughing on first
This commit is contained in:
Torsten Ruger
2015-05-15 16:45:36 +03:00
parent 164816c441
commit 0731a6061a
8 changed files with 155 additions and 55 deletions

View File

@ -7,20 +7,32 @@ module Parfait
#
# Words are constant, maybe like js strings, ruby symbols
# Words are short, but may have spaces
# big TODO , this has NO encoding, a char takes a machine word. Go fix.
class Word < Object
# initialize with length. For now we try to keep all non-parfait (including String) out
# Virtual provides methods to create Parfait objects from ruby
def initialize length
super
set_length( length , 32 )
def initialize len
super()
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 - 1, 32 ) unless len == 0
end
def length()
obj_len = internal_object_length
return obj_len
end
def empty?
return self.length == 0
end
def set_length(len , fill_char)
return if len < 0
counter = self.length()
return if len >= counter
internal_object_grow( ( len + 1 ) / 2)
return if counter >= len
internal_object_grow( len )
while( counter < len)
set_char( counter , fill_char)
counter = counter + 1
@ -28,36 +40,25 @@ module Parfait
end
def set_char at , char
raise "char out of range #{char}" if (char < 0) or (char > 65000)
raise "char not fixnum #{char}" unless char.kind_of? Fixnum
index = range_correct_index(at)
was = internal_object_get(index / 2 )
if index % 2
char = (char << 16) + (was & 0xFFFF)
else
char = char + (was & 0xFFFF0000)
end
internal_object_set( index / 2 , char )
internal_object_set( index , char )
end
def get_char at
index = range_correct_index(at)
whole = internal_object_get(index / 2 )
if index % 2
char = whole >> 16
else
char = whole & 0xFFFF
end
return char
return internal_object_get(index)
end
def range_correct_index at
index = at
index = self.length + at if at < 0
raise "index out of range #{at}" if index < 0
raise "index out of bounds #{at} > #{self.length}" if (index < 0) or (index > self.length)
return index
end
def == other
return false if other.class != self.class
return false if other.length != self.length
len = self.length
while len
@ -66,7 +67,7 @@ module Parfait
end
return true
end
def result= value
raise "called"
class_for(MoveInstruction).new(value , self , :opcode => :mov)