raz / commit e24e7c75

commit
e24e7c75c6fde602575b2beb0fd35595e84bb269
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
7916b9fd
release
v0.10.0
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.

patch · log

6 files changed, +41 -110

PLAN.md (history) modified +2 -2

@@ -18,8 +18,8 @@
1818 names, and has its own targets; nothing in `src/` or `lib/` changes.
1919
2020 The language gained `try` (report 2026-10.20) for this: the parser's
21-errors will return an `ERROR` that `try` passes up, and `self/Jump.raz`,
22-which the scanner's `fail` uses now, goes when step 4 lands.
21+errors return an `ERROR` (`Scan.Failed`) that `try` passes up to where
22+it reads on, in place of the seed's `longjmp`.
2323
2424 Each unit is ported in the seed's order (leaves first) and tested on
2525 its own against the seed's, on the same inputs, since no program can

self/Jump.raz deleted +0 -56

@@ -1,56 +0,0 @@
1-(* Jump: setjmp and longjmp, for a compiler that goes on after an error.
2- The parser reads on from the statement or declaration after one it
3- found wrong; raz has no exceptions, so the scanner's Fail, which the
4- parser's errors end in, jumps back to where the parser saved its
5- place. Written in assembly, since no raz code can return twice.
6-
7- Depends on: nothing.
8- Leaves out: saving anything but what System V says a procedure must
9- keep (rbx, rbp, rsp, r12 to r15) and the place to go on at. A
10- variable changed between save and jump may be held in a register
11- that jump restores, so a variable that is read after the jump is
12- written before save, or lives in memory the compiler can't reach
13- (a field of a record by pointer, a module's variable). *)
14-module Jump;
15-
16-type
17- Env* = record
18- rbx, rbp, rsp, r12, r13, r14, r15, rip: INT;
19- end;
20-
21-(* Saves the place after the call in e, and returns 0. When jump(e) is
22- called later, from any procedure that save's caller is still under,
23- save returns again, with 1. *)
24-proc save*(e: ^Env): INT;
25-code
26- mov rax, [rsp];
27- lea rcx, [rsp + 8];
28- mov [rdi], rbx;
29- mov [rdi + 8], rbp;
30- mov [rdi + 16], rcx;
31- mov [rdi + 24], r12;
32- mov [rdi + 32], r13;
33- mov [rdi + 40], r14;
34- mov [rdi + 48], r15;
35- mov [rdi + 56], rax;
36- xor eax, eax;
37- ret;
38-end save;
39-
40-(* Goes back to the save that filled e, which returns 1. *)
41-proc jump*(e: ^Env);
42-code
43- mov rbx, [rdi];
44- mov rbp, [rdi + 8];
45- mov rsp, [rdi + 16];
46- mov r12, [rdi + 24];
47- mov r13, [rdi + 32];
48- mov r14, [rdi + 40];
49- mov r15, [rdi + 48];
50- mov rcx, [rdi + 56];
51- mov eax, 1;
52- push rcx; (* a jump to rcx, for which the assembler has no form: *)
53- ret; (* push the place, and return to it *)
54-end jump;
55-
56-end Jump.

self/README.md modified +6 -6

@@ -25,12 +25,12 @@
2525 what the parser sets in the seed's `diag` and `scan` (`quiet`,
2626 `werror`, `recovery`, `skipping`) have setters. The `sbuf` array,
2727 which can't be sliced outside, is `strBytes`.
28-- **Going on after an error.** raz has no `setjmp`: `Jump.raz` is the
29- two in assembly. The parser's 200 or so error sites are deep in its
30- recursion and all end in a jump back to the statement or declaration
31- being read, which is how the seed reports up to 20 errors in a run
32- (15 of the 267 tests of messages show more than one). The assembler
33- has no `jmp reg`, so `jump` is `push rcx; ret`.
28+- **Going on after an error.** raz has no `setjmp`, and the seed's parser
29+ ends its errors in a jump back to the statement or declaration being
30+ read, which is how it reports up to 20 errors in a run. The language
31+ gained `try` (report 2026-10.20) for this: `Scan.mark` and the others
32+ show the error and return `Scan.Failed`, which the parser passes up
33+ with `try` to where it reads on. `test/Recover.raz` is the pattern.
3434 - **`NO_COLOR` set to nothing.** `Sys.env` gives "" for a variable that
3535 is unset and for one that is empty, so the port reads the second as
3636 the first. no-color.org says a variable that is empty doesn't turn

self/Scan.raz modified +13 -28

@@ -6,13 +6,13 @@
66 compares names as small integers. The scanner also says where the
77 current symbol is, for messages.
88
9- Depends on: Sys, to read files; Diag, to report errors; Jump, to go
10- on after the parser's errors.
9+ Depends on: Sys, to read files; Diag, to report errors.
1110 Leaves out: recovery from errors in the text itself, such as a string
1211 not closed: the first such error stops the compiler. The parser goes
13- on after its own errors, through recovery below. *)
12+ on after its own errors: mark and the others report one and give
13+ back Failed, which it passes up with try to where it can read on. *)
1414 module Scan;
15-import Sys, Heap, Mem, Fmt, Str, Diag, Jump;
15+import Sys, Heap, Mem, Fmt, Str, Diag;
1616
1717 type
1818 NamePtr = ^U8;
@@ -45,6 +45,7 @@
4545 end;
4646
4747 const
48+ Failed*: ERROR = "reported"; (* an error has been shown: the parser returns it *)
4849 MaxIdent* = 63;
4950 MaxStr* = 65536;
5051 NumWords = 43;
@@ -73,9 +74,6 @@
7374 slen*: INT; (* sString: how many bytes *)
7475 sbuf: [MaxStr + 1]U8; (* sString: its bytes, then a zero *)
7576
76- (* Where the parser goes on after an error: the innermost statement or
77- declaration being read. nil stops the compiler at the first error. *)
78- recovery: ^Jump.Env;
7977 (* The parser is skipping what it has read once already, after an
8078 error, so slips in it aren't reported again. *)
8179 skipping: BOOL;
@@ -102,11 +100,6 @@
102100 tableMask: INT;
103101
104102 (* Exported variables can't be set from outside, so these do it. *)
105-proc setRecovery*(r: ^Jump.Env);
106-begin
107- recovery := r;
108-end;
109-
110103 proc setSkipping*(yes: BOOL);
111104 begin
112105 skipping := yes;
@@ -290,33 +283,25 @@
290283 return col;
291284 end;
292285
293-(* After an error in the parser's construct, reported with its notes
294- and help: the parser goes on from the recovery point. *)
295-proc fail*;
296-begin
297- if recovery = nil then Diag.finish; end;
298- Jump.jump(recovery);
299-end;
300-
301-(* An error at p: reported, then the parser goes on from its recovery
302- point. *)
303-proc markAt*(p: Diag.Pos; msg: []U8);
286+(* An error at p, shown: the parser returns Failed, which try passes up
287+ to where it goes on. *)
288+proc markAt*(p: Diag.Pos; msg: []U8): ERROR;
304289 begin
305290 Diag.error(p, msg);
306- fail;
291+ return Failed;
307292 end;
308293
309-proc mark*(msg: []U8);
294+proc mark*(msg: []U8): ERROR;
310295 begin
311- markAt(tokPos(), msg);
296+ return markAt(tokPos(), msg);
312297 end;
313298
314299 (* markAt, with a line of help after the message. *)
315-proc markHelp*(p: Diag.Pos; msg, hint: []U8);
300+proc markHelp*(p: Diag.Pos; msg, hint: []U8): ERROR;
316301 begin
317302 Diag.error(p, msg);
318303 Diag.help(hint);
319- fail;
304+ return Failed;
320305 end;
321306
322307 (* An error in the text at bytes a to b of the file being read, which

self/check.sh modified +1 -1

@@ -32,4 +32,4 @@
3232 ./raz -Werror -I self -o $b/recover self/test/Recover.raz
3333 printf 'a bad b bad bad c\nd bad\n' >$b/rec.raz
3434 r=$($b/recover $b/rec.raz 2>/dev/null) || test $? = 1
35-test "$r" = "8 names, 4 jumps, 4 errors" && echo "recover ok"
35+test "$r" = "8 names, 4 failed, 4 errors" && echo "recover ok"

self/test/Recover.raz modified +19 -17

@@ -1,36 +1,38 @@
1-(* Tests Jump through Scan.fail: a driver reads a file's symbols and
2- reports each name `bad` as an error, which jumps back to where it
3- saved its place and goes on, as the parser does after an error. The
4- count lives in a module variable, which a jump leaves as it was. *)
1+(* Tests going on after an error, as the parser does with try: a driver
2+ reads a file's symbols and reports each name `bad` as an error,
3+ several calls deep; the error comes back up through them, and the
4+ driver reads on from the next symbol. *)
55 module Recover;
6-import Out, Files, Diag, Scan, Jump;
6+import Out, Files, Diag, Scan;
77
8-var
9- env: Jump.Env;
10- jumps, names: INT;
8+var failed, names: INT;
119
12-proc deep(n: INT);
10+proc deep(n: INT): ERROR;
1311 begin
14- if n > 0 then deep(n - 1); else Scan.mark("a bad name"); end;
12+ if n > 0 then
13+ try deep(n - 1);
14+ return nil;
15+ end;
16+ return Scan.mark("a bad name");
1517 end;
1618
1719 proc main*;
20+var err: ERROR;
1821 begin
1922 if LEN(Scan.open(Files.arg(1))) # 0 then Diag.fatal("no file"); end;
20- Scan.setRecovery(ADR(env));
21- if Jump.save(ADR(env)) # 0 then INC(jumps); end;
2223 while Scan.tok # Scan.sEof do
2324 Scan.next;
2425 if Scan.tok = Scan.sIdent then
2526 INC(names);
26- if Scan.nameOf(Scan.id) = "bad" then deep(5); end;
27+ if Scan.nameOf(Scan.id) = "bad" then
28+ err := deep(5);
29+ if err # nil then INC(failed); end;
30+ end;
2731 end;
2832 end;
29- Out.int(names); Out.str(" names, "); Out.int(jumps); Out.str(" jumps, ");
33+ Out.int(names); Out.str(" names, "); Out.int(failed); Out.str(" failed, ");
3034 Out.int(Diag.errors); Out.str(" errors\n");
31- Scan.setRecovery(nil);
32- Scan.mark("last");
33- Out.str("not reached\n");
35+ Diag.finish;
3436 end;
3537
3638 end Recover.