PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/ui/web/lib/Reconnoiter_RPN.php

https://github.com/brad-marshall/reconnoiter
PHP | 86 lines | 85 code | 1 blank | 0 comment | 16 complexity | a8ab507c86cf05cd177a1f6878fc4525 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, 0BSD
  1. <?php
  2. class Reconnoiter_RPN {
  3. function rpn_eval($value, $expr) {
  4. $s = array();
  5. $ops = explode(",", $expr);
  6. array_unshift($s, $value);
  7. foreach($ops as $op) {
  8. switch($op) {
  9. case 'ln':
  10. $v = array_shift($s);
  11. array_unshift($s, is_null($v) ? $v : log($v)); break;
  12. case 'round':
  13. $r = array_shift($s);
  14. $l = array_shift($s);
  15. array_unshift($s, is_null($l) ? $l : round($l,$r));
  16. break;
  17. case 'floor':
  18. $v = array_shift($s);
  19. array_unshift($s, is_null($v) ? $v : floor($v)); break;
  20. case 'ceil':
  21. $v = array_shift($s);
  22. array_unshift($s, is_null($v) ? $v : ceil($v)); break;
  23. case 'log':
  24. $r = array_shift($s);
  25. $l = array_shift($s);
  26. array_unshift($s, is_null($l) ? $l : log($l,$r));
  27. break;
  28. case 'e':
  29. array_unshift($s, exp(1)); break;
  30. case 'pi':
  31. array_unshift($s, pi()); break;
  32. case '^':
  33. $r = array_shift($s);
  34. $l = array_shift($s);
  35. array_unshift($s, (is_null($r) || is_null($l)) ? NULL : pow($l,$r));
  36. break;
  37. case '-':
  38. $r = array_shift($s);
  39. $l = array_shift($s);
  40. array_unshift($s, (is_null($r) || is_null($l)) ? NULL : ($l-$r));
  41. break;
  42. case '/':
  43. $r = array_shift($s);
  44. $l = array_shift($s);
  45. array_unshift($s, (is_null($r) || is_null($l) || $r == 0) ? NULL : ($l/$r));
  46. break;
  47. case '.':
  48. array_unshift($s, $s[array_shift($s)]); break;
  49. case '+':
  50. $r = array_shift($s);
  51. $l = array_shift($s);
  52. array_unshift($s, (is_null($r) || is_null($l)) ? NULL : ($l+$r));
  53. break;
  54. case '*':
  55. $r = array_shift($s);
  56. $l = array_shift($s);
  57. array_unshift($s, (is_null($r) || is_null($l)) ? NULL : ($l*$r));
  58. break;
  59. case 'auto':
  60. $v = array_shift($s);
  61. array_unshift($s, is_null($v) ? $v : $this->autounits($v)); break;
  62. case 'min':
  63. $r = array_shift($s);
  64. $l = array_shift($s);
  65. if(is_null($r)) array_unshift($s,$l);
  66. else if(is_null($l)) array_unshift($s,$r);
  67. else array_unshift($s, min($r,$l));
  68. break;
  69. case 'max':
  70. $r = array_shift($s);
  71. $l = array_shift($s);
  72. if(is_null($r)) array_unshift($s,$l);
  73. else if(is_null($l)) array_unshift($s,$r);
  74. else array_unshift($s, max($r,$l));
  75. break;
  76. default:
  77. if(preg_match('/^-?\d+$/', $op)) {
  78. array_unshift($s, $op);
  79. }
  80. }
  81. }
  82. $value = array_shift($s);
  83. return $value;
  84. }
  85. }