From e73bb95254d2a3bbdcb9b53badc0b5a152c27f69 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:07:59 +0100 Subject: [PATCH] Gen: a loop condition taken back takes back its jumps to the exit --- diff --git a/src/Gen.raz b/src/Gen.raz index 7c89238..6b3a05e 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -86,7 +86,7 @@ end; (* The code made so far, to go back to for a second try at a body *) - CodeMark* = record pc*, fixups*, lines*, said*, fails*: INT; end; + CodeMark* = record pc*, fixups*, lines*, said*, fails*, exit*: INT; end; (* For entryPoint: the address of chunk, put in the variable whose chunk is into before the program starts; or, for C's symbols, the @@ -2128,7 +2128,7 @@ proc markCode*(var m: CodeMark); begin m.pc := Chunk.pc; m.fixups := Chunk.numFixups; m.lines := Chunk.numLines; m.said := X.saidCount(); - m.fails := numFails; + m.fails := numFails; m.exit := exitL; end; (* The code made since m taken back, as if it had never been made; the @@ -2138,6 +2138,7 @@ Chunk.rewindTo(m.pc, m.fixups, m.lines); X.unsay(m.said); numFails := m.fails; + exitL := m.exit; (* a try or return in it made jumps to the exit *) end; (* For a second try at the body: the code as it was at m, and the frame diff --git a/test/run/TryInLoopCondition.expect b/test/run/TryInLoopCondition.expect new file mode 100644 index 0000000..9766475 --- /dev/null +++ b/test/run/TryInLoopCondition.expect @@ -0,0 +1 @@ +ok diff --git a/test/run/TryInLoopCondition.raz b/test/run/TryInLoopCondition.raz new file mode 100644 index 0000000..6e0c3e4 --- /dev/null +++ b/test/run/TryInLoopCondition.raz @@ -0,0 +1,37 @@ +(* A try in a loop's condition: the condition is compiled once to find + its type and taken back, and the jump to the procedure's exit that + its try made must go with it. *) +module TryInLoopCondition; +import Sys; + +var n: INT; + +(* True while n is below 3; fails when n reaches 5. *) +proc below(): (BOOL, ERROR); +begin + INC(n); + return n < 3, nil; +end; + +proc plain(): ERROR; +begin + while try below() do _ := try below(); end; + return nil; +end; + +proc deferred(): ERROR; +begin + defer n := 0; + while try below() do _ := try below(); end; + return nil; +end; + +proc main*; +begin + n := 0; + if plain() # nil then Sys.exit(1); end; + if deferred() # nil then Sys.exit(1); end; + if n # 0 then Sys.exit(1); end; + _ := Sys.write(Sys.stdout, "ok\n"); +end; +end TryInLoopCondition.