Package maelstrom.funge.interpreter.operator

Source Code of maelstrom.funge.interpreter.operator.ArithmeticOperator$Divide

package maelstrom.funge.interpreter.operator;

import maelstrom.funge.interpreter.Funge;
import maelstrom.funge.interpreter.stack.Stack;


public abstract class ArithmeticOperator {

  // Adds the two top most numbers on the stack
  public static class Add implements Operator {

    public void perform(Funge funge) {
      Stack stack = funge.getStackStack().getStack();
      stack.push((stack.pop() + stack.pop()));
    }

    public String getDescription() {
      return "Pops b, a off the stack. Pushes a + b";
    }
  }

  // Subtracts the two top most numbers on the stack
  public static class Subtract implements Operator {

    public void perform(Funge funge) {
      Stack stack = funge.getStackStack().getStack();
      stack.push(0 - (stack.pop() - stack.pop()));
    }

    public String getDescription() {
      return "Pops b, a off the stack. Pushes a - b";
    }
  }

  // Adds the two top most numbers on the stack
  public static class Multiply implements Operator {

    public void perform(Funge funge) {
      Stack stack = funge.getStackStack().getStack();
      stack.push((stack.pop() * stack.pop()));
    }

    public String getDescription() {
      return "Pops b, a off the stack. Pushes a * b";
    }
  }

  // Divides the two top most numbers on the stack
  public static class Divide implements Operator {

    public void perform(Funge funge) {
      Stack stack = funge.getStackStack().getStack();

      long b = stack.pop();
      long a = stack.pop();

      if (b == 0) {
        stack.push(0);
      } else {
        stack.push(a / b);
      }
    }

    public String getDescription() {
      return "Pops b, a off the stack. Pushes a / b";
    }
  }

  // Divides the two top most numbers on the stack
  public static class Modulus implements Operator {

    public void perform(Funge funge) {
      Stack stack = funge.getStackStack().getStack();

      long b = stack.pop();
      long a = stack.pop();

      if (b == 0) {
        stack.push(0);
      } else {
        stack.push(a % b);
      }
    }

    public String getDescription() {
      return "Pops b, a off the stack. Pushes a % b";
    }
  }
}
TOP

Related Classes of maelstrom.funge.interpreter.operator.ArithmeticOperator$Divide

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.