Skip to content

Commit e05b5b1

Browse files
committed
Extract pipeline emulation into trait
Once php/php-src#17118 is merged, we can create a PHP 8.5 emitter which does not include it and emits pipes natively
1 parent 4357341 commit e05b5b1

8 files changed

+59
-27
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php namespace lang\ast\emit;
2+
3+
use lang\ast\nodes\{CallableExpression, CallableNewExpression, Variable};
4+
5+
/**
6+
* Emulates pipelines
7+
*
8+
* @see https://wiki.php.net/rfc/pipe-operator-v3#precedence
9+
* @test lang.ast.unittest.emit.PipelinesTest
10+
*/
11+
trait EmulatePipelines {
12+
13+
protected function emitPipeTarget($result, $target, $arg) {
14+
if ($target instanceof CallableNewExpression) {
15+
$target->type->arguments= [new Variable(substr($arg, 1))];
16+
$this->emitOne($result, $target->type);
17+
$target->type->arguments= null;
18+
} else if ($target instanceof CallableExpression) {
19+
$this->emitOne($result, $target->expression);
20+
$result->out->write('('.$arg.')');
21+
} else {
22+
$result->out->write('(');
23+
$this->emitOne($result, $target);
24+
$result->out->write(')('.$arg.')');
25+
}
26+
}
27+
28+
protected function emitPipe($result, $pipe) {
29+
30+
// $expr |> strtoupper(...) => [$arg= $expr, strtoupper($arg)][1]
31+
$t= $result->temp();
32+
$result->out->write('['.$t.'=');
33+
$this->emitOne($result, $pipe->expression);
34+
$result->out->write(',');
35+
$this->emitPipeTarget($result, $pipe->target, $t);
36+
$result->out->write('][1]');
37+
}
38+
39+
protected function emitNullsafePipe($result, $pipe) {
40+
41+
// $expr ?|> strtoupper(...) => null === ($arg= $expr) ? null : strtoupper($arg)
42+
$t= $result->temp();
43+
$result->out->write('null===('.$t.'=');
44+
$this->emitOne($result, $pipe->expression);
45+
$result->out->write(')?null:');
46+
$this->emitPipeTarget($result, $pipe->target, $t);
47+
}
48+
}

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

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,40 +1158,20 @@ protected function emitNullsafeInstance($result, $instance) {
11581158
$this->emitOne($result, $instance->member);
11591159
}
11601160

1161-
protected function emitPipeTarget($result, $target, $arg) {
1162-
if ($target instanceof CallableNewExpression) {
1163-
$target->type->arguments= [new Variable(substr($arg, 1))];
1164-
$this->emitOne($result, $target->type);
1165-
$target->type->arguments= null;
1166-
} else if ($target instanceof CallableExpression) {
1167-
$this->emitOne($result, $target->expression);
1168-
$result->out->write('('.$arg.')');
1169-
} else {
1170-
$result->out->write('(');
1171-
$this->emitOne($result, $target);
1172-
$result->out->write(')('.$arg.')');
1173-
}
1174-
}
1175-
11761161
protected function emitPipe($result, $pipe) {
1177-
1178-
// $expr |> strtoupper(...) => [$arg= $expr, strtoupper($arg)][1]
1179-
$t= $result->temp();
1180-
$result->out->write('['.$t.'=');
11811162
$this->emitOne($result, $pipe->expression);
1182-
$result->out->write(',');
1183-
$this->emitPipeTarget($result, $pipe->target, $t);
1184-
$result->out->write('][1]');
1163+
$result->out->write('|>');
1164+
$this->emitOne($result, $pipe->target);
11851165
}
11861166

11871167
protected function emitNullsafePipe($result, $pipe) {
11881168

1189-
// $expr ?|> strtoupper(...) => null === ($arg= $expr) ? null : strtoupper($arg)
1169+
// $expr ?|> strtoupper(...) => null === ($t= $expr) ? null : $t |> strtoupper(...)
11901170
$t= $result->temp();
11911171
$result->out->write('null===('.$t.'=');
11921172
$this->emitOne($result, $pipe->expression);
1193-
$result->out->write(')?null:');
1194-
$this->emitPipeTarget($result, $pipe->target, $t);
1173+
$result->out->write(')?null:'.$t.'|>');
1174+
$this->emitOne($result, $pipe->target);
11951175
}
11961176

11971177
protected function emitUnpack($result, $unpack) {

src/main/php/lang/ast/emit/PHP74.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class PHP74 extends PHP {
1414
AttributesAsComments,
1515
CallablesAsClosures,
1616
ChainScopeOperators,
17+
EmulatePipelines,
1718
MatchAsTernaries,
1819
NonCapturingCatchVariables,
1920
NullsafeAsTernaries,

src/main/php/lang/ast/emit/PHP80.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class PHP80 extends PHP {
2121
use
2222
ArrayUnpackUsingMerge,
2323
CallablesAsClosures,
24+
EmulatePipelines,
2425
OmitConstantTypes,
2526
ReadonlyClasses,
2627
RewriteBlockLambdaExpressions,

src/main/php/lang/ast/emit/PHP81.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
class PHP81 extends PHP {
2222
use
23+
EmulatePipelines,
2324
RewriteBlockLambdaExpressions,
2425
RewriteDynamicClassConstants,
2526
RewriteStaticVariableInitializations,

src/main/php/lang/ast/emit/PHP82.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
class PHP82 extends PHP {
2222
use
23+
EmulatePipelines,
2324
RewriteBlockLambdaExpressions,
2425
RewriteDynamicClassConstants,
2526
RewriteStaticVariableInitializations,

src/main/php/lang/ast/emit/PHP83.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @see https://wiki.php.net/rfc#php_83
1919
*/
2020
class PHP83 extends PHP {
21-
use RewriteBlockLambdaExpressions, RewriteProperties;
21+
use EmulatePipelines, RewriteBlockLambdaExpressions, RewriteProperties;
2222

2323
public $targetVersion= 80300;
2424

src/main/php/lang/ast/emit/PHP84.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @see https://wiki.php.net/rfc#php_84
1919
*/
2020
class PHP84 extends PHP {
21-
use RewriteBlockLambdaExpressions;
21+
use EmulatePipelines, RewriteBlockLambdaExpressions;
2222

2323
public $targetVersion= 80400;
2424

0 commit comments

Comments
 (0)