Skip to content

Commit 174bb3f

Browse files
committed
WIP: Preparation for BinaryOperandNode with $left and $right instead 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
1 parent 4e4175e commit 174bb3f

File tree

5 files changed

+229
-150
lines changed

5 files changed

+229
-150
lines changed

src/Parser/Ast/BinaryOperandNodes.php

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,16 @@
2525
use PackageFactory\ComponentEngine\Definition\BinaryOperator;
2626
use PackageFactory\ComponentEngine\Parser\Tokenizer\Scanner;
2727
use PackageFactory\ComponentEngine\Parser\Tokenizer\Token;
28-
use PackageFactory\ComponentEngine\Parser\Tokenizer\TokenType;
2928

3029
/**
3130
* @implements \IteratorAggregate<ExpressionNode>
3231
*/
3332
final class BinaryOperandNodes implements \IteratorAggregate, \JsonSerializable
3433
{
35-
/**
36-
* @var ExpressionNode[]
37-
*/
38-
public readonly array $rest;
39-
4034
private function __construct(
4135
public readonly ExpressionNode $first,
42-
ExpressionNode ...$rest
36+
public readonly ExpressionNode $second
4337
) {
44-
$this->rest = $rest;
4538
}
4639

4740
/**
@@ -55,48 +48,23 @@ public static function fromTokens(ExpressionNode $first, \Iterator $tokens, Bina
5548
$precedence = $operator->toPrecedence();
5649
$operands = [$first];
5750

58-
while (true) {
59-
Scanner::skipSpaceAndComments($tokens);
60-
61-
$operands[] = ExpressionNode::fromTokens($tokens, $precedence);
62-
63-
Scanner::skipSpaceAndComments($tokens);
51+
Scanner::skipSpaceAndComments($tokens);
6452

65-
if (Scanner::isEnd($tokens)) {
66-
break;
67-
}
53+
$operands[] = ExpressionNode::fromTokens($tokens, $precedence);
6854

69-
switch (Scanner::type($tokens)) {
70-
case TokenType::BRACKET_ROUND_CLOSE:
71-
case TokenType::BRACKET_CURLY_CLOSE:
72-
case TokenType::BRACKET_SQUARE_CLOSE:
73-
case TokenType::ARROW_SINGLE:
74-
case TokenType::QUESTIONMARK:
75-
break 2;
76-
case $operator->toTokenType():
77-
Scanner::skipOne($tokens);
78-
break;
79-
default:
80-
if ($precedence->mustStopAt(Scanner::type($tokens))) {
81-
break 2;
82-
} else {
83-
Scanner::assertType($tokens, $operator->toTokenType());
84-
}
85-
break;
86-
}
87-
}
55+
Scanner::skipSpaceAndComments($tokens);
8856

8957
return new self(...$operands);
9058
}
9159

9260
public function getIterator(): \Traversable
9361
{
9462
yield $this->first;
95-
yield from $this->rest;
63+
yield $this->second;
9664
}
9765

9866
public function jsonSerialize(): mixed
9967
{
100-
return [$this->first, ...$this->rest];
68+
return [$this->first, $this->second];
10169
}
10270
}

src/Target/Php/Transpiler/BinaryOperation/BinaryOperationTranspiler.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,10 @@ public function transpile(BinaryOperationNode $binaryOperationNode): string
5959
shouldAddQuotesIfNecessary: true
6060
);
6161

62-
$result = $expressionTranspiler->transpile($binaryOperationNode->operands->first);
63-
$operator = sprintf(' %s ', $this->transpileBinaryOperator($binaryOperationNode->operator));
62+
$first = $expressionTranspiler->transpile($binaryOperationNode->operands->first);
63+
$operator = $this->transpileBinaryOperator($binaryOperationNode->operator);
64+
$second = $expressionTranspiler->transpile($binaryOperationNode->operands->second);
6465

65-
foreach ($binaryOperationNode->operands->rest as $operandNode) {
66-
$result .= $operator;
67-
$result .= $expressionTranspiler->transpile($operandNode);
68-
}
69-
70-
return sprintf('(%s)', $result);
66+
return sprintf('(%s %s %s)', $first, $operator, $second);
7167
}
7268
}

0 commit comments

Comments
 (0)