2015-05-13 15:06:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
module Parfait
|
|
|
|
# A word is a a short sequence of characters
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
# Words are constant, maybe like js strings, ruby symbols
|
|
|
|
# Words are short, but may have spaces
|
2015-05-15 15:45:36 +02:00
|
|
|
|
2015-05-17 13:41:18 +02:00
|
|
|
# Words are objects, that means they carry Layout as index 0
|
|
|
|
# So all indexes are offset by one in the implementation
|
|
|
|
# Object length is measured in non-layout cells though
|
|
|
|
|
2015-05-15 15:45:36 +02:00
|
|
|
# big TODO , this has NO encoding, a char takes a machine word. Go fix.
|
2015-05-13 15:06:38 +02:00
|
|
|
class Word < Object
|
2015-05-14 19:39:12 +02:00
|
|
|
# initialize with length. For now we try to keep all non-parfait (including String) out
|
|
|
|
# Virtual provides methods to create Parfait objects from ruby
|
2015-05-15 15:45:36 +02:00
|
|
|
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
|
2015-05-17 14:34:45 +02:00
|
|
|
set_length( len , 32 ) unless len == 0
|
2015-05-15 15:45:36 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def length()
|
2015-05-17 14:34:45 +02:00
|
|
|
obj_len = internal_object_length - 1
|
2015-05-15 15:45:36 +02:00
|
|
|
return obj_len
|
2015-05-14 19:39:12 +02:00
|
|
|
end
|
|
|
|
|
2015-05-15 15:45:36 +02:00
|
|
|
def empty?
|
|
|
|
return self.length == 0
|
|
|
|
end
|
2015-05-14 19:39:12 +02:00
|
|
|
|
|
|
|
def set_length(len , fill_char)
|
2015-05-17 14:34:45 +02:00
|
|
|
return if len <= 0
|
2015-05-14 19:39:12 +02:00
|
|
|
counter = self.length()
|
2015-05-15 15:45:36 +02:00
|
|
|
return if counter >= len
|
2015-05-17 14:34:45 +02:00
|
|
|
internal_object_grow( len + 1)
|
2015-05-18 09:10:31 +02:00
|
|
|
counter = counter + 1
|
2015-05-14 19:39:12 +02:00
|
|
|
while( counter < len)
|
|
|
|
set_char( counter , fill_char)
|
|
|
|
counter = counter + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_char at , char
|
2015-05-15 15:45:36 +02:00
|
|
|
raise "char not fixnum #{char}" unless char.kind_of? Fixnum
|
2015-05-14 19:39:12 +02:00
|
|
|
index = range_correct_index(at)
|
2015-05-17 14:34:45 +02:00
|
|
|
internal_object_set( index + 1 , char )
|
2015-05-13 15:17:10 +02:00
|
|
|
end
|
|
|
|
|
2015-05-14 19:39:12 +02:00
|
|
|
def get_char at
|
|
|
|
index = range_correct_index(at)
|
2015-05-17 14:34:45 +02:00
|
|
|
return internal_object_get(index + 1)
|
2015-05-14 19:39:12 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def range_correct_index at
|
|
|
|
index = at
|
|
|
|
index = self.length + at if at < 0
|
2015-05-18 09:10:31 +02:00
|
|
|
raise "index must be positive , not #{at}" if (index <= 0)
|
|
|
|
raise "index too large #{at} > #{self.length}" if (index > self.length)
|
2015-05-14 19:39:12 +02:00
|
|
|
return index
|
|
|
|
end
|
|
|
|
|
|
|
|
def == other
|
2015-05-15 15:45:36 +02:00
|
|
|
return false if other.class != self.class
|
2015-05-14 19:39:12 +02:00
|
|
|
return false if other.length != self.length
|
|
|
|
len = self.length
|
2015-05-15 19:31:43 +02:00
|
|
|
while(len >= 0)
|
2015-05-14 19:39:12 +02:00
|
|
|
return false if self.get_char(len) != other.get_char(len)
|
|
|
|
len = len - 1
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2015-05-15 15:45:36 +02:00
|
|
|
|
2015-05-15 20:11:29 +02:00
|
|
|
def is_value?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def to_sof
|
|
|
|
"Parfait::Word('#{to_s}')"
|
|
|
|
end
|
2015-05-18 09:47:29 +02:00
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
# this should call parfait get_class, alas that is not implemented yet
|
|
|
|
return false if other.class != self.class
|
|
|
|
return false if other.length != self.length
|
|
|
|
index = self.length
|
|
|
|
while(index > 0)
|
|
|
|
return false if other.get_char(index) != self.get_char(index)
|
|
|
|
index = index - 1
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2015-05-13 15:17:10 +02:00
|
|
|
def result= value
|
|
|
|
raise "called"
|
|
|
|
class_for(MoveInstruction).new(value , self , :opcode => :mov)
|
|
|
|
end
|
|
|
|
def mem_length
|
|
|
|
padded(1 + string.length)
|
|
|
|
end
|
|
|
|
def position
|
|
|
|
return @position if @position
|
|
|
|
return @string.position if @string.position
|
|
|
|
super
|
|
|
|
end
|
2015-05-13 15:06:38 +02:00
|
|
|
end
|
|
|
|
end
|