Sha256: 04fea4da5b1d0219cdf4a95ff4444a018898e2e73fc3be52705fa9ae0dba0a59

Contents?: true

Size: 990 Bytes

Versions: 3

Compression:

Stored size: 990 Bytes

Contents

# 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+
#

class CodeNthPointInstruction < Instruction # was CODE.EXTRACT in Push3
  def preconditions?
    needs :int, 1
    needs :code, 1
  end
  
  def setup
    @arg1 = @context.pop_value(:int)
    @arg2 = @context.pop_value(:code)
  end
  
  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
  
  def cleanup
    pushes :code, @result
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nudge-0.2.9 lib/instructions/code/code_nth_point.rb
nudge-0.2.8 lib/instructions/code/code_nth_point.rb
nudge-0.2.7 lib/instructions/code/code_nth_point.rb