Skip to content

Commit 49cadaa

Browse files
authored
Merge pull request #59 from xp-framework/feature/logical-assignment-operators
Implement logical AND and OR assignment operators (&&=, ||=)
2 parents 6a1a5be + 89f85a8 commit 49cadaa

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

src/main/php/lang/ast/syntax/PHP.class.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
ArrayLiteral,
55
Annotation,
66
Annotations,
7+
Assignment,
78
Block,
89
Braced,
910
BreakStatement,
@@ -233,8 +234,15 @@ public function __construct() {
233234
$this->assignment('>>=');
234235
$this->assignment('<<=');
235236
$this->assignment('??=');
236-
$this->assignment('&&=');
237-
$this->assignment('||=');
237+
238+
// Logical AND and OR, not supported in PHP
239+
foreach (['&&=', '||='] as $op) {
240+
$this->symbol($op, 10)->led= function($parse, $token, $left) use($op) {
241+
$assign= new Assignment($left, $op, $this->expression($parse, 9), $left->line);
242+
$assign->kind= 'logicalassignment';
243+
return $assign;
244+
};
245+
}
238246

239247
// This is ambiguous:
240248
//

src/test/php/lang/ast/unittest/parse/OperatorTest.class.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,21 @@ public function prefix($operator) {
6464
);
6565
}
6666

67-
#[Test, Values(['=', '+=', '-=', '*=', '/=', '%=', '.=', '**=', '&=', '|=', '^=', '>>=', '<<=', '??=', '&&=', '||='])]
67+
#[Test, Values(['=', '+=', '-=', '*=', '/=', '%=', '.=', '**=', '&=', '|=', '^=', '>>=', '<<=', '??='])]
6868
public function assignment($operator) {
6969
$this->assertParsed(
7070
[new Assignment(new Variable('a', self::LINE), $operator, new Variable('b', self::LINE), self::LINE)],
7171
'$a '.$operator.' $b;'
7272
);
7373
}
7474

75+
#[Test, Values(['&&=', '||='])]
76+
public function logical_assignment($operator) {
77+
$assignment= new Assignment(new Variable('a', self::LINE), $operator, new Variable('b', self::LINE), self::LINE);
78+
$assignment->kind= 'logicalassignment';
79+
$this->assertParsed([$assignment], '$a '.$operator.' $b;');
80+
}
81+
7582
#[Test]
7683
public function assignment_to_offset() {
7784
$target= new OffsetExpression(new Variable('a', self::LINE), new Literal('0', self::LINE), self::LINE);

0 commit comments

Comments
 (0)