Skip to content

Conversation

@mhsdesign
Copy link
Contributor

@mhsdesign mhsdesign commented Apr 23, 2023

... instead of multiple operands

i inlined $left and $right into the BinaryOperationNode:

final class BinaryOperationNode
{
    private function __construct(
        public readonly ExpressionNode $left,
        public readonly BinaryOperator $operator,
        public readonly ExpressionNode $right
    ) {
    }
}

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 === true correctly to php (by wrapping it in parenthesis): (true === true) === true
without parenthesis it is not valid php (its valid ecmascript though ^^)

The above mentioned case with === true is obviously an edge case and currently 1 + 1 + 1 is also transpiled with parenthesis: (1 + 1) + 1 which are optional and not needed by php - we still need to decide if we like this see #16 (comment).

…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 mhsdesign requested a review from grebaldi April 23, 2023 13:34
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
    }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind those :) All's good 👍

@mhsdesign
Copy link
Contributor Author

mhsdesign commented Apr 24, 2023

This pr will also make implementing #8 and #14 easier

@mhsdesign mhsdesign changed the title WIP: TASK Binary expression ast with only right and left DISCUSSION: WIP: TASK Binary expression ast with only right and left Apr 24, 2023
@mhsdesign mhsdesign added the help wanted Extra attention is needed label Apr 24, 2023
@mhsdesign mhsdesign changed the title DISCUSSION: WIP: TASK Binary expression ast with only right and left TASK Binary expression ast with only right and left Apr 26, 2023
@mhsdesign mhsdesign marked this pull request as ready for review April 26, 2023 08:40
@mhsdesign mhsdesign removed the help wanted Extra attention is needed label Apr 26, 2023
@mhsdesign mhsdesign changed the title TASK Binary expression ast with only right and left TASK Binary expression ast with only two operands: right and left Apr 26, 2023
@mhsdesign mhsdesign changed the title TASK Binary expression ast with only two operands: right and left TASK Binary expression ast with only two operands: left and right Apr 26, 2023
Copy link
Member

@grebaldi grebaldi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Tests are green
  • One fewer class
  • Waaaay better, semantically
  • Everything gets easier

What's not to like? 😃

Thanks a lot @mhsdesign!

@mhsdesign mhsdesign merged commit 8bf9426 into main Apr 28, 2023
@mhsdesign mhsdesign deleted the taks/binaryExpressionAstWithOnlyRightAndLeft branch April 28, 2023 21:54
@mhsdesign
Copy link
Contributor Author

:D

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants