You are here

function units_mathematical_expression_create_from_postfix in Units of Measurement 7.2

Create mathematical expression object from postfix (reverse polish) notation.

Parameters

string $postfix_mathematical_expression: Mathematical expression written down in postfix (reverse polish) notation

Return value

\UnitsMathematicalExpressionWrapper In-memory representation of the provided mathematical expression

Throws

\UnitsMathematicalExpressionMalformedException

6 calls to units_mathematical_expression_create_from_postfix()
UnitsMathematicalOperatorNonLinear::decompose in ./units.module
Decompose (simplify) this mathematical expression.
units_field_display_mathematical_expression in units_field/units_field.module
Fetch expected output format from the provided display.
units_field_field_formatter_settings_form in units_field/units_field.module
Implements hook_field_formatter_settings_form().
units_field_field_settings_form in units_field/units_field.module
Implements hook_field_settings_form().
units_mathematical_expression_create_from_infix in ./units.module
Create mathematical expression object from infix notation.

... See full list

File

./units.module, line 743
Provide API for managing and converting units of measurement.

Code

function units_mathematical_expression_create_from_postfix($postfix_mathematical_expression) {
  $postfix_mathematical_expression_string = $postfix_mathematical_expression;
  $postfix_mathematical_expression = array_filter(explode(' ', $postfix_mathematical_expression), 'units_mathematical_expression_array_filter');
  if (empty($postfix_mathematical_expression)) {
    return NULL;
  }
  $operators = units_get_operator_by_sign();
  $stack = array();
  foreach ($postfix_mathematical_expression as $token) {
    if (!isset($operators[$token])) {

      // This is either a unit or a constant.
      if (is_numeric($token) || $token == UNITS_QUANTITY) {
        $token = new UnitsConstantMathematicalExpression($token);
      }
      else {
        $unit = units_unit_machine_name_load($token);
        if (!$unit) {
          throw new UnitsMathematicalExpressionMalformedException(t('Unknown token: @token in expression: @expression', array(
            '@token' => $token,
            '@expression' => $postfix_mathematical_expression_string,
          )));
        }
        $token = $unit;
      }
    }
    else {

      // TODO: shouldn't these stack pops be executed through units_mathematical_expression_postfix_stack_pop()?
      $operand2 = array_pop($stack);
      $operand1 = array_pop($stack);
      $token = units_mathematical_expression_operator_construct($operators[$token], $operand1, $operand2);
    }
    array_push($stack, $token);
  }

  // TODO: shouldn't these stack pops be executed through units_mathematical_expression_postfix_stack_pop()?
  $result = array_pop($stack);
  if (!empty($stack)) {

    // The stack must be empty now since we've supposedly processed the whole
    // expression. Be it not the case, we may not trust the $result and better
    // thrown an exception.
    throw new UnitsMathematicalExpressionMalformedException();
  }
  $wrapper = new UnitsMathematicalExpressionWrapper();
  $wrapper
    ->setExpression($result);
  return $wrapper;
}