Sha256: 27b0b11fc9c636c6d1575584c2ea3b31c133d2f570a877e21dd27d9824b1ae01

Contents?: true

Size: 761 Bytes

Versions: 3

Compression:

Stored size: 761 Bytes

Contents

# pops the top 2 items of the +:int+ stack;
# pushes a ValuePoint with their (integer) quotient onto the +:int+ stack
#
# note: the top item is the denominator, the second item is the numerator
#
# note: will push an +:error+ ValuePoint instead of an +:int+ if the numerator is 0
#
# *needs:* 2 +:int+
#
# *pushes:* 1 +:int+
#

class IntDivideInstruction < Instruction
  def preconditions?
    needs :int, 2
  end
  def setup
    @arg2 = @context.pop_value(:int)
    @arg1 = @context.pop_value(:int)
  end
  def derive
    if @arg2 != 0
      @quotient = @arg1 / @arg2
      @result = ValuePoint.new("int", @quotient)
    else
      raise InstructionMethodError, "#{self.class} cannot divide by 0"
    end
  end
  def cleanup
    pushes :int, @result
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nudge-0.2.9 lib/instructions/int/int_divide.rb
nudge-0.2.8 lib/instructions/int/int_divide.rb
nudge-0.2.7 lib/instructions/int/int_divide.rb