lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.37k stars 156 forks source link

Fixed scoping issues of for loops in global scope #2672

Closed tanay-man closed 2 months ago

tanay-man commented 2 months ago

fixes #2608 , fixes #1475.

kmr-srbh commented 2 months ago

@tanay-man I think this should also fix #1475. Could you please verify it?

kmr-srbh commented 2 months ago

Please make the changes to handle subscripts also.

tanay-man commented 2 months ago

@tanay-man I think this should also fix #1475. Could you please verify it?

It does fix the issue.

kmr-srbh commented 2 months ago

It does fix the issue.

Great! Please link the PR to close that too.

tanay-man commented 2 months ago

Please make the changes to handle subscripts also.

The same solution doesn't work for the subscripts. It produces asr but it cannot be converted to llvm further. I'll have to look into it for further analysis.

kmr-srbh commented 2 months ago

Please make the changes to handle subscripts also.

The same solution doesn't work for the subscripts. It produces asr but it cannot be converted to llvm further. I'll have to look into it for further analysis.

I tested the changes locally. They work for both subscripts of strings and lists.

List

from lpython import i32

i: i32
my_list: list[i32] = [1, 2, 3, 4, 5]
for i in my_list[1:3]:
    print(i)
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
2
3

String

c: str
s: str = "abcde"
for c in s[1:3]:
    print(c)
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
b
c
kmr-srbh commented 2 months ago

Issue #2608 , Issue #1475.

@tanay-man you can change this to 'fixes #2608, fixes #1475' to link this PR to the issues. :+1: