feat(php): 添加对多维标准数组维度访问的支持

- 在 CompilerBase 中实现多维数组维度提取和转换逻辑
- 为 StdArrayParser 的 parseStdArrayDimFetch 方法添加 toArray 参数支持
- 修正 std::array 维度访问边界检查和错误消息格式
- 添加多维数组维度访问验证以防止维度不匹配错误
- 更新示例代码以测试多维标准数组功能
pull/1/head
韩天峰 4 months ago
parent 817c056859
commit faf6fe7a6b
  1. 7
      examples/array-loop/std-array.php
  2. 3
      src/Php/CompilerBase.php
  3. 10
      src/Php/Parser/StdArrayParser.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]);
}

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

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

Loading…
Cancel
Save