rubyx/lib/parfait/binary_code.rb

45 lines
1.2 KiB
Ruby
Raw Normal View History

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.
#
class BinaryCode < Data16
2018-03-26 13:04:13 +02:00
attr_reader :next
def initialize(total_size)
super()
if total_size > self.data_length
2018-03-26 13:04:13 +02:00
@next = BinaryCode.new(total_size - data_length - 1) #one for the jump
end
#puts "Init with #{total_size} for #{object_id}"
end
2015-05-28 20:10:27 +02:00
def to_s
"BinaryCode #{}"
end
def data_length
14
end
2018-03-26 13:04:13 +02:00
def byte_length
4*data_length
end
2018-03-26 13:04:13 +02:00
def set_char(index , char)
if index >= byte_length
puts "Pass it on #{index} for #{object_id}"
2018-03-26 13:04:13 +02:00
return @next.set_char( char , index - byte_length)
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)
end
def total_byte_length(start = 0 )
start += 4*14
return start unless self.next
self.next.total_byte_length(start)
2015-05-28 20:10:27 +02:00
end
end
end