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

Skip to content

Implementing for-else and while-else loops #2555

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 16 commits into from
Mar 25, 2024

Conversation

advikkabra
Copy link
Collaborator

Adding support for for-else and while-else loops according to #2353 .
I have implemented a while_else pass which adds flag variables to all while else loops and adds an if check at the end of the loops.
I have also added tests for the same.

@advikkabra
Copy link
Collaborator Author

I think the tests which are failing have been updated in my pull request.

@Thirumalai-Shaktivel Thirumalai-Shaktivel marked this pull request as draft March 7, 2024 05:12
@Thirumalai-Shaktivel
Copy link
Collaborator

Please mark this PR as ready for review once it is ready.

@advikkabra advikkabra marked this pull request as ready for review March 7, 2024 12:43
Copy link
Collaborator

@Thirumalai-Shaktivel Thirumalai-Shaktivel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!
I have left minor formatting and tests improvements.

@Thirumalai-Shaktivel
Copy link
Collaborator

The tests fail because of reference not being updated.
Do ./run_tests.py -u to update tests.

@Thirumalai-Shaktivel
Copy link
Collaborator

@Shaikh-Ubaid, do you see any design improvements in while_else.cpp

@advikkabra
Copy link
Collaborator Author

@Thirumalai-Shaktivel I have added all the suggested improvements. It is ready for review.

tests/loop8.py Outdated
nested_loop_for_for()
nested_loop_for_while()
nested_loop_while_for()
nested_loop_while_while()
Copy link
Collaborator

@Thirumalai-Shaktivel Thirumalai-Shaktivel Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the tests you have added in the tests folder generates the runtime outputs, right?

Then, I think we have to move all these tests into integration_tests, register them in integration_tests/CMakeLists.txt and test using integration_tests/run_tests.py instead of adding them to tests.toml

We use tests.toml only when it generates the ASR but not runtime output or LLVM.

Also, I think one or two tests is enough. Move all the functions to one or two files.
Along with the print statements use assert statements as you did for loop_09.py.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you forgot to add the nested tests. You can submit a new PR for it.

def nested_loop_for_for():
    i: i32
    j: i32
    for i in range(2):
        print("outer: " + str(i))
        for j in range(10, 20):
            print("  inner: " + str(j))
            break
    else:
        print("no break in outer loop")

def nested_loop_for_while():
    i: i32
    j: i32 = 10
    for i in range(2):
        print("outer: " + str(i))
        while j < 20:
            print("  inner: " + str(j))
            break
    else:
        print("no break in outer loop")

def nested_loop_while_for():
    i: i32 = 0
    j: i32
    while i < 2:
        print("outer: " + str(i))
        i += 1
        for j in range(10, 20):
            print("  inner: " + str(j))
            break
    else:
        print("no break in outer loop")

def nested_loop_while_while():
    i: i32 = 0
    j: i32 = 10
    while i < 2:
        print("outer: " + str(i))
        i += 1
        while j < 20:
            print("  inner: " + str(j))
            break
    else:
        print("no break in outer loop")

nested_loop_for_for()
nested_loop_for_while()
nested_loop_while_for()
nested_loop_while_while()

@Thirumalai-Shaktivel
Copy link
Collaborator

Update the branch and resolve conflicts.

@advikkabra
Copy link
Collaborator Author

I am not sure why tests are failing now. What is the issue?

Copy link
Collaborator

@Thirumalai-Shaktivel Thirumalai-Shaktivel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@Thirumalai-Shaktivel Thirumalai-Shaktivel enabled auto-merge (squash) March 23, 2024 06:39
@Thirumalai-Shaktivel
Copy link
Collaborator

Thirumalai-Shaktivel commented Mar 23, 2024

For every main branch merge or rebase, we recommend a clean build and then update tests. I think the issue was related to that.

We do:

$ git merge lpython main
or 
$ git rebase main
$ git clean -dfx
$ ./build.sh
$ ./run_tests.py -u

@Thirumalai-Shaktivel Thirumalai-Shaktivel merged commit bf16a3d into lcompilers:main Mar 25, 2024
Comment on lines +993 to +998
Vec<ASR::stmt_t*> orelse;
orelse.reserve(al, loop.n_orelse);
for (size_t i = 0; i < loop.n_orelse; i++)
orelse.push_back(al, loop.m_orelse[i]);
ASR::stmt_t *while_loop_stmt = ASRUtils::STMT(ASR::make_WhileLoop_t(al, loc,
loop.m_name, cond, body.p, body.size()));
loop.m_name, cond, body.p, body.size(), orelse.p, orelse.size()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels redundant. orelse being pushed to a new memory location. Nothing else I think.

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