Skip to content

Commit 38e51cf

Browse files
committed
TASK: Remove arithmetic operators from BinaryOperator enum
1 parent f94e242 commit 38e51cf

File tree

20 files changed

+99
-311
lines changed

20 files changed

+99
-311
lines changed

src/Definition/BinaryOperator.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ enum BinaryOperator: string
2929
case AND = 'AND';
3030
case OR = 'OR';
3131

32-
case PLUS = 'PLUS';
33-
case MINUS = 'MINUS';
34-
case MULTIPLY_BY = 'MULTIPLY_BY';
35-
case DIVIDE_BY = 'DIVIDE_BY';
36-
case MODULO = 'MODULO';
37-
3832
case EQUAL = 'EQUAL';
3933
case NOT_EQUAL = 'NOT_EQUAL';
4034
case GREATER_THAN = 'GREATER_THAN';
@@ -48,12 +42,6 @@ public static function fromTokenType(TokenType $tokenType): self
4842
TokenType::OPERATOR_BOOLEAN_AND => self::AND,
4943
TokenType::OPERATOR_BOOLEAN_OR => self::OR,
5044

51-
TokenType::OPERATOR_ARITHMETIC_PLUS => self::PLUS,
52-
TokenType::OPERATOR_ARITHMETIC_MINUS => self::MINUS,
53-
TokenType::OPERATOR_ARITHMETIC_MULTIPLY_BY => self::MULTIPLY_BY,
54-
TokenType::OPERATOR_ARITHMETIC_DIVIDE_BY => self::DIVIDE_BY,
55-
TokenType::OPERATOR_ARITHMETIC_MODULO => self::MODULO,
56-
5745
TokenType::COMPARATOR_EQUAL => self::EQUAL,
5846
TokenType::COMPARATOR_NOT_EQUAL => self::NOT_EQUAL,
5947
TokenType::COMPARATOR_GREATER_THAN => self::GREATER_THAN,
@@ -72,13 +60,6 @@ public function toPrecedence(): Precedence
7260

7361
self::OR => Precedence::LOGICAL_OR,
7462

75-
self::PLUS,
76-
self::MINUS => Precedence::DASH,
77-
78-
self::MULTIPLY_BY,
79-
self::DIVIDE_BY,
80-
self::MODULO => Precedence::POINT,
81-
8263
self::EQUAL,
8364
self::NOT_EQUAL => Precedence::EQUALITY,
8465

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ private function transpileBinaryOperator(BinaryOperator $binaryOperator): string
3838
return match ($binaryOperator) {
3939
BinaryOperator::AND => '&&',
4040
BinaryOperator::OR => '||',
41-
BinaryOperator::PLUS => '+',
42-
BinaryOperator::MINUS => '-',
43-
BinaryOperator::MULTIPLY_BY => '*',
44-
BinaryOperator::DIVIDE_BY => '/',
45-
BinaryOperator::MODULO => '%',
4641
BinaryOperator::EQUAL => '===',
4742
BinaryOperator::NOT_EQUAL => '!==',
4843
BinaryOperator::GREATER_THAN => '>',

src/TypeSystem/Resolver/BinaryOperation/BinaryOperationTypeResolver.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
use PackageFactory\ComponentEngine\TypeSystem\Resolver\Expression\ExpressionTypeResolver;
2828
use PackageFactory\ComponentEngine\TypeSystem\ScopeInterface;
2929
use PackageFactory\ComponentEngine\TypeSystem\Type\BooleanType\BooleanType;
30-
use PackageFactory\ComponentEngine\TypeSystem\Type\NumberType\NumberType;
3130
use PackageFactory\ComponentEngine\TypeSystem\Type\UnionType\UnionType;
3231
use PackageFactory\ComponentEngine\TypeSystem\TypeInterface;
3332

@@ -44,12 +43,6 @@ public function resolveTypeOf(BinaryOperationNode $binaryOperationNode): TypeInt
4443
BinaryOperator::AND,
4544
BinaryOperator::OR => $this->resolveTypeOfBooleanOperation($binaryOperationNode),
4645

47-
BinaryOperator::PLUS,
48-
BinaryOperator::MINUS,
49-
BinaryOperator::MULTIPLY_BY,
50-
BinaryOperator::DIVIDE_BY,
51-
BinaryOperator::MODULO => $this->resolveTypeOfArithmeticOperation($binaryOperationNode),
52-
5346
BinaryOperator::EQUAL,
5447
BinaryOperator::NOT_EQUAL,
5548
BinaryOperator::GREATER_THAN,
@@ -70,20 +63,4 @@ private function resolveTypeOfBooleanOperation(BinaryOperationNode $binaryOperat
7063
$expressionTypeResolver->resolveTypeOf($binaryOperationNode->right)
7164
);
7265
}
73-
74-
private function resolveTypeOfArithmeticOperation(BinaryOperationNode $binaryOperationNode): TypeInterface
75-
{
76-
$expressionTypeResolver = new ExpressionTypeResolver(
77-
scope: $this->scope
78-
);
79-
80-
foreach ([$binaryOperationNode->left, $binaryOperationNode->right] as $operandNode) {
81-
$typeOfOperandNode = $expressionTypeResolver->resolveTypeOf($operandNode);
82-
if (!$typeOfOperandNode->is(NumberType::get())) {
83-
throw new \Exception('@TODO: Operand must be of type number');
84-
}
85-
}
86-
87-
return NumberType::get();
88-
}
8966
}

test/Integration/Examples/Expression/Expression.afx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ export component Expression {
33
b: number
44

55
return a <= 120
6-
? b * a + (17 % b)
7-
: b / a
8-
}
6+
? b || a || 17
7+
: b && a
8+
}

test/Integration/Examples/Expression/Expression.ast.json

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@
5656
"true": {
5757
"type": "BinaryOperationNode",
5858
"payload": {
59-
"operator": "PLUS",
59+
"operator": "OR",
6060
"operands": [
6161
{
6262
"type": "BinaryOperationNode",
6363
"payload": {
64-
"operator": "MULTIPLY_BY",
64+
"operator": "OR",
6565
"operands": [
6666
{
6767
"type": "Identifier",
@@ -75,22 +75,10 @@
7575
}
7676
},
7777
{
78-
"type": "BinaryOperationNode",
78+
"type": "IntegerLiteralNode",
7979
"payload": {
80-
"operator": "MODULO",
81-
"operands": [
82-
{
83-
"type": "IntegerLiteralNode",
84-
"payload": {
85-
"value": "17",
86-
"format": "DECIMAL"
87-
}
88-
},
89-
{
90-
"type": "Identifier",
91-
"payload": "b"
92-
}
93-
]
80+
"value": "17",
81+
"format": "DECIMAL"
9482
}
9583
}
9684
]
@@ -99,7 +87,7 @@
9987
"false": {
10088
"type": "BinaryOperationNode",
10189
"payload": {
102-
"operator": "DIVIDE_BY",
90+
"operator": "AND",
10391
"operands": [
10492
{
10593
"type": "Identifier",

test/Integration/Examples/Expression/Expression.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public function __construct(
1616

1717
public function render(): string
1818
{
19-
return (string) (($this->a <= 120) ? (($this->b * $this->a) + (17 % $this->b)) : ($this->b / $this->a));
19+
return (string) (($this->a <= 120) ? (($this->b || $this->a) || 17) : ($this->b && $this->a));
2020
}
2121
}

test/Integration/Examples/Expression/Expression.tokens.json

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@
140140
"value": " "
141141
},
142142
{
143-
"type": "OPERATOR_ARITHMETIC_MULTIPLY_BY",
144-
"value": "*"
143+
"type": "OPERATOR_BOOLEAN_OR",
144+
"value": "||"
145145
},
146146
{
147147
"type": "SPACE",
@@ -156,41 +156,17 @@
156156
"value": " "
157157
},
158158
{
159-
"type": "OPERATOR_ARITHMETIC_PLUS",
160-
"value": "+"
159+
"type": "OPERATOR_BOOLEAN_OR",
160+
"value": "||"
161161
},
162162
{
163163
"type": "SPACE",
164164
"value": " "
165165
},
166-
{
167-
"type": "BRACKET_ROUND_OPEN",
168-
"value": "("
169-
},
170166
{
171167
"type": "NUMBER_DECIMAL",
172168
"value": "17"
173169
},
174-
{
175-
"type": "SPACE",
176-
"value": " "
177-
},
178-
{
179-
"type": "OPERATOR_ARITHMETIC_MODULO",
180-
"value": "%"
181-
},
182-
{
183-
"type": "SPACE",
184-
"value": " "
185-
},
186-
{
187-
"type": "STRING",
188-
"value": "b"
189-
},
190-
{
191-
"type": "BRACKET_ROUND_CLOSE",
192-
"value": ")"
193-
},
194170
{
195171
"type": "END_OF_LINE",
196172
"value": "\n"
@@ -216,8 +192,8 @@
216192
"value": " "
217193
},
218194
{
219-
"type": "OPERATOR_ARITHMETIC_DIVIDE_BY",
220-
"value": "/"
195+
"type": "OPERATOR_BOOLEAN_AND",
196+
"value": "&&"
221197
},
222198
{
223199
"type": "SPACE",
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
export component Numbers {
22
return
33
# Decimal
4-
0 +
5-
1234567890 +
6-
42 +
4+
0 ||
5+
1234567890 ||
6+
42 ||
77

88
# Binary
9-
0b10000000000000000000000000000000 +
10-
0b01111111100000000000000000000000 +
11-
0B00000000011111111111111111111111 +
9+
0b10000000000000000000000000000000 ||
10+
0b01111111100000000000000000000000 ||
11+
0B00000000011111111111111111111111 ||
1212

1313
# Octal
14-
0o755 +
15-
0o644 +
14+
0o755 ||
15+
0o644 ||
1616

1717
# Hexadecimal
18-
0xFFFFFFFFFFFFFFFFF +
19-
0x123456789ABCDEF +
20-
0xA +
18+
0xFFFFFFFFFFFFFFFFF ||
19+
0x123456789ABCDEF ||
20+
0xA ||
2121

2222
# With Exponent
23-
1E3 +
24-
2e6 +
23+
1E3 ||
24+
2e6 ||
2525

2626
# With Floating Point
27-
123.456 +
28-
0.1e2 +
27+
123.456 ||
28+
0.1e2 ||
2929
.22
3030
}

test/Integration/Examples/Numbers/Numbers.ast.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,77 +11,77 @@
1111
"returnExpression": {
1212
"type": "BinaryOperationNode",
1313
"payload": {
14-
"operator": "PLUS",
14+
"operator": "OR",
1515
"operands": [
1616
{
1717
"type": "BinaryOperationNode",
1818
"payload": {
19-
"operator": "PLUS",
19+
"operator": "OR",
2020
"operands": [
2121
{
2222
"type": "BinaryOperationNode",
2323
"payload": {
24-
"operator": "PLUS",
24+
"operator": "OR",
2525
"operands": [
2626
{
2727
"type": "BinaryOperationNode",
2828
"payload": {
29-
"operator": "PLUS",
29+
"operator": "OR",
3030
"operands": [
3131
{
3232
"type": "BinaryOperationNode",
3333
"payload": {
34-
"operator": "PLUS",
34+
"operator": "OR",
3535
"operands": [
3636
{
3737
"type": "BinaryOperationNode",
3838
"payload": {
39-
"operator": "PLUS",
39+
"operator": "OR",
4040
"operands": [
4141
{
4242
"type": "BinaryOperationNode",
4343
"payload": {
44-
"operator": "PLUS",
44+
"operator": "OR",
4545
"operands": [
4646
{
4747
"type": "BinaryOperationNode",
4848
"payload": {
49-
"operator": "PLUS",
49+
"operator": "OR",
5050
"operands": [
5151
{
5252
"type": "BinaryOperationNode",
5353
"payload": {
54-
"operator": "PLUS",
54+
"operator": "OR",
5555
"operands": [
5656
{
5757
"type": "BinaryOperationNode",
5858
"payload": {
59-
"operator": "PLUS",
59+
"operator": "OR",
6060
"operands": [
6161
{
6262
"type": "BinaryOperationNode",
6363
"payload": {
64-
"operator": "PLUS",
64+
"operator": "OR",
6565
"operands": [
6666
{
6767
"type": "BinaryOperationNode",
6868
"payload": {
69-
"operator": "PLUS",
69+
"operator": "OR",
7070
"operands": [
7171
{
7272
"type": "BinaryOperationNode",
7373
"payload": {
74-
"operator": "PLUS",
74+
"operator": "OR",
7575
"operands": [
7676
{
7777
"type": "BinaryOperationNode",
7878
"payload": {
79-
"operator": "PLUS",
79+
"operator": "OR",
8080
"operands": [
8181
{
8282
"type": "BinaryOperationNode",
8383
"payload": {
84-
"operator": "PLUS",
84+
"operator": "OR",
8585
"operands": [
8686
{
8787
"type": "IntegerLiteralNode",

test/Integration/Examples/Numbers/Numbers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ final class Numbers extends BaseClass
1010
{
1111
public function render(): string
1212
{
13-
return (string) (((((((((((((((0 + 1234567890) + 42) + 0b10000000000000000000000000000000) + 0b01111111100000000000000000000000) + 0b00000000011111111111111111111111) + 0o755) + 0o644) + 0xFFFFFFFFFFFFFFFFF) + 0x123456789ABCDEF) + 0xA) + 1E3) + 2e6) + 123.456) + 0.1e2) + .22);
13+
return (string) (((((((((((((((0 || 1234567890) || 42) || 0b10000000000000000000000000000000) || 0b01111111100000000000000000000000) || 0b00000000011111111111111111111111) || 0o755) || 0o644) || 0xFFFFFFFFFFFFFFFFF) || 0x123456789ABCDEF) || 0xA) || 1E3) || 2e6) || 123.456) || 0.1e2) || .22);
1414
}
1515
}

0 commit comments

Comments
 (0)