From 668244ae6433dac0a6ebd6c278f9188bfa0735e4 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 29 Dec 2025 19:31:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20BUG=EF=BC=8Cswitch=20?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=A7=A3=E6=9E=90=E5=A4=B1=E8=B4=A5=E6=97=B6?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E6=81=A2=E5=A4=8D=E4=BD=9C=E7=94=A8=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Php/Translator.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 5fec899d..d4ed5d23 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -262,7 +262,7 @@ class Translator extends \PhpAot\Core\Translator $lines = []; foreach ($stmts as $v) { $class = $v->getType(); - $this->writeLog('Line ' . $this->getLine($v) . ': ' . $class); + $this->writeLog('Line ' . $this->getLine($v) . ': ' . $class); switch ($class) { case 'Stmt_Expression': $lines[] = $this->parseExpr($v->expr) . ';'; @@ -588,7 +588,7 @@ class Translator extends \PhpAot\Core\Translator { $exprType = $expr->getType(); if ($this->debugLine === $expr->getLine()) { - var_dump($expr, $exprType); + var_dump($exprType); } switch ($exprType) { case 'Expr_Cast_Int': @@ -1330,13 +1330,16 @@ class Translator extends \PhpAot\Core\Translator return $name; } - private function parseSwitch(mixed $v) + private function parseSwitch(mixed $v): string { $cond = $v->cond; $tmp_var = $this->addTmpVar(); $type = $this->detectExprType($cond); $var_def = $type . ' ' . $tmp_var . ' = ' . $this->parseExpr($cond) . ';' . PHP_EOL; + // 保存作用域,switch 可能会解析失败,在这个过程中会增加变量,需重置 + $scope = $this->scope; + if ($type === self::TYPE_INT or $type === self::TYPE_FLOAT) { $code = 'switch (' . $tmp_var . ') {' . PHP_EOL; $this->indentLevel++; @@ -1346,6 +1349,7 @@ class Translator extends \PhpAot\Core\Translator } else { $condType = $case->cond->getType(); if ($condType !== 'Scalar_Int' and $condType !== 'Scalar_Float') { + $this->scope = $scope; goto _fail; } $code .= $this->getIndent() . 'case ' . $this->parseScalarValue($case->cond) . ': {' . PHP_EOL;