-
Notifications
You must be signed in to change notification settings - Fork 0
Implement partial function application #188
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
Open
thekid
wants to merge
18
commits into
master
Choose a base branch
from
feature/pfa
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
10eaf45
Implement partial function application
thekid 02aa85b
Add test showing by-ref arguments do not work
thekid fa9bde8
Add test showing execution order differs
thekid 15452e6
Make evaluation order consistent with PHP
thekid c833be0
Optimize passing constant expressions
thekid ea7d77e
QA: APIdoc readability
thekid 6970210
Add test for PFA inside annotations
thekid c31dedb
Fix PFA in combination with pipeline operator optimizations
thekid bfa707c
Verify `?` placeholder also works in `new`
thekid 4bc33f5
Implement support for named arguments
thekid 61c4476
Show how the `...` placeholder may appear in the middle of arguments
thekid a7fa148
Optimize pipelines with single-placeholder PFAs
thekid e0bdbd9
Add test for partial constructor application
thekid eef8baf
Add test for static methods used w/ partial function applications
thekid c247e5f
Test PFAs work with __invoke() and __call()
thekid 5134c3f
Verify first-class callable and PFAs both return `Closure` instances
thekid 314383b
QA: Remove unused import
thekid a8d1dde
MFH
thekid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php namespace lang\ast\emit; | ||
|
||
use lang\ast\nodes\Literal; | ||
|
||
/** | ||
* Rewrites `clone(...)` and partial function applications | ||
* | ||
* @see https://wiki.php.net/rfc/clone_with_v2 | ||
* @see https://wiki.php.net/rfc/partial_function_application_v2 | ||
*/ | ||
trait RewriteCallables { | ||
use RewritePartialFunctionApplications { emitCallable as emitPartial; } | ||
|
||
protected function emitCallable($result, $callable) { | ||
if ($callable->expression instanceof Literal && 'clone' === $callable->expression->expression) { | ||
$result->out->write('fn($o) => clone $o'); | ||
} else { | ||
$this->emitPartial($result, $callable); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/php/lang/ast/emit/RewritePartialFunctionApplications.class.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php namespace lang\ast\emit; | ||
|
||
use lang\ast\nodes\{Placeholder, Variable, UnpackExpression}; | ||
|
||
/** | ||
* Rewrites partial function application as follows: | ||
* | ||
* ```php | ||
* // Input: | ||
* $f= str_replace('test', 'ok', ?); | ||
* | ||
* // Output: | ||
* $f= fn($arg) => str_replace('test', 'ok', $arg); | ||
* ``` | ||
* | ||
* Keeps evaluation order consistent with native implementation: | ||
* | ||
* ```php | ||
* // Input: | ||
* $f= str_replace('test', result(), ?); | ||
* | ||
* // Output: | ||
* $f= [ | ||
* $temp= result(), | ||
* fn($arg) => str_replace('test', $temp, $arg) | ||
* ][1]; | ||
* ``` | ||
* | ||
* @see https://wiki.php.net/rfc/partial_function_application_v2 | ||
*/ | ||
trait RewritePartialFunctionApplications { | ||
|
||
protected function emitCallable($result, $callable) { | ||
if ([Placeholder::$VARIADIC] !== $callable->arguments) { | ||
$sig= ''; | ||
$pass= $init= []; | ||
foreach ($callable->arguments as $name => $argument) { | ||
if (Placeholder::$VARIADIC === $argument) { | ||
$t= $result->temp(); | ||
$sig.= ',...'.$t; | ||
$pass[$name]= new UnpackExpression(new Variable(substr($t, 1))); | ||
} else if (Placeholder::$ARGUMENT === $argument) { | ||
$t= $result->temp(); | ||
$sig.= ','.$t; | ||
$pass[$name]= new Variable(substr($t, 1)); | ||
} else if ($this->isConstant($result, $argument)) { | ||
$pass[$name]= $argument; | ||
} else { | ||
$t= $result->temp(); | ||
$pass[$name]= new Variable(substr($t, 1)); | ||
$init[$t]= $argument; | ||
} | ||
} | ||
|
||
// Initialize any non-constant expressions in place | ||
if ($init) { | ||
$result->out->write('['); | ||
foreach ($init as $t => $argument) { | ||
$result->out->write($t.'='); | ||
$this->emitOne($result, $argument); | ||
$result->out->write(','); | ||
} | ||
} | ||
|
||
// Emit closure invoking the callable expression | ||
$result->out->write('fn('.substr($sig, 1).')=>'); | ||
$this->emitOne($result, $callable->expression); | ||
$result->out->write('('); | ||
$this->emitArguments($result, $pass); | ||
$result->out->write(')'); | ||
$init && $result->out->write(']['.sizeof($init).']'); | ||
} else { | ||
parent::emitCallable($result, $callable); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.