Class: CodeContainerInstruction

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

Overview

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

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



44
45
46
# File 'lib/instructions/code/code_container.rb', line 44

def cleanup
  pushes :code, @result
end

#deriveObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/instructions/code/code_container.rb', line 30

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

#preconditions?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/instructions/code/code_container.rb', line 21

def preconditions?
  needs :code, 2
end

#setupObject



25
26
27
28
# File 'lib/instructions/code/code_container.rb', line 25

def setup
  @searching_in_this = @context.pop_value(:code)
  @searching_for_this = @context.pop_value(:code)
end