raz / commit 768afb54

commit
768afb541b46aedac571b14b4fb248bceef1e5b9
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
8892c580
match on a string

A match takes a []U8, or what converts to one, with string constants
as labels, none the same as another, and an else arm (report
2026-10.25, section 7.4). Item.matchBegin puts the string's slice in a
slot, and Parse.strArm compares each label with it in turn, arm by
arm, by Item.matchStr.

patch · log

14 files changed, +167 -12

conform/err/MatchStrNoElse.raz (history) added +9 -0

@@ -0,0 +1,9 @@
1+module MatchStrNoElse;
2+proc f(s: []U8; v: []I32);
3+var n: INT;
4+begin
5+ match s of
6+ | "a": n := 1;
7+ end;
8+end;
9+end MatchStrNoElse.

conform/err/MatchStrTwice.raz (history) added +11 -0

@@ -0,0 +1,11 @@
1+module MatchStrTwice;
2+proc f(s: []U8; v: []I32);
3+var n: INT;
4+begin
5+ match s of
6+ | "a", "b": n := 1;
7+ | "b": n := 2;
8+ | else:
9+ end;
10+end;
11+end MatchStrTwice.

conform/err/MatchStrVar.raz (history) added +10 -0

@@ -0,0 +1,10 @@
1+module MatchStrVar;
2+proc f(s: []U8; v: []I32);
3+var n: INT;
4+begin
5+ match s of
6+ | s: n := 1;
7+ | else:
8+ end;
9+end;
10+end MatchStrVar.

conform/run/MatchStr.expect (history) added +4 -0

@@ -0,0 +1,4 @@
1+111223000
2+array
3+slice
4+constant

conform/run/MatchStr.raz (history) added +42 -0

@@ -0,0 +1,42 @@
1+(* match on a string: its labels string constants, compared as = compares strings *)
2+module MatchStr;
3+import Out;
4+
5+const Plus = "plus";
6+
7+type Op = (none, add, sub, mul);
8+
9+proc op(word: []U8): Op;
10+begin
11+ match word of
12+ | "add", Plus, `+`: return add;
13+ | "sub", "-": return sub;
14+ | "mul": return mul;
15+ | "": return none;
16+ | else: return none;
17+ end;
18+end;
19+
20+proc main*;
21+var a: [3]U8; s: []U8;
22+begin
23+ Out.int(I64(op("add"))); Out.int(I64(op("plus"))); Out.int(I64(op("+")));
24+ Out.int(I64(op("sub"))); Out.int(I64(op("-"))); Out.int(I64(op("mul")));
25+ Out.int(I64(op("mu"))); Out.int(I64(op("mull"))); Out.int(I64(op(""))); Out.ln;
26+ a := "sub";
27+ match a of
28+ | "sub": Out.str("array\n");
29+ | else:
30+ end;
31+ s := a[0..2];
32+ match s of
33+ | "su": Out.str("slice\n");
34+ | else: Out.str("no\n");
35+ end;
36+ match "abc" of
37+ | "abc": Out.str("constant\n");
38+ | else:
39+ end;
40+end;
41+
42+end MatchStr.

doc/compiler.md (history) modified +9 -3

@@ -19,8 +19,8 @@
1919 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2020 | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler |
2121 | `src/Gen.raz` | 2,516 | 1,800 | The stack machine, for x86-64: values, operations, checks, calls, frames |
22-| `src/Item.raz` | 1,767 | 1,462 | Items: types, constants, variables, operators, conversions, calls |
23-| `src/Parse.raz` | 3,907 | 3,405 | Declarations, statements, expressions, built-ins, recovery, warnings |
22+| `src/Item.raz` | 1,782 | 1,473 | Items: types, constants, variables, operators, conversions, calls |
23+| `src/Parse.raz` | 3,949 | 3,441 | Declarations, statements, expressions, built-ins, recovery, warnings |
2424 | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
2525 | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers |
2626 | `src/Raz.raz` | 305 | 238 | The command line |
@@ -1318,7 +1318,13 @@
13181318 `dispatch` makes a jump table (gen's `jumpTable`, whose comment shows
13191319 the code) for four labels or more that lie close together, else
13201320 compares and branches one label after another with `Item.matchRange`, each
1321-jumping back to its arm. `deferStatement` is worth reading: a deferred statement
1321+jumping back to its arm. A `match` on a string is tested arm by arm,
1322+as a variant's is: `Item.matchBegin` puts the string's slice in a slot
1323+(`seqSlice`, as `=` does), and `strArm` compares each label with it by
1324+`Item.matchStr`, gen's `compareSeq` and a jump, to the arm if they are
1325+equal or, for the arm's last label, past it if they aren't. It keeps
1326+each label's bytes, as their chunk and length, to refuse a second one
1327+the same. `deferStatement` is worth reading: a deferred statement
13221328 is compiled where it stands, as a little subroutine of the procedure
13231329 that the code jumps over (gen's `deferBegin` and `deferEnd`), and
13241330 `runDefers` emits a call to it at every way out of its statement list:

src/Item.raz (history) modified +18 -3

@@ -1467,14 +1467,18 @@
14671467 return nil;
14681468 end;
14691469
1470-(* The slot match tests: a variant's address, or an enumeration's or an
1471- integer's value. *)
1470+(* The slot match tests: a variant's address, an enumeration's or an
1471+ integer's value, or a string's slice, which x then is. *)
14721472 proc matchBegin*(var x: Item): (I64, ERROR);
14731473 var p: I64;
14741474 begin
14751475 if x.typ^.form in {Sym.fEnum, Sym.fInt} then return toSlot(x); end;
1476+ if isSeq(x.typ) and Sym.same(elemOf(x.typ), Sym.tU8) then
1477+ try seqSlice(x);
1478+ return x.off, nil;
1479+ end;
14761480 if (x.mode # mVar) or (x.typ^.form # Sym.fVariant) then
1477- return 0, Scan.markAt(x.pos, say1("match needs a variant, an enumeration or an integer, found {}", what(x)));
1481+ return 0, Scan.markAt(x.pos, say1("match needs a variant, an enumeration, an integer or a string, found {}", what(x)));
14781482 end;
14791483 if x.ro then try copyToTemp(x); end; (* its arms' names stand for a copy's fields *)
14801484 try address(x);
@@ -1492,6 +1496,17 @@
14921496 Gen.pushVar(a, 8, false, Gen.kWord);
14931497 Gen.pushConst(Gen.kWord, tag);
14941498 Gen.jumpCmp(Scan.sNeq, false, L);
1499+end;
1500+
1501+(* For a label s of a match on the string whose slice is in slot p:
1502+ to body if they are equal, or, for the arm's last label, to L if
1503+ they aren't. *)
1504+proc matchStr*(p: I64; var s: Item; last: BOOL; var L, body: INT): ERROR;
1505+begin
1506+ try seqSlice(s);
1507+ Gen.compareSeq(p, s.off, 0, true);
1508+ if last then Gen.jumpIf(L, false); else Gen.jumpIf(body, true); end;
1509+ return nil;
14951510 end;
14961511
14971512 (* To body if the integer in slot p is from lo to hi: less its lo, it is

src/Parse.raz (history) modified +43 -1

@@ -1848,6 +1848,43 @@
18481848 return nil;
18491849 end;
18501850
1851+(* An arm of a match on a string, whose slice is in slot p: its labels,
1852+ string constants, none equal to another (report section 7.4), each
1853+ compared in turn as its arm is reached, then its statements. ls
1854+ keeps each label's chunk in lo and its length in hi, for the
1855+ comparing of labels. *)
1856+proc strArm(p: I64; var ls: Labels; var endL: INT): ERROR;
1857+var x: TItem; i, L, body: INT; last: BOOL; s: []U8;
1858+begin
1859+ L := Gen.NoLabel;
1860+ body := Gen.NoLabel;
1861+ repeat
1862+ try expression(x, nil);
1863+ if x.mode # Item.mStr then
1864+ return Scan.markAt(x.pos, say1("a label of a match on a string is a string constant, found {}", Item.what(x)));
1865+ end;
1866+ s := Chunk.rodata[Chunk.chunks[x.chunk].start..Chunk.chunks[x.chunk].start + INT(x.val)];
1867+ for i := 0 to ls.n - 1 do
1868+ if s = Chunk.rodata[Chunk.chunks[ls.lo[i]].start..Chunk.chunks[ls.lo[i]].start + INT(ls.hi[i])] then
1869+ return Scan.markAt(x.pos, say1("{} is in another label too", Diag.source(x.pos)));
1870+ end;
1871+ end;
1872+ if ls.n >= MaxLabels then return Scan.markAt(x.pos, tooMany(MaxLabels, "labels in one match")); end;
1873+ ls.lo[ls.n] := x.chunk; ls.hi[ls.n] := x.val;
1874+ INC(ls.n);
1875+ last := Scan.tok # Scan.sComma;
1876+ try Item.matchStr(p, x, last, L, body);
1877+ if last then break; end;
1878+ Scan.next;
1879+ until false;
1880+ try expect(Scan.sColon);
1881+ Gen.fix(body);
1882+ try statements();
1883+ Gen.jump(endL);
1884+ Gen.fix(L);
1885+ return nil;
1886+end;
1887+
18511888 const
18521889 MinTable = 4; (* fewer labels are tested one by one *)
18531890 TableDensity = 8; (* a table has at most this many entries to a label *)
@@ -1896,13 +1933,14 @@
18961933 proc matchStatement(start: Diag.Pos; ind: INT): ERROR;
18971934 var
18981935 x: TItem; t: PType; c, f, o: PObj; p: I64; L, endL, n, first, disp, elseAt: INT;
1899- seen: [256]BOOL; hasElse, all, byValue: BOOL; endAt: Diag.Pos; ls: Labels;
1936+ seen: [256]BOOL; hasElse, all, byValue, isStr: BOOL; endAt: Diag.Pos; ls: Labels;
19001937 begin
19011938 Scan.next;
19021939 try expression(x, nil);
19031940 if x.typ^.form = Sym.fConst then try Item.coerce(x, Sym.tI64); end;
19041941 p := try Item.matchBegin(x);
19051942 t := x.typ;
1943+ isStr := t^.form = Sym.fSlice; (* matchBegin made a string a slice *)
19061944 try expect(Scan.sOf);
19071945 hasElse := false;
19081946 all := true;
@@ -1926,6 +1964,8 @@
19261964 hasElse := true;
19271965 elsif t^.form = Sym.fInt then
19281966 try intArm(t, ls, endL);
1967+ elsif isStr then
1968+ try strArm(p, ls, endL);
19291969 else
19301970 first := ls.n;
19311971 L := Gen.NoLabel;
@@ -1990,6 +2030,8 @@
19902030 ends := all;
19912031 if (t^.form = Sym.fInt) and not hasElse then
19922032 return Scan.markHelp(endAt, "a match on an integer needs an else arm", "it may be empty: | else:");
2033+ elsif isStr and not hasElse then
2034+ return Scan.markHelp(endAt, "a match on a string needs an else arm", "it may be empty: | else:");
19932035 end;
19942036 if not hasElse then
19952037 c := t^.fields;

src/check.sh modified +6 -3

@@ -6,10 +6,13 @@
66 set -e
77 b=build/src
88 # The seed predates raw strings and match on a string (report
9-# 2026-10.25): the files that use them are the compiler's alone.
10-newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz"
9+# 2026-10.25): the files that use them are the compiler's alone, and
10+# so are those whose message says what match takes.
11+newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz
12+ conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz
13+ conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz"
1114 newer() {
12- case " $newer " in *" $1 "*) return 0 ;; esac
15+ for g in $newer; do [ $g = $1 ] && return 0; done
1316 return 1
1417 }
1518 mkdir -p $b

test/msg/AtMatch.err (history) modified +1 -1

@@ -1,4 +1,4 @@
1-conform/err/AtMatch.raz:5:11: error: match needs a variant, an enumeration or an integer, found F64
1+conform/err/AtMatch.raz:5:11: error: match needs a variant, an enumeration, an integer or a string, found F64
22 5 | match x + 1.0 of end;
33 | ^^^^^^^
44 raz: 1 error

test/msg/MatchReal.err (history) modified +1 -1

@@ -1,4 +1,4 @@
1-conform/err/MatchReal.raz:4:11: error: match needs a variant, an enumeration or an integer, found F64
1+conform/err/MatchReal.raz:4:11: error: match needs a variant, an enumeration, an integer or a string, found F64
22 4 | match n of
33 | ^
44 raz: 1 error

test/msg/MatchStrNoElse.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/MatchStrNoElse.raz:7:5: error: a match on a string needs an else arm
2+ 7 | end;
3+ | ^^^
4+help: it may be empty: | else:
5+raz: 1 error

test/msg/MatchStrTwice.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/MatchStrTwice.raz:7:7: error: "b" is in another label too
2+ 7 | | "b": n := 2;
3+ | ^^^
4+raz: 1 error

test/msg/MatchStrVar.err (history) added +4 -0

@@ -0,0 +1,4 @@
1+conform/err/MatchStrVar.raz:6:7: error: a label of a match on a string is a string constant, found []U8
2+ 6 | | s: n := 1;
3+ | ^
4+raz: 1 error