Alg_Math

The Booster for WooCommerce Alg Math class.

Defined (1)

The class is defined in the following location(s).

/includes/lib/PHPMathParser/Math.php  
  1. class Alg_Math { 
  2.  
  3. protected $variables = array(); 
  4.  
  5. public function evaluate($string) { 
  6. $stack = $this->parse($string); 
  7. return $this->run($stack); 
  8.  
  9. public function parse($string) { 
  10. $tokens = $this->tokenize($string); 
  11. $output = new Alg_Stack(); 
  12. $operators = new Alg_Stack(); 
  13. foreach ($tokens as $token) { 
  14. $token = $this->extractVariables($token); 
  15. $expression = Alg_TerminalExpression::factory($token); 
  16. if ($expression->isOperator()) { 
  17. $this->parseOperator($expression, $output, $operators); 
  18. } elseif ($expression->isParenthesis()) { 
  19. $this->parseParenthesis($expression, $output, $operators); 
  20. } else { 
  21. $output->push($expression); 
  22. while (($op = $operators->pop())) { 
  23. if ($op->isParenthesis()) { 
  24. throw new RuntimeException('Mismatched Parenthesis'); 
  25. $output->push($op); 
  26. return $output; 
  27.  
  28. public function registerVariable($name, $value) { 
  29. $this->variables[$name] = $value; 
  30.  
  31. public function run(Alg_Stack $stack) { 
  32. while (($operator = $stack->pop()) && $operator->isOperator()) { 
  33. $value = $operator->operate($stack); 
  34. if (!is_null($value)) { 
  35. $stack->push(Alg_TerminalExpression::factory($value)); 
  36. return $operator ? $operator->render() : $this->render($stack); 
  37.  
  38. protected function extractVariables($token) { 
  39. if ($token[0] == '$') { 
  40. $key = substr($token, 1); 
  41. return isset($this->variables[$key]) ? $this->variables[$key] : 0; 
  42. return $token; 
  43.  
  44. protected function render(Alg_Stack $stack) { 
  45. $output = ''; 
  46. while (($el = $stack->pop())) { 
  47. $output .= $el->render(); 
  48. if ($output) { 
  49. return $output; 
  50. throw new RuntimeException('Could not render output'); 
  51.  
  52. protected function parseParenthesis(Alg_TerminalExpression $expression, Alg_Stack $output, Alg_Stack $operators) { 
  53. if ($expression->isOpen()) { 
  54. $operators->push($expression); 
  55. } else { 
  56. $clean = false; 
  57. while (($end = $operators->pop())) { 
  58. if ($end->isParenthesis()) { 
  59. $clean = true; 
  60. break; 
  61. } else { 
  62. $output->push($end); 
  63. if (!$clean) { 
  64. throw new RuntimeException('Mismatched Parenthesis'); 
  65.  
  66. protected function parseOperator(Alg_TerminalExpression $expression, Alg_Stack $output, Alg_Stack $operators) { 
  67. $end = $operators->poke(); 
  68. if (!$end) { 
  69. $operators->push($expression); 
  70. } elseif ($end->isOperator()) { 
  71. do { 
  72. if ($expression->isLeftAssoc() && $expression->getPrecedence() <= $end->getPrecedence()) { 
  73. $output->push($operators->pop()); 
  74. } elseif (!$expression->isLeftAssoc() && $expression->getPrecedence() < $end->getPrecedence()) { 
  75. $output->push($operators->pop()); 
  76. } else { 
  77. break; 
  78. } while (($end = $operators->poke()) && $end->isOperator()); 
  79. $operators->push($expression); 
  80. } else { 
  81. $operators->push($expression); 
  82.  
  83. protected function tokenize($string) { 
  84. $parts = preg_split('((\f+|\+|-|\(|\)|\*|\^|/)|\s+)', $string, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); 
  85. $parts = array_map('trim', $parts); 
  86. return $parts; 
  87.