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.             $v = (string) $v;
  47.             if(substr($v01=== '"' || substr($v01=== '\''{
  48.                 $vmod strtolower(substr($v1-1));
  49.             else {
  50.                 $vmod strtolower($v);
  51.             }
  52.             switch($vmod{
  53.  
  54.             case 'and':
  55.                 $p['&&';
  56.                 break;
  57.             case 'or':
  58.                 $p['||';
  59.                 break;
  60.             case '==':
  61.             case 'eq':
  62.                 $p['==';
  63.                 break;
  64.             case '<>':
  65.             case '!=':
  66.             case 'ne':
  67.             case 'neq':
  68.                 $p['!=';
  69.                 break;
  70.             case '>=':
  71.             case 'gte':
  72.             case 'ge':
  73.                 $p['>=';
  74.                 break;
  75.             case '<=':
  76.             case 'lte':
  77.             case 'le':
  78.                 $p['<=';
  79.                 break;
  80.             case '>':
  81.             case 'gt':
  82.                 $p['>';
  83.                 break;
  84.             case '<':
  85.             case 'lt':
  86.                 $p['<';
  87.                 break;
  88.             case '===':
  89.                 $p['===';
  90.                 break;
  91.             case '!==':
  92.                 $p['!==';
  93.                 break;
  94.             case 'is':
  95.                 if (isset($params[$k+1]&& strtolower(trim($params[$k+1]'"\'')) === 'not'{
  96.                     $negate true;
  97.                     next($params);
  98.                 else {
  99.                     $negate false;
  100.                 }
  101.                 $ptr 1+(int)$negate;
  102.                 if (!isset($params[$k+$ptr])) {
  103.                     $params[$k+$ptr'';
  104.                 else {
  105.                     $params[$k+$ptrtrim($params[$k+$ptr]'"\'');
  106.                 }
  107.                 switch($params[$k+$ptr]{
  108.  
  109.                 case 'div':
  110.                     if (isset($params[$k+$ptr+1]&& strtolower(trim($params[$k+$ptr+1]'"\'')) === 'by'{
  111.                         $p[' % '.$params[$k+$ptr+2].' '.($negate?'!':'=').'== 0';
  112.                         next($params);
  113.                         next($params);
  114.                         next($params);
  115.                     else {
  116.                         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]);
  117.                     }
  118.                     break;
  119.                 case 'even':
  120.                     $a array_pop($p);
  121.                     if (isset($params[$k+$ptr+1]&& strtolower(trim($params[$k+$ptr+1]'"\'')) === 'by'{
  122.                         $b $params[$k+$ptr+2];
  123.                         $p['('.$a .' / '.$b.') % 2 '.($negate?'!':'=').'== 0';
  124.                         next($params);
  125.                         next($params);
  126.                     else {
  127.                         $p[$a.' % 2 '.($negate?'!':'=').'== 0';
  128.                     }
  129.                     next($params);
  130.                     break;
  131.                 case 'odd':
  132.                     $a array_pop($p);
  133.                     if (isset($params[$k+$ptr+1]&& strtolower(trim($params[$k+$ptr+1]'"\'')) === 'by'{
  134.                         $b $params[$k+$ptr+2];
  135.                         $p['('.$a .' / '.$b.') % 2 '.($negate?'=':'!').'== 0';
  136.                         next($params);
  137.                         next($params);
  138.                     else {
  139.                         $p[$a.' % 2 '.($negate?'=':'!').'== 0';
  140.                     }
  141.                     next($params);
  142.                     break;
  143.                 default:
  144.                     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]);
  145.  
  146.                 }
  147.                 break;
  148.             case '%':
  149.             case 'mod':
  150.                 $p['%';
  151.                 break;
  152.             case '!':
  153.             case 'not':
  154.                 $p['!';
  155.                 break;
  156.             default:
  157.                 $p[$v;
  158.  
  159.             }
  160.         }
  161.  
  162.         return $p;
  163.     }
  164.  
  165.     public static function preProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$type)
  166.     {
  167.         return '';
  168.     }
  169.  
  170.     public static function postProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$content)
  171.     {
  172.         $params $compiler->getCompiledParams($params);
  173.  
  174.         $pre Dwoo_Compiler::PHP_OPEN.'if ('.implode(' 'self::replaceKeywords($params['*']$compiler)).") {\n".Dwoo_Compiler::PHP_CLOSE;
  175.  
  176.         $post Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
  177.  
  178.         if (isset($params['hasElse'])) {
  179.             $post .= $params['hasElse'];
  180.         }
  181.  
  182.         return $pre $content $post;
  183.     }
  184. }

Documentation generated on Sun, 07 Sep 2008 23:57:51 +0200 by phpDocumentor 1.4.0