feat(aot): 改进switch语句编译逻辑支持默认分支

- 添加了default分支的特殊处理逻辑
- 实现了defaultOnly标识用于优化条件判断
- 修正了switch case条件列表的构建方式
- 添加了对多个测试用例的支持验证
- 优化了分支条件的代码生成策略
- 确保了break语句后的正确流程控制
pull/1/head
韩天峰 5 months ago
parent 21b7de3905
commit a204ebf1e7
  1. 14
      src/Php/CompilerBase.php
  2. 0
      tests/aot/switch/001.phpt
  3. 0
      tests/aot/switch/002.phpt
  4. 18
      tests/aot/switch/003.phpt
  5. 27
      tests/aot/switch/004.phpt

@ -3398,11 +3398,11 @@ class CompilerBase extends \PhpAot\Core\Translator
$code = 'do {' . PHP_EOL;
$this->indentLevel++;
$condList = [];
$defaultCase = false;
$defaultOnly = true;
foreach ($v->cases as $case) {
if (empty($case->cond)) {
if ($condList) {
$this->fatalError($case, 'switch case must be first');
}
$defaultCase = true;
} else {
$condList[] = $tmp_var . '==' . $this->parseIdentifier($case->cond);
}
@ -3423,11 +3423,13 @@ class CompilerBase extends \PhpAot\Core\Translator
$this->fatalError($case, 'switch case must end with return/break/exit/throw, ' . $lastExpr->getType() . ' given');
}
if ($condList) {
if ($defaultCase) {
$else = $defaultOnly ? '' : 'else';
$code .= $this->getIndent() . $else . ' {' . PHP_EOL;
} else {
$code .= $this->getIndent() . 'if (' . implode(' || ', $condList) . ') {' . PHP_EOL;
$defaultOnly = false;
$condList = [];
} else {
$code .= $this->getIndent() . 'else {' . PHP_EOL;
}
$code .= $this->parseStmts($stmts);

@ -0,0 +1,18 @@
--TEST--
switch
--FILE--
<?php
function main()
{
$c = 100;
switch ($c) {
case 100:
case 99:
default:
echo "default\n";
break;
}
}
?>
--EXPECT--
default

@ -0,0 +1,27 @@
--TEST--
switch
--FILE--
<?php
function foo($c)
{
switch ($c) {
case 1:
case 2:
echo "case 1\n";
break;
case 100:
case 99:
default:
echo "default\n";
break;
}
}
function main() {
foo(1);
foo(100);
}
?>
--EXPECT--
case 1
default
Loading…
Cancel
Save