38 lines
1002 B
PHP
38 lines
1002 B
PHP
<?php
|
|
|
|
namespace Picea\ControlStructure;
|
|
|
|
class ForToken implements ControlStructure {
|
|
|
|
public array $token = [ "for", "endfor" ];
|
|
|
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
|
switch($token) {
|
|
case "for":
|
|
$uid = "$".uniqid("for_");
|
|
|
|
$context->iterationStack[] = [
|
|
'or' => false,
|
|
'uid' => $uid,
|
|
'token' => 'endfor',
|
|
];
|
|
|
|
return "<?php for ($arguments): {$uid} = 1; ?>";
|
|
|
|
case "endfor":
|
|
$last = end($context->iterationStack);
|
|
|
|
if ( $last['or'] === false ) {
|
|
$output = "<?php endfor; ?>";
|
|
}
|
|
else {
|
|
$output = "<?php endif; unset({$last['uid']}) ?>";
|
|
}
|
|
|
|
array_pop($context->iterationStack);
|
|
|
|
return $output;
|
|
}
|
|
}
|
|
}
|