Skip to content

Commit

Permalink
Fix children() accessor for match expressions and conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Feb 22, 2021
1 parent c072371 commit 4cd9e38
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ XP AST ChangeLog

## ?.?.? / ????-??-??

## 7.0.3 / 2021-02-22

* Fixed children() accessor for `match` expressions and conditions, see
xp-framework/compiler#101
(@thekid)
* Fixed PHP 8.1 compatiblity by ensuring we do not pass NULL to strlen()
(@thekid)

Expand Down
4 changes: 1 addition & 3 deletions src/main/php/lang/ast/nodes/MatchCondition.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public function children() {
foreach ($this->expressions as $expression) {
yield $expression;
}
foreach ($this->body as $node) {
yield $node;
}
yield $this->body;
}
}
3 changes: 2 additions & 1 deletion src/main/php/lang/ast/nodes/MatchExpression.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public function __construct($expression, $cases, $default, $line= -1) {

/** @return iterable */
public function children() {
yield $this->expression;
if (null !== $this->expression) yield $this->expression;
foreach ($this->cases as $element) {
yield $element;
}
if (null !== $this->default) yield $this->default;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace lang\ast\unittest\parse;

use lang\ast\nodes\{MatchExpression, MatchCondition, Literal, Variable};
use lang\ast\nodes\{MatchExpression, MatchCondition, Literal, Variable, Block, ReturnStatement};
use unittest\{Assert, Test};

class MatchExpressionTest extends ParseTest {
Expand All @@ -25,6 +25,15 @@ public function match() {
);
}

#[Test]
public function match_with_block() {
$default= new Block([new ReturnStatement(new Literal('false', self::LINE), self::LINE)], self::LINE);
$this->assertParsed(
[new MatchExpression(new Variable('arg', self::LINE), [], $default, self::LINE)],
'match ($arg) { default => { return false; } };'
);
}

#[Test]
public function match_without_condition() {
$this->assertParsed(
Expand Down

0 comments on commit 4cd9e38

Please sign in to comment.