示例#1
0
 function compile_node($options)
 {
     $options['indent'] .= TAB;
     $set = '';
     $body = $this->body;
     if ($body->is_empty()) {
         $body = '';
     } else {
         if ($this->returns) {
             $body->make_return($rvar = $options['scope']->free_variable('results'));
             $set = "{$this->tab}{$rvar} = [];\n";
         }
         if ($this->guard) {
             if (count($body->expressions) > 1) {
                 array_unshift($body->expressions, yy('If', yy('Parens', $this->guard)->invert(), yy('Literal', 'continue')));
             } else {
                 $body = yy_Block::wrap(array(yy('If', $this->guard, $body)));
             }
         }
         $body = "\n" . $body->compile($options, LEVEL_TOP) . "\n{$this->tab}";
     }
     $code = $set . $this->tab . 'while (' . $this->condition->compile($options, LEVEL_PAREN) . ") {{$body}}";
     if ($this->returns) {
         $code .= "\n{$this->tab}return {$rvar};";
     }
     return $code;
 }
示例#2
0
 function compile_node($options)
 {
     $options['indent'] .= TAB;
     $set = '';
     $body = $this->body;
     if ($body->is_empty()) {
         $body = '';
     } else {
         if ($options['level'] > LEVEL_TOP || $this->returns) {
             $rvar = $options['scope']->free_variable('results');
             $set = "{$this->tab}{$rvar} = [];\n";
             if ($body) {
                 $body = yy_Push::wrap($rvar, $body);
             }
         }
         if ($this->guard) {
             $body = yy_Block::wrap(array(yy('If', $this->guard, $body)));
         }
         $body = "\n" . $body->compile($options, LEVEL_TOP) . "\n{$this->tab}";
     }
     $code = $set . $this->tab . 'while (' . $this->condition->compile($options, LEVEL_PAREN) . ") {{$body}}";
     if ($this->returns) {
         $code .= "\n{$this->tab}return {$rvar};";
     }
     return $code;
 }
示例#3
0
 static function wrap($expressions, $statement = NULL, $no_return = FALSE)
 {
     if ($expressions->jumps()) {
         return $expressions;
     }
     $func = yy('Code', array(), yy_Block::wrap(array($expressions)));
     $args = array();
     if (($mentions_args = $expressions->contains('yy_Closure::literal_args')) || $expressions->contains('yy_Closure::literal_this')) {
         $meth = yy('Literal', $mentions_args ? 'apply' : 'call');
         $args = array(yy('Literal', 'this'));
         if ($mentions_args) {
             $args[] = yy('Literal', 'arguments');
         }
         $func = yy('Value', $func, array(yy('Access', $meth)));
     }
     $func->no_return = $no_return;
     $call = yy('Call', $func, $args);
     return $statement ? yy_Block::wrap(array($call)) : $call;
 }
示例#4
0
 function compile_node($options)
 {
     $body = yy_Block::wrap(array($this->body));
     $last_jumps = last($body->expressions);
     $last_jumps = $last_jumps ? $last_jumps->jumps() : FALSE;
     if ($last_jumps && $last_jumps instanceof yy_Return) {
         $this->returns = FALSE;
     }
     if ($this->range) {
         $source = $this->source->base;
     } else {
         $source = $this->source;
     }
     $scope = $options['scope'];
     $name = $this->name ? $this->name->compile($options, LEVEL_LIST) : FALSE;
     $index = $this->index ? $this->index->compile($options, LEVEL_LIST) : FALSE;
     if ($name && !$this->pattern) {
         $scope->find($name, array('immediate' => TRUE));
     }
     if ($index) {
         $scope->find($index, array('immediate' => TRUE));
     }
     if ($this->returns) {
         $rvar = $scope->free_variable('results');
     }
     $ivar = $this->object ? $index : $scope->free_variable('i');
     $kvar = $this->range ? $name ? $name : ($index ? $index : $ivar) : ($index ? $index : $ivar);
     $kvar_assign = $kvar !== $ivar ? "{$kvar} = " : '';
     if ($this->step && !$this->range) {
         $stepvar = $scope->free_variable('step');
     }
     if ($this->pattern) {
         $name = $ivar;
     }
     $var_part = '';
     $guard_part = '';
     $def_part = '';
     $idt1 = $this->tab . TAB;
     if ($this->range) {
         $for_part = $source->compile(array_merge($options, array('index' => $ivar, 'name' => $name, 'step' => $this->step)));
     } else {
         $svar = $this->source->compile($options, LEVEL_LIST);
         if (($name || $this->own) && !preg_match(IDENTIFIER, $svar)) {
             $ref = $scope->free_variable('ref');
             $def_part = "{$this->tab}{$ref} = {$svar};\n";
             $svar = $ref;
         }
         if ($name && !$this->pattern) {
             $name_part = "{$name} = {$svar}[{$kvar}]";
         }
         if (!$this->object) {
             $lvar = $scope->free_variable('len');
             $for_var_part = "{$kvar_assign}{$ivar} = 0, {$lvar} = {$svar}.length";
             if ($this->step) {
                 $for_var_part .= ", {$stepvar} = " . $this->step->compile($options, LEVEL_OP);
             }
             $step_part = $kvar_assign . ($this->step ? "{$ivar} += {$stepvar}" : ($kvar !== $ivar ? "++{$ivar}" : "{$ivar}++"));
             $for_part = "{$for_var_part}; {$ivar} < {$lvar}; {$step_part}";
         }
     }
     if ($this->returns) {
         $result_part = "{$this->tab}{$rvar} = [];\n";
         $return_result = "\n{$this->tab}return {$rvar};";
         $body->make_return($rvar);
     }
     if ($this->guard) {
         if ($body->expressions) {
             array_unshift($body->expressions, yy('If', yy('Parens', $this->guard)->invert(), yy('Literal', 'continue')));
         } else {
             $body = yy_Block::wrap(array(yy('If', $this->guard, $body)));
         }
     }
     if ($this->pattern) {
         array_unshift($body->expressions, yy('Assign', $this->name, yy('Literal', "{$svar}[{$kvar}]")));
     }
     $def_part .= $this->pluck_direct_call($options, $body);
     if (isset($name_part) && $name_part) {
         $var_part = "\n{$idt1}{$name_part};";
     }
     if ($this->object) {
         $for_part = "{$kvar} in {$svar}";
         if ($this->own) {
             $guard_part = "\n{$idt1}if (!" . utility('hasProp') . ".call({$svar}, {$kvar})) continue;";
         }
     }
     $body = $body->compile(array_merge($options, array('indent' => $idt1)), LEVEL_TOP);
     if ($body) {
         $body = "\n{$body}\n";
     }
     return "{$def_part}" . (isset($result_part) ? $result_part : '') . "{$this->tab}for ({$for_part}) {{$guard_part}{$var_part}{$body}{$this->tab}}" . (isset($return_result) ? $return_result : '');
 }