Sha256: 21b3c78527d19b44dc1360209cd20eebbcab882a61a83bc29807eade80438a1e

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

# pops the top 2 items from the +:code+ stack;
# pushes a new +:code+ item containing a block, which is a copy of the first block in the first argument
# which contains as a child an exact copy of the second argument
#
# note: order matters, and the top stack item is the second argument, the second stack item is the first
#
# For example:
#   first_argument , second_argument -> code_container result
#   [a, b] , z -> []
#   [a,b] , a -> [a,b]
#   [[a,b],c] , a -> [a,b]
#   [a,[b,[c,d]]] , b -> [b,[c,d]]
#   [a,b] , [a,b] -> [a,b]
#
# *needs:* 2 +:code+
#
# *pushes:* 1 +:code+
#

class CodeContainerInstruction < Instruction
  def preconditions?
    needs :code, 2
  end
  
  def setup
    @searching_in_this = @context.pop_value(:code)
    @searching_for_this = @context.pop_value(:code)
  end
  
  def derive
    needle = NudgeProgram.new(@searching_for_this).linked_code.blueprint
    haystack_program = NudgeProgram.new(@searching_in_this)
    haystack = haystack_program.linked_code
    
    if needle == haystack.blueprint
      result_value = "block {}"
    else
      where = haystack.find_index {|point| point.blueprint == needle}
      result_value = where.nil? ? "block {}" : haystack_program[where].blueprint
    end
    @result = ValuePoint.new("code", result_value)
  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_container.rb
nudge-0.2.8 lib/instructions/code/code_container.rb
nudge-0.2.7 lib/instructions/code/code_container.rb