Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logical Operators and Assignment Expressions: #133

Open
thekid opened this issue Jan 29, 2022 · 1 comment
Open

Logical Operators and Assignment Expressions: #133

thekid opened this issue Jan 29, 2022 · 1 comment

Comments

@thekid
Copy link
Member

thekid commented Jan 29, 2022

Add ||= and &&= operators accompanying the ??= operator:

$a ||= $b; // equivalent of $a || ($a= $b);
$a &&= $b; // equivalent of $a && ($a= $b);
$a ??= $b; // equivalent of $a ?? ($a= $b); - EXISTS in PHP!

https://github.com/tc39/proposal-logical-assignment
dotnet/csharplang#1718

@thekid
Copy link
Member Author

thekid commented Mar 24, 2024

Implementation:

diff --git a/src/main/php/lang/ast/emit/PHP.class.php b/src/main/php/lang/ast/emit/PHP.class.php
index ac3d93a..79cf654 100755
--- a/src/main/php/lang/ast/emit/PHP.class.php
+++ b/src/main/php/lang/ast/emit/PHP.class.php
@@ -789,7 +789,15 @@ abstract class PHP extends Emitter {
 
   protected function emitAssignment($result, $assignment) {
     $this->emitAssign($result, $assignment->variable);
-    $result->out->write($assignment->operator);
+
+    if ('||=' === $assignment->operator || '&&=' === $assignment->operator) {
+      $result->out->write(substr($assignment->operator, 0, 2));
+      $this->emitOne($result, $assignment->variable);
+      $result->out->write('=');
+    } else {
+      $result->out->write($assignment->operator);
+    }
+
     $this->emitOne($result, $assignment->expression);
   }
 
diff --git a/src/test/php/lang/ast/unittest/emit/OperatorTest.class.php b/src/test/php/lang/ast/unittest/emit/OperatorTest.class.php
index 0e1833b..7c87653 100755
--- a/src/test/php/lang/ast/unittest/emit/OperatorTest.class.php
+++ b/src/test/php/lang/ast/unittest/emit/OperatorTest.class.php
@@ -65,4 +65,28 @@ class OperatorTest extends EmittingTest {
 
     Assert::equals($expected, $r[0]);
   }
+
+  #[Test, Values([[false, true], [true, true]])]
+  public function logical_or_assignment($value, $expected) {
+    $r= $this->run('class %T {
+      public function run($arg) {
+        $arg||= true;
+        return $arg;
+      }
+    }', $value);
+
+    Assert::equals($expected, $r);
+  }
+
+  #[Test, Values([[true, false], [false, false]])]
+  public function logical_and_assignment($value, $expected) {
+    $r= $this->run('class %T {
+      public function run($arg) {
+        $arg&&= false;
+        return $arg;
+      }
+    }', $value);
+
+    Assert::equals($expected, $r);
+  }
 }
\ No newline at end of file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant