| 1 | (* A try in a loop's condition: the condition is compiled once to find |
| 2 | its type and taken back, and the jump to the procedure's exit that |
| 3 | its try made must go with it. *) |
| 4 | module TryInLoopCondition; |
| 5 | import Sys; |
| 6 | |
| 7 | var n: INT; |
| 8 | |
| 9 | (* True while n is below 3; fails when n reaches 5. *) |
| 10 | proc below(): (BOOL, ERROR); |
| 11 | begin |
| 12 | INC(n); |
| 13 | return n < 3, nil; |
| 14 | end; |
| 15 | |
| 16 | proc plain(): ERROR; |
| 17 | begin |
| 18 | while try below() do _ := try below(); end; |
| 19 | return nil; |
| 20 | end; |
| 21 | |
| 22 | proc deferred(): ERROR; |
| 23 | begin |
| 24 | defer n := 0; |
| 25 | while try below() do _ := try below(); end; |
| 26 | return nil; |
| 27 | end; |
| 28 | |
| 29 | proc main*; |
| 30 | begin |
| 31 | n := 0; |
| 32 | if plain() # nil then Sys.exit(1); end; |
| 33 | if deferred() # nil then Sys.exit(1); end; |
| 34 | if n # 0 then Sys.exit(1); end; |
| 35 | _ := Sys.write(Sys.stdout, "ok\n"); |
| 36 | end; |
| 37 | end TryInLoopCondition. |