Skip to content

Commit f2500fa

Browse files
authored
Add list comparison (#2025)
1 parent 4667708 commit f2500fa

File tree

6 files changed

+418
-12
lines changed

6 files changed

+418
-12
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ RUN(NAME test_list_repeat LABELS cpython llvm NOFAST)
457457
RUN(NAME test_list_reverse LABELS cpython llvm)
458458
RUN(NAME test_list_pop LABELS cpython llvm NOFAST) # TODO: Remove NOFAST from here.
459459
RUN(NAME test_list_pop2 LABELS cpython llvm NOFAST) # TODO: Remove NOFAST from here.
460+
RUN(NAME test_list_compare LABELS cpython llvm)
460461
RUN(NAME test_tuple_01 LABELS cpython llvm c)
461462
RUN(NAME test_tuple_02 LABELS cpython llvm c NOFAST)
462463
RUN(NAME test_tuple_03 LABELS cpython llvm c)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from lpython import i32, f64
2+
3+
def test_list_compare():
4+
l1: list[i32] = [1, 2, 3]
5+
l2: list[i32] = [1, 2, 3, 4]
6+
l3: list[tuple[i32, f64, str]] = [(1, 2.0, 'a'), (3, 4.0, 'b')]
7+
l4: list[tuple[i32, f64, str]] = [(1, 3.0, 'a')]
8+
l5: list[list[str]] = [[''], ['']]
9+
l6: list[str] = []
10+
l7: list[str] = []
11+
t1: tuple[i32, i32]
12+
t2: tuple[i32, i32]
13+
i: i32
14+
15+
assert l1 < l2 and l1 <= l2
16+
assert not l1 > l2 and not l1 >= l2
17+
i = l2.pop()
18+
i = l2.pop()
19+
assert l2 < l1 and l1 > l2 and l1 >= l2
20+
assert not (l1 < l2)
21+
22+
l1 = [3, 4, 5]
23+
l2 = [1, 6, 7]
24+
assert l1 > l2 and l1 >= l2
25+
assert not l1 < l2 and not l1 <= l2
26+
27+
l1 = l2
28+
assert l1 == l2 and l1 <= l2 and l1 >= l2
29+
assert not l1 < l2 and not l1 > l2
30+
31+
assert l4 > l3 and l4 >= l3
32+
l4[0] = l3[0]
33+
assert l4 < l3
34+
35+
for i in range(0, 10):
36+
if i % 2 == 0:
37+
l6.append('a')
38+
else:
39+
l7.append('a')
40+
l5[0] = l6
41+
l5[1] = l7
42+
if i % 2 == 0:
43+
assert l5[1 - i % 2] < l5[i % 2]
44+
assert l5[1 - i % 2] <= l5[i % 2]
45+
assert not l5[1 - i % 2] > l5[i % 2]
46+
assert not l5[1 - i % 2] >= l5[i % 2]
47+
48+
t1 = (1, 2)
49+
t2 = (2, 3)
50+
assert t1 < t2 and t1 <= t2
51+
assert not t1 > t2 and not t1 >= t2
52+
53+
test_list_compare()

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,10 +1427,31 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
14271427
this->visit_expr(*x.m_right);
14281428
llvm::Value* right = tmp;
14291429
ptr_loads = ptr_loads_copy;
1430-
tmp = llvm_utils->is_equal_by_value(left, right, *module,
1431-
ASRUtils::expr_type(x.m_left));
1432-
if (x.m_op == ASR::cmpopType::NotEq) {
1433-
tmp = builder->CreateNot(tmp);
1430+
1431+
ASR::ttype_t* int32_type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, 4));
1432+
1433+
if(x.m_op == ASR::cmpopType::Eq || x.m_op == ASR::cmpopType::NotEq) {
1434+
tmp = llvm_utils->is_equal_by_value(left, right, *module,
1435+
ASRUtils::expr_type(x.m_left));
1436+
if (x.m_op == ASR::cmpopType::NotEq) {
1437+
tmp = builder->CreateNot(tmp);
1438+
}
1439+
}
1440+
else if(x.m_op == ASR::cmpopType::Lt) {
1441+
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
1442+
ASRUtils::expr_type(x.m_left), 0, int32_type);
1443+
}
1444+
else if(x.m_op == ASR::cmpopType::LtE) {
1445+
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
1446+
ASRUtils::expr_type(x.m_left), 1, int32_type);
1447+
}
1448+
else if(x.m_op == ASR::cmpopType::Gt) {
1449+
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
1450+
ASRUtils::expr_type(x.m_left), 2, int32_type);
1451+
}
1452+
else if(x.m_op == ASR::cmpopType::GtE) {
1453+
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
1454+
ASRUtils::expr_type(x.m_left), 3, int32_type);
14341455
}
14351456
}
14361457

@@ -1761,10 +1782,28 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
17611782
this->visit_expr(*x.m_right);
17621783
llvm::Value* right = tmp;
17631784
ptr_loads = ptr_loads_copy;
1764-
tmp = llvm_utils->is_equal_by_value(left, right, *module,
1765-
ASRUtils::expr_type(x.m_left));
1766-
if (x.m_op == ASR::cmpopType::NotEq) {
1767-
tmp = builder->CreateNot(tmp);
1785+
if(x.m_op == ASR::cmpopType::Eq || x.m_op == ASR::cmpopType::NotEq) {
1786+
tmp = llvm_utils->is_equal_by_value(left, right, *module,
1787+
ASRUtils::expr_type(x.m_left));
1788+
if (x.m_op == ASR::cmpopType::NotEq) {
1789+
tmp = builder->CreateNot(tmp);
1790+
}
1791+
}
1792+
else if(x.m_op == ASR::cmpopType::Lt) {
1793+
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
1794+
ASRUtils::expr_type(x.m_left), 0);
1795+
}
1796+
else if(x.m_op == ASR::cmpopType::LtE) {
1797+
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
1798+
ASRUtils::expr_type(x.m_left), 1);
1799+
}
1800+
else if(x.m_op == ASR::cmpopType::Gt) {
1801+
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
1802+
ASRUtils::expr_type(x.m_left), 2);
1803+
}
1804+
else if(x.m_op == ASR::cmpopType::GtE) {
1805+
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
1806+
ASRUtils::expr_type(x.m_left), 3);
17681807
}
17691808
}
17701809

0 commit comments

Comments
 (0)