picea/src/ControlStructure/ForToken.php
Dave Mc Nicoll ce4ef6351a - Small refractoring of code, merge tokens
- Completed block token
2020-05-04 22:50:02 -04:00

36 lines
952 B
PHP

<?php
namespace Picea\ControlStructure;
class ForToken implements ControlStructure {
public array $token = [ "for", "endfor" ];
public function parse(/*\Picae\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":
if ( end($context->iterationStack)['or'] === false ) {
$output = "<?php endfor; ?>";
}
else {
$output = "<?php endif; ?>";
}
array_pop($context->iterationStack);
return $output;
}
}
}