Class: CodeGsubInstruction
- Inherits:
-
Instruction
- Object
- Instruction
- CodeGsubInstruction
- Defined in:
- lib/instructions/code/code_gsub.rb
Overview
Here, we will pop three :code items, find all occurrences of the appropriate point in the changed codeblock, and then replace them in last-to-first order (so the point counts discovered initially don’t change during substitution)
Instance Attribute Summary
Attributes inherited from Instruction
Instance Method Summary collapse
- #cleanup ⇒ Object
- #derive ⇒ Object
-
#preconditions? ⇒ Boolean
was CODE.SUBST in Push3.
- #setup ⇒ Object
Methods inherited from Instruction
all_instructions, #go, inherited, #initialize, #needs, #pushes, to_nudgecode
Constructor Details
This class inherits a constructor from Instruction
Instance Method Details
#cleanup ⇒ Object
32 33 34 |
# File 'lib/instructions/code/code_gsub.rb', line 32 def cleanup pushes :code, @result end |
#derive ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/instructions/code/code_gsub.rb', line 14 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 |
#preconditions? ⇒ Boolean
was CODE.SUBST in Push3
6 7 8 |
# File 'lib/instructions/code/code_gsub.rb', line 6 def preconditions? needs :code, 3 end |
#setup ⇒ Object
9 10 11 12 13 |
# File 'lib/instructions/code/code_gsub.rb', line 9 def setup @host_tree = @context.pop_value(:code) @old_subtree = @context.pop_value(:code) @new_subtree = @context.pop_value(:code) end |