From bc2e708770da25e1b04b0f7658ba165eff28bbf7 Mon Sep 17 00:00:00 2001 From: tanay-man Date: Sun, 28 Apr 2024 14:48:00 +0530 Subject: [PATCH 01/10] Fixed scoping issues of for loops in global scope --- src/lpython/semantics/python_ast_to_asr.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 06da016050..1f29c01b56 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5568,7 +5568,12 @@ class BodyVisitor : public CommonVisitor { ASR::asr_t* assign = ASR::make_Assignment_t(al, x.base.base.loc, ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, tmp_assign_variable_sym)), target, nullptr); - current_body->push_back(al, ASRUtils::STMT(assign)); + if(current_body){ + current_body->push_back(al, ASRUtils::STMT(assign)); + }else{ + global_init.reserve(al, x.n_body + 1); + global_init.push_back(al,assign); + } loop_end = for_iterable_helper(tmp_assign_name, x.base.base.loc, explicit_iter_name); for_iter_type = loop_end; LCOMPILERS_ASSERT(loop_end); From 2ce50434ec33ddc22c0b06680aa92be5cb2b837f Mon Sep 17 00:00:00 2001 From: tanay-man Date: Sun, 28 Apr 2024 15:49:05 +0530 Subject: [PATCH 02/10] Covered subscripts --- src/lpython/semantics/python_ast_to_asr.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 1f29c01b56..576e2c1f1d 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5536,7 +5536,11 @@ class BodyVisitor : public CommonVisitor { ASR::asr_t* assign = ASR::make_Assignment_t(al, x.base.base.loc, ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, tmp_assign_variable_sym)), target, nullptr); - current_body->push_back(al, ASRUtils::STMT(assign)); + if(current_body){ + current_body->push_back(al, ASRUtils::STMT(assign)); + }else{ + global_init.push_back(al,assign); + } loop_end = for_iterable_helper(tmp_assign_name, x.base.base.loc, explicit_iter_name); for_iter_type = loop_end; LCOMPILERS_ASSERT(loop_end); @@ -5571,7 +5575,6 @@ class BodyVisitor : public CommonVisitor { if(current_body){ current_body->push_back(al, ASRUtils::STMT(assign)); }else{ - global_init.reserve(al, x.n_body + 1); global_init.push_back(al,assign); } loop_end = for_iterable_helper(tmp_assign_name, x.base.base.loc, explicit_iter_name); From 00d77ae9ed09eca172cbc695c5ee22f42fb4c83e Mon Sep 17 00:00:00 2001 From: tanay-man Date: Mon, 29 Apr 2024 11:19:32 +0530 Subject: [PATCH 03/10] Considered edge case of nested list --- src/lpython/semantics/python_ast_to_asr.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 576e2c1f1d..d495cb4d8a 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5516,9 +5516,7 @@ class BodyVisitor : public CommonVisitor { loop_src_var_name = AST::down_cast(sbt->m_value)->m_id; visit_Subscript(*sbt); ASR::expr_t *target = ASRUtils::EXPR(tmp); - ASR::symbol_t *loop_src_var_symbol = current_scope->resolve_symbol(loop_src_var_name); - ASR::ttype_t *loop_src_var_ttype = ASRUtils::symbol_type(loop_src_var_symbol); - + ASR::ttype_t *loop_src_var_ttype = ASRUtils::expr_type(target); // Create a temporary variable that will contain the evaluated value of Subscript std::string tmp_assign_name = current_scope->get_unique_name("__tmp_assign_for_loop", false); SetChar variable_dependencies_vec; From 6eed31f11ca195a00f0853e9cd2708ccee966220 Mon Sep 17 00:00:00 2001 From: tanay-man Date: Mon, 29 Apr 2024 18:50:50 +0530 Subject: [PATCH 04/10] Added white-spaces --- src/lpython/semantics/python_ast_to_asr.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index d495cb4d8a..461d9ff2bd 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5534,10 +5534,10 @@ class BodyVisitor : public CommonVisitor { ASR::asr_t* assign = ASR::make_Assignment_t(al, x.base.base.loc, ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, tmp_assign_variable_sym)), target, nullptr); - if(current_body){ + if (current_body) { current_body->push_back(al, ASRUtils::STMT(assign)); - }else{ - global_init.push_back(al,assign); + } else { + global_init.push_back(al, assign); } loop_end = for_iterable_helper(tmp_assign_name, x.base.base.loc, explicit_iter_name); for_iter_type = loop_end; @@ -5570,10 +5570,10 @@ class BodyVisitor : public CommonVisitor { ASR::asr_t* assign = ASR::make_Assignment_t(al, x.base.base.loc, ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, tmp_assign_variable_sym)), target, nullptr); - if(current_body){ + if (current_body) { current_body->push_back(al, ASRUtils::STMT(assign)); - }else{ - global_init.push_back(al,assign); + } else { + global_init.push_back(al, assign); } loop_end = for_iterable_helper(tmp_assign_name, x.base.base.loc, explicit_iter_name); for_iter_type = loop_end; From 764ebc646d26425743cdf0763af1dda9d70a24a4 Mon Sep 17 00:00:00 2001 From: tanay-man Date: Mon, 29 Apr 2024 21:02:25 +0530 Subject: [PATCH 05/10] Added integration test --- integration_tests/loop_07.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/integration_tests/loop_07.py b/integration_tests/loop_07.py index c0b045d5bb..76e1e14f3a 100644 --- a/integration_tests/loop_07.py +++ b/integration_tests/loop_07.py @@ -15,3 +15,39 @@ def main0(): assert y_sum == 500 main0() + +#checking for loops in the global scope +sum: i32 = 0 +i: i32 +for i in [1, 2, 3, 4]: + print(i) + sum += i +assert sum == 10 + +alphabets: str = "" +c: str +for c in "abcde": + print(c) + alphabets += c +assert alphabets == "abcde" + +alphabets = "" +s : str = "abcde" +for c in s[1:4]: + alphabets += c +print(alphabets) +assert alphabets == "bcd" + +sum = 0 +num_list : list[i32] = [1, 2, 3, 4] +for i in num_list[1:3]: + print(i) + sum += i +assert sum == 5 + +sum = 0 +nested_list : list[list[i32]] = [[1, 2, 3, 4]] +for i in nested_list[0]: + print(i) + sum += i +assert sum == 10 \ No newline at end of file From 4f2ffc4f4cef684151011e8e2f964fc285d4b685 Mon Sep 17 00:00:00 2001 From: tanay-man Date: Mon, 29 Apr 2024 21:41:02 +0530 Subject: [PATCH 06/10] Removed c backend from test loop_7 --- integration_tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 93da87d7ef..6bfcc57fe5 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -514,7 +514,7 @@ RUN(NAME loop_03 LABELS cpython llvm llvm_jit c wasm wasm_x64) RUN(NAME loop_04 LABELS cpython llvm llvm_jit c) RUN(NAME loop_05 LABELS cpython llvm llvm_jit c) RUN(NAME loop_06 LABELS cpython llvm llvm_jit c NOFAST) -RUN(NAME loop_07 LABELS cpython llvm llvm_jit c) +RUN(NAME loop_07 LABELS cpython llvm llvm_jit) RUN(NAME loop_08 LABELS cpython llvm llvm_jit c) RUN(NAME loop_09 LABELS cpython llvm llvm_jit) RUN(NAME loop_10 LABELS cpython llvm llvm_jit) From 167d99cc45a9b064cc030691079e5b30b5680afb Mon Sep 17 00:00:00 2001 From: tanay-man <93091118+tanay-man@users.noreply.github.com> Date: Tue, 30 Apr 2024 09:27:39 +0530 Subject: [PATCH 07/10] Update src/lpython/semantics/python_ast_to_asr.cpp Co-authored-by: Shaikh Ubaid --- src/lpython/semantics/python_ast_to_asr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 461d9ff2bd..b3da0d3144 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5534,7 +5534,7 @@ class BodyVisitor : public CommonVisitor { ASR::asr_t* assign = ASR::make_Assignment_t(al, x.base.base.loc, ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, tmp_assign_variable_sym)), target, nullptr); - if (current_body) { + if (current_body != nullptr) { current_body->push_back(al, ASRUtils::STMT(assign)); } else { global_init.push_back(al, assign); From d516102e4e05de6926f4c13a35f7cde1cce9e83d Mon Sep 17 00:00:00 2001 From: tanay-man Date: Tue, 30 Apr 2024 09:39:52 +0530 Subject: [PATCH 08/10] Split new test from loop_07 to loop_11 and others --- integration_tests/CMakeLists.txt | 3 +- integration_tests/loop_07.py | 36 -------------------- integration_tests/loop_11.py | 37 +++++++++++++++++++++ src/lpython/semantics/python_ast_to_asr.cpp | 2 +- 4 files changed, 40 insertions(+), 38 deletions(-) create mode 100644 integration_tests/loop_11.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 6bfcc57fe5..4ddd08c540 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -514,10 +514,11 @@ RUN(NAME loop_03 LABELS cpython llvm llvm_jit c wasm wasm_x64) RUN(NAME loop_04 LABELS cpython llvm llvm_jit c) RUN(NAME loop_05 LABELS cpython llvm llvm_jit c) RUN(NAME loop_06 LABELS cpython llvm llvm_jit c NOFAST) -RUN(NAME loop_07 LABELS cpython llvm llvm_jit) +RUN(NAME loop_07 LABELS cpython llvm llvm_jit c) RUN(NAME loop_08 LABELS cpython llvm llvm_jit c) RUN(NAME loop_09 LABELS cpython llvm llvm_jit) RUN(NAME loop_10 LABELS cpython llvm llvm_jit) +RUN(NAME loop_11 LABELS cpython llvm llvm_jit) RUN(NAME if_01 LABELS cpython llvm llvm_jit c wasm wasm_x86 wasm_x64) RUN(NAME if_02 LABELS cpython llvm llvm_jit c wasm wasm_x86 wasm_x64) RUN(NAME if_03 FAIL LABELS cpython llvm llvm_jit c NOFAST) diff --git a/integration_tests/loop_07.py b/integration_tests/loop_07.py index 76e1e14f3a..c0b045d5bb 100644 --- a/integration_tests/loop_07.py +++ b/integration_tests/loop_07.py @@ -15,39 +15,3 @@ def main0(): assert y_sum == 500 main0() - -#checking for loops in the global scope -sum: i32 = 0 -i: i32 -for i in [1, 2, 3, 4]: - print(i) - sum += i -assert sum == 10 - -alphabets: str = "" -c: str -for c in "abcde": - print(c) - alphabets += c -assert alphabets == "abcde" - -alphabets = "" -s : str = "abcde" -for c in s[1:4]: - alphabets += c -print(alphabets) -assert alphabets == "bcd" - -sum = 0 -num_list : list[i32] = [1, 2, 3, 4] -for i in num_list[1:3]: - print(i) - sum += i -assert sum == 5 - -sum = 0 -nested_list : list[list[i32]] = [[1, 2, 3, 4]] -for i in nested_list[0]: - print(i) - sum += i -assert sum == 10 \ No newline at end of file diff --git a/integration_tests/loop_11.py b/integration_tests/loop_11.py new file mode 100644 index 0000000000..0863297572 --- /dev/null +++ b/integration_tests/loop_11.py @@ -0,0 +1,37 @@ +from lpython import i32 + +#checking for loops in the global scope +sum: i32 = 0 +i: i32 +for i in [1, 2, 3, 4]: + print(i) + sum += i +assert sum == 10 + +alphabets: str = "" +c: str +for c in "abcde": + print(c) + alphabets += c +assert alphabets == "abcde" + +alphabets = "" +s : str = "abcde" +for c in s[1:4]: + alphabets += c +print(alphabets) +assert alphabets == "bcd" + +sum = 0 +num_list : list[i32] = [1, 2, 3, 4] +for i in num_list[1:3]: + print(i) + sum += i +assert sum == 5 + +sum = 0 +nested_list : list[list[i32]] = [[1, 2, 3, 4]] +for i in nested_list[0]: + print(i) + sum += i +assert sum == 10 \ No newline at end of file diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index b3da0d3144..e2c2f1384d 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -5570,7 +5570,7 @@ class BodyVisitor : public CommonVisitor { ASR::asr_t* assign = ASR::make_Assignment_t(al, x.base.base.loc, ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, tmp_assign_variable_sym)), target, nullptr); - if (current_body) { + if (current_body != nullptr) { current_body->push_back(al, ASRUtils::STMT(assign)); } else { global_init.push_back(al, assign); From b5a92a95e6b05effc6fa8cb8983f004aeb1010f9 Mon Sep 17 00:00:00 2001 From: tanay-man Date: Wed, 1 May 2024 09:04:22 +0530 Subject: [PATCH 09/10] Added print statements before assert --- integration_tests/loop_11.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/integration_tests/loop_11.py b/integration_tests/loop_11.py index 0863297572..de3845e5db 100644 --- a/integration_tests/loop_11.py +++ b/integration_tests/loop_11.py @@ -6,6 +6,7 @@ for i in [1, 2, 3, 4]: print(i) sum += i +print("sum = "+str(sum)) assert sum == 10 alphabets: str = "" @@ -13,13 +14,14 @@ for c in "abcde": print(c) alphabets += c +print("alphabets = "+alphabets) assert alphabets == "abcde" alphabets = "" s : str = "abcde" for c in s[1:4]: alphabets += c -print(alphabets) +print("alphabets = "+alphabets) assert alphabets == "bcd" sum = 0 @@ -27,6 +29,7 @@ for i in num_list[1:3]: print(i) sum += i +print("sum = "+str(sum)) assert sum == 5 sum = 0 @@ -34,4 +37,5 @@ for i in nested_list[0]: print(i) sum += i +print("sum = "+str(sum)) assert sum == 10 \ No newline at end of file From 0b69fe2c7de762d497b3fc215d0e87190397d6ad Mon Sep 17 00:00:00 2001 From: tanay-man Date: Wed, 1 May 2024 09:16:04 +0530 Subject: [PATCH 10/10] Removed explicit cast to str --- integration_tests/loop_11.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/integration_tests/loop_11.py b/integration_tests/loop_11.py index de3845e5db..c5db7a40a9 100644 --- a/integration_tests/loop_11.py +++ b/integration_tests/loop_11.py @@ -6,7 +6,7 @@ for i in [1, 2, 3, 4]: print(i) sum += i -print("sum = "+str(sum)) +print("sum = ",sum) assert sum == 10 alphabets: str = "" @@ -14,14 +14,15 @@ for c in "abcde": print(c) alphabets += c -print("alphabets = "+alphabets) +print("alphabets = ",alphabets) assert alphabets == "abcde" alphabets = "" s : str = "abcde" for c in s[1:4]: + print(c) alphabets += c -print("alphabets = "+alphabets) +print("alphabets = ",alphabets) assert alphabets == "bcd" sum = 0 @@ -29,7 +30,7 @@ for i in num_list[1:3]: print(i) sum += i -print("sum = "+str(sum)) +print("sum = ",sum) assert sum == 5 sum = 0 @@ -37,5 +38,5 @@ for i in nested_list[0]: print(i) sum += i -print("sum = "+str(sum)) +print("sum = ",sum) assert sum == 10 \ No newline at end of file