Ejemplo n.º 1
0
 static function generator($compiler, $args)
 {
     $count = hexec('count', $args[0]);
     $strlen = hexec('strlen', $args[0]);
     $vars = hexec('count', hexec('get_object_vars', $args[0]));
     $guess = hexpr_cond(hexec('is_array', $args[0]), hexec('count', $args[0]), hexec('strlen', $args[0]));
     if (Haanga_AST::is_var($args[0])) {
         /* if it is a variable, best effort to detect
            its type at compile time */
         $value = $compiler->get_context($args[0]['var']);
         if (is_array($value)) {
             return $count;
         } else {
             if (is_string($value)) {
                 return $strlen;
             } else {
                 if (is_object($value)) {
                     return $vars;
                 } else {
                     return $gess;
                 }
             }
         }
     }
     if (Haanga_AST::is_str($args[0])) {
         return $strlen;
     }
     return $guess;
 }
Ejemplo n.º 2
0
 static function generator($compiler, $args)
 {
     if (Haanga_AST::is_str($args[0])) {
         return hexec('strlen', $args[0]);
     }
     return hexpr_cond(hexec('is_array', $args[0]), hexec('count', $args[0]), hexec('strlen', $args[0]));
 }
Ejemplo n.º 3
0
 static function generator($cmp, $args, $redirected)
 {
     if (count($args) != 3 && count($args) != 4) {
         throw new Haanga_CompilerException("Memeame_Pagination requires 3 or 4 parameters");
     }
     if (count($args) == 3) {
         $args[3] = 5;
     }
     $current = hvar('mnm_current');
     $total = hvar('mnm_total');
     $start = hvar('mnm_start');
     $end = hvar('mnm_end');
     $prev = hvar('mnm_prev');
     $next = hvar('mnm_next');
     $pages = 'mnm_pages';
     $code = hcode();
     $code->decl($current, $args[0]);
     $code->decl($total, hexec('ceil', hexpr($args[2], '/', $args[1])));
     $code->decl($start, hexec('max', hexpr($current, '-', hexec('intval', hexpr($args[3], '/', 2))), 1));
     $code->decl($end, hexpr($start, '+', $args[3], '-', 1));
     $code->decl($prev, hexpr_cond(hexpr(1, '==', $current), FALSE, hexpr($current, '-', 1)));
     $code->decl($next, hexpr_cond(hexpr($args[2], '<', 0, '||', $current, '<', $total), hexpr($current, '+', 1), FALSE));
     $code->decl('mnm_pages', hexec('range', $start, hexpr_cond(hexpr($end, '<', $total), $end, $total)));
     $cmp->set_safe($current);
     $cmp->set_safe($total);
     $cmp->set_safe($prev);
     $cmp->set_safe($next);
     $cmp->set_safe($pages);
     return $code;
 }
Ejemplo n.º 4
0
 /**
  *  firstof tag
  *
  */
 static function generator($cmp, $args)
 {
     $count = count($args);
     $args = array_reverse($args);
     for ($i = 0; $i < $count; $i++) {
         if (isset($expr) && Haanga_AST::is_var($args[$i])) {
             $expr = hexpr_cond(hexpr(hexec('empty', $args[$i]), '==', FALSE), $args[$i], $expr);
         } else {
             $expr = $args[$i];
         }
     }
     return $expr;
 }
Ejemplo n.º 5
0
 static function generator($compiler, $args)
 {
     if (count($args) > 1) {
         if (!Haanga_AST::is_str($args[1])) {
             $compiler->Error("pluralize: First parameter must be an string");
         }
         $parts = explode(",", $args[1]['string']);
         $singular = "";
         if (count($parts) == 1) {
             $plural = $parts[0];
         } else {
             $singular = $parts[0];
             $plural = $parts[1];
         }
     } else {
         $singular = "";
         $plural = "s";
     }
     return hexpr_cond(hexpr($args[0], '<=', 1), $singular, $plural);
 }
Ejemplo n.º 6
0
 static function generator($cmp, $args, $declared)
 {
     static $cycle = 0;
     if (!isset($cmp->cycle)) {
         $cmp->cycle = array();
     }
     $code = hcode();
     $index = 'index_' . $cycle;
     $def = 'def_cycle_' . $cycle;
     if (count($args) == 1 && Haanga_AST::is_var($args[0]) && isset($cmp->cycle[$args[0]['var']])) {
         $id = $cmp->cycle[$args[0]['var']];
         $index = 'index_' . $id;
         $def = 'def_cycle_' . $id;
     } else {
         if (!$declared) {
             $code->do_if(hexpr(hexec('isset', hvar($def)), '==', FALSE));
         }
         $code->decl($def, $args);
         if (!$declared) {
             $code->do_endif();
         }
     }
     /* isset($var) == FALSE */
     $expr = hexpr(hexec('isset', hvar($index)), '==', FALSE);
     $inc = hexpr(hexpr(hexpr(hvar($index), '+', 1)), '%', hexec('count', hvar($def)));
     if (!$declared) {
         if (isset($id)) {
             $code->decl($index, $inc);
         } else {
             $code->decl($index, hexpr_cond($expr, 0, $inc));
         }
         $code->end();
         $var = hvar($def, hvar($index));
         $cmp->do_print($code, $var);
     } else {
         $code->decl($index, -1);
         $cmp->cycle[$declared] = $cycle;
     }
     $cycle++;
     return $code;
 }
Ejemplo n.º 7
0
 protected function generate_op_block($details, &$body)
 {
     if (is_array($details['name'])) {
         $name = "";
         foreach ($details['name'] as $part) {
             if (is_string($part)) {
                 $name .= "{$part}";
             } else {
                 if (is_array($part)) {
                     if (Haanga_AST::is_str($part)) {
                         $name .= "{$part['string']}";
                     } elseif (isset($part['object'])) {
                         $name .= "{$part['object']}";
                     } else {
                         throw new Exception("Invalid blockname");
                     }
                 }
             }
             $name .= ".";
         }
         $details['name'] = substr($name, 0, -1);
     }
     $this->in_block++;
     $this->blocks[] = $details['name'];
     $block_name = hvar('blocks', $details['name']);
     $this->ob_start($body);
     $buffer_var = 'buffer' . $this->ob_start;
     $content = hcode();
     $this->generate_op_code($details['body'], $content);
     $body->append_ast($content);
     $this->ob_start--;
     $buffer = hvar($buffer_var);
     /* {{{ */
     /**
      *  isset previous block (parent block)?
      *  TRUE
      *      has reference to self::$block_var ?
      *      TRUE
      *          replace self::$block_var for current block value (buffer)
      *      FALSE
      *          print parent block
      *  FALSE
      *      print current block
      *
      */
     $declare = hexpr_cond(hexec('isset', $block_name), hexpr_cond(hexpr(hexec('strpos', $block_name, self::$block_var), '===', FALSE), $block_name, hexec('str_replace', self::$block_var, $buffer, $block_name)), $buffer);
     /* }}} */
     if (!$this->subtemplate) {
         $this->do_print($body, $declare);
     } else {
         $body->decl($block_name, $declare);
         if ($this->in_block > 1) {
             $this->do_print($body, $block_name);
         }
     }
     array_pop($this->blocks);
     $this->in_block--;
 }
Ejemplo n.º 8
0
 static function generator($compiler, $args)
 {
     return hexpr_cond(hexpr(hexec('empty', $args[0]), '==', TRUE), $args[1], $args[0]);
 }