Skip to content

[ASR/LLVM]: Implement Tuple/List Compare #1342

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

Merged
merged 15 commits into from
Dec 12, 2022
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 @@ -290,6 +290,7 @@ RUN(NAME test_builtin_round LABELS cpython llvm c)
RUN(NAME test_builtin_divmod LABELS cpython llvm c)
RUN(NAME test_math1 LABELS cpython llvm c)
RUN(NAME test_math_02 LABELS cpython llvm)
RUN(NAME test_pass_compare LABELS cpython llvm)
RUN(NAME test_c_interop_01 LABELS cpython llvm c)
RUN(NAME test_c_interop_02 LABELS cpython llvm c
EXTRAFILES test_c_interop_02b.c)
Expand Down
61 changes: 61 additions & 0 deletions integration_tests/test_pass_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from ltypes import i32


def f():
a11: tuple[i32, i32]
b11: tuple[i32, i32]
Copy link
Collaborator

@czgdp1807 czgdp1807 Dec 5, 2022

Choose a reason for hiding this comment

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

Test for nested tuples as well with mixed types like, tuple[i32, tuple[i64, f32], str].

Copy link
Collaborator

@czgdp1807 czgdp1807 Dec 11, 2022

Choose a reason for hiding this comment

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

This case (see #1342 (comment)) is not yet covered in the tests. Please test for this first.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This case is not yet covered in the tests. Please test for this first.

I avoided using f32 because of precision issues in comparison. I added a test for the mixed types:

    a: tuple[tuple[i32, i32], str, bool]
    b: tuple[tuple[i32, i32], str, bool]
    a = (a11, 'ok', True)
    b = (b11, 'ok', True)
    assert a == b
    b = (b11, 'notok', True)
    assert a != b

a11 = (1, 2)
b11 = (1, 2)
assert a11 == b11
c11: tuple[i32, i32]
c11 = (1, 3)
assert a11 != c11 and c11 != b11
a: tuple[tuple[i32, i32], str, bool]
b: tuple[tuple[i32, i32], str, bool]
a = (a11, 'ok', True)
b = (b11, 'ok', True)
assert a == b
b = (b11, 'notok', True)
assert a != b


def g():
a11: list[i32]
b11: list[i32]
a11 = [1, 2, 3, 4]
b11 = [1, 2, 3, 4]
assert a11 == b11
a11.append(5)
assert a11 != b11
c11: list[i32] = []
assert a11 != c11

d11: list[str] = ['a', 'b', '^', '*']
e11: list[str] = ['a', 'b', '^']
assert d11 != e11
e11.append('*')
assert d11 == e11

f11: list[tuple[i32, i32]] = []
x: tuple[i32, i32]
i: i32
for i in range(10):
x = (i, i+2)
f11.append(x)

g11: list[tuple[i32, i32]] = []
for i in range(9):
x = (i, i+2)
g11.append(x)
assert g11 != f11
x = (9, 11)
g11.append(x)
assert g11 == f11


def check():
f()
g()


check()
2 changes: 2 additions & 0 deletions src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,14 @@ expr
| ListConstant(expr* args, ttype type)
| ListLen(expr arg, ttype type, expr? value)
| ListConcat(expr left, expr right, ttype type, expr? value)
| ListCompare(expr left, cmpop op, expr right, ttype type, expr? value)

| SetConstant(expr* elements, ttype type)
| SetLen(expr arg, ttype type, expr? value)

| TupleConstant(expr* elements, ttype type)
| TupleLen(expr arg, ttype type, expr value)
| TupleCompare(expr left, cmpop op, expr right, ttype type, expr? value)

| StringConstant(string s, ttype type)
| StringConcat(expr left, expr right, ttype type, expr? value)
Expand Down
1 change: 1 addition & 0 deletions src/libasr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ set(SRC
pass/update_array_dim_intrinsic_calls.cpp
pass/pass_array_by_data.cpp
pass/pass_list_expr.cpp
pass/pass_compare.cpp

asr_verify.cpp
asr_utils.cpp
Expand Down
4 changes: 4 additions & 0 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6042,6 +6042,10 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
target_type = get_type_from_ttype_t_util(arg_type);
break ;
}
case (ASR::ttypeType::Tuple) : {
target_type = get_type_from_ttype_t_util(arg_type);
break ;
}
default :
throw CodeGenError("Type " + ASRUtils::type_to_str(arg_type) + " not implemented yet.");
}
Expand Down
Loading