Sha256: 7b17d72aebaa36e58936cf069e5b6c3ac54ca8e742953f9f3d2d2f14b76698f6

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

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

class CodeNthCdrInstruction < Instruction
  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)
    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
  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_cdr.rb
nudge-0.2.8 lib/instructions/code/code_nth_cdr.rb
nudge-0.2.7 lib/instructions/code/code_nth_cdr.rb