WC_Eval_Math::pfx()private staticWC 1.0

Evaluate postfix notation.

Метод класса: WC_Eval_Math{}

Хуков нет.

Возвращает

Разное.

Использование

$result = WC_Eval_Math::pfx( $tokens, $vars );
$tokens(разное) (обязательный)
-
$vars(массив)
-
По умолчанию: array()

Код WC_Eval_Math::pfx() WC 8.7.0

private static function pfx( $tokens, $vars = array() ) {
	if ( false == $tokens ) {
		return false;
	}
	$stack = new WC_Eval_Math_Stack;

	foreach ( $tokens as $token ) { // nice and easy
		// if the token is a binary operator, pop two values off the stack, do the operation, and push the result back on
		if ( in_array( $token, array( '+', '-', '*', '/', '^' ) ) ) {
			if ( is_null( $op2 = $stack->pop() ) ) {
				return self::trigger( "internal error" );
			}
			if ( is_null( $op1 = $stack->pop() ) ) {
				return self::trigger( "internal error" );
			}
			switch ( $token ) {
				case '+':
					$stack->push( $op1 + $op2 );
					break;
				case '-':
					$stack->push( $op1 - $op2 );
					break;
				case '*':
					$stack->push( $op1 * $op2 );
					break;
				case '/':
					if ( 0 == $op2 ) {
						return self::trigger( 'division by zero' );
					}
					$stack->push( $op1 / $op2 );
					break;
				case '^':
					$stack->push( pow( $op1, $op2 ) );
					break;
			}
			// if the token is a unary operator, pop one value off the stack, do the operation, and push it back on
		} elseif ( '_' === $token ) {
			$stack->push( -1 * $stack->pop() );
			// if the token is a function, pop arguments off the stack, hand them to the function, and push the result back on
		} elseif ( ! preg_match( "/^([a-z]\w*)\($/", $token, $matches ) ) {
			if ( is_numeric( $token ) ) {
				$stack->push( $token );
			} elseif ( array_key_exists( $token, self::$v ) ) {
				$stack->push( self::$v[ $token ] );
			} elseif ( array_key_exists( $token, $vars ) ) {
				$stack->push( $vars[ $token ] );
			} else {
				return self::trigger( "undefined variable '$token'" );
			}
		}
	}
	// when we're out of tokens, the stack should have a single element, the final result
	if ( 1 != $stack->count ) {
		return self::trigger( "internal error" );
	}
	return $stack->pop();
}