Class: CodeReplaceNthPointInstruction

Inherits:
Instruction show all
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

#context

Instance Method Summary collapse

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

#cleanupObject



38
39
40
# File 'lib/instructions/code/code_replace_nth_point.rb', line 38

def cleanup
  pushes :code, @result
end

#deriveObject



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

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/instructions/code/code_replace_nth_point.rb', line 13

def preconditions?
  needs :int, 1
  needs :code, 2
end

#setupObject



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