2015-05-26 19:17:43 +02:00
|
|
|
module Parfait
|
2015-06-03 09:01:59 +02:00
|
|
|
|
2018-03-26 13:04:13 +02:00
|
|
|
# A typed method object is a description of the method, it's name etc
|
|
|
|
#
|
|
|
|
# But the code that the method represents, the binary, is held as an array
|
|
|
|
# in these. As Objects are fixed size (this one 16 words), we use linked list
|
|
|
|
# and as the last code of each link is a jump to the next link.
|
|
|
|
#
|
2018-03-25 17:22:02 +02:00
|
|
|
class BinaryCode < Data16
|
2018-03-26 13:04:13 +02:00
|
|
|
attr_reader :next
|
2018-03-25 17:22:02 +02:00
|
|
|
|
|
|
|
def initialize(total_size)
|
|
|
|
super()
|
2018-03-28 11:49:17 +02:00
|
|
|
extend_to(total_size)
|
|
|
|
#puts "Init with #{total_size} for #{object_id}"
|
|
|
|
(1..(data_length+1)).each{ |index| set_word(index , 0) }
|
|
|
|
end
|
|
|
|
def extend_to(total_size)
|
2018-03-25 17:22:02 +02:00
|
|
|
if total_size > self.data_length
|
2018-04-03 14:07:36 +02:00
|
|
|
@next = BinaryCode.new(1) unless @next
|
|
|
|
@next.extend_to(total_size - data_length)
|
2018-03-25 17:22:02 +02:00
|
|
|
end
|
|
|
|
end
|
2015-05-28 20:10:27 +02:00
|
|
|
def to_s
|
2018-03-25 17:22:02 +02:00
|
|
|
"BinaryCode #{}"
|
|
|
|
end
|
2018-03-29 16:38:59 +02:00
|
|
|
def each_word
|
|
|
|
index = 1
|
2018-04-01 11:13:14 +02:00
|
|
|
while( index <= data_length)
|
|
|
|
yield get_word(index)
|
2018-03-29 16:38:59 +02:00
|
|
|
index += 1
|
|
|
|
end
|
|
|
|
end
|
2018-03-26 18:17:30 +02:00
|
|
|
#16 - 2 -1 , two instance varaibles and one for the jump
|
2018-03-25 17:22:02 +02:00
|
|
|
def data_length
|
2018-03-26 13:37:55 +02:00
|
|
|
13
|
2018-03-25 17:22:02 +02:00
|
|
|
end
|
2018-03-26 13:04:13 +02:00
|
|
|
def byte_length
|
2018-03-25 17:22:02 +02:00
|
|
|
4*data_length
|
|
|
|
end
|
2018-03-26 17:14:52 +02:00
|
|
|
def set_word(index , word)
|
|
|
|
raise "invalid index #{index}" if index < 1
|
|
|
|
if index > data_length + 1
|
2018-04-03 14:07:36 +02:00
|
|
|
#raise "invalid index #{index}" unless @next
|
|
|
|
extend_to( index )
|
2018-03-26 17:14:52 +02:00
|
|
|
@next.set_word( index - data_length , word)
|
|
|
|
end
|
|
|
|
set_internal_word(index + 2 , word)
|
|
|
|
end
|
|
|
|
def get_word(index)
|
|
|
|
raise "invalid index #{index}" if index < 1
|
|
|
|
if index > data_length + 1
|
|
|
|
raise "invalid index #{index}" unless @next
|
|
|
|
return @next.get_word( index - data_length)
|
|
|
|
end
|
|
|
|
get_internal_word(index + 2)
|
|
|
|
end
|
2018-03-26 13:04:13 +02:00
|
|
|
def set_char(index , char)
|
|
|
|
if index >= byte_length
|
2018-03-26 18:41:30 +02:00
|
|
|
#puts "Pass it on #{index} for #{self.object_id}:#{@next.object_id}"
|
|
|
|
return @next.set_char( index - byte_length , char )
|
2018-03-25 17:22:02 +02:00
|
|
|
end
|
|
|
|
word_index = (index - 1) / 4 + 2
|
|
|
|
old = get_internal_word( word_index )
|
2018-03-26 13:04:13 +02:00
|
|
|
old = old && char << ((index-1)%4)
|
|
|
|
set_internal_word(word_index , char)
|
2018-03-25 17:22:02 +02:00
|
|
|
end
|
|
|
|
def total_byte_length(start = 0 )
|
2018-03-26 13:37:55 +02:00
|
|
|
start += self.byte_length
|
2018-03-25 17:22:02 +02:00
|
|
|
return start unless self.next
|
|
|
|
self.next.total_byte_length(start)
|
2015-05-28 20:10:27 +02:00
|
|
|
end
|
2015-05-26 19:17:43 +02:00
|
|
|
end
|
|
|
|
end
|