Skip to content

Commit

Permalink
Merge pull request #2224 from Shaikh-Ubaid/c_fix_list_concat_and_section
Browse files Browse the repository at this point in the history
C: Fix ListConcat and ListSection
  • Loading branch information
Shaikh-Ubaid authored Jul 30, 2023
2 parents 638b052 + 7cf2135 commit 7481e00
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
2 changes: 2 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ RUN(NAME test_list_09 LABELS cpython llvm c NOFAST)
RUN(NAME test_list_10 LABELS cpython llvm c NOFAST)
RUN(NAME test_list_11 LABELS cpython llvm c)
RUN(NAME test_list_section LABELS cpython llvm c NOFAST)
RUN(NAME test_list_section2 LABELS cpython llvm c NOFAST)
RUN(NAME test_list_count LABELS cpython llvm)
RUN(NAME test_list_index LABELS cpython llvm)
RUN(NAME test_list_index2 LABELS cpython llvm)
Expand All @@ -509,6 +510,7 @@ RUN(NAME test_list_pop LABELS cpython llvm NOFAST) # TODO: Remove NOFAST f
RUN(NAME test_list_pop2 LABELS cpython llvm NOFAST) # TODO: Remove NOFAST from here.
RUN(NAME test_list_pop3 LABELS cpython llvm)
RUN(NAME test_list_compare LABELS cpython llvm)
RUN(NAME test_list_concat LABELS cpython llvm c NOFAST)
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
18 changes: 18 additions & 0 deletions integration_tests/test_list_concat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from lpython import i32, f64

def test_list_concat():
t1: list[i32]
t1 = [2] + [3]
print(t1)
assert len(t1) == 2
assert t1[0] == 2
assert t1[1] == 3

t2: list[f64]
t2 = [3.14, -4.5] + [1.233, -0.012, 5555.50]
print(t2)
assert len(t2) == 5
assert abs(t2[0] - 3.14) <= 1e-5
assert abs(t2[-1] - 5555.50) <= 1e-5

test_list_concat()
12 changes: 12 additions & 0 deletions integration_tests/test_list_section2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from lpython import i32


def test_list_section():
x: list[i32]
x = [5, -6, 7, -1, 2, 10, -8, 15]

n: i32 = len(x[1:4])
print(n)
assert n == 3

test_list_section()
17 changes: 4 additions & 13 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1308,11 +1308,7 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
if( is_target_list && is_value_list ) {
ASR::List_t* list_target = ASR::down_cast<ASR::List_t>(ASRUtils::expr_type(x.m_target));
std::string list_dc_func = c_ds_api->get_list_deepcopy_func(list_target);
if (ASR::is_a<ASR::ListConcat_t>(*x.m_value)) {
src += indent + list_dc_func + "(" + value + ", &" + target + ");\n\n";
} else {
src += indent + list_dc_func + "(&" + value + ", &" + target + ");\n\n";
}
src += indent + list_dc_func + "(&" + value + ", &" + target + ");\n\n";
} else if ( is_target_tup && is_value_tup ) {
ASR::Tuple_t* tup_target = ASR::down_cast<ASR::Tuple_t>(ASRUtils::expr_type(x.m_target));
std::string dc_func = c_ds_api->get_tuple_deepcopy_func(tup_target);
Expand Down Expand Up @@ -1565,16 +1561,11 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
bracket_open++;
self().visit_expr(*x.m_left);
std::string left = std::move(src);
if (!ASR::is_a<ASR::ListConcat_t>(*x.m_left)) {
left = "&" + left;
}
self().visit_expr(*x.m_right);
bracket_open--;
std::string rig = std::move(src);
if (!ASR::is_a<ASR::ListConcat_t>(*x.m_right)) {
rig = "&" + rig;
}
src = check_tmp_buffer() + list_concat_func + "(" + left + ", " + rig + ")";
tmp_buffer_src.push_back(check_tmp_buffer());
src = "(*" + list_concat_func + "(&" + left + ", &" + rig + "))";
}

void visit_ListSection(const ASR::ListSection_t& x) {
Expand Down Expand Up @@ -1620,7 +1611,7 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
right + ", " + step + ", " + l_present + ", " + r_present + ");\n";
const_var_names[get_hash((ASR::asr_t*)&x)] = var_name;
tmp_buffer_src.push_back(tmp_src_gen);
src = "* " + var_name;
src = "(*" + var_name + ")";
}

void visit_ListClear(const ASR::ListClear_t& x) {
Expand Down

0 comments on commit 7481e00

Please sign in to comment.