Add support for for-else
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:
-
ForElseVisitor:ForElseis visited and converted toDoLoopwith an initial assignment to a boolean flag variable (_no_break_N), and anIfblock at the end that executesorelseas long as the flag remains true. The flag variable corresponding to theDoLoopis recorded indoLoopFlagMap. -
ExitVisitor:Exitis visited, and its enclosingDoLoopis recorded in a stack. If anExitis found, and the enclosingDoLoopis originally aForElse, the corresponding flag is set to false.
Some tests are added, under tests/for_else.
@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: https://github.com/lcompilers/lpython/issues/2353#issuecomment-1742743488?
@certik I think this PR can be closed as #2555 was merged.