/** * 弹出(组装好的)操作符 * * @return 操作符 */ Operator popOperator() { if (operatorStack.isEmpty()) throw I18nExceptionFactory.createIllegalStateException( "ExpressionReducer.miss.left.parenthesis"); Operator operator = (Operator) operatorStack.pop(); // 从参数栈弹出所需参数组装操作表达式 if (operator instanceof BinaryOperator) { BinaryOperator binaryOperator = (BinaryOperator) operator; if (parameterStack.isEmpty()) throw I18nExceptionFactory.createIllegalStateException( "ExpressionReducer.operator.miss.parameter"); Expression rightParameter = (Expression) parameterStack.pop(); // 调整参数顺序 if (parameterStack.isEmpty()) throw I18nExceptionFactory.createIllegalStateException( "ExpressionReducer.operator.miss.parameter"); Expression leftParameter = (Expression) parameterStack.pop(); parameterStack.push( populateBinaryOperator( (BinaryOperatorImpl) binaryOperator, leftParameter, rightParameter)); // 将组装好的表达式作为参数压入 } else if (operator instanceof UnaryOperator) { UnaryOperator unaryOperator = (UnaryOperator) operator; if (parameterStack.isEmpty()) throw I18nExceptionFactory.createIllegalStateException( "ExpressionReducer.operator.miss.parameter"); Expression parameter = (Expression) parameterStack.pop(); parameterStack.push( populateUnaryOperator((UnaryOperatorImpl) unaryOperator, parameter)); // 将组装好的表达式作为参数压入 } return operator; }
/** * 弹出(最后的)结果 * * @return 结果 */ Expression popResult() { while (!operatorStack.isEmpty()) { Operator operator = popOperator(); // 弹出栈中所有操作符 if (operator == Parenthesis.LEFT_PARENTHESIS) throw I18nExceptionFactory.createIllegalStateException( "ExpressionReducer.miss.right.parenthesis"); } Expression result = (Expression) parameterStack.pop(); Assert.assertTrue( parameterStack.isEmpty(), "ExpressionReducer.operator.miss.parameter"); // 后验条件 return result; }