Skip to content

Commit

Permalink
Map binary comparison operators
Browse files Browse the repository at this point in the history
  • Loading branch information
electroly committed Jun 12, 2024
1 parent 9dd8c3c commit 67d435e
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/compiler/emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,15 @@ static void emitBinaryExpression(const BinaryExpressionNode& expressionNode, Pro
auto rhsType = binarySuffix->rightOperand->evaluatedType;
emitExpression(*binarySuffix->rightOperand, state);
auto suffixResultType = binarySuffix->evaluatedType;
if (lhsType->kind == Kind::kNumber && rhsType->kind == Kind::kNumber) {

if (!lhsType->isValueType() && !rhsType->isValueType() &&
binarySuffix->binaryOperator == BinaryOperator::kEquals) {
state->syscall(Opcode::kSystemCallV, SystemCall::kObjectEquals, 0, 2);
} else if (
!lhsType->isValueType() && !rhsType->isValueType() &&
binarySuffix->binaryOperator == BinaryOperator::kNotEquals) {
state->syscall(Opcode::kSystemCallV, SystemCall::kObjectNotEquals, 0, 2);
} else if (lhsType->kind == Kind::kNumber && rhsType->kind == Kind::kNumber) {
SystemCall systemCall{};
switch (binarySuffix->binaryOperator) {
case BinaryOperator::kEquals:
Expand Down Expand Up @@ -596,12 +604,6 @@ static void emitBinaryExpression(const BinaryExpressionNode& expressionNode, Pro
state->syscall(Opcode::kSystemCallO, SystemCall::kObjectListConcat, 0, 2);
}
break;
case BinaryOperator::kEquals:
state->syscall(Opcode::kSystemCallV, SystemCall::kObjectEquals, 0, 2);
break;
case BinaryOperator::kNotEquals:
state->syscall(Opcode::kSystemCallV, SystemCall::kObjectNotEquals, 0, 2);
break;
default:
throw std::runtime_error("not impl");
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/CompilerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,12 @@ COMPILER_TEST(maps, Map_plus_operator_1)
COMPILER_TEST(maps, Map_plus_operator_2)
COMPILER_TEST(maps, Map_plus_operator_3)
COMPILER_TEST(maps, dim_map_with_yield_list)
COMPILER_TEST(maps, map_comparison_equals)
COMPILER_TEST(maps, map_comparison_greater_than)
COMPILER_TEST(maps, map_comparison_greater_than_equals)
COMPILER_TEST(maps, map_comparison_less_than)
COMPILER_TEST(maps, map_comparison_less_than_equals)
COMPILER_TEST(maps, map_comparison_not_equals)
COMPILER_TEST(maps, map_number_number_not_found)
COMPILER_TEST(maps, map_number_number_set)
COMPILER_TEST(maps, map_number_record_not_found)
Expand Down
17 changes: 17 additions & 0 deletions src/test/programs/maps/map_comparison_equals.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
sub Main()
dim a as Map from Number to Number
a(5) = 10
a(6) = 11
dim b as Map from Number to Number
b(6) = 11
b(5) = 10
dim c as Map from Number to Number
c(50) = 100
if a = a then print "a = a"
if a = b then print "a = b"
if a = c then print "a = c"
end sub

--output--
a = a
a = b
12 changes: 12 additions & 0 deletions src/test/programs/maps/map_comparison_greater_than.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sub Main()
dim a as Map from Number to Number
dim b as Map from Number to Number
if a > b then print "wrong"
end sub

--output--
Compiler error
kTypeMismatch
Main
4:10
The ">" operator does not support the type "Map from Number to Number".
12 changes: 12 additions & 0 deletions src/test/programs/maps/map_comparison_greater_than_equals.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sub Main()
dim a as Map from Number to Number
dim b as Map from Number to Number
if a >= b then print "wrong"
end sub

--output--
Compiler error
kTypeMismatch
Main
4:10
The ">=" operator does not support the type "Map from Number to Number".
12 changes: 12 additions & 0 deletions src/test/programs/maps/map_comparison_less_than.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sub Main()
dim a as Map from Number to Number
dim b as Map from Number to Number
if a < b then print "wrong"
end sub

--output--
Compiler error
kTypeMismatch
Main
4:10
The "<" operator does not support the type "Map from Number to Number".
12 changes: 12 additions & 0 deletions src/test/programs/maps/map_comparison_less_than_equals.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sub Main()
dim a as Map from Number to Number
dim b as Map from Number to Number
if a <= b then print "wrong"
end sub

--output--
Compiler error
kTypeMismatch
Main
4:10
The "<=" operator does not support the type "Map from Number to Number".
15 changes: 15 additions & 0 deletions src/test/programs/maps/map_comparison_not_equals.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sub Main()
dim a as Map from Number to Number
a(5) = 10
a(6) = 11
dim b as Map from Number to Number
b(6) = 11
b(5) = 10
dim c as Map from Number to Number
c(50) = 100
if a <> b then print "a <> b"
if a <> c then print "a <> c"
end sub

--output--
a <> c

0 comments on commit 67d435e

Please sign in to comment.