src/main/java/org/roisoleil/litexp/Expression.java
/**
	 * Evaluates the expression.
	 * 
	 * @param stripTrailingZeros
	 *            If set to <code>true</code> trailing zeros in the result are
	 *            stripped.
	 * 
	 * @return The result of the expression.
	 */
Evaluates the expression.

@param stripTrailingZeros
If set to true trailing zeros in the result are
stripped.

@return The result of the expression.
Evaluates the expression . @param stripTrailingZeros If set to true trailing zeros in the result are stripped . @return The result of the expression .
public Operand eval() {
		Deque<Operand> stack = new ArrayDeque<>();
		Operand result = null;
		for (final Token token : getRPN()) {
			switch (token.type) {
			case UNARY_OPERATOR: {
				Operand value = stack.pop();
				result = unaryOperators.get(token.surface).eval(value);
				stack.push(result);
				break;
			}
			case OPERATOR:
				Operand rightValue = stack.pop();
				Operand leftValue = stack.pop();
				result = operators.get(token.surface).eval(leftValue, rightValue);
				stack.push(result);
				break;
			case VARIABLE:
				result = operandFactory.createOperand(token, () -> {
					if (!variables.containsKey(token.surface)) {
						throw new LitExpException("Unknown variable: " + token);
					}
					return variables.get(token.surface).getValue();
				});
				stack.push(result);
				break;
			case FUNCTION:
				Function function = functions.get(token.surface.toUpperCase(Locale.ROOT));
				List<Operand> arguments = new ArrayList<>(
						function.isVariableArguments() ? 0 : function.getNumberArguments());
				while (!stack.isEmpty() && stack.peek() != PARAMS_START) {
					arguments.add(0, stack.pop());
				}
				if (stack.peek() == PARAMS_START) {
					stack.pop();
				}
				result = function.eval(arguments);
				stack.push(result);
				break;
			case OPEN_BRACKET:
				stack.push(PARAMS_START);
				break;
			case NUMBER:
				result = operandFactory.createOperand(token, () -> token.surface);
				stack.push(result);
				break;
			case STRING:
				result = operandFactory.createOperand(token, () -> token.surface);
				stack.push(result);
				break;
			default:
				throw new LitExpException(
						"Unexpected token '" + token.surface + "' at character position " + token.end);
			}
		}
		return stack.pop();
	}
public Operand eval ( ) { Deque < Operand > stack = new ArrayDeque < > ( ) ; Operand result = null ; for ( final Token token : getRPN ( ) ) { switch ( token . type ) { case UNARY_OPERATOR : { Operand value = stack . pop ( ) ; result = unaryOperators . get ( token . surface ) . eval ( value ) ; stack . push ( result ) ; break ; } case OPERATOR : Operand rightValue = stack . pop ( ) ; Operand leftValue = stack . pop ( ) ; result = operators . get ( token . surface ) . eval ( leftValue , rightValue ) ; stack . push ( result ) ; break ; case VARIABLE : result = operandFactory . createOperand ( token , ( ) -> { if ( ! variables . containsKey ( token . surface ) ) { throw new LitExpException ( "Unknown variable: " + token ) ; } return variables . get ( token . surface ) . getValue ( ) ; } ) ; stack . push ( result ) ; break ; case FUNCTION : Function function = functions . get ( token . surface . toUpperCase ( Locale . ROOT ) ) ; List < Operand > arguments = new ArrayList < > ( function . isVariableArguments ( ) ? 0 : function . getNumberArguments ( ) ) ; while ( ! stack . isEmpty ( ) && stack . peek ( ) != PARAMS_START ) { arguments . add ( 0 , stack . pop ( ) ) ; } if ( stack . peek ( ) == PARAMS_START ) { stack . pop ( ) ; } result = function . eval ( arguments ) ; stack . push ( result ) ; break ; case OPEN_BRACKET : stack . push ( PARAMS_START ) ; break ; case NUMBER : result = operandFactory . createOperand ( token , ( ) -> token . surface ) ; stack . push ( result ) ; break ; case STRING : result = operandFactory . createOperand ( token , ( ) -> token . surface ) ; stack . push ( result ) ; break ; default : throw new LitExpException ( "Unexpected token '" + token . surface + "' at character position " + token . end ) ; } } return stack . pop ( ) ; }
Evaluates the expression.
Evaluates the expression .