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

Skip to content

Add support for for-else #1711

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

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft

Conversation

thebesttv
Copy link

This is my attempt at adding support for for-else (issue #1442, according to czgdp1807's comment).

A new ASR node ForElse is added. In visit_For() at python_ast_to_asr.cpp, loops with non-zero n_orelse are converted to ForElse.

An ASR pass "for_else", &pass_replace_forelse} is added, which consists of two steps:

  1. ForElseVisitor: ForElse is visited and converted to DoLoop with an initial assignment to a boolean flag variable (_no_break_N), and an If block at the end that executes orelse as long as the flag remains true. The flag variable corresponding to the DoLoop is recorded in doLoopFlagMap.
  2. ExitVisitor: Exit is visited, and its enclosing DoLoop is recorded in a stack. If an Exit is found, and the enclosing DoLoop is originally a ForElse, the corresponding flag is set to false.

Some tests are added, under tests/for_else.

@certik
Copy link
Contributor

certik commented Oct 3, 2023

@thebesttv I am really sorry, we somehow totally missed this PR. Next time please don't hesitate to ping us if we forget. We usually are pretty good about that. Also when the PR is ready, as this one seems, go ahead and mark it "Ready for review". I think what you implemented one approach --- a separate node that gets transformed via ASR->ASR pass.

Why not adding a new member to DoLoop: #2353 (comment)?

@kmr-srbh
Copy link
Contributor

kmr-srbh commented Apr 20, 2024

@certik I think this PR can be closed as #2555 was merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants