package org.nfunk.jep.function;

import java.util.Stack;
import org.nfunk.jep.ParseException;

public class Sum extends PostfixMathCommand {
  private Add addFun = new Add();
  
  public void run(Stack paramStack) throws ParseException {
    Number number2;
    if (null == paramStack)
      throw new ParseException("Stack argument null"); 
    Number number1 = (Number)paramStack.pop();
    if (number1 instanceof Number) {
      number2 = number1;
    } else {
      throw new ParseException("Invalid parameter type");
    } 
    for (byte b = 1; b < this.curNumberOfParameters; b++) {
      number1 = (Number)paramStack.pop();
      if (number1 instanceof Number) {
        number2 = this.addFun.add(number1, number2);
      } else {
        throw new ParseException("Invalid parameter type");
      } 
      b++;
    } 
    paramStack.push(number2);
  }
}
