- Added a loop index in AbstractLoop control struct.

This commit is contained in:
Dave Mc Nicoll committed 2025-10-15 14:57:42 +00:00
1 parent e737e82d7e
commit ef95576d74
10 files changed
+73 -36

No files matched your search

+36 -20
View File
@@ -4,30 +4,18 @@ namespace Picea\ControlStructure;
abstract class AbstractLoop implements ControlStructure {
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
protected function openLoop(\Picea\Compiler\Context &$context, ?string $arguments, string $token,) : string
{
switch($token) {
case "while":
case "foreach":
$name = "$".uniqid("{$token}_");
$stack = array_filter($context->iterationStack ?? [], function($item) {
return ! $item['or'];
});
$count = count($stack);
$name = "$".uniqid("{$token}_");
if ( $count > 0 ) {
$name .= "[" . end($stack)['uid'] . "]";
}
$stack = array_filter($context->iterationStack ?? [], function($item) {
return ! $item['or'];
});
$context->iterationStack[] = [
'or' => false,
'uid' => $name,
'token' => "end{$token}",
];
$count = count($stack);
return "<?php $token ($arguments): $name = ( $name ?? 0 ) + 1; ?>";
<<<<<<< HEAD
return "<?php $token ($arguments): \$__loop_index = $name = ( $name ?? 0 ) + 1; ?>";
case "endwhile":
case "endforeach":
@@ -43,6 +31,34 @@ abstract class AbstractLoop implements ControlStructure {
array_pop($context->iterationStack);
return $output;
=======
if ( $count > 0 ) {
$name .= "[" . end($stack)['uid'] . "]";
>>>>>>> 579a917 (- Added a loop index in AbstractLoop control struct.)
}
$context->iterationStack[] = [
'or' => false,
'uid' => $name,
'token' => "end{$token}",
];
return "<?php $token ($arguments): \$__loop_index = $name = ( $name ?? 0 ) + 1; ?>";
}
protected function closeLoop(\Picea\Compiler\Context &$context, ?string $arguments, string $token,) : string
{
$last = end($context->iterationStack);
if ( $last['or'] === false ) {
$output = "<?php $token; ?>";
}
else {
$output = "<?php endif; if ( isset({$last['uid']}) ) unset({$last['uid']}); ?>";
}
array_pop($context->iterationStack);
return $output;
}
}