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