Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ASR: Fix explicit iter variable #2237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ RUN(NAME loop_03 LABELS cpython llvm c wasm wasm_x64)
RUN(NAME loop_04 LABELS cpython llvm c)
RUN(NAME loop_05 LABELS cpython llvm c)
RUN(NAME loop_06 LABELS cpython llvm c NOFAST)
RUN(NAME loop_07 LABELS cpython llvm c)
RUN(NAME if_01 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
RUN(NAME if_02 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
RUN(NAME if_03 FAIL LABELS cpython llvm c NOFAST)
Expand Down Expand Up @@ -717,7 +718,7 @@ RUN(NAME test_package_01 LABELS cpython llvm NOFAST)
RUN(NAME test_pkg_lpdraw LABELS cpython llvm wasm)
RUN(NAME test_pkg_lnn_01 LABELS cpython llvm NOFAST)
RUN(NAME test_pkg_lnn_02 LABELS cpython llvm NOFAST)
RUN(NAME test_pkg_lpconvexhull LABELS cpython c)
RUN(NAME test_pkg_lpconvexhull LABELS cpython llvm c NOFAST)

RUN(NAME generics_01 LABELS cpython llvm c)
RUN(NAME generics_02 LABELS cpython llvm c)
Expand Down
17 changes: 17 additions & 0 deletions integration_tests/loop_07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from lpython import i32

def main0():
points: list[tuple[i32, i32]] = [(445, 193), (138, 28), (418, 279)]
point: tuple[i32, i32]
x_sum: i32 = 0
y_sum: i32 = 0
for point in points:
print(point)
x_sum += point[0]
y_sum += point[1]

print(x_sum, y_sum)
assert x_sum == 1001
assert y_sum == 500

main0()
17 changes: 9 additions & 8 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5503,9 +5503,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
if (!is_explicit_iterator_required) {
a_kind = ASRUtils::extract_kind_from_ttype_t(ASRUtils::expr_type(target));
}
ASR::ttype_t *a_type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, a_kind));
ASR::expr_t *constant_one = ASR::down_cast<ASR::expr_t>(ASR::make_IntegerConstant_t(
al, x.base.base.loc, 1, a_type));
ASR::do_loop_head_t head = make_do_loop_head(loop_start, loop_end, inc, a_kind,
x.base.base.loc);

Expand All @@ -5520,18 +5517,22 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
// add an assignment instruction to body to assign value of loop_src_var at an index to the loop_target_var
LCOMPILERS_ASSERT(current_scope->get_symbol(explicit_iter_name) != nullptr);
auto explicit_iter_var = ASR::make_Var_t(al, x.base.base.loc, current_scope->get_symbol(explicit_iter_name));
ASR::ttype_t *a_type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, a_kind));
ASR::expr_t *constant_one = ASR::down_cast<ASR::expr_t>(ASR::make_IntegerConstant_t(
al, x.base.base.loc, 1, a_type));
auto index_plus_one = ASR::make_IntegerBinOp_t(al, x.base.base.loc, ASRUtils::EXPR(explicit_iter_var),
ASR::binopType::Add, constant_one, a_type, nullptr);
auto loop_src_var = ASR::make_Var_t(al, x.base.base.loc, current_scope->resolve_symbol(loop_src_var_name));
ASR::expr_t* loop_src_var = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, current_scope->resolve_symbol(loop_src_var_name)));
ASR::ttype_t* loop_src_var_ttype = ASRUtils::expr_type(loop_src_var);
ASR::asr_t* loop_src_var_element = nullptr;
if (ASR::is_a<ASR::StringLen_t>(*for_iter_type)) {
loop_src_var_element = ASR::make_StringItem_t(
al, x.base.base.loc, ASRUtils::EXPR(loop_src_var),
ASRUtils::EXPR(index_plus_one), a_type, nullptr);
al, x.base.base.loc, loop_src_var,
ASRUtils::EXPR(index_plus_one), ASRUtils::get_contained_type(loop_src_var_ttype), nullptr);
} else if (ASR::is_a<ASR::ListLen_t>(*for_iter_type)) {
loop_src_var_element = ASR::make_ListItem_t(
al, x.base.base.loc, ASRUtils::EXPR(loop_src_var),
ASRUtils::EXPR(explicit_iter_var), a_type, nullptr);
al, x.base.base.loc, loop_src_var,
ASRUtils::EXPR(explicit_iter_var), ASRUtils::get_contained_type(loop_src_var_ttype), nullptr);
}
auto loop_target_assignment = ASR::make_Assignment_t(al, x.base.base.loc, target, ASRUtils::EXPR(loop_src_var_element), nullptr);
body.push_back(al, ASRUtils::STMT(loop_target_assignment));
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-test_builtin_str-580e920.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-test_builtin_str-580e920.stdout",
"stdout_hash": "5e1687a9cce6c997ff5c0ff9c15dc4435f1abbd581fb77d647d9afe4",
"stdout_hash": "5869436dc7dad9581fe5088ceb646bb304ddc9f8ef790e6550687c9b",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-test_builtin_str-580e920.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@
(Integer 4)
()
)
(Integer 4)
(Character 1 -2 ())
()
)
()
Expand Down