Class: Instruction

Inherits:
Object
  • Object
show all
Defined in:
lib/instructions/infrastructure.rb

Direct Known Subclasses

BoolAndInstruction, BoolDefineInstruction, BoolDepthInstruction, BoolDuplicateInstruction, BoolEqualQInstruction, BoolFlushInstruction, BoolFromFloatInstruction, BoolFromIntInstruction, BoolNotInstruction, BoolOrInstruction, BoolPopInstruction, BoolRandomInstruction, BoolRotateInstruction, BoolShoveInstruction, BoolSwapInstruction, BoolXorInstruction, BoolYankInstruction, BoolYankdupInstruction, CodeAtomQInstruction, CodeBackbonePointsInstruction, CodeCarInstruction, CodeCdrInstruction, CodeConcatenateInstruction, CodeConsInstruction, CodeContainerInstruction, CodeContainsQInstruction, CodeDefineInstruction, CodeDepthInstruction, CodeDiscrepancyInstruction, CodeDoCountInstruction, CodeDoRangeInstruction, CodeDoTimesInstruction, CodeDuplicateInstruction, CodeEqualQInstruction, CodeExecuteInstruction, CodeExecuteThenPopInstruction, CodeFlushInstruction, CodeFromBoolInstruction, CodeFromFloatInstruction, CodeFromIntInstruction, CodeFromNameInstruction, CodeGsubInstruction, CodeIfInstruction, CodeInstructionsInstruction, CodeListInstruction, CodeMemberQInstruction, CodeNameLookupInstruction, CodeNoopInstruction, CodeNthCdrInstruction, CodeNthInstruction, CodeNthPointInstruction, CodeNullQInstruction, CodeParsesQInstruction, CodePointsInstruction, CodePopInstruction, CodePositionInstruction, CodeQuoteInstruction, CodeReplaceNthPointInstruction, CodeRotateInstruction, CodeShoveInstruction, CodeSwapInstruction, CodeYankInstruction, CodeYankdupInstruction, ExecDefineInstruction, ExecDepthInstruction, ExecDoCountInstruction, ExecDoRangeInstruction, ExecDoTimesInstruction, ExecDuplicateInstruction, ExecEqualQInstruction, ExecFlushInstruction, ExecIfInstruction, ExecKInstruction, ExecPopInstruction, ExecRotateInstruction, ExecSInstruction, ExecShoveInstruction, ExecSwapInstruction, ExecYInstruction, ExecYankInstruction, ExecYankdupInstruction, FloatAbsInstruction, FloatAddInstruction, FloatCosineInstruction, FloatDefineInstruction, FloatDepthInstruction, FloatDivideInstruction, FloatDuplicateInstruction, FloatEqualQInstruction, FloatFlushInstruction, FloatFromBoolInstruction, FloatFromIntInstruction, FloatFromProportionInstruction, FloatGreaterThanQInstruction, FloatIfInstruction, FloatLessThanQInstruction, FloatMaxInstruction, FloatMinInstruction, FloatModuloInstruction, FloatMultiplyInstruction, FloatNegativeInstruction, FloatPopInstruction, FloatPowerInstruction, FloatRandomInstruction, FloatRotateInstruction, FloatShoveInstruction, FloatSineInstruction, FloatSqrtInstruction, FloatSubtractInstruction, FloatSwapInstruction, FloatTangentInstruction, FloatYankInstruction, FloatYankdupInstruction, IntAbsInstruction, IntAddInstruction, IntDefineInstruction, IntDepthInstruction, IntDivideInstruction, IntDuplicateInstruction, IntEqualQInstruction, IntFlushInstruction, IntFromBoolInstruction, IntFromFloatInstruction, IntGreaterThanQInstruction, IntIfInstruction, IntLessThanQInstruction, IntMaxInstruction, IntMinInstruction, IntModuloInstruction, IntMultiplyInstruction, IntNegativeInstruction, IntPopInstruction, IntPowerInstruction, IntRandomInstruction, IntRotateInstruction, IntShoveInstruction, IntSubtractInstruction, IntSwapInstruction, IntYankInstruction, IntYankdupInstruction, NameDepthInstruction, NameDisableLookupInstruction, NameDuplicateInstruction, NameEqualQInstruction, NameFlushInstruction, NameNextInstruction, NamePopInstruction, NameRandomBoundInstruction, NameRotateInstruction, NameShoveInstruction, NameSwapInstruction, NameUnbindInstruction, NameYankInstruction, NameYankdupInstruction, ProportionBoundedAddInstruction, ProportionBoundedDivideInstruction, ProportionBoundedSubtractInstruction, ProportionFromFloatInstruction, ProportionMaxInstruction, ProportionMinInstruction, ProportionMultiplyInstruction

Defined Under Namespace

Classes: CodeOversizeError, InstructionMethodError, MissingInstructionError, NaNResultError, NotEnoughStackItems

Constant Summary collapse

@@all_instructions =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Instruction

Returns a new instance of Instruction.



35
36
37
# File 'lib/instructions/infrastructure.rb', line 35

def initialize(context)
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



2
3
4
# File 'lib/instructions/infrastructure.rb', line 2

def context
  @context
end

Class Method Details

.all_instructionsObject



11
12
13
# File 'lib/instructions/infrastructure.rb', line 11

def self.all_instructions
  @@all_instructions
end

.inherited(subclass) ⇒ Object



6
7
8
9
# File 'lib/instructions/infrastructure.rb', line 6

def self.inherited(subclass)
  @@all_instructions << subclass
  super
end

.to_nudgecodeObject



15
16
17
# File 'lib/instructions/infrastructure.rb', line 15

def self.to_nudgecode
  self.to_s.slice(0..-12).underscore
end

Instance Method Details

#cleanupObject



90
91
92
# File 'lib/instructions/infrastructure.rb', line 90

def cleanup
  raise "Your Instruction class should define its own #cleanup method"
end

#deriveObject



86
87
88
# File 'lib/instructions/infrastructure.rb', line 86

def derive
  raise "Your Instruction class should define its own #derive method"
end

#goObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/instructions/infrastructure.rb', line 65

def go
  if self.preconditions?
    self.setup
    self.derive
    self.cleanup
  end
rescue NotEnoughStackItems, MissingInstructionError,
  InstructionMethodError, NaNResultError, FloatDomainError, CodeOversizeError => exc
  msg = ValuePoint.new("error", exc.message)
  pushes :error, msg
end

#needs(infrastructure, minimum = 1) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/instructions/infrastructure.rb', line 40

def needs(infrastructure, minimum = 1)
  unless infrastructure.is_a?(Symbol)
    raise(InstructionNotFoundError, "#{infrastructure.to_s} is not a known Instruction") unless
      Object.const_defined?(infrastructure.to_s)
    raise(MissingInstructionError, "#{self.class} needs #{infrastructure}") unless
      @context.instructions.include?(infrastructure)
  else
    iNeed = @context.stacks[infrastructure]
    if @context.stacks[infrastructure].depth < minimum
      raise NotEnoughStackItems,
        "Stack #{infrastructure.to_s} too small: #{self.class.to_nudgecode} needs at least #{minimum} items"
    end
  end
  return true
end

#preconditions?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/instructions/infrastructure.rb', line 78

def preconditions?
  raise "Your Instruction class should define its own #preconditions? method"
end

#pushes(stackName, literal) ⇒ Object



57
58
59
60
61
62
# File 'lib/instructions/infrastructure.rb', line 57

def pushes(stackName, literal)
  if (stackName.to_s == "code") && (literal.value.length > self.context.code_char_limit)
    raise(CodeOversizeError, ":code cannot have more than #{self.context.code_char_limit} chars")
  end
  @context.stacks[stackName].push literal
end

#setupObject



82
83
84
# File 'lib/instructions/infrastructure.rb', line 82

def setup
  raise "Your Instruction class should define its own #setup method"
end