Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"keywords": ["module", "xp"],
"require" : {
"xp-framework/core": "^11.0 | ^10.0",
"xp-framework/ast": "^9.1",
"xp-framework/ast": "^9.2",
"php" : ">=7.0.0"
},
"require-dev" : {
Expand Down
11 changes: 11 additions & 0 deletions src/main/php/lang/ast/Emitter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public static function forRuntime($runtime, $emitters= []) {
throw new IllegalArgumentException('XP Compiler does not support '.$runtime.' yet');
}

/**
* Raises an exception
*
* @param string $error
* @param ?Throwable $cause
* @return never
*/
public function raise($error, $cause= null) {
throw new IllegalStateException($error, $cause);
}

/**
* Transforms nodes of a certain kind using the given function, which
* may return either single node, which will be then emitted, or an
Expand Down
4 changes: 3 additions & 1 deletion src/main/php/lang/ast/emit/ArrayUnpackUsingMerge.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ protected function emitArray($result, $array) {

$unpack= false;
foreach ($array->values as $pair) {
if ('unpack' === $pair[1]->kind) {
$element= $pair[1] ?? $this->raise('Cannot use empty array elements in arrays');
if ('unpack' === $element->kind) {
$unpack= true;
break;
}
Expand All @@ -32,6 +33,7 @@ protected function emitArray($result, $array) {
$this->emitOne($result, $pair[0]);
$result->out->write('=>');
}

if ('unpack' === $pair[1]->kind) {
if ('array' === $pair[1]->expression->kind) {
$result->out->write('],');
Expand Down
6 changes: 4 additions & 2 deletions src/main/php/lang/ast/emit/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ protected function emitArray($result, $array) {
$this->emitOne($result, $pair[0]);
$result->out->write('=>');
}
$this->emitOne($result, $pair[1]);
$this->emitOne($result, $pair[1] ?? $this->raise('Cannot use empty array elements in arrays'));
$result->out->write(',');
}
$result->out->write(']');
Expand Down Expand Up @@ -701,7 +701,9 @@ protected function emitAssign($result, $target) {
$this->emitOne($result, $pair[0]);
$result->out->write('=>');
}
$this->emitAssign($result, $pair[1]);
if ($pair[1]) {
$this->emitAssign($result, $pair[1]);
}
$result->out->write(',');
}
$result->out->write(')');
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/lang/ast/emit/PHP70.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function emitAssignment($result, $assignment) {
// Rewrite destructuring unless assignment consists only of variables
$r= false;
foreach ($assignment->variable->values as $pair) {
if (!$pair[0] && $pair[1] instanceof Variable) continue;
if (null === $pair[0] && (null === $pair[1] || $pair[1] instanceof Variable)) continue;
$r= true;
break;
}
Expand Down
22 changes: 18 additions & 4 deletions src/main/php/lang/ast/emit/RewriteAssignments.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait RewriteAssignments {

protected function rewriteDestructuring($result, $assignment) {
$t= $result->temp();
$result->out->write('null===('.$t.'=');
$result->out->write('is_array('.$t.'=');

// Create reference to right-hand if possible
$r= $assignment->expression;
Expand All @@ -25,8 +25,12 @@ protected function rewriteDestructuring($result, $assignment) {
}

$this->emitOne($result, $assignment->expression);
$result->out->write(')?null:[');
$result->out->write(')?[');
foreach ($assignment->variable->values as $i => $pair) {
if (null === $pair[1]) {
$result->out->write('null,');
continue;
}

// Assign by reference
if ($pair[1] instanceof UnaryExpression) {
Expand All @@ -44,7 +48,17 @@ protected function rewriteDestructuring($result, $assignment) {
$result->out->write($i.'],');
}
}
$result->out->write(']');
$result->out->write(']:([');
foreach ($assignment->variable->values as $pair) {
if ($pair[1] instanceof UnaryExpression) {
$this->emitAssign($result, $pair[1]->expression);
$result->out->write('=null,');
} else if ($pair[1]) {
$this->emitAssign($result, $pair[1]);
$result->out->write('=null,');
}
}
$result->out->write(']?'.$t.':null)');
}

protected function emitAssignment($result, $assignment) {
Expand All @@ -62,7 +76,7 @@ protected function emitAssignment($result, $assignment) {
// Rewrite destructuring unless assignment consists only of variables
$r= false;
foreach ($assignment->variable->values as $pair) {
if ($pair[1] instanceof Variable) continue;
if (null === $pair[1] || $pair[1] instanceof Variable) continue;
$r= true;
break;
}
Expand Down
34 changes: 34 additions & 0 deletions src/test/php/lang/ast/unittest/emit/ArraysTest.class.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace lang\ast\unittest\emit;

use lang\IllegalStateException;
use unittest\{Assert, Test, Values};

class ArraysTest extends EmittingTest {
Expand Down Expand Up @@ -39,6 +40,15 @@ public function run() {
Assert::equals([1, 2, 3], $r);
}

#[Test, Values(['[1, , 3]', '[1, , ]', '[, 1]']), Expect(class: IllegalStateException::class, withMessage: 'Cannot use empty array elements in arrays')]
public function arrays_cannot_have_empty_elements($input) {
$r= $this->run('class <T> {
public function run() {
return '.$input.';
}
}');
}

#[Test]
public function destructuring() {
$r= $this->run('class <T> {
Expand All @@ -51,6 +61,30 @@ public function run() {
Assert::equals([1, 2], $r);
}

#[Test]
public function destructuring_with_empty_between() {
$r= $this->run('class <T> {
public function run() {
[$a, , $b]= [1, 2, 3];
return [$a, $b];
}
}');

Assert::equals([1, 3], $r);
}

#[Test]
public function destructuring_with_empty_start() {
$r= $this->run('class <T> {
public function run() {
[, $a, $b]= [1, 2, 3];
return [$a, $b];
}
}');

Assert::equals([2, 3], $r);
}

#[Test]
public function destructuring_map() {
$r= $this->run('class <T> {
Expand Down