/** returns the OperationEval concrete impl instance corresponding to the supplied operationPtg */ public static ValueEval evaluate( OperationPtg ptg, ValueEval[] args, OperationEvaluationContext ec) { if (ptg == null) { throw new IllegalArgumentException("ptg must not be null"); } Function result = _instancesByPtgClass.get(ptg); if (result != null) { return result.evaluate(args, ec.getRowIndex(), (short) ec.getColumnIndex()); } if (ptg instanceof AbstractFunctionPtg) { AbstractFunctionPtg fptg = (AbstractFunctionPtg) ptg; int functionIndex = fptg.getFunctionIndex(); switch (functionIndex) { case FunctionMetadataRegistry.FUNCTION_INDEX_INDIRECT: return Indirect.instance.evaluate(args, ec); case FunctionMetadataRegistry.FUNCTION_INDEX_EXTERNAL: return UserDefinedFunction.instance.evaluate(args, ec); } return FunctionEval.getBasicFunction(functionIndex) .evaluate(args, ec.getRowIndex(), (short) ec.getColumnIndex()); } throw new RuntimeException("Unexpected operation ptg class (" + ptg.getClass().getName() + ")"); }
// visibility raised for testing /* package */ ValueEval evaluateFormula(OperationEvaluationContext ec, Ptg[] ptgs) { Stack<ValueEval> stack = new Stack<ValueEval>(); for (int i = 0, iSize = ptgs.length; i < iSize; i++) { // since we don't know how to handle these yet :( Ptg ptg = ptgs[i]; if (ptg instanceof AttrPtg) { AttrPtg attrPtg = (AttrPtg) ptg; if (attrPtg.isSum()) { // Excel prefers to encode 'SUM()' as a tAttr token, but this evaluator // expects the equivalent function token byte nArgs = 1; // tAttrSum always has 1 parameter ptg = new FuncVarPtg("SUM", nArgs); } } if (ptg instanceof ControlPtg) { // skip Parentheses, Attr, etc continue; } if (ptg instanceof MemFuncPtg) { // can ignore, rest of tokens for this expression are in OK RPN order continue; } if (ptg instanceof MemErrPtg) { continue; } ValueEval opResult; if (ptg instanceof OperationPtg) { OperationPtg optg = (OperationPtg) ptg; if (optg instanceof UnionPtg) { continue; } OperationEval operation = OperationEvaluatorFactory.create(optg); int numops = operation.getNumberOfOperands(); ValueEval[] ops = new ValueEval[numops]; // storing the ops in reverse order since they are popping for (int j = numops - 1; j >= 0; j--) { ValueEval p = stack.pop(); ops[j] = p; } // logDebug("invoke " + operation + " (nAgs=" + numops + ")"); opResult = operation.evaluate(ops, ec); if (opResult == MissingArgEval.instance) { opResult = BlankEval.INSTANCE; } } else { opResult = getEvalForPtg(ptg, ec); } if (opResult == null) { throw new RuntimeException("Evaluation result must not be null"); } // logDebug("push " + opResult); stack.push(opResult); } ValueEval value = stack.pop(); if (!stack.isEmpty()) { throw new IllegalStateException("evaluation stack not empty"); } value = dereferenceValue(value, ec.getRowIndex(), ec.getColumnIndex()); if (value == BlankEval.INSTANCE) { // Note Excel behaviour here. A blank final final value is converted to zero. return NumberEval.ZERO; // Formulas _never_ evaluate to blank. If a formula appears to have evaluated to // blank, the actual value is empty string. This can be verified with ISBLANK(). } return value; }