raz / test / run / TryInLoopCondition.raz

37 lines · 802 B · raw · history · last change e73bb952

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. *)
4module TryInLoopCondition;
5import Sys;
6
7var n: INT;
8
9(* True while n is below 3; fails when n reaches 5. *)
10proc below(): (BOOL, ERROR);
11begin
12 INC(n);
13 return n < 3, nil;
14end;
15
16proc plain(): ERROR;
17begin
18 while try below() do _ := try below(); end;
19 return nil;
20end;
21
22proc deferred(): ERROR;
23begin
24 defer n := 0;
25 while try below() do _ := try below(); end;
26 return nil;
27end;
28
29proc main*;
30begin
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");
36end;
37end TryInLoopCondition.