Source for file if.php

Documentation is available at if.php

  1. <?php
  2.  
  3. /**
  4.  * Conditional block, the syntax is very similar to the php one, allowing () || && and
  5.  * other php operators. Additional operators and their equivalent php syntax are as follow :
  6.  *
  7.  * eq -> ==
  8.  * neq or ne -> !=
  9.  * gte or ge -> >=
  10.  * lte or le -> <=
  11.  * gt -> >
  12.  * lt -> <
  13.  * mod -> %
  14.  * not -> !
  15.  * X is [not] div by Y -> (X % Y) == 0
  16.  * X is [not] even [by Y] -> (X % 2) == 0 or ((X/Y) % 2) == 0
  17.  * X is [not] odd [by Y] -> (X % 2) != 0 or ((X/Y) % 2) != 0
  18.  *
  19.  * This software is provided 'as-is', without any express or implied warranty.
  20.  * In no event will the authors be held liable for any damages arising from the use of this software.
  21.  *
  22.  * This file is released under the LGPL
  23.  * "GNU Lesser General Public License"
  24.  * More information can be found here:
  25.  * {@link http://www.gnu.org/copyleft/lesser.html}
  26.  *
  27.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  28.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  29.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  30.  * @link       http://dwoo.org/
  31.  * @version    0.9.1
  32.  * @date       2008-05-30
  33.  * @package    Dwoo
  34.  */
  35. {
  36.     public function init(array $rest)
  37.     {
  38.     }
  39.  
  40.     public static function replaceKeywords(array $paramsDwoo_Compiler $compiler)
  41.     {
  42.         $p array();
  43.  
  44.         reset($params);
  45.         while (list($k,$veach($params)) {
  46.             switch($v{
  47.  
  48.             case '"<<"':
  49.             case '">>"':
  50.             case '"&&"':
  51.             case '"||"':
  52.             case '"|"':
  53.             case '"^"':
  54.             case '"&"':
  55.             case '"~"':
  56.             case '"("':
  57.             case '")"':
  58.             case '","':
  59.             case '"+"':
  60.             case '"-"':
  61.             case '"*"':
  62.             case '"/"':
  63.                 $p[trim($v'"');
  64.                 break;
  65.             case '"and"':
  66.                 $p['&&';
  67.                 break;
  68.             case '"or"':
  69.                 $p['||';
  70.                 break;
  71.             case '"=="':
  72.             case '"eq"':
  73.                 $p['==';
  74.                 break;
  75.             case '"<>"':
  76.             case '"!="':
  77.             case '"ne"':
  78.             case '"neq"':
  79.                 $p['!=';
  80.                 break;
  81.             case '">="':
  82.             case '"gte"':
  83.             case '"ge"':
  84.                 $p['>=';
  85.                 break;
  86.             case '"<="':
  87.             case '"lte"':
  88.             case '"le"':
  89.                 $p['<=';
  90.                 break;
  91.             case '">"':
  92.             case '"gt"':
  93.                 $p['>';
  94.                 break;
  95.             case '"<"':
  96.             case '"lt"':
  97.                 $p['<';
  98.                 break;
  99.             case '"==="':
  100.                 $p['===';
  101.                 break;
  102.             case '"!=="':
  103.                 $p['!==';
  104.                 break;
  105.             case '"is"':
  106.                 if ($params[$k+1=== '"not"'{
  107.                     $negate true;
  108.                     next($params);
  109.                 else {
  110.                     $negate false;
  111.                 }
  112.                 $ptr 1+(int)$negate;
  113.                 switch($params[$k+$ptr]{
  114.  
  115.                 case '"div"':
  116.                     if (isset($params[$k+$ptr+1]&& $params[$k+$ptr+1=== '"by"'{
  117.                         $p[' % '.$params[$k+$ptr+2].' '.($negate?'!':'=').'== 0';
  118.                         next($params);
  119.                         next($params);
  120.                         next($params);
  121.                     else {
  122.                         throw new Dwoo_Compilation_Exception($compiler'If : Syntax error : syntax should be "if $a is [not] div by $b", found '.$params[$k-1].' is '.($negate?'not ':'').'div '.$params[$k+$ptr+1].' '.$params[$k+$ptr+2]);
  123.                     }
  124.                     break;
  125.                 case '"even"':
  126.                     $a array_pop($p);
  127.                     if (isset($params[$k+$ptr+1]&& $params[$k+$ptr+1=== '"by"'{
  128.                         $b $params[$k+$ptr+2];
  129.                         $p['('.$a .' / '.$b.') % 2 '.($negate?'!':'=').'== 0';
  130.                         next($params);
  131.                         next($params);
  132.                     else {
  133.                         $p[$a.' % 2 '.($negate?'!':'=').'== 0';
  134.                     }
  135.                     next($params);
  136.                     break;
  137.                 case '"odd"':
  138.                     $a array_pop($p);
  139.                     if (isset($params[$k+$ptr+1]&& $params[$k+$ptr+1=== '"by"'{
  140.                         $b $params[$k+$ptr+2];
  141.                         $p['('.$a .' / '.$b.') % 2 '.($negate?'=':'!').'== 0';
  142.                         next($params);
  143.                         next($params);
  144.                     else {
  145.                         $p[$a.' % 2 '.($negate?'=':'!').'== 0';
  146.                     }
  147.                     next($params);
  148.                     break;
  149.                 default:
  150.                     throw new Dwoo_Compilation_Exception($compiler'If : Syntax error : syntax should be "if $a is [not] (div|even|odd) [by $b]", found '.$params[$k-1].' is '.$params[$k+$ptr+1]);
  151.  
  152.                 }
  153.                 break;
  154.             case '"%"':
  155.             case '"mod"':
  156.                 $p['%';
  157.                 break;
  158.             case '"!"':
  159.             case '"not"':
  160.                 $p['!';
  161.                 break;
  162.             default:
  163.                 $p[$v;
  164.  
  165.             }
  166.         }
  167.  
  168.         return $p;
  169.     }
  170.  
  171.     public static function preProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$type)
  172.     {
  173.         $currentBlock =$compiler->getCurrentBlock();
  174.         $currentBlock['params']['postOutput'Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
  175.  
  176.         $params $compiler->getCompiledParams($params);
  177.  
  178.         return Dwoo_Compiler::PHP_OPEN.'if ('.implode(' 'self::replaceKeywords($params['*']$compiler)).") {\n".Dwoo_Compiler::PHP_CLOSE;
  179.     
  180.  
  181.     public static function postProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$content)
  182.     {
  183.         return $content $params['postOutput'];
  184.     }
  185. }

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