package expression;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @author Doschennikov Nikita (me@fymio.us)
 */
public class Power extends AbstractBinaryOperation {
    public Power(AbstractExpression l, AbstractExpression r) { super(l, r); }

    @Override protected String  getOperator()  { return "**"; }
    @Override protected int     getPriority()  { return 3; }
    @Override protected boolean isRightAssoc() { return true; }

    @Override
    protected int applyInt(int base, int exp) {
        if (exp < 0) throw new ArithmeticException("negative exponent");
        if (base == 0 && exp == 0) throw new ArithmeticException("zero to the power of zero");
        if (exp == 0) return 1;
        if (base == 0) return 0;
        int result = 1;
        int b = base;
        int e = exp;
        while (e > 0) {
            if ((e & 1) == 1) {
                result = checkedMul(result, b);
            }
            if (e > 1) b = checkedMul(b, b);
            e >>= 1;
        }
        return result;
    }

    private static int checkedMul(int a, int b) {
        if (a == 0 || b == 0) return 0;
        if (a == Integer.MIN_VALUE && b == -1) throw new OverflowException("power");
        if (b == Integer.MIN_VALUE && a == -1) throw new OverflowException("power");
        int result = a * b;
        if (result / a != b) throw new OverflowException("power");
        return result;
    }

    @Override protected BigInteger applyBi(BigInteger a, BigInteger b) { return a.pow(b.intValueExact()); }
    @Override protected BigDecimal applyBd(BigDecimal a, BigDecimal b) { return a.pow(b.intValueExact()); }
}