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

Add support for pipelines with |> and ?|> #181

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
@@ -8,7 +8,7 @@
"require" : {
"xp-framework/core": "^12.0 | ^11.6 | ^10.16",
"xp-framework/reflection": "^3.2 | ^2.15",
"xp-framework/ast": "^11.3",
"xp-framework/ast": "dev-feature/pipelines as 11.4.0",
"php" : ">=7.4.0"
},
"require-dev" : {
40 changes: 40 additions & 0 deletions src/main/php/lang/ast/emit/PHP.class.php
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@
ArrayLiteral,
BinaryExpression,
Block,
CallableExpression,
CallableNewExpression,
Comment,
Expression,
InstanceExpression,
@@ -1158,6 +1160,44 @@ protected function emitNullsafeInstance($result, $instance) {
$this->emitOne($result, $instance->member);
}

protected function emitPipeTarget($result, $pipe, $argument) {

// $expr |> new T(...) => new T($expr)
if ($pipe->target instanceof CallableNewExpression) {
$pipe->target->type->arguments= [$argument];
$this->emitOne($result, $pipe->target->type);
$pipe->target->type->arguments= null;
return;
}

// $expr |> strtoupper(...) => strtoupper($expr)
// $expr |> fn($x) => $x * 2 => (fn($x) => $x * 2)($expr)
if ($pipe->target instanceof CallableExpression) {
$this->emitOne($result, $pipe->target->expression);
} else {
$result->out->write('(');
$this->emitOne($result, $pipe->target);
$result->out->write(')');
}

$result->out->write('(');
$this->emitOne($result, $argument);
$result->out->write(')');
}

protected function emitPipe($result, $pipe) {
$this->emitPipeTarget($result, $pipe, $pipe->expression);
}

protected function emitNullsafePipe($result, $pipe) {
$t= $result->temp();
$result->out->write('null===('.$t.'=');
$this->emitOne($result, $pipe->expression);
$result->out->write(')?null:');

$this->emitPipeTarget($result, $pipe, new Variable(substr($t, 1)));
}

protected function emitUnpack($result, $unpack) {
$result->out->write('...');
$this->emitOne($result, $unpack->expression);
159 changes: 159 additions & 0 deletions src/test/php/lang/ast/unittest/emit/PipelinesTest.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php namespace lang\ast\unittest\emit;

use test\{Assert, Test, Values};

/** @see https://wiki.php.net/rfc/pipe-operator-v3 */
class PipelinesTest extends EmittingTest {

#[Test]
public function pipe_to_callable() {
$r= $this->run('class %T {
public function run() {
return "test" |> strtoupper(...);
}
}');

Assert::equals('TEST', $r);
}

#[Test]
public function pipe_to_variable() {
$r= $this->run('class %T {
public function run() {
$f= strtoupper(...);
return "test" |> $f;
}
}');

Assert::equals('TEST', $r);
}

#[Test]
public function pipe_to_callable_string() {
$r= $this->run('class %T {
public function run() {
return "test" |> "strtoupper";
}
}');

Assert::equals('TEST', $r);
}

#[Test]
public function pipe_to_callable_array() {
$r= $this->run('class %T {
public function toUpper($x) { return strtoupper($x); }

public function run() {
return "test" |> [$this, "toUpper"];
}
}');

Assert::equals('TEST', $r);
}

#[Test]
public function pipe_to_callable_without_all_args() {
$r= $this->run('class %T {
public function run() {
return "A&B" |> htmlspecialchars(...);
}
}');

Assert::equals('A&amp;B', $r);
}

#[Test]
public function pipe_to_callable_new() {
$r= $this->run('class %T {
public function run() {
return "2024-03-27" |> new \util\Date(...);
}
}');

Assert::equals('2024-03-27', $r->toString('Y-m-d'));
}

#[Test]
public function pipe_to_callable_anonymous_new() {
$r= $this->run('class %T {
public function run() {
return "2024-03-27" |> new class(...) {
public function __construct(public string $value) { }
};
}
}');

Assert::equals('2024-03-27', $r->value);
}

#[Test]
public function pipe_to_closure() {
$r= $this->run('class %T {
public function run() {
return "test" |> fn($x) => $x.": OK";
}
}');

Assert::equals('test: OK', $r);
}

#[Test]
public function pipe_chain() {
$r= $this->run('class %T {
public function run() {
return " test " |> trim(...) |> strtoupper(...);
}
}');

Assert::equals('TEST', $r);
}

#[Test, Values([[['test'], 'TEST'], [[], null]])]
public function nullsafe_pipe($input, $expected) {
$r= $this->run('class %T {
public function run($arg) {
return array_shift($arg) ?|> strtoupper(...);
}
}', $input);

Assert::equals($expected, $r);
}

#[Test, Values([[null, null], ['test', 'TEST'], [' test ', 'TEST']])]
public function nullsafe_chain($input, $expected) {
$r= $this->run('class %T {
public function run($arg) {
return $arg ?|> trim(...) ?|> strtoupper(...);
}
}', $input);

Assert::equals($expected, $r);
}

#[Test]
public function precedence() {
$r= $this->run('class %T {
public function run() {
return "te"."st" |> strtoupper(...);
}
}');

Assert::equals('TEST', $r);
}

#[Test]
public function rfc_example() {
$r= $this->run('class %T {
public function run() {
return "Hello World"
|> "htmlentities"
|> str_split(...)
|> fn($x) => array_map(strtoupper(...), $x)
|> fn($x) => array_filter($x, fn($v) => $v != "O")
;
}
}');
Assert::equals(['H', 'E', 'L', 'L', ' ', 'W', 'R', 'L', 'D'], array_values($r));
}
}
Loading
Oops, something went wrong.