picea/src/ControlStructure/ForToken.php
Dave Mc Nicoll bb29a56077 - Added while/endwhile and do/while syntaxes
- Fix the abstractclass requirement of parse() function
2022-12-19 19:36:39 +00:00

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;
}
}
}