Conversation
…perands: `true && true && true`
…of multiple operands i have the feeling this will make working with the BinaryOperandNode (eg type resolving/inference) easier. Also the php and js AST use the $left $right and $operator as a sideeffect it transpiles - transpile `true === true === true` correctly (with Parenthesis): `(true === true) === true` without Parenthesis its invalid php
mhsdesign
commented
Apr 23, 2023
| public function render(): string | ||
| { | ||
| return (string) (0 + 1234567890 + 42 + 0b10000000000000000000000000000000 + 0b01111111100000000000000000000000 + 0b00000000011111111111111111111111 + 0o755 + 0o644 + 0xFFFFFFFFFFFFFFFFF + 0x123456789ABCDEF + 0xA + 1E3 + 2e6 + 123.456 + 0.1e2 + .22); | ||
| return (string) (((((((((((((((0 + 1234567890) + 42) + 0b10000000000000000000000000000000) + 0b01111111100000000000000000000000) + 0b00000000011111111111111111111111) + 0o755) + 0o644) + 0xFFFFFFFFFFFFFFFFF) + 0x123456789ABCDEF) + 0xA) + 1E3) + 2e6) + 123.456) + 0.1e2) + .22); |
Contributor
Author
There was a problem hiding this comment.
if you prefer not having those parenthesis here, i experimented with a shouldAddParenthesisIfNecessary flag, this way we could transpile without parenthesis (if the operand is the same in chained binary ops)
public function transpile(BinaryOperationNode $binaryOperationNode): string
{
$expressionTranspiler = new ExpressionTranspiler(
scope: $this->scope,
shouldAddQuotesIfNecessary: true,
shouldAddParenthesisIfNecessary: !(
$binaryOperationNode->operands->first->root instanceof BinaryOperationNode
&& $binaryOperationNode->operands->first->root->operator === $binaryOperationNode->operator
)
);
$first = $expressionTranspiler->transpile($binaryOperationNode->operands->first);
$operator = $this->transpileBinaryOperator($binaryOperationNode->operator);
$second = $expressionTranspiler->transpile($binaryOperationNode->operands->second);
if ($this->shouldAddParenthesis) {
return sprintf('(%s %s %s)', $first, $operator, $second);
} else {
return sprintf('%s %s %s', $first, $operator, $second);
}
}
Member
There was a problem hiding this comment.
I don't mind those :) All's good 👍
Contributor
Author
grebaldi
approved these changes
Apr 27, 2023
Member
grebaldi
left a comment
There was a problem hiding this comment.
- Tests are green
- One fewer class
- Waaaay better, semantically
- Everything gets easier
What's not to like? 😃
Thanks a lot @mhsdesign!
Contributor
Author
|
:D |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
... instead of multiple operands
i inlined $left and $right into the BinaryOperationNode:
this will make working with the BinaryOperandNode (eg type resolving/narrowing) much easier.
Also the php and js AST also use the fields $left $right and $operator instead of a collection (even when the same operator is used).
as a sideeffect we now also transpile
true === true === truecorrectly to php (by wrapping it in parenthesis):(true === true) === truewithout parenthesis it is not valid php (its valid ecmascript though ^^)
The above mentioned case with
=== trueis obviously an edge case and currently1 + 1 + 1is also transpiled with parenthesis:(1 + 1) + 1which are optional and not needed by php - we still need to decide if we like this see #16 (comment).