Skip to content

Commit

Permalink
Fix bug in printing a list (#2654)
Browse files Browse the repository at this point in the history
* Fix bug in printing a list

* Update tests

* Add integration test

* Fix bugs

* Update references

* Fix tests for C backend
  • Loading branch information
advikkabra authored Apr 19, 2024
1 parent ac21bb2 commit fce0b35
Show file tree
Hide file tree
Showing 6 changed files with 1,613 additions and 1,067 deletions.
16 changes: 16 additions & 0 deletions integration_tests/test_list_11.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
from lpython import i32

l: list[i32] = [1, 2]

def add_item(i: i32) -> list[i32]:
l.append(i)
return l


def return_empty_list_of_tuples() -> list[i32]:
return []

Expand All @@ -19,12 +26,21 @@ def test_iterate_over_string():
assert s == temp[i]
i+=1

def test_issue_2639():
print(add_item(3))

assert len(l) == 3
assert l[0] == 1
assert l[1] == 2
assert l[2] == 3

def main0():
x: list[i32] = return_empty_list_of_tuples()
print(len(x))

assert len(x) == 0
test_issue_1882()
test_iterate_over_string()
test_issue_2639()

main0()
17 changes: 15 additions & 2 deletions src/libasr/pass/print_list_tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,23 @@ class PrintListTupleVisitor
list_iter_var_name, al, current_scope, int_type);
}

std::string list_var_name;
ASR::expr_t *list_var;
{
list_var_name =
current_scope->get_unique_name("__list_var", false);
list_var = PassUtils::create_auxiliary_variable(loc,
list_var_name, al, current_scope, ASRUtils::expr_type(list_expr));
}

ASR::stmt_t *assign_stmt = ASRUtils::STMT(
ASR::make_Assignment_t(al, loc, list_var, list_expr, nullptr));

ASR::expr_t *list_item = ASRUtils::EXPR(
ASR::make_ListItem_t(al, loc, list_expr,
ASR::make_ListItem_t(al, loc, list_var,
list_iter_var, listC->m_type, nullptr));
ASR::expr_t *list_len = ASRUtils::EXPR(ASR::make_ListLen_t(
al, loc, list_expr, int_type, nullptr));
al, loc, list_var, int_type, nullptr));
ASR::expr_t *constant_one = ASRUtils::EXPR(
ASR::make_IntegerConstant_t(al, loc, 1, int_type));
ASR::expr_t *list_len_minus_one =
Expand Down Expand Up @@ -199,6 +211,7 @@ class PrintListTupleVisitor
al, loc, nullptr, loop_head, loop_body.p, loop_body.size(), nullptr, 0));

{
print_pass_result_tmp.push_back(al, assign_stmt);
print_pass_result_tmp.push_back(al, print_open_bracket);
print_pass_result_tmp.push_back(al, loop);
print_pass_result_tmp.push_back(al, print_close_bracket);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "pass_print_list_tuple-print_02-09600eb.stdout",
"stdout_hash": "b518803746ffd1666ff29f4bfa2347eb621d81af5e52dc36964cd249",
"stdout_hash": "2831d417b5508b57e5e64c51339eb96f4d9aaf3559ee19c31dd0bb3c",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading

0 comments on commit fce0b35

Please sign in to comment.