Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
namespace PackageFactory\ComponentEngine\Target\Php\Transpiler\BinaryOperation;

use PackageFactory\ComponentEngine\Definition\BinaryOperator;
use PackageFactory\ComponentEngine\Parser\Ast\BinaryOperandNodes;
use PackageFactory\ComponentEngine\Parser\Ast\BinaryOperationNode;
use PackageFactory\ComponentEngine\Target\Php\Transpiler\Expression\ExpressionTranspiler;
use PackageFactory\ComponentEngine\TypeSystem\Resolver\BinaryOperation\BinaryOperationTypeResolver;
use PackageFactory\ComponentEngine\TypeSystem\ScopeInterface;
use PackageFactory\ComponentEngine\TypeSystem\Type\StringType\StringType;

final class BinaryOperationTranspiler
{
Expand All @@ -51,7 +54,7 @@ private function transpileBinaryOperator(BinaryOperator $binaryOperator): string
BinaryOperator::LESS_THAN_OR_EQUAL => '<='
};
}

public function transpile(BinaryOperationNode $binaryOperationNode): string
{
$expressionTranspiler = new ExpressionTranspiler(
Expand All @@ -60,7 +63,16 @@ public function transpile(BinaryOperationNode $binaryOperationNode): string
);

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

$binaryOperationTypeResolver = new BinaryOperationTypeResolver(scope: $this->scope);

if ($binaryOperationNode->operator === BinaryOperator::PLUS
&& $binaryOperationTypeResolver->resolveTypeOf($binaryOperationNode)->is(StringType::get())
) {
$operator = ' . ';
Comment on lines +69 to +72
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes this is partially weird, i initially had the logic checkIfAnyOfBinaryOperandNodesIsOfTypeString in this class, but then rembered the BinaryOperationTypeResolver should implement it.
This way we make indirectly use of it

} else {
$operator = sprintf(' %s ', $this->transpileBinaryOperator($binaryOperationNode->operator));
}

foreach ($binaryOperationNode->operands->rest as $operandNode) {
$result .= $operator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use PackageFactory\ComponentEngine\TypeSystem\ScopeInterface;
use PackageFactory\ComponentEngine\TypeSystem\Type\BooleanType\BooleanType;
use PackageFactory\ComponentEngine\TypeSystem\Type\NumberType\NumberType;
use PackageFactory\ComponentEngine\TypeSystem\Type\StringType\StringType;
use PackageFactory\ComponentEngine\TypeSystem\Type\UnionType\UnionType;
use PackageFactory\ComponentEngine\TypeSystem\TypeInterface;

Expand All @@ -41,6 +42,11 @@ public function __construct(

public function resolveTypeOf(BinaryOperationNode $binaryOperationNode): TypeInterface
{
if ($binaryOperationNode->operator === BinaryOperator::PLUS
&& $this->checkIfAnyOfBinaryOperandNodesIsOfTypeString($binaryOperationNode->operands)) {
return StringType::get();
}

return match ($binaryOperationNode->operator) {
BinaryOperator::AND,
BinaryOperator::OR => $this->resolveTypeOfBooleanOperation($binaryOperationNode->operands),
Expand Down Expand Up @@ -92,4 +98,18 @@ private function resolveTypeOfArithmeticOperation(BinaryOperandNodes $operandNod

return $numberType;
}

private function checkIfAnyOfBinaryOperandNodesIsOfTypeString(BinaryOperandNodes $operands): bool
{
$expressionTypeResolver = new ExpressionTypeResolver(
scope: $this->scope
);
foreach ($operands as $operand) {
$operandResolvesToTypeString = $expressionTypeResolver->resolveTypeOf($operand)->is(StringType::get());
if ($operandResolvesToTypeString) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use PackageFactory\ComponentEngine\Parser\Ast\ExpressionNode;
use PackageFactory\ComponentEngine\Test\Unit\TypeSystem\Scope\Fixtures\DummyScope;
use PackageFactory\ComponentEngine\Target\Php\Transpiler\BinaryOperation\BinaryOperationTranspiler;
use PackageFactory\ComponentEngine\TypeSystem\Type\NumberType\NumberType;
use PackageFactory\ComponentEngine\TypeSystem\Type\StringType\StringType;
use PHPUnit\Framework\TestCase;

final class BinaryOperationTranspilerTest extends TestCase
Expand Down Expand Up @@ -102,8 +104,19 @@ public function binaryOperationExamples(): array
];
}

public function stringConcatenationExamples(): array
{
return [
'"foo" + "bar"' => ['"foo" + "bar"', '(\'foo\' . \'bar\')'],
'someString + "bar"' => ['someString + "bar"', '($this->someString . \'bar\')'],
'8 + 15 + 42 + someString' => ['8 + 15 + 42 + someString', '(8 . 15 . 42 . $this->someString)'],
'someNumber + someString' => ['someNumber + someString', '($this->someNumber . $this->someString)']
];
}

/**
* @dataProvider binaryOperationExamples
* @dataProvider stringConcatenationExamples
* @test
* @param string $binaryOperationAsString
* @param string $expectedTranspilationResult
Expand All @@ -112,7 +125,12 @@ public function binaryOperationExamples(): array
public function transpilesBinaryOperationNodes(string $binaryOperationAsString, string $expectedTranspilationResult): void
{
$binaryOperationTranspiler = new BinaryOperationTranspiler(
scope: new DummyScope()
scope: new DummyScope(identifierToTypeMap: [
"a" => NumberType::get(),
"b" => NumberType::get(),
"someString" => StringType::get(),
"someNumber" => NumberType::get(),
])
);
$binaryOperationNode = ExpressionNode::fromString($binaryOperationAsString)->root;
assert($binaryOperationNode instanceof BinaryOperationNode);
Expand All @@ -126,4 +144,4 @@ public function transpilesBinaryOperationNodes(string $binaryOperationAsString,
$actualTranspilationResult
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,30 @@ public function binaryOperationExamples(): array
];
}

public function stringConcatenationExamples(): array
{
return [
'"foo" + "bar"' => ['"foo" + "mhs"', StringType::get()],
'someString + "bar"' => ['someString + "bar"', StringType::get()],
'8 + 15 + 42 + someString' => ['8 + 15 + 42 + someString', StringType::get()],
'someNumber + someString' => ['someNumber + someString', StringType::get()]
];
}

/**
* @dataProvider binaryOperationExamples
* @dataProvider stringConcatenationExamples
* @test
* @param string $binaryOperationAsString
* @param TypeInterface $expectedType
* @return void
*/
public function resolvesBinaryOperationToResultingType(string $binaryOperationAsString, TypeInterface $expectedType): void
{
$scope = new DummyScope();
$scope = new DummyScope(identifierToTypeMap: [
"someString" => StringType::get(),
"someNumber" => NumberType::get()
]);
$binaryOperationTypeResolver = new BinaryOperationTypeResolver(
scope: $scope
);
Expand Down