Class: CodeReplaceNthPointInstruction
- Inherits:
-
Instruction
- Object
- Instruction
- CodeReplaceNthPointInstruction
- Defined in:
- lib/instructions/code/code_replace_nth_point.rb
Overview
pops the top two :code
items (“host” and “target”), and one :int
item (the “index”); pushes a new :code
item, which is the indexth program point of the host
is replaced by the target
(as a subtree)
note: order of arguments is important; the top :code
item is the “target” argument
needs: 2 :code
, 1 :int
pushes: 1 :code
Instance Attribute Summary
Attributes inherited from Instruction
Instance Method Summary collapse
- #cleanup ⇒ Object
- #derive ⇒ Object
-
#preconditions? ⇒ Boolean
was CODE.INSERT 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
38 39 40 |
# File 'lib/instructions/code/code_replace_nth_point.rb', line 38 def cleanup pushes :code, @result end |
#derive ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/instructions/code/code_replace_nth_point.rb', line 22 def derive acceptor = NudgeProgram.new(@accept) raise InstructionMethodError, "#{self.class.to_nudgecode} cannot work with unparseable code" unless acceptor.parses? insertion = NudgeProgram.new(@insert) raise InstructionMethodError, "#{self.class.to_nudgecode} cannot work with unparseable code" unless insertion.parses? scale = acceptor.points which_pt = if @where < 1 || @where > scale (@where % acceptor.points) + 1 else @where end new_tree = acceptor.replace_point(which_pt, insertion.linked_code).blueprint @result = ValuePoint.new("code", new_tree) end |
#preconditions? ⇒ Boolean
was CODE.INSERT in Push3
13 14 15 16 |
# File 'lib/instructions/code/code_replace_nth_point.rb', line 13 def preconditions? needs :int, 1 needs :code, 2 end |
#setup ⇒ Object
17 18 19 20 21 |
# File 'lib/instructions/code/code_replace_nth_point.rb', line 17 def setup @where = @context.pop_value(:int) @accept = @context.pop_value(:code) @insert = @context.pop_value(:code) end |