fix(core): 修复数字文件名编译问题并优化代码结构

- 在run-tests.php中为数字文件名添加'app_'前缀以避免命名冲突
- 在Translator.php中为数字目标名称添加'app_'前缀以确保有效标识符
- 重构NativeCallToCDecl函数中的参数列表构建逻辑,调整代码顺序以提高可读性
- 添加测试用例验证岛屿周长计算算法的正确性
pull/1/head
韩天峰 7 months ago
parent 92bd1fe56c
commit d81c4cda62
  1. 3
      run-tests.php
  2. 13
      src/Php/Translator.php
  3. 52
      tests/misc/1.phpt

@ -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');
}

@ -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;
}

@ -0,0 +1,52 @@
--TEST--
mixed/1.phpt
--FILE--
<?php
class Solution
{
protected $grid;
public $result = 0;
public function __construct(array $grid)
{
$this->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)
Loading…
Cancel
Save