Class: RegularExpression::Bytecode::Compiled

Inherits:
Object
  • Object
show all
Defined in:
lib/regular_expression/bytecode.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(insns, labels) ⇒ Compiled

Returns a new instance of Compiled.



164
165
166
167
# File 'lib/regular_expression/bytecode.rb', line 164

def initialize(insns, labels)
  @insns = insns
  @labels = labels
end

Instance Attribute Details

#insnsObject (readonly)

Returns the value of attribute insns.



162
163
164
# File 'lib/regular_expression/bytecode.rb', line 162

def insns
  @insns
end

#labelsObject (readonly)

Returns the value of attribute labels.



162
163
164
# File 'lib/regular_expression/bytecode.rb', line 162

def labels
  @labels
end

Instance Method Details

#dumpObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/regular_expression/bytecode.rb', line 169

def dump
  output = StringIO.new

  # Labels store name -> address, but if we want to print the label name
  # at its address, we need to store the address to the name as well.
  reverse_labels = {}
  labels.each do |label, n|
    reverse_labels[n] = label
  end

  insns.each_with_index do |insn, n|
    label = reverse_labels[n]
    output.puts("#{label}:") if label
    output.puts("  #{insn}")
  end

  output.string
end