Method: CodeGsubInstruction#derive

Defined in:
lib/instructions/code/code_gsub.rb

#deriveObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/instructions/code/code_gsub.rb', line 22

def derive
  new_tree = NudgeProgram.new(@new_subtree)
  raise InstructionMethodError,
    "#{self.class.to_nudgecode} cannot work with unparseable code" unless new_tree.parses?
  old_tree = NudgeProgram.new(@old_subtree)
  raise InstructionMethodError,
    "#{self.class.to_nudgecode} cannot work with unparseable code" unless old_tree.parses?
  host = NudgeProgram.new(@host_tree)
  raise InstructionMethodError,
    "#{self.class.to_nudgecode} cannot work with unparseable code" unless host.parses?
  
  found_at = [] # need to iterate through the tree the slow way to capture the indices (I think)
  host.linked_code.each_with_index do |point, index|
    found_at << (index+1) if point.blueprint == old_tree.blueprint
  end
  found_at.reverse.each {|index| host = host.replace_point(index, new_tree.linked_code)}
  @result = ValuePoint.new("code", host.blueprint)
end