Beispiel #1
0
 function __construct($expression = '')
 {
     $this->expression = str_replace(' ', '', $expression);
     preg_match_all('%(?:(?P<dice>' . DICE_REGEX . ')|(?P<set>\\d*\\[[^\\]]+\\])|(?P<numeral>[\\d\\.]+)|(?P<operator>[+\\-*^><=/])|(?P<variable>\\$[a-z_]+)|(?P<parens>[()]))%i', $this->expression, $matches, PREG_SET_ORDER);
     $stack = array();
     foreach ($matches as $match) {
         $match = array_filter($match);
         if (isset($match['numeral'])) {
             $this->rpn[] = $match['numeral'];
             $this->infix[] = $match['numeral'];
         } elseif (isset($match['dice'])) {
             $dice = new CalcDice($match['dice']);
             $this->rpn[] = $dice->value();
             $this->infix[] = $dice;
         } elseif (isset($match['set'])) {
             $this->rpn[] = new CalcSet($match['set']);
             $this->infix[] = end($this->rpn);
         } elseif (isset($match['operator'])) {
             while (count($stack) > 0 && end($stack) != '(' && $this->ooo[$match['operator']] <= $this->ooo[end($stack)]) {
                 $this->rpn[] = array_pop($stack);
             }
             $stack[] = $match['operator'];
             $this->infix[] = $match['operator'];
         } elseif (isset($match['variable'])) {
             $this->rpn[] = $match['variable'];
             $this->infix[] = end($this->rpn);
         } elseif (isset($match['parens'])) {
             $this->infix[] = $match['parens'];
             if ($match['parens'] == '(') {
                 $stack[] = $match['parens'];
             } else {
                 while (count($stack) > 0 && end($stack) != '(') {
                     $this->rpn[] = array_pop($stack);
                 }
                 array_pop($stack);
             }
         } else {
             $stack = array('Invalid token:', $match);
             break;
         }
     }
     while (count($stack) > 0) {
         $this->rpn[] = array_pop($stack);
     }
 }
Beispiel #2
0
 protected function match_dice($dice) {
     $dice          = new CalcDice($dice);
     $this->rpn[]   = $dice->value();
     $this->infix[] = $dice;
 }