Sha256: ef8aebbd8c41401c38915d13def55bf5793b7ee06763a82d0566e5c2fe52d4dc

Contents?: true

Size: 911 Bytes

Versions: 3

Compression:

Stored size: 911 Bytes

Contents

# pops the top two +:code+ items;
# pushes a new +:int+ item, with value equal to the program point number in the first argument
# where the second argument appears as a subtree (or 0 otherwise)
#
# note: order of arguments is important; the top stack item is the second argument
#
# *needs:* 2 +:code+
#
# *pushes:* 1 +:int+
#

class CodePositionInstruction < Instruction
  def preconditions?
    needs :code, 2
  end
  def setup
    @arg2 = @context.pop_value(:code)
    @arg1 = @context.pop_value(:code)
  end
  def derive
    looking_for_this = NudgeProgram.new(@arg1).linked_code
    inside_this = NudgeProgram.new(@arg2).linked_code
    if inside_this && looking_for_this
      index = inside_this.find_index {|point| point.blueprint == looking_for_this.blueprint} || -2
    else
      index = -2
    end
    @result = ValuePoint.new("int", index+1)
  end
  def cleanup
    pushes :int, @result
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nudge-0.2.9 lib/instructions/code/code_position.rb
nudge-0.2.8 lib/instructions/code/code_position.rb
nudge-0.2.7 lib/instructions/code/code_position.rb