raz / commit e73bb952

commit
e73bb95254d2a3bbdcb9b53badc0b5a152c27f69
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
065335aa
Gen: a loop condition taken back takes back its jumps to the exit

patch · log

3 files changed, +41 -2

src/Gen.raz modified +3 -2

@@ -86,7 +86,7 @@
8686 end;
8787
8888 (* The code made so far, to go back to for a second try at a body *)
89- CodeMark* = record pc*, fixups*, lines*, said*, fails*: INT; end;
89+ CodeMark* = record pc*, fixups*, lines*, said*, fails*, exit*: INT; end;
9090
9191 (* For entryPoint: the address of chunk, put in the variable whose
9292 chunk is into before the program starts; or, for C's symbols, the
@@ -2128,7 +2128,7 @@
21282128 proc markCode*(var m: CodeMark);
21292129 begin
21302130 m.pc := Chunk.pc; m.fixups := Chunk.numFixups; m.lines := Chunk.numLines; m.said := X.saidCount();
2131- m.fails := numFails;
2131+ m.fails := numFails; m.exit := exitL;
21322132 end;
21332133
21342134 (* The code made since m taken back, as if it had never been made; the
@@ -2138,6 +2138,7 @@
21382138 Chunk.rewindTo(m.pc, m.fixups, m.lines);
21392139 X.unsay(m.said);
21402140 numFails := m.fails;
2141+ exitL := m.exit; (* a try or return in it made jumps to the exit *)
21412142 end;
21422143
21432144 (* For a second try at the body: the code as it was at m, and the frame

test/run/TryInLoopCondition.expect (history) added +1 -0

@@ -0,0 +1 @@
1+ok

test/run/TryInLoopCondition.raz (history) added +37 -0

@@ -0,0 +1,37 @@
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.