Class: CodeNthCdrInstruction

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

Overview

pops the top :code item and :int item (ā€œNā€); pushes a new :code item, which is the result of removing the first N backbone elements of the :code

If the :code is not a block, it is wrapped in one first; if N is less than 1, the original code (or newly wrapped atom) is returned; otherwise, the number of items removed is actually the backbone length modulo N

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



39
40
41
# File 'lib/instructions/code/code_nth_cdr.rb', line 39

def cleanup
  pushes :code, @result
end

#deriveObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/instructions/code/code_nth_cdr.rb', line 22

def derive
  tree = NudgeProgram.new(@arg2)
  raise InstructionMethodError,
    "#{self.class.to_nudgecode} cannot parse the argument" unless tree.parses?
  backbone = tree.linked_code
  if backbone.kind_of?(CodeblockPoint)
    b_len = backbone.contents.length
    if b_len > 0
      new_tree = CodeblockPoint.new(backbone.contents.slice(@arg1 % b_len, b_len))
    else
      new_tree = backbone
    end
  else
    new_tree = @arg1 < 1 ? CodeblockPoint.new([tree.linked_code]) : CodeblockPoint.new([])
  end
  @result = ValuePoint.new("code", new_tree.blueprint)
end

#preconditions?Boolean

Returns:

  • (Boolean)


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

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

#setupObject



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

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