diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 93da87d7ef..4ddd08c540 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -518,6 +518,7 @@ 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_11.py b/integration_tests/loop_11.py new file mode 100644 index 0000000000..c5db7a40a9 --- /dev/null +++ b/integration_tests/loop_11.py @@ -0,0 +1,42 @@ +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 +print("sum = ",sum) +assert sum == 10 + +alphabets: str = "" +c: str +for c in "abcde": + print(c) + alphabets += c +print("alphabets = ",alphabets) +assert alphabets == "abcde" + +alphabets = "" +s : str = "abcde" +for c in s[1:4]: + print(c) + alphabets += c +print("alphabets = ",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 +print("sum = ",sum) +assert sum == 5 + +sum = 0 +nested_list : list[list[i32]] = [[1, 2, 3, 4]] +for i in nested_list[0]: + print(i) + sum += i +print("sum = ",sum) +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 06da016050..e2c2f1384d 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; @@ -5536,7 +5534,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 != nullptr) { + 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); @@ -5568,7 +5570,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 != nullptr) { + 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);