1. Phix : loop question for Pete
- Posted by ChrisB (moderator) Feb 26, 2018
- 1047 views
for i = 1 to length(something) if its_ok then do_stuff end if exit end for --loop exited after 1st iteration no matter what
have you made this an illegal / unsupported construct?
Chris
2. Re: Phix : loop question for Pete
- Posted by petelomax Feb 26, 2018
- 1041 views
Yes, unfortunately I had to.
25/10/2017 An "illegal/unsupported construct" error now occurs if an end for statement is immediately preceded by an unconditional exit. Where possible, use a simple if construct instead. This proved necessary because the opEndFor was being skipped, but that sets the zero iterations jump. Example: for i=1 to length(s) do ?s[i] exit end for (s[1] errored on length(s)=0, because the <1 jump was wrong.) However this sort of thing is (still) fine: for i=1 to length(s) do ?s[i] if "abc"="def" then ?"what??" else exit end if end for (assuming the compiler does not optimise that test away too) Update: Problem resurfaced. There are now fixes for this in both pmain.e/DoFor() and pilx86.e/jskip().
Feel free to try and improve this. I've tried and failed on at least three occasions, perhaps a fresh perspective would help.
What happens is that the code in pilx86.e line 10787
-- finally, batchpatch zero-iteration jmp(s) [if any] at loop start:
is not executed, if an unconditional exit causes the opEndFor to be skipped. Note that error can also happen for some always-true cases such as if DEBUG then exit end if.
Perhaps opEndFor could/should be split into opEndFor and opPatchForStart, with the latter not being skipped by an unconditional exit, i.e. that new opcode emitted after the final "backpatch(exitBP,..)" in pmain.e/DoFor().
[I don't think that I actually tried, or previously thought of doing that...]
Pete
3. Re: Phix : loop question for Pete
- Posted by ChrisB (moderator) Feb 26, 2018
- 1011 views
Hi
Good to know.
Perhaps we shouldn't be using unconditional exits anyway, and making sure we code in a clearer fashion. However, might I request that the error message be changed to something a little more related to the issue, for instance unconditional exit / illegal construct (do you really need a loop here?)
Cheers
Chris