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; }
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->range ? $name : $index; $ivar = $ivar ? $ivar : $scope->free_variable('i'); 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, '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}[{$ivar}]"; } if (!$this->object) { $lvar = $scope->free_variable('len'); $for_var_part = "{$ivar} = 0, {$lvar} = {$svar}.length"; if ($this->step) { $for_var_part .= ", {$stepvar} = " . $this->step->compile($options, LEVEL_OP); } $step_part = $this->step ? "{$ivar} += {$stepvar}" : "{$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 = yy_Push::wrap($rvar, $body); } if ($this->guard) { $body = yy_Block::wrap(array(yy('If', $this->guard, $body))); } if ($this->pattern) { array_unshift($body->expressions, yy('Assign', $this->name, yy('Literal', "{$svar}[{$ivar}]"))); } $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 = "{$ivar} in {$svar}"; if ($this->own) { $guard_part = "\n{$idt1}if (!" . utility('hasProp') . ".call({$svar}, {$ivar})) 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 : ''); }