Class: CodePositionInstruction

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

Overview

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

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



30
31
32
# File 'lib/instructions/code/code_position.rb', line 30

def cleanup
  pushes :int, @result
end

#deriveObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/instructions/code/code_position.rb', line 20

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

#preconditions?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/instructions/code/code_position.rb', line 13

def preconditions?
  needs :code, 2
end

#setupObject



16
17
18
19
# File 'lib/instructions/code/code_position.rb', line 16

def setup
  @arg2 = @context.pop_value(:code)
  @arg1 = @context.pop_value(:code)
end