diff options
Diffstat (limited to 'src/ste/Calc.php')
-rw-r--r-- | src/ste/Calc.php | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/src/ste/Calc.php b/src/ste/Calc.php index e700c4f..64d2b7a 100644 --- a/src/ste/Calc.php +++ b/src/ste/Calc.php @@ -3,11 +3,15 @@ namespace kch42\ste; /* Class Calc contains static methods needed by <ste:calc /> */ -class Calc { - private function __construct() {} +class Calc +{ + private function __construct() + { + } /* We could also just eval() the $infix_math code, but this is much cooler :-D (Parser inception) */ - public static function shunting_yard($infix_math) { + public static function shunting_yard($infix_math) + { $operators = array( "+" => array("l", 2), "-" => array("l", 2), @@ -20,16 +24,18 @@ class Calc { ); preg_match_all("/\s*(?:(?:[+\\-\\*\\/\\^\\(\\)])|(\\d*[\\.]?\\d*))\\s*/s", $infix_math, $tokens, PREG_PATTERN_ORDER); - $tokens_raw = array_filter(array_map('trim', $tokens[0]), function($x) { return ($x === "0") || (!empty($x)); }); + $tokens_raw = array_filter(array_map('trim', $tokens[0]), function ($x) { + return ($x === "0") || (!empty($x)); + }); $output_queue = array(); $op_stack = array(); - $lastpriority = NULL; + $lastpriority = null; /* Make - unary, if neccessary */ $tokens = array(); - foreach($tokens_raw as $token) { + foreach ($tokens_raw as $token) { $priority = isset($operators[$token]) ? $operators[$token][1] : -1; - if(($token == "-") && (($lastpriority === NULL) || ($lastpriority >= 0))) { + if (($token == "-") && (($lastpriority === null) || ($lastpriority >= 0))) { $priority = $operators["_"][1]; $tokens[] = "_"; } else { @@ -38,35 +44,35 @@ class Calc { $lastpriority = $priority; } - while(!empty($tokens)) { + while (!empty($tokens)) { $token = array_shift($tokens); - if(is_numeric($token)) { + if (is_numeric($token)) { $output_queue[] = $token; - } else if($token == "(") { + } elseif ($token == "(") { $op_stack[] = $token; - } else if($token == ")") { + } elseif ($token == ")") { $lbr_found = false; - while(!empty($op_stack)) { + while (!empty($op_stack)) { $op = array_pop($op_stack); - if($op == "(") { + if ($op == "(") { $lbr_found = true; break; } $output_queue[] = $op; } - if(!$lbr_found) { + if (!$lbr_found) { throw new RuntimeError("Bracket mismatch."); } - } else if(!isset($operators[$token])) { + } elseif (!isset($operators[$token])) { throw new RuntimeError("Invalid token ($token): Not a number, bracket or operator. Stop."); } else { $priority = $operators[$token][1]; - if($operators[$token][0] == "l") { - while((!empty($op_stack)) and ($priority <= $operators[$op_stack[count($op_stack)-1]][1])) { + if ($operators[$token][0] == "l") { + while ((!empty($op_stack)) and ($priority <= $operators[$op_stack[count($op_stack)-1]][1])) { $output_queue[] = array_pop($op_stack); } } else { - while((!empty($op_stack)) and ($priority < $operators[$op_stack[count($op_stack)-1]][1])) { + while ((!empty($op_stack)) and ($priority < $operators[$op_stack[count($op_stack)-1]][1])) { $output_queue[] = array_pop($op_stack); } } @@ -74,9 +80,9 @@ class Calc { } } - while(!empty($op_stack)) { + while (!empty($op_stack)) { $op = array_pop($op_stack); - if($op == "(") { + if ($op == "(") { throw new RuntimeError("Bracket mismatch..."); } $output_queue[] = $op; @@ -85,18 +91,20 @@ class Calc { return $output_queue; } - public static function pop2(&$array) { + public static function pop2(&$array) + { $rv = array(array_pop($array), array_pop($array)); - if(array_search(NULL, $rv, true) !== false) { + if (array_search(null, $rv, true) !== false) { throw new RuntimeError("Not enough numbers on stack. Invalid formula."); } return $rv; } - public static function calc_rpn($rpn) { + public static function calc_rpn($rpn) + { $stack = array(); - foreach($rpn as $token) { - switch($token) { + foreach ($rpn as $token) { + switch ($token) { case "+": list($b, $a) = self::pop2($stack); $stack[] = $a + $b; @@ -119,7 +127,7 @@ class Calc { break; case "_": $a = array_pop($stack); - if($a === NULL) { + if ($a === null) { throw new RuntimeError("Not enough numbers on stack. Invalid formula."); } $stack[] = -$a; @@ -132,7 +140,8 @@ class Calc { return array_pop($stack); } - public static function calc($expr) { + public static function calc($expr) + { return self::calc_rpn(self::shunting_yard($expr)); } -}
\ No newline at end of file +} |