Sha256: 92c134e3f99fb920d0db962b4413b33dca5b29488b4a31ea81a7d2be5b13c32c

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

# pops the top two +:code+ items ("host" and "target"), and one +:int+ item (the "index");
# pushes a new +:code+ item, which is the +index+th program point of the +host+
# is replaced by the +target+ (as a subtree)
#
# note: order of arguments is important; the top +:code+ item is the "target" argument
#
# *needs:* 2 +:code+, 1 +:int+
#
# *pushes:* 1 +:code+
#

class CodeReplaceNthPointInstruction < Instruction # was CODE.INSERT in Push3
  def preconditions?
    needs :int, 1
    needs :code, 2
  end
  def setup
    @where = @context.pop_value(:int)
    @accept = @context.pop_value(:code)
    @insert = @context.pop_value(:code)
  end
  def derive
    acceptor = NudgeProgram.new(@accept)
    raise InstructionMethodError,
      "#{self.class.to_nudgecode} cannot work with unparseable code" unless acceptor.parses?
    insertion = NudgeProgram.new(@insert)
    raise InstructionMethodError,
      "#{self.class.to_nudgecode} cannot work with unparseable code" unless insertion.parses?
    scale = acceptor.points
    which_pt = if @where < 1 || @where > scale
      (@where % acceptor.points) + 1
    else
      @where
    end
    new_tree = acceptor.replace_point(which_pt, insertion.linked_code).blueprint
    @result = ValuePoint.new("code", new_tree)
  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_replace_nth_point.rb
nudge-0.2.8 lib/instructions/code/code_replace_nth_point.rb
nudge-0.2.7 lib/instructions/code/code_replace_nth_point.rb