- Small refractoring of code, merge tokens
- Completed block token
This commit is contained in:
1 parent
bf3816c3db
commit
ce4ef6351a
21 files changed
+370
-194
No files matched your search
@@ -4,23 +4,38 @@ namespace Picea\ControlStructure;
|
||||
|
||||
class ForeachToken implements ControlStructure {
|
||||
|
||||
public string $token = "foreach";
|
||||
public array $token = [ "foreach", "endforeach" ];
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
$name = "$".uniqid("foreach_");
|
||||
|
||||
$count = count($context->iterationStack ?? []);
|
||||
|
||||
if ( $count > 0 ) {
|
||||
$name .= "[" . $context->iterationStack[$count - 1]['uid'] . "]";
|
||||
}
|
||||
switch($token) {
|
||||
case "foreach":
|
||||
$name = "$".uniqid("foreach_");
|
||||
|
||||
$context->iterationStack[] = [
|
||||
'or' => false,
|
||||
'uid' => $name,
|
||||
'token' => 'endforeach',
|
||||
];
|
||||
|
||||
return "<?php foreach ($arguments): $name = ( $name ?? 0 ) + 1; ; ?>";
|
||||
$count = count($context->iterationStack ?? []);
|
||||
|
||||
if ( $count > 0 ) {
|
||||
$name .= "[" . $context->iterationStack[$count - 1]['uid'] . "]";
|
||||
}
|
||||
|
||||
$context->iterationStack[] = [
|
||||
'or' => false,
|
||||
'uid' => $name,
|
||||
'token' => 'endforeach',
|
||||
];
|
||||
|
||||
return "<?php foreach ($arguments): $name = ( $name ?? 0 ) + 1; ; ?>";
|
||||
|
||||
case "endforeach":
|
||||
if ( end($context->iterationStack)['or'] === false ) {
|
||||
$output = "<?php endforeach; ?>";
|
||||
}
|
||||
else {
|
||||
$output = "<?php endif; ?>";
|
||||
}
|
||||
|
||||
array_pop($context->iterationStack);
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user