Class: CodeNthPointInstruction

Inherits:
Instruction show all
Defined in:
lib/instructions/code/code_nth_point.rb

Overview

pops the top :code item and :int item (“N”); pushes a new :code item containing the Nth program point of the :code

If the :code is not a block, it’s replaced intact; if the :code value cannot be parsed, an :error is pushed instead of a :code item; otherwise, the index is chosen as N modulo the length of the number of program points.

needs: 1 :code and 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



33
34
35
# File 'lib/instructions/code/code_nth_point.rb', line 33

def cleanup
  pushes :code, @result
end

#deriveObject



24
25
26
27
28
29
30
31
# File 'lib/instructions/code/code_nth_point.rb', line 24

def derive
  tree = NudgeProgram.new(@arg2)
  tree_size = tree.points
  raise InstructionMethodError, "#{self.class} divied by zero" if tree_size < 1
  which = (@arg1-1) % tree_size + 1
  pt = tree[which]
  @result = ValuePoint.new("code", pt.blueprint)
end

#preconditions?Boolean

was CODE.EXTRACT in Push3

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/instructions/code/code_nth_point.rb', line 14

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

#setupObject



19
20
21
22
# File 'lib/instructions/code/code_nth_point.rb', line 19

def setup
  @arg1 = @context.pop_value(:int)
  @arg2 = @context.pop_value(:code)
end