protected function compute($expression)
 {
     // Load the compiler
     $compiler = \Hoa\Compiler\Llk::load(new \Hoa\File\Read('hoa://Library/Math/Arithmetic.pp'));
     // Load the visitor, aka the "evaluator"
     $visitor = new \Hoa\Math\Visitor\Arithmetic();
     // Parse the expression
     $ast = $compiler->parse($expression);
     // Evaluate
     $result = $visitor->visit($ast);
     return $result;
 }
Exemple #2
0
 protected function command_math(Shotbow_ChatBot_User $sender, $arguments)
 {
     if (empty($arguments)) {
         $this->postMessage('You need to provide a mathematical expression for me to solve if you\'re going to use this command.');
     } else {
         try {
             $compiler = Hoa\Compiler\Llk\Llk::load(new Hoa\File\Read('hoa://Library/Math/Arithmetic.pp'));
             $visitor = new Hoa\Math\Visitor\Arithmetic();
             $ast = $compiler->parse($arguments);
             $result = $visitor->visit($ast);
             $this->postMessage($arguments . ' = ' . $result);
         } catch (Exception $e) {
             $this->postMessage('I\'m sorry, but I don\'t recognize that as a mathematical expression.  Feel free to try another.');
         }
     }
 }