Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 0 additions & 102 deletions src/Parser/Ast/BinaryOperandNodes.php

This file was deleted.

16 changes: 12 additions & 4 deletions src/Parser/Ast/BinaryOperationNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
final class BinaryOperationNode implements \JsonSerializable
{
private function __construct(
public readonly ExpressionNode $left,
public readonly BinaryOperator $operator,
public readonly BinaryOperandNodes $operands
public readonly ExpressionNode $right
) {
}

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

Scanner::skipOne($tokens);

$operands = BinaryOperandNodes::fromTokens($left, $tokens, $operator);
$precedence = $operator->toPrecedence();

Scanner::skipSpaceAndComments($tokens);

$right = ExpressionNode::fromTokens($tokens, $precedence);

Scanner::skipSpaceAndComments($tokens);

return new self(
left: $left,
operator: $operator,
operands: $operands
right: $right
);
}

Expand All @@ -60,7 +68,7 @@ public function jsonSerialize(): mixed
'type' => 'BinaryOperationNode',
'payload' => [
'operator' => $this->operator,
'operands' => $this->operands
'operands' => [$this->left, $this->right]
]
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,10 @@ public function transpile(BinaryOperationNode $binaryOperationNode): string
shouldAddQuotesIfNecessary: true
);

$result = $expressionTranspiler->transpile($binaryOperationNode->operands->first);
$operator = sprintf(' %s ', $this->transpileBinaryOperator($binaryOperationNode->operator));
$left = $expressionTranspiler->transpile($binaryOperationNode->left);
$operator = $this->transpileBinaryOperator($binaryOperationNode->operator);
$right = $expressionTranspiler->transpile($binaryOperationNode->right);

foreach ($binaryOperationNode->operands->rest as $operandNode) {
$result .= $operator;
$result .= $expressionTranspiler->transpile($operandNode);
}

return sprintf('(%s)', $result);
return sprintf('(%s %s %s)', $left, $operator, $right);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
namespace PackageFactory\ComponentEngine\TypeSystem\Resolver\BinaryOperation;

use PackageFactory\ComponentEngine\Definition\BinaryOperator;
use PackageFactory\ComponentEngine\Parser\Ast\BinaryOperandNodes;
use PackageFactory\ComponentEngine\Parser\Ast\BinaryOperationNode;
use PackageFactory\ComponentEngine\TypeSystem\Resolver\Expression\ExpressionTypeResolver;
use PackageFactory\ComponentEngine\TypeSystem\ScopeInterface;
Expand All @@ -43,13 +42,13 @@ public function resolveTypeOf(BinaryOperationNode $binaryOperationNode): TypeInt
{
return match ($binaryOperationNode->operator) {
BinaryOperator::AND,
BinaryOperator::OR => $this->resolveTypeOfBooleanOperation($binaryOperationNode->operands),
BinaryOperator::OR => $this->resolveTypeOfBooleanOperation($binaryOperationNode),

BinaryOperator::PLUS,
BinaryOperator::MINUS,
BinaryOperator::MULTIPLY_BY,
BinaryOperator::DIVIDE_BY,
BinaryOperator::MODULO => $this->resolveTypeOfArithmeticOperation($binaryOperationNode->operands),
BinaryOperator::MODULO => $this->resolveTypeOfArithmeticOperation($binaryOperationNode),

BinaryOperator::EQUAL,
BinaryOperator::NOT_EQUAL,
Expand All @@ -60,36 +59,31 @@ public function resolveTypeOf(BinaryOperationNode $binaryOperationNode): TypeInt
};
}

private function resolveTypeOfBooleanOperation(BinaryOperandNodes $operandNodes): TypeInterface
private function resolveTypeOfBooleanOperation(BinaryOperationNode $binaryOperationNode): TypeInterface
{
$expressionTypeResolver = new ExpressionTypeResolver(
scope: $this->scope
);
$operandTypes = [];

foreach ($operandNodes as $operandNode) {
$operandTypes[] = $expressionTypeResolver->resolveTypeOf($operandNode);
}

return UnionType::of(...$operandTypes);
return UnionType::of(
$expressionTypeResolver->resolveTypeOf($binaryOperationNode->left),
$expressionTypeResolver->resolveTypeOf($binaryOperationNode->right)
);
}

private function resolveTypeOfArithmeticOperation(BinaryOperandNodes $operandNodes): TypeInterface
private function resolveTypeOfArithmeticOperation(BinaryOperationNode $binaryOperationNode): TypeInterface
{
$expressionTypeResolver = new ExpressionTypeResolver(
scope: $this->scope
);
$numberType = NumberType::get();

foreach ($operandNodes as $operandNode) {
foreach ([$binaryOperationNode->left, $binaryOperationNode->right] as $operandNode) {
$typeOfOperandNode = $expressionTypeResolver->resolveTypeOf($operandNode);
$typeOfOperandNodeIsNumberType = $typeOfOperandNode->is($numberType);

if (!$typeOfOperandNodeIsNumberType) {
if (!$typeOfOperandNode->is(NumberType::get())) {
throw new \Exception('@TODO: Operand must be of type number');
}
}

return $numberType;
return NumberType::get();
}
}
Loading