From faf6fe7a6b55045d3c3c5d1757f47443d75e6dfa Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 6 May 2026 20:11:25 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E5=A4=9A=E7=BB=B4=E6=A0=87=E5=87=86=E6=95=B0=E7=BB=84=E7=BB=B4?= =?UTF-8?q?=E5=BA=A6=E8=AE=BF=E9=97=AE=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 CompilerBase 中实现多维数组维度提取和转换逻辑 - 为 StdArrayParser 的 parseStdArrayDimFetch 方法添加 toArray 参数支持 - 修正 std::array 维度访问边界检查和错误消息格式 - 添加多维数组维度访问验证以防止维度不匹配错误 - 更新示例代码以测试多维标准数组功能 --- examples/array-loop/std-array.php | 7 ++++--- src/Php/CompilerBase.php | 3 +++ src/Php/Parser/StdArrayParser.php | 10 +++++++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/examples/array-loop/std-array.php b/examples/array-loop/std-array.php index c52e68ea..a3084560 100644 --- a/examples/array-loop/std-array.php +++ b/examples/array-loop/std-array.php @@ -4,11 +4,12 @@ use native_types; function main() { - $array = std::array(std::array(native_types::type_int, 10), 10); + $array = std::array(std::array(std::array(native_types::type_int, 13), 16), 19); $index = 9; $index2 = 5; - $array[$index2][$index] = 2026; + $array[$index2][$index][0] = 2026; // $array = std::array(native_types::type_int, 100); // $array[99] = 2026; - var_dump($array); + var_dump($array[$index2][$index][0]); + var_dump($array[$index2][$index]); } \ No newline at end of file diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 9c7815ab..98ccf039 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3454,6 +3454,9 @@ class CompilerBase extends \PhpAot\Core\Translator protected function parseArg(Node\Arg $arg): string { + if ($this->isArrayDimFetch($arg->value) and $this->isStdArrayExpr($arg->value)) { + return $this->convertArrayExpr($this->parseStdArrayDimFetch($arg->value, true)); + } $expr = $this->parseIdentifier($arg->value); if ($this->isVarExpr($arg->value) and $this->isStdArray($arg->value->name)) { return $this->convertArrayExpr($expr); diff --git a/src/Php/Parser/StdArrayParser.php b/src/Php/Parser/StdArrayParser.php index fcd7c725..f41b254d 100644 --- a/src/Php/Parser/StdArrayParser.php +++ b/src/Php/Parser/StdArrayParser.php @@ -56,7 +56,7 @@ trait StdArrayParser return $arrayDimFetch . ' ' . $binaryOp . '= ' . $this->convertExprFromType($info['type'], $this->parseExpr($expr->expr)); } - protected function parseStdArrayDimFetch(Expr\ArrayDimFetch $expr): string + protected function parseStdArrayDimFetch(Expr\ArrayDimFetch $expr, bool $toArray = false): string { $tmp = $expr; $nesting = []; @@ -66,12 +66,12 @@ trait StdArrayParser while (true) { if ($this->isArrayDimFetch($tmp)) { if ($tmp->dim === null) { - $this->fatalError($tmp, 'std::array() expects an index'); + $this->fatalError($tmp, 'std::array expects an index'); } $size = $info['sizes'][$level]; if ($this->isScalarInt($tmp->dim)) { if ($tmp->dim->value < 0 || $tmp->dim->value >= $size) { - $this->fatalError($tmp, "Array index out of bounds: index {$tmp->dim->value}, size {$size}"); + $this->fatalError($tmp, "std::array index out of bounds: index {$tmp->dim->value}, size {$size}"); } } $index = $this->parseExpr($tmp->dim); @@ -84,6 +84,10 @@ trait StdArrayParser } } + if (!$toArray and count($nesting) !== count($info['sizes']) + 1) { + $this->fatalError($expr, 'Wrong dimension access std::array used'); + } + return implode('', array_reverse($nesting)); }