From e24e7c75c6fde602575b2beb0fd35595e84bb269 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Tue, 13 Oct 2026 16:06:48 +0100 Subject: [PATCH] Self: Scan reports an error and returns Failed; Jump goes mark, markAt and markHelp show the error and return Scan.Failed, for the parser to pass up with try; no setjmp, no assembly, no recovery variable. Recover tests the pattern. --- diff --git a/PLAN.md b/PLAN.md index 3997953..e33afd8 100644 --- a/PLAN.md +++ b/PLAN.md @@ -18,8 +18,8 @@ names, and has its own targets; nothing in `src/` or `lib/` changes. The language gained `try` (report 2026-10.20) for this: the parser's -errors will return an `ERROR` that `try` passes up, and `self/Jump.raz`, -which the scanner's `fail` uses now, goes when step 4 lands. +errors return an `ERROR` (`Scan.Failed`) that `try` passes up to where +it reads on, in place of the seed's `longjmp`. Each unit is ported in the seed's order (leaves first) and tested on its own against the seed's, on the same inputs, since no program can diff --git a/self/Jump.raz b/self/Jump.raz deleted file mode 100644 index b537d5e..0000000 --- a/self/Jump.raz +++ /dev/null @@ -1,56 +0,0 @@ -(* Jump: setjmp and longjmp, for a compiler that goes on after an error. - The parser reads on from the statement or declaration after one it - found wrong; raz has no exceptions, so the scanner's Fail, which the - parser's errors end in, jumps back to where the parser saved its - place. Written in assembly, since no raz code can return twice. - - Depends on: nothing. - Leaves out: saving anything but what System V says a procedure must - keep (rbx, rbp, rsp, r12 to r15) and the place to go on at. A - variable changed between save and jump may be held in a register - that jump restores, so a variable that is read after the jump is - written before save, or lives in memory the compiler can't reach - (a field of a record by pointer, a module's variable). *) -module Jump; - -type - Env* = record - rbx, rbp, rsp, r12, r13, r14, r15, rip: INT; - end; - -(* Saves the place after the call in e, and returns 0. When jump(e) is - called later, from any procedure that save's caller is still under, - save returns again, with 1. *) -proc save*(e: ^Env): INT; -code - mov rax, [rsp]; - lea rcx, [rsp + 8]; - mov [rdi], rbx; - mov [rdi + 8], rbp; - mov [rdi + 16], rcx; - mov [rdi + 24], r12; - mov [rdi + 32], r13; - mov [rdi + 40], r14; - mov [rdi + 48], r15; - mov [rdi + 56], rax; - xor eax, eax; - ret; -end save; - -(* Goes back to the save that filled e, which returns 1. *) -proc jump*(e: ^Env); -code - mov rbx, [rdi]; - mov rbp, [rdi + 8]; - mov rsp, [rdi + 16]; - mov r12, [rdi + 24]; - mov r13, [rdi + 32]; - mov r14, [rdi + 40]; - mov r15, [rdi + 48]; - mov rcx, [rdi + 56]; - mov eax, 1; - push rcx; (* a jump to rcx, for which the assembler has no form: *) - ret; (* push the place, and return to it *) -end jump; - -end Jump. diff --git a/self/README.md b/self/README.md index 5e7faa6..94d5764 100644 --- a/self/README.md +++ b/self/README.md @@ -25,12 +25,12 @@ what the parser sets in the seed's `diag` and `scan` (`quiet`, `werror`, `recovery`, `skipping`) have setters. The `sbuf` array, which can't be sliced outside, is `strBytes`. -- **Going on after an error.** raz has no `setjmp`: `Jump.raz` is the - two in assembly. The parser's 200 or so error sites are deep in its - recursion and all end in a jump back to the statement or declaration - being read, which is how the seed reports up to 20 errors in a run - (15 of the 267 tests of messages show more than one). The assembler - has no `jmp reg`, so `jump` is `push rcx; ret`. +- **Going on after an error.** raz has no `setjmp`, and the seed's parser + ends its errors in a jump back to the statement or declaration being + read, which is how it reports up to 20 errors in a run. The language + gained `try` (report 2026-10.20) for this: `Scan.mark` and the others + show the error and return `Scan.Failed`, which the parser passes up + with `try` to where it reads on. `test/Recover.raz` is the pattern. - **`NO_COLOR` set to nothing.** `Sys.env` gives "" for a variable that is unset and for one that is empty, so the port reads the second as the first. no-color.org says a variable that is empty doesn't turn diff --git a/self/Scan.raz b/self/Scan.raz index ba4c449..d484b71 100644 --- a/self/Scan.raz +++ b/self/Scan.raz @@ -6,13 +6,13 @@ compares names as small integers. The scanner also says where the current symbol is, for messages. - Depends on: Sys, to read files; Diag, to report errors; Jump, to go - on after the parser's errors. + Depends on: Sys, to read files; Diag, to report errors. Leaves out: recovery from errors in the text itself, such as a string not closed: the first such error stops the compiler. The parser goes - on after its own errors, through recovery below. *) + on after its own errors: mark and the others report one and give + back Failed, which it passes up with try to where it can read on. *) module Scan; -import Sys, Heap, Mem, Fmt, Str, Diag, Jump; +import Sys, Heap, Mem, Fmt, Str, Diag; type NamePtr = ^U8; @@ -45,6 +45,7 @@ end; const + Failed*: ERROR = "reported"; (* an error has been shown: the parser returns it *) MaxIdent* = 63; MaxStr* = 65536; NumWords = 43; @@ -73,9 +74,6 @@ slen*: INT; (* sString: how many bytes *) sbuf: [MaxStr + 1]U8; (* sString: its bytes, then a zero *) - (* Where the parser goes on after an error: the innermost statement or - declaration being read. nil stops the compiler at the first error. *) - recovery: ^Jump.Env; (* The parser is skipping what it has read once already, after an error, so slips in it aren't reported again. *) skipping: BOOL; @@ -102,11 +100,6 @@ tableMask: INT; (* Exported variables can't be set from outside, so these do it. *) -proc setRecovery*(r: ^Jump.Env); -begin - recovery := r; -end; - proc setSkipping*(yes: BOOL); begin skipping := yes; @@ -290,33 +283,25 @@ return col; end; -(* After an error in the parser's construct, reported with its notes - and help: the parser goes on from the recovery point. *) -proc fail*; -begin - if recovery = nil then Diag.finish; end; - Jump.jump(recovery); -end; - -(* An error at p: reported, then the parser goes on from its recovery - point. *) -proc markAt*(p: Diag.Pos; msg: []U8); +(* An error at p, shown: the parser returns Failed, which try passes up + to where it goes on. *) +proc markAt*(p: Diag.Pos; msg: []U8): ERROR; begin Diag.error(p, msg); - fail; + return Failed; end; -proc mark*(msg: []U8); +proc mark*(msg: []U8): ERROR; begin - markAt(tokPos(), msg); + return markAt(tokPos(), msg); end; (* markAt, with a line of help after the message. *) -proc markHelp*(p: Diag.Pos; msg, hint: []U8); +proc markHelp*(p: Diag.Pos; msg, hint: []U8): ERROR; begin Diag.error(p, msg); Diag.help(hint); - fail; + return Failed; end; (* An error in the text at bytes a to b of the file being read, which diff --git a/self/check.sh b/self/check.sh index 7f94c79..02b3ac9 100755 --- a/self/check.sh +++ b/self/check.sh @@ -32,4 +32,4 @@ ./raz -Werror -I self -o $b/recover self/test/Recover.raz printf 'a bad b bad bad c\nd bad\n' >$b/rec.raz r=$($b/recover $b/rec.raz 2>/dev/null) || test $? = 1 -test "$r" = "8 names, 4 jumps, 4 errors" && echo "recover ok" +test "$r" = "8 names, 4 failed, 4 errors" && echo "recover ok" diff --git a/self/test/Recover.raz b/self/test/Recover.raz index a2c9756..cf0ae41 100644 --- a/self/test/Recover.raz +++ b/self/test/Recover.raz @@ -1,36 +1,38 @@ -(* Tests Jump through Scan.fail: a driver reads a file's symbols and - reports each name `bad` as an error, which jumps back to where it - saved its place and goes on, as the parser does after an error. The - count lives in a module variable, which a jump leaves as it was. *) +(* Tests going on after an error, as the parser does with try: a driver + reads a file's symbols and reports each name `bad` as an error, + several calls deep; the error comes back up through them, and the + driver reads on from the next symbol. *) module Recover; -import Out, Files, Diag, Scan, Jump; +import Out, Files, Diag, Scan; -var - env: Jump.Env; - jumps, names: INT; +var failed, names: INT; -proc deep(n: INT); +proc deep(n: INT): ERROR; begin - if n > 0 then deep(n - 1); else Scan.mark("a bad name"); end; + if n > 0 then + try deep(n - 1); + return nil; + end; + return Scan.mark("a bad name"); end; proc main*; +var err: ERROR; begin if LEN(Scan.open(Files.arg(1))) # 0 then Diag.fatal("no file"); end; - Scan.setRecovery(ADR(env)); - if Jump.save(ADR(env)) # 0 then INC(jumps); end; while Scan.tok # Scan.sEof do Scan.next; if Scan.tok = Scan.sIdent then INC(names); - if Scan.nameOf(Scan.id) = "bad" then deep(5); end; + if Scan.nameOf(Scan.id) = "bad" then + err := deep(5); + if err # nil then INC(failed); end; + end; end; end; - Out.int(names); Out.str(" names, "); Out.int(jumps); Out.str(" jumps, "); + Out.int(names); Out.str(" names, "); Out.int(failed); Out.str(" failed, "); Out.int(Diag.errors); Out.str(" errors\n"); - Scan.setRecovery(nil); - Scan.mark("last"); - Out.str("not reached\n"); + Diag.finish; end; end Recover.