Class: CodeNthCdrInstruction

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

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



27
28
29
# File 'lib/instructions/code/code_nth_cdr.rb', line 27

def cleanup
  pushes :code, @result
end

#deriveObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/instructions/code/code_nth_cdr.rb', line 10

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 = CodeblockPoint.new([])
    end
  else
    new_tree = @arg1 > 0 ? CodeblockPoint.new([tree.linked_code]) : CodeblockPoint.new([])
  end
  @result = ValuePoint.new("code", new_tree.blueprint)
end

#preconditions?Boolean

Returns:

  • (Boolean)


2
3
4
5
# File 'lib/instructions/code/code_nth_cdr.rb', line 2

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

#setupObject



6
7
8
9
# File 'lib/instructions/code/code_nth_cdr.rb', line 6

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