Class: LexerKit::IR::ConstantPool
- Inherits:
-
Object
- Object
- LexerKit::IR::ConstantPool
- Defined in:
- lib/lexer_kit/ir/constant_pool.rb
Overview
ConstantPool stores string constants (delimiters, keywords, etc.)
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Class Method Summary collapse
-
.from_binary(bytes) ⇒ Array(ConstantPool, Integer)
Decode from binary.
Instance Method Summary collapse
-
#add(value) ⇒ Integer
Add a constant and return its ID.
-
#add_uninterned(value) ⇒ Integer
Add a constant without interning.
-
#get(id) ⇒ String
Get constant by ID.
-
#initialize ⇒ ConstantPool
constructor
A new instance of ConstantPool.
- #inspect ⇒ Object
-
#replace(id, value) ⇒ Object
Replace an existing constant by ID.
-
#size ⇒ Integer
Number of constants.
-
#to_binary ⇒ String
Encode to binary.
Constructor Details
#initialize ⇒ ConstantPool
Returns a new instance of ConstantPool.
9 10 11 12 |
# File 'lib/lexer_kit/ir/constant_pool.rb', line 9 def initialize @entries = [] @index = {} end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
7 8 9 |
# File 'lib/lexer_kit/ir/constant_pool.rb', line 7 def entries @entries end |
Class Method Details
.from_binary(bytes) ⇒ Array(ConstantPool, Integer)
Decode from binary
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lexer_kit/ir/constant_pool.rb', line 83 def self.from_binary(bytes) pos = 0 count = bytes.byteslice(pos, 2).unpack1("S>") pos += 2 pool = new count.times do len = bytes.byteslice(pos, 2).unpack1("S>") pos += 2 value = bytes.byteslice(pos, len) pool.add(value) pos += len end [pool, pos] end |
Instance Method Details
#add(value) ⇒ Integer
Add a constant and return its ID
17 18 19 20 21 22 23 24 25 |
# File 'lib/lexer_kit/ir/constant_pool.rb', line 17 def add(value) value = value.b.freeze return @index[value] if @index.key?(value) id = @entries.size @entries << value @index[value] = id id end |
#add_uninterned(value) ⇒ Integer
Add a constant without interning
30 31 32 33 34 35 |
# File 'lib/lexer_kit/ir/constant_pool.rb', line 30 def add_uninterned(value) value = value.b.freeze id = @entries.size @entries << value id end |
#get(id) ⇒ String
Get constant by ID
53 54 55 |
# File 'lib/lexer_kit/ir/constant_pool.rb', line 53 def get(id) @entries[id] end |
#inspect ⇒ Object
102 103 104 |
# File 'lib/lexer_kit/ir/constant_pool.rb', line 102 def inspect "#<ConstantPool size=#{@entries.size}>" end |
#replace(id, value) ⇒ Object
Replace an existing constant by ID
40 41 42 43 44 45 46 47 48 |
# File 'lib/lexer_kit/ir/constant_pool.rb', line 40 def replace(id, value) value = value.b.freeze old = @entries[id] @entries[id] = value if old && @index[old] == id @index.delete(old) end @index[value] = id unless @index.key?(value) end |
#size ⇒ Integer
Number of constants
59 60 61 |
# File 'lib/lexer_kit/ir/constant_pool.rb', line 59 def size @entries.size end |
#to_binary ⇒ String
Encode to binary
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/lexer_kit/ir/constant_pool.rb', line 65 def to_binary parts = [] # Count (u16) parts << [@entries.size].pack("S>") # Entries: [length (u16), bytes...] @entries.each do |entry| parts << [entry.bytesize].pack("S>") parts << entry end parts.join end |