Skip to content

Commit

Permalink
Support bool typed keys in dict (lcompilers#1771)
Browse files Browse the repository at this point in the history
Co-authored-by: Gagandeep Singh <gdp.1807@gmail.com>
  • Loading branch information
kabra1110 and czgdp1807 committed May 13, 2023
1 parent bb8bfe7 commit b9850da
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ RUN(NAME test_dict_08 LABELS cpython llvm c)
RUN(NAME test_dict_09 LABELS cpython llvm c)
RUN(NAME test_dict_10 LABELS cpython llvm) # TODO: Add support of dict with string in C backend
RUN(NAME test_dict_11 LABELS cpython llvm c)
RUN(NAME test_dict_bool LABELS cpython llvm)
RUN(NAME test_for_loop LABELS cpython llvm c)
RUN(NAME modules_01 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
RUN(NAME modules_02 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
Expand Down
47 changes: 47 additions & 0 deletions integration_tests/test_dict_bool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from lpython import i32, f64

def test_dict_bool():
d_int: dict[bool, i32] = {}
d_float: dict[bool, f64] = {}
d_str: dict[bool, str] = {}
i: i32
j: f64
s: str = ""
l_str: list[str] = ["a", "b", "c", "d"]

for i in range(10):
d_int[True] = i
assert d_int[True] == i

for i in range(10, 20):
d_int[True] = i
d_int[False] = i + 1
assert d_int[True] == d_int[False] - 1
assert d_int[True] == i

d_int[True] = 0
d_int[False] = d_int[True]

for i in range(10, 99):
d_int[i%2 == 0] = d_int[i%2 == 0] + 1
assert d_int[True] == d_int[False] + 1
assert d_int[True] == 45

j = 0.0
while j < 1.0:
d_float[False] = j + 1.0
d_float[True] = d_float[False] * d_float[False]
assert d_float[True] == (j + 1.0) * (j + 1.0)
assert d_float[False] == j + 1.0
j = j + 0.1

d_str[False] = s

for i in range(len(l_str)):
d_str[True] = d_str[False]
s += l_str[i]
d_str[False] = s
assert d_str[True] + l_str[i] == d_str[False]
assert d_str[False] == s

test_dict_bool()
3 changes: 3 additions & 0 deletions src/libasr/codegen/llvm_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,9 @@ namespace LCompilers {
}
return tuple_hash;
}
case ASR::ttypeType::Logical: {
return builder->CreateZExt(key, llvm::Type::getInt32Ty(context));
}
default: {
throw LCompilersException("Hashing " + ASRUtils::type_to_str_python(key_asr_type) +
" isn't implemented yet.");
Expand Down

0 comments on commit b9850da

Please sign in to comment.