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.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  23.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  24.  * @license    http://dwoo.org/LICENSE   Modified BSD License
  25.  * @link       http://dwoo.org/
  26.  * @version    1.0.0
  27.  * @date       2008-10-23
  28.  * @package    Dwoo
  29.  */
  30. {
  31.     public function init(array $rest)
  32.     {
  33.     }
  34.  
  35.     public static function replaceKeywords(array $paramsarray $tokensDwoo_Compiler $compiler)
  36.     {
  37.         $p array();
  38.  
  39.         reset($params);
  40.         while (list($k,$veach($params)) {
  41.             $v = (string) $v;
  42.             if(substr($v01=== '"' || substr($v01=== '\''{
  43.                 $vmod strtolower(substr($v1-1));
  44.             else {
  45.                 $vmod strtolower($v);
  46.             }
  47.             switch($vmod{
  48.  
  49.             case 'and':
  50.                 if ($tokens[$k=== Dwoo_Compiler::T_UNQUOTED_STRING{
  51.                     $p['&&';
  52.                 else {
  53.                     $p[$v;
  54.                 }
  55.                 break;
  56.             case 'or':
  57.                 if ($tokens[$k=== Dwoo_Compiler::T_UNQUOTED_STRING{
  58.                     $p['||';
  59.                 else {
  60.                     $p[$v;
  61.                 }
  62.                 break;
  63.             case 'eq':
  64.                 if ($tokens[$k=== Dwoo_Compiler::T_UNQUOTED_STRING{
  65.                     $p['==';
  66.                 else {
  67.                     $p[$v;
  68.                 }
  69.                 break;
  70.             case 'ne':
  71.             case 'neq':
  72.                 if ($tokens[$k=== Dwoo_Compiler::T_UNQUOTED_STRING{
  73.                     $p['!=';
  74.                 else {
  75.                     $p[$v;
  76.                 }
  77.                 break;
  78.             case 'gte':
  79.             case 'ge':
  80.                 if ($tokens[$k=== Dwoo_Compiler::T_UNQUOTED_STRING{
  81.                     $p['>=';
  82.                 else {
  83.                     $p[$v;
  84.                 }
  85.                 break;
  86.             case 'lte':
  87.             case 'le':
  88.                 if ($tokens[$k=== Dwoo_Compiler::T_UNQUOTED_STRING{
  89.                     $p['<=';
  90.                 else {
  91.                     $p[$v;
  92.                 }
  93.                 break;
  94.             case 'gt':
  95.                 if ($tokens[$k=== Dwoo_Compiler::T_UNQUOTED_STRING{
  96.                     $p['>';
  97.                 else {
  98.                     $p[$v;
  99.                 }
  100.                 break;
  101.             case 'lt':
  102.                 if ($tokens[$k=== Dwoo_Compiler::T_UNQUOTED_STRING{
  103.                     $p['<';
  104.                 else {
  105.                     $p[$v;
  106.                 }
  107.                 break;
  108.             case 'mod':
  109.                 if ($tokens[$k=== Dwoo_Compiler::T_UNQUOTED_STRING{
  110.                     $p['%';
  111.                 else {
  112.                     $p[$v;
  113.                 }
  114.                 break;
  115.             case 'not':
  116.                 if ($tokens[$k=== Dwoo_Compiler::T_UNQUOTED_STRING{
  117.                     $p['!';
  118.                 else {
  119.                     $p[$v;
  120.                 }
  121.                 break;
  122.             case '<>':
  123.                 $p['!=';
  124.                 break;
  125.             case '==':
  126.             case '!=':
  127.             case '>=':
  128.             case '<=':
  129.             case '>':
  130.             case '<':
  131.             case '===':
  132.             case '!==':
  133.             case '%':
  134.             case '!':
  135.                 $p[$vmod;
  136.                 break;
  137.             case 'is':
  138.                 if ($tokens[$k!== Dwoo_Compiler::T_UNQUOTED_STRING{
  139.                     $p[$v;
  140.                     break;
  141.                 }
  142.                 if (isset($params[$k+1]&& strtolower(trim($params[$k+1]'"\'')) === 'not' && $tokens[$k+1=== Dwoo_Compiler::T_UNQUOTED_STRING{
  143.                     $negate true;
  144.                     next($params);
  145.                 else {
  146.                     $negate false;
  147.                 }
  148.                 $ptr 1+(int)$negate;
  149.                 if ($tokens[$k+$ptr!== Dwoo_Compiler::T_UNQUOTED_STRING{
  150.                     break;
  151.                 }
  152.                 if (!isset($params[$k+$ptr])) {
  153.                     $params[$k+$ptr'';
  154.                 else {
  155.                     $params[$k+$ptrtrim($params[$k+$ptr]'"\'');
  156.                 }
  157.                 switch($params[$k+$ptr]{
  158.  
  159.                 case 'div':
  160.                     if (isset($params[$k+$ptr+1]&& strtolower(trim($params[$k+$ptr+1]'"\'')) === 'by'{
  161.                         $p[' % '.$params[$k+$ptr+2].' '.($negate?'!':'=').'== 0';
  162.                         next($params);
  163.                         next($params);
  164.                         next($params);
  165.                     else {
  166.                         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]);
  167.                     }
  168.                     break;
  169.                 case 'even':
  170.                     $a array_pop($p);
  171.                     if (isset($params[$k+$ptr+1]&& strtolower(trim($params[$k+$ptr+1]'"\'')) === 'by'{
  172.                         $b $params[$k+$ptr+2];
  173.                         $p['('.$a .' / '.$b.') % 2 '.($negate?'!':'=').'== 0';
  174.                         next($params);
  175.                         next($params);
  176.                     else {
  177.                         $p[$a.' % 2 '.($negate?'!':'=').'== 0';
  178.                     }
  179.                     next($params);
  180.                     break;
  181.                 case 'odd':
  182.                     $a array_pop($p);
  183.                     if (isset($params[$k+$ptr+1]&& strtolower(trim($params[$k+$ptr+1]'"\'')) === 'by'{
  184.                         $b $params[$k+$ptr+2];
  185.                         $p['('.$a .' / '.$b.') % 2 '.($negate?'=':'!').'== 0';
  186.                         next($params);
  187.                         next($params);
  188.                     else {
  189.                         $p[$a.' % 2 '.($negate?'=':'!').'== 0';
  190.                     }
  191.                     next($params);
  192.                     break;
  193.                 default:
  194.                     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]);
  195.  
  196.                 }
  197.                 break;
  198.             default:
  199.                 $p[$v;
  200.  
  201.             }
  202.         }
  203.  
  204.         return $p;
  205.     }
  206.  
  207.     public static function preProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$type)
  208.     {
  209.         return '';
  210.     }
  211.  
  212.     public static function postProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$content)
  213.     {
  214.         $tokens $compiler->getParamTokens($params);
  215.         $params $compiler->getCompiledParams($params);
  216.         $pre Dwoo_Compiler::PHP_OPEN.'if ('.implode(' 'self::replaceKeywords($params['*']$tokens['*']$compiler)).") {\n".Dwoo_Compiler::PHP_CLOSE;
  217.  
  218.         $post Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
  219.  
  220.         if (isset($params['hasElse'])) {
  221.             $post .= $params['hasElse'];
  222.         }
  223.  
  224.         return $pre $content $post;
  225.     }
  226. }

Documentation generated on Sun, 07 Feb 2010 17:04:01 +0000 by phpDocumentor 1.4.0