Class: CodeNthInstruction

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

Overview

pops the top :code item and :int item (“N”); pushes a new :code item containing the Nth backbone element of the :code, if it’s a block

If the :code is not a block, it’s replaced intact; if the :code is an empty block, an :error is pushed instead of a :code item; otherwise, the index is chosen as N modulo the length of the block’s backbone.

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.rb', line 33

def cleanup
  pushes :code, @result
end

#deriveObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/instructions/code/code_nth.rb', line 22

def derive
  tree = NudgeProgram.new(@arg2)
  if tree.linked_code.kind_of?(CodeblockPoint)
    pts = tree[1].contents.length
    raise InstructionMethodError, "#{self.class} can't work on empty blocks" if pts < 1
    val = tree[1].contents[@arg1 % pts]
  else
    val = tree
  end
  @result = ValuePoint.new("code", val.blueprint)
end

#preconditions?Boolean

Returns:

  • (Boolean)


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

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

#setupObject



18
19
20
21
# File 'lib/instructions/code/code_nth.rb', line 18

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