Class: CodeDoCountInstruction

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

Overview

Pops one “count” item from the :int stack, and one item from the :code stack. The net effect of the instruction (unless interfered with by another operation) is to evaluate the :code item “count” times.

If the count is negative, an :error item will be pushed to the :error stack. Otherwise, a ValuePoint containing the following “macro” is created and pushed onto the :exec stack:

block {
  value «int»
  value «int»
  do exec_do_range
  popped item
}
«int» 0
«int» count - 1

where popped_item is the code from the :code stack, and count - 1 is a decrement of the “count” value.

needs: ExecDoRangeInstruction must be active in the context for this to work; needs 1 :int and 1 :code item

pushes: it’s complicated…

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



42
43
44
45
46
# File 'lib/instructions/code/code_do_count.rb', line 42

def cleanup
  recursor = CodeblockPoint.new([ValuePoint.new("int",0), @one_less,
    InstructionPoint.new("exec_do_range"),@codeblock])
  pushes :exec, recursor
end

#deriveObject



37
38
39
40
# File 'lib/instructions/code/code_do_count.rb', line 37

def derive
  @codeblock = NudgeProgram.new(@code).linked_code
  @one_less = ValuePoint.new("int",@destination.value-1)
end

#preconditions?Boolean

Returns:

  • (Boolean)


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

def preconditions?
  needs ExecDoRangeInstruction
  needs :code, 1
  needs :int, 1
end

#setupObject



30
31
32
33
34
35
# File 'lib/instructions/code/code_do_count.rb', line 30

def setup
  @destination = @context.pop(:int)
  @code = @context.pop_value(:code)
  raise InstructionMethodError,
    "#{self.class.to_nudgecode} needs a positive argument" if @destination.value < 1
end