package org.nfunk.jep.function;

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

public class Add extends PostfixMathCommand {
  public void run(Stack paramStack) throws ParseException {
    checkStack(paramStack);
    Object object = paramStack.pop();
    for (byte b = 1; b < this.curNumberOfParameters; b++) {
      Object object1 = paramStack.pop();
      object = add(object1, object);
    } 
    paramStack.push(object);
  }
  
  public Object add(Object paramObject1, Object paramObject2) throws ParseException {
    if (paramObject1 instanceof Complex) {
      if (paramObject2 instanceof Complex)
        return add((Complex)paramObject1, (Complex)paramObject2); 
      if (paramObject2 instanceof Number)
        return add((Complex)paramObject1, (Number)paramObject2); 
    } else if (paramObject1 instanceof Number) {
      if (paramObject2 instanceof Complex)
        return add((Complex)paramObject2, (Number)paramObject1); 
      if (paramObject2 instanceof Number)
        return add((Number)paramObject1, (Number)paramObject2); 
    } else if (paramObject1 instanceof String && paramObject2 instanceof String) {
      return (String)paramObject1 + (String)paramObject2;
    } 
    throw new ParseException("Invalid parameter type");
  }
  
  public Double add(Number paramNumber1, Number paramNumber2) {
    return new Double(paramNumber1.doubleValue() + paramNumber2.doubleValue());
  }
  
  public Complex add(Complex paramComplex1, Complex paramComplex2) {
    return new Complex(paramComplex1.re() + paramComplex2.re(), paramComplex1.im() + paramComplex2.im());
  }
  
  public Complex add(Complex paramComplex, Number paramNumber) {
    return new Complex(paramComplex.re() + paramNumber.doubleValue(), paramComplex.im());
  }
}
