From c8d8d1c9baa40b9bdbe5a47e6946f8c7d9553873 Mon Sep 17 00:00:00 2001 From: NathanFreeman <1056159381@qq.com> Date: Sun, 28 Jun 2026 14:05:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20while=20do...while?= =?UTF-8?q?=E5=92=8Cif=E4=B8=AD=E7=9A=84=E5=88=A4=E6=96=AD=E6=98=AF?= =?UTF-8?q?=E8=B5=8B=E5=80=BC=E5=AF=BC=E8=87=B4=E7=9A=84C++=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E8=AD=A6=E5=91=8A=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Php/CompilerBase.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index f9ac543a..bf3af3f6 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3346,6 +3346,7 @@ class CompilerBase extends \PhpAot\Core\Translator protected function parseIf(Node\Stmt\If_ $v): string { $cond = $this->parseExpr($v->cond); + $cond = $this->parseConditionAssign($v->cond, $cond); $code = $this->parseBeforeStmtLines() . PHP_EOL; $code .= 'if (' . $cond . ') {' . PHP_EOL; @@ -3381,6 +3382,7 @@ class CompilerBase extends \PhpAot\Core\Translator protected function parseWhile(Node\Stmt\While_ $v): string { $cond = $this->parseExpr($v->cond); + $cond = $this->parseConditionAssign($v->cond, $cond); $stmts = $v->stmts; $code = $this->parseBeforeStmtLines() . PHP_EOL; @@ -3401,6 +3403,7 @@ class CompilerBase extends \PhpAot\Core\Translator { $stmts = $v->stmts; $cond = $this->parseExpr($v->cond); + $cond = $this->parseConditionAssign($v->cond, $cond); $code = $this->parseBeforeStmtLines() . PHP_EOL; $code .= 'do {' . PHP_EOL; $code .= $this->parseBlockStmts($stmts); @@ -3437,6 +3440,15 @@ class CompilerBase extends \PhpAot\Core\Translator return $tmpVar; } + /** + * while, do while 判断中赋值,在C++编译中会提示 + * suggest parentheses around assignment used as truth value + */ + protected function parseConditionAssign(NodeAbstract $cond, string $code): string + { + return $cond instanceof Expr\Assign ? '(' . $code . ')' : $code; + } + protected function packData(string $bytes): string { $out = '';