- Added while/endwhile and do/while syntaxes
- Fix the abstractclass requirement of parse() function
This commit is contained in:
1 parent
33298ff2e4
commit
bb29a56077
36 files changed
+115
-73
No files matched your search
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Picea\ControlStructure;
|
||||
|
||||
abstract class AbstractLoop implements ControlStructure {
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
|
||||
switch($token) {
|
||||
case "while":
|
||||
case "foreach":
|
||||
$name = "$".uniqid("{$token}_");
|
||||
|
||||
$stack = array_filter($context->iterationStack ?? [], function($item) {
|
||||
return ! $item['or'];
|
||||
});
|
||||
|
||||
$count = count($stack);
|
||||
|
||||
if ( $count > 0 ) {
|
||||
$name .= "[" . end($stack)['uid'] . "]";
|
||||
}
|
||||
|
||||
$context->iterationStack[] = [
|
||||
'or' => false,
|
||||
'uid' => $name,
|
||||
'token' => "end{$token}",
|
||||
];
|
||||
|
||||
return "<?php $token ($arguments): $name = ( $name ?? 0 ) + 1; ?>";
|
||||
|
||||
case "endwhile":
|
||||
case "endforeach":
|
||||
$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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user