Source for file math.php

Documentation is available at math.php

  1. <?php
  2.  
  3. /**
  4.  * Computes a mathematical equation
  5.  * <pre>
  6.  *  * equation : the equation to compute, it can include normal variables with $foo or special math variables without the dollar sign
  7.  *  * format : output format, see {@link http://php.net/sprintf} for details
  8.  *  * assign : if set, the output is assigned into the given variable name instead of being output
  9.  *  * rest : all math specific variables that you use must be defined, see the example
  10.  * </pre>
  11.  * Example :
  12.  *
  13.  * <code>
  14.  * {$c=2}
  15.  * {math "(a+b)*$c/4" a=3 b=5}
  16.  *
  17.  * output is : 4 ( = (3+5)*2/4)
  18.  * </code>
  19.  *
  20.  * This software is provided 'as-is', without any express or implied warranty.
  21.  * In no event will the authors be held liable for any damages arising from the use of this software.
  22.  *
  23.  * This file is released under the LGPL
  24.  * "GNU Lesser General Public License"
  25.  * More information can be found here:
  26.  * {@link http://www.gnu.org/copyleft/lesser.html}
  27.  *
  28.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  29.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  30.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  31.  * @link       http://dwoo.org/
  32.  * @version    0.9.1
  33.  * @date       2008-05-30
  34.  * @package    Dwoo
  35.  */
  36. function Dwoo_Plugin_math_compile(Dwoo_Compiler $compiler$equation$format=''$assign=''array $rest=array())
  37. {
  38.     /**
  39.      * Holds the allowed function, characters, operators and constants
  40.      */
  41.     $allowed array
  42.     (
  43.         '0''1''2''3''4''5''6''7''8''9',
  44.         '+''-''/''*''.'' ''<<''>>''%''&''^''|''~',
  45.         'abs(''ceil(''floor(''exp(''log10(',
  46.         'cos(''sin(''sqrt(''tan(',
  47.         'M_PI''INF''M_E',
  48.     );
  49.  
  50.     /**
  51.      * Holds the functions that can accept multiple arguments
  52.      */
  53.     $funcs array
  54.     (
  55.          'round(''log(''pow(',
  56.         'max(''min(''rand(',
  57.      );
  58.  
  59.     $equation $equationSrc str_ireplace(array('pi''M_PI()''inf'' e ')array('M_PI''M_PI''INF'' M_E ')$equation);
  60.  
  61.     $delim $equation[0];
  62.     $open $delim.'.';
  63.     $close '.'.$delim;
  64.     $equation substr($equation1-1);
  65.     $out '';
  66.     $ptr 1;
  67.     $allowcomma 0;
  68.     while (strlen($equation0{
  69.         $substr substr($equation0$ptr);
  70.         if (array_search($substr$allowed!== false{
  71.             // allowed string
  72.             $out.=$substr;
  73.             $equation substr($equation$ptr);
  74.             $ptr 0;
  75.         elseif (array_search($substr$funcs!== false{
  76.             // allowed func
  77.             $out.=$substr;
  78.             $equation substr($equation$ptr);
  79.             $ptr 0;
  80.             $allowcomma++;
  81.             if ($allowcomma === 1{
  82.                 $allowed[',';
  83.             }
  84.         elseif (isset($rest[$substr])) {
  85.             // variable
  86.             $out.=$rest[$substr];
  87.             $equation substr($equation$ptr);
  88.             $ptr 0;
  89.         elseif ($substr === $open{
  90.             // pre-replaced variable
  91.             preg_match('#.*\((?:[^()]*?|(?R))\)'.str_replace('.''\\.'$close).'#'substr($equation2)$m);
  92.             if (empty($m)) {
  93.                 preg_match('#.*?'.str_replace('.''\\.'$close).'#'substr($equation2)$m);
  94.             }
  95.             $out.=substr($m[0]0-2);
  96.             $equation substr($equationstrlen($m[0])+2);
  97.             $ptr 0;
  98.         elseif ($substr==='('{
  99.             // opening parenthesis
  100.             if ($allowcomma>0{
  101.                 $allowcomma++;
  102.             }
  103.  
  104.             $out.=$substr;
  105.             $equation substr($equation$ptr);
  106.             $ptr 0;
  107.         elseif ($substr===')'{
  108.             // closing parenthesis
  109.             if ($allowcomma>0{
  110.                 $allowcomma--;
  111.                 if ($allowcomma===0{
  112.                     array_pop($allowed);
  113.                 }
  114.             }
  115.  
  116.             $out.=$substr;
  117.             $equation substr($equation$ptr);
  118.             $ptr 0;
  119.         elseif ($ptr >= strlen($equation)) {
  120.             // parse error if we've consumed the entire equation without finding anything valid
  121.             throw new Dwoo_Compilation_Exception($compiler'Math : Syntax error or variable undefined in equation '.$equationSrc.' at '.$substr);
  122.             return;
  123.         else {
  124.             // nothing special, advance
  125.             $ptr++;
  126.         }
  127.     }
  128.     if ($format !== '\'\''{
  129.         $out 'sprintf('.$format.', '.$out.')';
  130.     }
  131.     if ($assign !== '\'\''{
  132.         return '($this->assignInScope('.$out.', '.$assign.'))';
  133.     }
  134.     return '('.$out.')';
  135. }

Documentation generated on Sat, 28 Jun 2008 01:38:30 +0200 by phpDocumentor 1.4.0