Пример #1
0
 protected function parseOperator(TerminalExpression $expression, Stack $output, Stack $operators)
 {
     $end = $operators->poke();
     if (!$end) {
         $operators->push($expression);
     } elseif (!$expression->isUnary() && $end->isOperator()) {
         do {
             if ($expression->isLeftAssoc() && $expression->getPrecedence() <= $end->getPrecedence()) {
                 $output->push($operators->pop());
             } elseif (!$expression->isLeftAssoc() && $expression->getPrecedence() < $end->getPrecedence()) {
                 $output->push($operators->pop());
             } else {
                 break;
             }
         } while (($end = $operators->poke()) && $end->isOperator());
         $operators->push($expression);
     } elseif ($expression->isUnary() && $end->isUnary()) {
         if ($expression->isNegative() == $end->isNegative()) {
             //very intruiging
             $operators->pop();
         } elseif ($expression->isNegative()) {
             $operators->pop();
             $operators->push($expression);
         }
     } else {
         $operators->push($expression);
     }
 }