Class: CodeDefineInstruction

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

Overview

pops the top item of the :code stack and the :name stack; if the name string is not a bound variable (as opposed to a local name), it binds the name to a new ValuePoint the :code value.

needs: 1 :code, 1 :name

pushes: nothing

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
# File 'lib/instructions/code/code_define.rb', line 28

def cleanup
end

#deriveObject



19
20
21
22
23
24
25
26
27
# File 'lib/instructions/code/code_define.rb', line 19

def derive
  if @context.variables.keys.include?(@ref)
    raise InstructionMethodError, "#{self.class.to_nudgecode} cannot change the value of a variable"
  else
    new_value = NudgeProgram.new(@code).linked_code
    raise InstructionMethodError, "#{self.class.to_nudgecode} cannot parse '#{@code}'" if new_value.is_a?(NilPoint)
    @context.names[@ref] = new_value
  end
end

#preconditions?Boolean

Returns:

  • (Boolean)


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

def preconditions?
  needs :code, 1
  needs :name, 1
end

#setupObject



15
16
17
18
# File 'lib/instructions/code/code_define.rb', line 15

def setup
  @ref = @context.pop(:name).name
  @code = @context.pop_value(:code)
end