From d81c4cda62d89009428789799a521c8b3cd69a48 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 5 Feb 2026 14:12:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20=E4=BF=AE=E5=A4=8D=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E6=96=87=E4=BB=B6=E5=90=8D=E7=BC=96=E8=AF=91=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E5=B9=B6=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在run-tests.php中为数字文件名添加'app_'前缀以避免命名冲突 - 在Translator.php中为数字目标名称添加'app_'前缀以确保有效标识符 - 重构NativeCallToCDecl函数中的参数列表构建逻辑,调整代码顺序以提高可读性 - 添加测试用例验证岛屿周长计算算法的正确性 --- run-tests.php | 3 +++ src/Php/Translator.php | 13 +++++++---- tests/misc/1.phpt | 52 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 tests/misc/1.phpt diff --git a/run-tests.php b/run-tests.php index 99771d4e..91227fb3 100755 --- a/run-tests.php +++ b/run-tests.php @@ -4205,6 +4205,9 @@ function compile_php_file(string $file): string } system('./bin/compiler.php ' . $file); $binary_file = str_replace('-', '_', basename($file, '.php')); + if (is_numeric($binary_file)) { + $binary_file = 'app_' . $binary_file; + } if (!file_exists($binary_file)) { throw new Exception('Compilation failed'); } diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 9652b3e0..f0edbef2 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -137,6 +137,9 @@ class Translator extends Preprocessor $this->climate->red('The target name [' . $name . '] must not be a reserved keyword'); exit(1); } + if (is_numeric($name)) { + $name = 'app_' . $name; + } $this->targetName = $name; } @@ -290,12 +293,12 @@ class Translator extends Preprocessor */ foreach ($this->nativeFunctions as $name => $func) { $code .= 'extern ' . $func->returnType . ' ' . self::PREFIX . $name . '('; + $list = []; + if ($func->method) { + $list[] = 'php::Object &this_'; + } $argInfoList = $func->argInfoList; if ($argInfoList) { - $list = []; - if ($func->method) { - $list[] = 'php::Object &this_'; - } foreach ($argInfoList as $argInfo) { $arg = $argInfo->type . ' ' . $argInfo->name; if ($argInfo->default) { @@ -303,8 +306,8 @@ class Translator extends Preprocessor } $list[] = $arg; } - $code .= implode(', ', $list); } + $code .= implode(', ', $list); $code .= ');' . PHP_EOL; } diff --git a/tests/misc/1.phpt b/tests/misc/1.phpt new file mode 100644 index 00000000..26401810 --- /dev/null +++ b/tests/misc/1.phpt @@ -0,0 +1,52 @@ +--TEST-- +mixed/1.phpt +--FILE-- +grid = $grid; + $this->islandPerimeter(); + } + + public function islandPerimeter() + { + if (empty($this->grid) || empty($this->grid[0])) return 0; + + foreach ($this->grid as $key => $val) { + + foreach ($this->grid[0] as $k => $v) { + if ($this->grid[$key][$k] == 0) continue; + $this->result += 4; + + if ($key > 0 && $this->grid[$key - 1][$k] == 1) { + $this->result -= 2; + } + if ($k > 0 && $this->grid[$key][$k - 1] == 1) { + $this->result -= 2; + } + } + } + } +} + +function main() +{ + $arr = [ + [0, 1, 0, 0], + [1, 1, 1, 0], + [0, 1, 0, 0], + [1, 1, 0, 0] + ]; + + $result = new Solution($arr); + var_dump($result->result); +} +?> +--EXPECT-- +int(16)