Class: LexerKit::IR::ConstantPool

Inherits:
Object
  • Object
show all
Defined in:
lib/lexer_kit/ir/constant_pool.rb

Overview

ConstantPool stores string constants (delimiters, keywords, etc.)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConstantPool

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

#entriesObject (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

Parameters:

  • bytes (String)

Returns:



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

Parameters:

  • value (String)

    constant value

Returns:

  • (Integer)

    constant 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

Parameters:

  • value (String)

    constant value

Returns:

  • (Integer)

    constant ID



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

Parameters:

  • id (Integer)

Returns:

  • (String)


53
54
55
# File 'lib/lexer_kit/ir/constant_pool.rb', line 53

def get(id)
  @entries[id]
end

#inspectObject



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

Parameters:

  • id (Integer)

    constant ID

  • value (String)

    new value



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

#sizeInteger

Number of constants

Returns:

  • (Integer)


59
60
61
# File 'lib/lexer_kit/ir/constant_pool.rb', line 59

def size
  @entries.size
end

#to_binaryString

Encode to binary

Returns:

  • (String)


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