Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list comparison #2025

Merged
merged 6 commits into from
Jul 9, 2023
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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ RUN(NAME test_list_repeat LABELS cpython llvm NOFAST)
RUN(NAME test_list_reverse LABELS cpython llvm)
RUN(NAME test_list_pop LABELS cpython llvm NOFAST) # TODO: Remove NOFAST from here.
RUN(NAME test_list_pop2 LABELS cpython llvm NOFAST) # TODO: Remove NOFAST from here.
RUN(NAME test_list_compare LABELS cpython llvm)
RUN(NAME test_tuple_01 LABELS cpython llvm c)
RUN(NAME test_tuple_02 LABELS cpython llvm c NOFAST)
RUN(NAME test_tuple_03 LABELS cpython llvm c)
Expand Down
53 changes: 53 additions & 0 deletions integration_tests/test_list_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from lpython import i32, f64

def test_list_compare():
l1: list[i32] = [1, 2, 3]
l2: list[i32] = [1, 2, 3, 4]
l3: list[tuple[i32, f64, str]] = [(1, 2.0, 'a'), (3, 4.0, 'b')]
l4: list[tuple[i32, f64, str]] = [(1, 3.0, 'a')]
l5: list[list[str]] = [[''], ['']]
l6: list[str] = []
l7: list[str] = []
t1: tuple[i32, i32]
t2: tuple[i32, i32]
i: i32

assert l1 < l2 and l1 <= l2
assert not l1 > l2 and not l1 >= l2
i = l2.pop()
i = l2.pop()
assert l2 < l1 and l1 > l2 and l1 >= l2
assert not (l1 < l2)

l1 = [3, 4, 5]
l2 = [1, 6, 7]
assert l1 > l2 and l1 >= l2
assert not l1 < l2 and not l1 <= l2

l1 = l2
assert l1 == l2 and l1 <= l2 and l1 >= l2
assert not l1 < l2 and not l1 > l2

assert l4 > l3 and l4 >= l3
l4[0] = l3[0]
assert l4 < l3

for i in range(0, 10):
if i % 2 == 0:
l6.append('a')
else:
l7.append('a')
l5[0] = l6
l5[1] = l7
if i % 2 == 0:
assert l5[1 - i % 2] < l5[i % 2]
assert l5[1 - i % 2] <= l5[i % 2]
assert not l5[1 - i % 2] > l5[i % 2]
assert not l5[1 - i % 2] >= l5[i % 2]

t1 = (1, 2)
t2 = (2, 3)
assert t1 < t2 and t1 <= t2
assert not t1 > t2 and not t1 >= t2

test_list_compare()
55 changes: 47 additions & 8 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1427,10 +1427,31 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
this->visit_expr(*x.m_right);
llvm::Value* right = tmp;
ptr_loads = ptr_loads_copy;
tmp = llvm_utils->is_equal_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left));
if (x.m_op == ASR::cmpopType::NotEq) {
tmp = builder->CreateNot(tmp);

ASR::ttype_t* int32_type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, 4));

if(x.m_op == ASR::cmpopType::Eq || x.m_op == ASR::cmpopType::NotEq) {
tmp = llvm_utils->is_equal_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left));
if (x.m_op == ASR::cmpopType::NotEq) {
tmp = builder->CreateNot(tmp);
}
}
else if(x.m_op == ASR::cmpopType::Lt) {
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left), 0, int32_type);
}
else if(x.m_op == ASR::cmpopType::LtE) {
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left), 1, int32_type);
}
else if(x.m_op == ASR::cmpopType::Gt) {
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left), 2, int32_type);
}
else if(x.m_op == ASR::cmpopType::GtE) {
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left), 3, int32_type);
}
}

Expand Down Expand Up @@ -1761,10 +1782,28 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
this->visit_expr(*x.m_right);
llvm::Value* right = tmp;
ptr_loads = ptr_loads_copy;
tmp = llvm_utils->is_equal_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left));
if (x.m_op == ASR::cmpopType::NotEq) {
tmp = builder->CreateNot(tmp);
if(x.m_op == ASR::cmpopType::Eq || x.m_op == ASR::cmpopType::NotEq) {
tmp = llvm_utils->is_equal_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left));
if (x.m_op == ASR::cmpopType::NotEq) {
tmp = builder->CreateNot(tmp);
}
}
else if(x.m_op == ASR::cmpopType::Lt) {
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left), 0);
}
else if(x.m_op == ASR::cmpopType::LtE) {
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left), 1);
}
else if(x.m_op == ASR::cmpopType::Gt) {
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left), 2);
}
else if(x.m_op == ASR::cmpopType::GtE) {
tmp = llvm_utils->is_ineq_by_value(left, right, *module,
ASRUtils::expr_type(x.m_left), 3);
}
}

Expand Down
Loading