Class: CodeDoTimesInstruction

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

Overview

Again, the original description of this instruction appears to be confusing “CODE.DO*RANGE” with “EXEC.DO*RANGE”; I’ve made the adjustment to the code so that the behavior is as described, not the implementation.

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



28
29
30
31
32
33
34
35
36
37
# File 'lib/instructions/code/code_do_times.rb', line 28

def cleanup
  if @finished
    pushes :exec, @codeblock
  else
    recursor = CodeblockPoint.new([@new_counter, @destination,
      InstructionPoint.new("exec_do_times"),@codeblock])
    pushes :exec, recursor
    pushes :exec, @codeblock
  end
end

#deriveObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/instructions/code/code_do_times.rb', line 16

def derive
  @finished = false
  if @counter.value == @destination.value
    @finished = true
  elsif @counter.value < @destination.value
    @new_counter = ValuePoint.new("int", @counter.value + 1)
  else
    @new_counter = ValuePoint.new("int", @counter.value - 1)
  end
  @codeblock = NudgeProgram.new(@code).linked_code
end

#preconditions?Boolean

Returns:

  • (Boolean)


4
5
6
7
8
# File 'lib/instructions/code/code_do_times.rb', line 4

def preconditions?
  needs ExecDoTimesInstruction
  needs :code, 1
  needs :int, 2
end

#setupObject



10
11
12
13
14
# File 'lib/instructions/code/code_do_times.rb', line 10

def setup
  @destination = @context.pop(:int)
  @counter = @context.pop(:int)
  @code = @context.pop_value(:code)
end