From 768afb541b46aedac571b14b4fb248bceef1e5b9 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Wed, 14 Oct 2026 22:32:02 +0000 Subject: [PATCH] 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. --- diff --git a/conform/err/MatchStrNoElse.raz b/conform/err/MatchStrNoElse.raz new file mode 100644 index 0000000..e1bf870 --- /dev/null +++ b/conform/err/MatchStrNoElse.raz @@ -0,0 +1,9 @@ +module MatchStrNoElse; +proc f(s: []U8; v: []I32); +var n: INT; +begin + match s of + | "a": n := 1; + end; +end; +end MatchStrNoElse. diff --git a/conform/err/MatchStrTwice.raz b/conform/err/MatchStrTwice.raz new file mode 100644 index 0000000..2113676 --- /dev/null +++ b/conform/err/MatchStrTwice.raz @@ -0,0 +1,11 @@ +module MatchStrTwice; +proc f(s: []U8; v: []I32); +var n: INT; +begin + match s of + | "a", "b": n := 1; + | "b": n := 2; + | else: + end; +end; +end MatchStrTwice. diff --git a/conform/err/MatchStrVar.raz b/conform/err/MatchStrVar.raz new file mode 100644 index 0000000..67daa18 --- /dev/null +++ b/conform/err/MatchStrVar.raz @@ -0,0 +1,10 @@ +module MatchStrVar; +proc f(s: []U8; v: []I32); +var n: INT; +begin + match s of + | s: n := 1; + | else: + end; +end; +end MatchStrVar. diff --git a/conform/run/MatchStr.expect b/conform/run/MatchStr.expect new file mode 100644 index 0000000..4776695 --- /dev/null +++ b/conform/run/MatchStr.expect @@ -0,0 +1,4 @@ +111223000 +array +slice +constant diff --git a/conform/run/MatchStr.raz b/conform/run/MatchStr.raz new file mode 100644 index 0000000..66411b7 --- /dev/null +++ b/conform/run/MatchStr.raz @@ -0,0 +1,42 @@ +(* match on a string: its labels string constants, compared as = compares strings *) +module MatchStr; +import Out; + +const Plus = "plus"; + +type Op = (none, add, sub, mul); + +proc op(word: []U8): Op; +begin + match word of + | "add", Plus, `+`: return add; + | "sub", "-": return sub; + | "mul": return mul; + | "": return none; + | else: return none; + end; +end; + +proc main*; +var a: [3]U8; s: []U8; +begin + Out.int(I64(op("add"))); Out.int(I64(op("plus"))); Out.int(I64(op("+"))); + Out.int(I64(op("sub"))); Out.int(I64(op("-"))); Out.int(I64(op("mul"))); + Out.int(I64(op("mu"))); Out.int(I64(op("mull"))); Out.int(I64(op(""))); Out.ln; + a := "sub"; + match a of + | "sub": Out.str("array\n"); + | else: + end; + s := a[0..2]; + match s of + | "su": Out.str("slice\n"); + | else: Out.str("no\n"); + end; + match "abc" of + | "abc": Out.str("constant\n"); + | else: + end; +end; + +end MatchStr. diff --git a/doc/compiler.md b/doc/compiler.md index 864b372..474681e 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -19,8 +19,8 @@ | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | | `src/Gen.raz` | 2,516 | 1,800 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Item.raz` | 1,767 | 1,462 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 3,907 | 3,405 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Item.raz` | 1,782 | 1,473 | Items: types, constants, variables, operators, conversions, calls | +| `src/Parse.raz` | 3,949 | 3,441 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 305 | 238 | The command line | @@ -1318,7 +1318,13 @@ `dispatch` makes a jump table (gen's `jumpTable`, whose comment shows the code) for four labels or more that lie close together, else compares and branches one label after another with `Item.matchRange`, each -jumping back to its arm. `deferStatement` is worth reading: a deferred statement +jumping back to its arm. A `match` on a string is tested arm by arm, +as a variant's is: `Item.matchBegin` puts the string's slice in a slot +(`seqSlice`, as `=` does), and `strArm` compares each label with it by +`Item.matchStr`, gen's `compareSeq` and a jump, to the arm if they are +equal or, for the arm's last label, past it if they aren't. It keeps +each label's bytes, as their chunk and length, to refuse a second one +the same. `deferStatement` is worth reading: a deferred statement is compiled where it stands, as a little subroutine of the procedure that the code jumps over (gen's `deferBegin` and `deferEnd`), and `runDefers` emits a call to it at every way out of its statement list: diff --git a/src/Item.raz b/src/Item.raz index 7e8e02f..761d5e0 100644 --- a/src/Item.raz +++ b/src/Item.raz @@ -1467,14 +1467,18 @@ return nil; end; -(* The slot match tests: a variant's address, or an enumeration's or an - integer's value. *) +(* The slot match tests: a variant's address, an enumeration's or an + integer's value, or a string's slice, which x then is. *) proc matchBegin*(var x: Item): (I64, ERROR); var p: I64; begin if x.typ^.form in {Sym.fEnum, Sym.fInt} then return toSlot(x); end; + if isSeq(x.typ) and Sym.same(elemOf(x.typ), Sym.tU8) then + try seqSlice(x); + return x.off, nil; + end; if (x.mode # mVar) or (x.typ^.form # Sym.fVariant) then - return 0, Scan.markAt(x.pos, say1("match needs a variant, an enumeration or an integer, found {}", what(x))); + return 0, Scan.markAt(x.pos, say1("match needs a variant, an enumeration, an integer or a string, found {}", what(x))); end; if x.ro then try copyToTemp(x); end; (* its arms' names stand for a copy's fields *) try address(x); @@ -1492,6 +1496,17 @@ Gen.pushVar(a, 8, false, Gen.kWord); Gen.pushConst(Gen.kWord, tag); Gen.jumpCmp(Scan.sNeq, false, L); +end; + +(* For a label s of a match on the string whose slice is in slot p: + to body if they are equal, or, for the arm's last label, to L if + they aren't. *) +proc matchStr*(p: I64; var s: Item; last: BOOL; var L, body: INT): ERROR; +begin + try seqSlice(s); + Gen.compareSeq(p, s.off, 0, true); + if last then Gen.jumpIf(L, false); else Gen.jumpIf(body, true); end; + return nil; end; (* To body if the integer in slot p is from lo to hi: less its lo, it is diff --git a/src/Parse.raz b/src/Parse.raz index 9cb4d89..4c808e7 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -1848,6 +1848,43 @@ return nil; end; +(* An arm of a match on a string, whose slice is in slot p: its labels, + string constants, none equal to another (report section 7.4), each + compared in turn as its arm is reached, then its statements. ls + keeps each label's chunk in lo and its length in hi, for the + comparing of labels. *) +proc strArm(p: I64; var ls: Labels; var endL: INT): ERROR; +var x: TItem; i, L, body: INT; last: BOOL; s: []U8; +begin + L := Gen.NoLabel; + body := Gen.NoLabel; + repeat + try expression(x, nil); + if x.mode # Item.mStr then + return Scan.markAt(x.pos, say1("a label of a match on a string is a string constant, found {}", Item.what(x))); + end; + s := Chunk.rodata[Chunk.chunks[x.chunk].start..Chunk.chunks[x.chunk].start + INT(x.val)]; + for i := 0 to ls.n - 1 do + if s = Chunk.rodata[Chunk.chunks[ls.lo[i]].start..Chunk.chunks[ls.lo[i]].start + INT(ls.hi[i])] then + return Scan.markAt(x.pos, say1("{} is in another label too", Diag.source(x.pos))); + end; + end; + if ls.n >= MaxLabels then return Scan.markAt(x.pos, tooMany(MaxLabels, "labels in one match")); end; + ls.lo[ls.n] := x.chunk; ls.hi[ls.n] := x.val; + INC(ls.n); + last := Scan.tok # Scan.sComma; + try Item.matchStr(p, x, last, L, body); + if last then break; end; + Scan.next; + until false; + try expect(Scan.sColon); + Gen.fix(body); + try statements(); + Gen.jump(endL); + Gen.fix(L); + return nil; +end; + const MinTable = 4; (* fewer labels are tested one by one *) TableDensity = 8; (* a table has at most this many entries to a label *) @@ -1896,13 +1933,14 @@ proc matchStatement(start: Diag.Pos; ind: INT): ERROR; var x: TItem; t: PType; c, f, o: PObj; p: I64; L, endL, n, first, disp, elseAt: INT; - seen: [256]BOOL; hasElse, all, byValue: BOOL; endAt: Diag.Pos; ls: Labels; + seen: [256]BOOL; hasElse, all, byValue, isStr: BOOL; endAt: Diag.Pos; ls: Labels; begin Scan.next; try expression(x, nil); if x.typ^.form = Sym.fConst then try Item.coerce(x, Sym.tI64); end; p := try Item.matchBegin(x); t := x.typ; + isStr := t^.form = Sym.fSlice; (* matchBegin made a string a slice *) try expect(Scan.sOf); hasElse := false; all := true; @@ -1926,6 +1964,8 @@ hasElse := true; elsif t^.form = Sym.fInt then try intArm(t, ls, endL); + elsif isStr then + try strArm(p, ls, endL); else first := ls.n; L := Gen.NoLabel; @@ -1990,6 +2030,8 @@ ends := all; if (t^.form = Sym.fInt) and not hasElse then return Scan.markHelp(endAt, "a match on an integer needs an else arm", "it may be empty: | else:"); + elsif isStr and not hasElse then + return Scan.markHelp(endAt, "a match on a string needs an else arm", "it may be empty: | else:"); end; if not hasElse then c := t^.fields; diff --git a/src/check.sh b/src/check.sh index 603dd37..72ac899 100755 --- a/src/check.sh +++ b/src/check.sh @@ -6,10 +6,13 @@ set -e b=build/src # The seed predates raw strings and match on a string (report -# 2026-10.25): the files that use them are the compiler's alone. -newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz" +# 2026-10.25): the files that use them are the compiler's alone, and +# so are those whose message says what match takes. +newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz + conform/run/MatchStr.raz conform/err/MatchStrTwice.raz conform/err/MatchStrNoElse.raz + conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz" newer() { - case " $newer " in *" $1 "*) return 0 ;; esac + for g in $newer; do [ $g = $1 ] && return 0; done return 1 } mkdir -p $b diff --git a/test/msg/AtMatch.err b/test/msg/AtMatch.err index 4b4bdbd..4e6ab54 100644 --- a/test/msg/AtMatch.err +++ b/test/msg/AtMatch.err @@ -1,4 +1,4 @@ -conform/err/AtMatch.raz:5:11: error: match needs a variant, an enumeration or an integer, found F64 +conform/err/AtMatch.raz:5:11: error: match needs a variant, an enumeration, an integer or a string, found F64 5 | match x + 1.0 of end; | ^^^^^^^ raz: 1 error diff --git a/test/msg/MatchReal.err b/test/msg/MatchReal.err index 0315d07..fdc1090 100644 --- a/test/msg/MatchReal.err +++ b/test/msg/MatchReal.err @@ -1,4 +1,4 @@ -conform/err/MatchReal.raz:4:11: error: match needs a variant, an enumeration or an integer, found F64 +conform/err/MatchReal.raz:4:11: error: match needs a variant, an enumeration, an integer or a string, found F64 4 | match n of | ^ raz: 1 error diff --git a/test/msg/MatchStrNoElse.err b/test/msg/MatchStrNoElse.err new file mode 100644 index 0000000..c241aa0 --- /dev/null +++ b/test/msg/MatchStrNoElse.err @@ -0,0 +1,5 @@ +conform/err/MatchStrNoElse.raz:7:5: error: a match on a string needs an else arm + 7 | end; + | ^^^ +help: it may be empty: | else: +raz: 1 error diff --git a/test/msg/MatchStrTwice.err b/test/msg/MatchStrTwice.err new file mode 100644 index 0000000..6e08892 --- /dev/null +++ b/test/msg/MatchStrTwice.err @@ -0,0 +1,4 @@ +conform/err/MatchStrTwice.raz:7:7: error: "b" is in another label too + 7 | | "b": n := 2; + | ^^^ +raz: 1 error diff --git a/test/msg/MatchStrVar.err b/test/msg/MatchStrVar.err new file mode 100644 index 0000000..469b16b --- /dev/null +++ b/test/msg/MatchStrVar.err @@ -0,0 +1,4 @@ +conform/err/MatchStrVar.raz:6:7: error: a label of a match on a string is a string constant, found []U8 + 6 | | s: n := 1; + | ^ +raz: 1 error