jacobaustin123 / Coral

The Coral Programming Language: a blazingly-fast, gradually-typed Python compiler with optional static typing for optimization and safety.
https://jacobaustin123.github.io/Coral/
Other
143 stars 6 forks source link

Variables defined in for, while, or range loops which do not run are defined in the outside scope #8

Open jacobaustin123 opened 5 years ago

jacobaustin123 commented 5 years ago

If a variable is defined in a for, while, or range loop which does not run because (a) the list is empty (b) the condition is initial false, or (c) the integer is less than or equal to zero, it will be transformed upon leaving the (un-run) loop and consequently defined. This is because STransforms are added as SStage(entry, loop, exit), and the entry and exit blocks, which contain the STransforms, run regardless of the condition. This can be fixed by refactoring the While, Range, and For types to include the entry and exit conditions, and only running them if the condition is true/valid. A basic example is:

for i in []:
    x = 4
    print(x)
print(x)

i = 0
while i > 0:
    x = 4
    print(x)

print(x)