Skip to content

Commit 8bf9426

Browse files
authored
Merge pull request #16 from PackageFactory/taks/binaryExpressionAstWithOnlyRightAndLeft
TASK Binary expression ast with only two operands: left and right
2 parents d78a07a + 7d37923 commit 8bf9426

File tree

8 files changed

+252
-233
lines changed

8 files changed

+252
-233
lines changed

src/Parser/Ast/BinaryOperandNodes.php

Lines changed: 0 additions & 102 deletions
This file was deleted.

src/Parser/Ast/BinaryOperationNode.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
final class BinaryOperationNode implements \JsonSerializable
3030
{
3131
private function __construct(
32+
public readonly ExpressionNode $left,
3233
public readonly BinaryOperator $operator,
33-
public readonly BinaryOperandNodes $operands
34+
public readonly ExpressionNode $right
3435
) {
3536
}
3637

@@ -46,11 +47,18 @@ public static function fromTokens(ExpressionNode $left, \Iterator $tokens): self
4647

4748
Scanner::skipOne($tokens);
4849

49-
$operands = BinaryOperandNodes::fromTokens($left, $tokens, $operator);
50+
$precedence = $operator->toPrecedence();
51+
52+
Scanner::skipSpaceAndComments($tokens);
53+
54+
$right = ExpressionNode::fromTokens($tokens, $precedence);
55+
56+
Scanner::skipSpaceAndComments($tokens);
5057

5158
return new self(
59+
left: $left,
5260
operator: $operator,
53-
operands: $operands
61+
right: $right
5462
);
5563
}
5664

@@ -60,7 +68,7 @@ public function jsonSerialize(): mixed
6068
'type' => 'BinaryOperationNode',
6169
'payload' => [
6270
'operator' => $this->operator,
63-
'operands' => $this->operands
71+
'operands' => [$this->left, $this->right]
6472
]
6573
];
6674
}

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+
$left = $expressionTranspiler->transpile($binaryOperationNode->left);
63+
$operator = $this->transpileBinaryOperator($binaryOperationNode->operator);
64+
$right = $expressionTranspiler->transpile($binaryOperationNode->right);
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)', $left, $operator, $right);
7167
}
7268
}

src/TypeSystem/Resolver/BinaryOperation/BinaryOperationTypeResolver.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
namespace PackageFactory\ComponentEngine\TypeSystem\Resolver\BinaryOperation;
2424

2525
use PackageFactory\ComponentEngine\Definition\BinaryOperator;
26-
use PackageFactory\ComponentEngine\Parser\Ast\BinaryOperandNodes;
2726
use PackageFactory\ComponentEngine\Parser\Ast\BinaryOperationNode;
2827
use PackageFactory\ComponentEngine\TypeSystem\Resolver\Expression\ExpressionTypeResolver;
2928
use PackageFactory\ComponentEngine\TypeSystem\ScopeInterface;
@@ -43,13 +42,13 @@ public function resolveTypeOf(BinaryOperationNode $binaryOperationNode): TypeInt
4342
{
4443
return match ($binaryOperationNode->operator) {
4544
BinaryOperator::AND,
46-
BinaryOperator::OR => $this->resolveTypeOfBooleanOperation($binaryOperationNode->operands),
45+
BinaryOperator::OR => $this->resolveTypeOfBooleanOperation($binaryOperationNode),
4746

4847
BinaryOperator::PLUS,
4948
BinaryOperator::MINUS,
5049
BinaryOperator::MULTIPLY_BY,
5150
BinaryOperator::DIVIDE_BY,
52-
BinaryOperator::MODULO => $this->resolveTypeOfArithmeticOperation($binaryOperationNode->operands),
51+
BinaryOperator::MODULO => $this->resolveTypeOfArithmeticOperation($binaryOperationNode),
5352

5453
BinaryOperator::EQUAL,
5554
BinaryOperator::NOT_EQUAL,
@@ -60,36 +59,31 @@ public function resolveTypeOf(BinaryOperationNode $binaryOperationNode): TypeInt
6059
};
6160
}
6261

63-
private function resolveTypeOfBooleanOperation(BinaryOperandNodes $operandNodes): TypeInterface
62+
private function resolveTypeOfBooleanOperation(BinaryOperationNode $binaryOperationNode): TypeInterface
6463
{
6564
$expressionTypeResolver = new ExpressionTypeResolver(
6665
scope: $this->scope
6766
);
68-
$operandTypes = [];
6967

70-
foreach ($operandNodes as $operandNode) {
71-
$operandTypes[] = $expressionTypeResolver->resolveTypeOf($operandNode);
72-
}
73-
74-
return UnionType::of(...$operandTypes);
68+
return UnionType::of(
69+
$expressionTypeResolver->resolveTypeOf($binaryOperationNode->left),
70+
$expressionTypeResolver->resolveTypeOf($binaryOperationNode->right)
71+
);
7572
}
7673

77-
private function resolveTypeOfArithmeticOperation(BinaryOperandNodes $operandNodes): TypeInterface
74+
private function resolveTypeOfArithmeticOperation(BinaryOperationNode $binaryOperationNode): TypeInterface
7875
{
7976
$expressionTypeResolver = new ExpressionTypeResolver(
8077
scope: $this->scope
8178
);
82-
$numberType = NumberType::get();
8379

84-
foreach ($operandNodes as $operandNode) {
80+
foreach ([$binaryOperationNode->left, $binaryOperationNode->right] as $operandNode) {
8581
$typeOfOperandNode = $expressionTypeResolver->resolveTypeOf($operandNode);
86-
$typeOfOperandNodeIsNumberType = $typeOfOperandNode->is($numberType);
87-
88-
if (!$typeOfOperandNodeIsNumberType) {
82+
if (!$typeOfOperandNode->is(NumberType::get())) {
8983
throw new \Exception('@TODO: Operand must be of type number');
9084
}
9185
}
9286

93-
return $numberType;
87+
return NumberType::get();
9488
}
9589
}

0 commit comments

Comments
 (0)