From 752767012f9d9fd83b1fe55fe64c0ea143aa4e25 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 10 Feb 2026 13:14:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(compiler):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9go?= =?UTF-8?q?to=E8=AF=AD=E5=8F=A5=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入OP_NOP常量用于处理标签后的空操作 - 修改语句解析循环以跟踪最后一条语句 - 在标签语句后添加空操作防止代码优化问题 - 添加jump02和jump03测试用例验证goto功能 - 创建mv.php脚本用于文件重命名和git添加操作 --- bin/mv.php | 18 ++++++++++++++++++ src/Php/CompilerBase.php | 7 ++++++- tests/zend/jump/jump02.phpt | 16 ++++++++++++++++ tests/zend/jump/jump03.phpt | 18 ++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100755 bin/mv.php create mode 100644 tests/zend/jump/jump02.phpt create mode 100644 tests/zend/jump/jump03.phpt diff --git a/bin/mv.php b/bin/mv.php new file mode 100755 index 00000000..0742a658 --- /dev/null +++ b/bin/mv.php @@ -0,0 +1,18 @@ +#!/usr/bin/env php +\n"; + exit(1); +} +$file = $argv[1]; +if (!file_exists($file)) { + echo "File not found: " . $file . "\n"; + exit(1); +} +$newFile = str_replace('-raw', '', $file); +$dir = dirname($newFile); +if (!is_dir($dir)) { + mkdir($dir, 0755, true); +} +rename($file, $newFile); +shell_exec('git add ' . $newFile); diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index b2f56923..1a8adace 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -61,6 +61,7 @@ class CompilerBase extends \PhpAot\Core\Translator public const string PREFIX = 'php_'; public const string OP_ISSET = 'isset'; public const string OP_EMPTY = 'empty'; + public const string OP_NOP = "if (0) {}\n"; protected string $phpxDir = '~/workspace/projects/phpx'; protected string $lang = 'PHP'; protected string $cppCompiler = 'g++'; @@ -872,7 +873,8 @@ class CompilerBase extends \PhpAot\Core\Translator { $lines = []; $inLoopTop = $this->inLoop; - foreach ($stmts as $v) { + $last = array_key_last($stmts); + foreach ($stmts as $i => $v) { $class = $v->getType(); $this->beforeStmtLines = []; $this->afterStmtLines = []; @@ -926,6 +928,9 @@ class CompilerBase extends \PhpAot\Core\Translator break; case 'Stmt_Label': $result = $this->parseLabel($v); + if ($i === $last) { + $result .= self::OP_NOP; + } break; case 'Stmt_Continue': $result = $this->parseContinue($v); diff --git a/tests/zend/jump/jump02.phpt b/tests/zend/jump/jump02.phpt new file mode 100644 index 00000000..ff83bc4b --- /dev/null +++ b/tests/zend/jump/jump02.phpt @@ -0,0 +1,16 @@ +--TEST-- +jump 02: goto forward +--FILE-- + 3) goto L2; +echo "$n: ok\n"; +$n++; +goto L1; +L2: +?> +--EXPECT-- +1: ok +2: ok +3: ok diff --git a/tests/zend/jump/jump03.phpt b/tests/zend/jump/jump03.phpt new file mode 100644 index 00000000..c43bb154 --- /dev/null +++ b/tests/zend/jump/jump03.phpt @@ -0,0 +1,18 @@ +--TEST-- +jump 03: goto inside control structures +--FILE-- + +--EXPECT-- +1: ok +2: ok