Esempio n. 1
0
 /**
  * @param $operators
  * @param $token
  * @param $output
  */
 private function handleOperator(Stack $operators, Operator $token, Stack $output)
 {
     while ($o2 = $operators->poke()) {
         switch (true) {
             case $o2 instanceof Parenthesis:
                 break 2;
             case $token->isLeftAssociative() && $token->precedence() <= $o2->precedence():
             case $token->isRightAssociative() && $token->precedence() < $o2->precedence():
                 $output->push($operators->pop());
                 break;
             default:
                 break 2;
         }
     }
     $operators->push($token);
 }
Esempio n. 2
0
 public function calculate(Token ...$tokenStream)
 {
     $stack = new Stack();
     foreach ($tokenStream as $token) {
         if ($token instanceof Operand) {
             $stack->push($token);
             continue;
         }
         if ($token instanceof Operator) {
             $stack->push($token->bla($stack->pop(), $stack->pop()));
         }
     }
     return $stack->pop()->value();
 }