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.
| @@ -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. |
| @@ -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. |
| @@ -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. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +111223000 | |
| 2 | +array | |
| 3 | +slice | |
| 4 | +constant |
| @@ -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. |
| @@ -19,8 +19,8 @@ | ||
| 19 | 19 | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | |
| 20 | 20 | | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler | |
| 21 | 21 | | `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 | | |
| 24 | 24 | | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places | |
| 25 | 25 | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | |
| 26 | 26 | | `src/Raz.raz` | 305 | 238 | The command line | |
| @@ -1318,7 +1318,13 @@ | ||
| 1318 | 1318 | `dispatch` makes a jump table (gen's `jumpTable`, whose comment shows |
| 1319 | 1319 | the code) for four labels or more that lie close together, else |
| 1320 | 1320 | 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 | |
| 1322 | 1328 | is compiled where it stands, as a little subroutine of the procedure |
| 1323 | 1329 | that the code jumps over (gen's `deferBegin` and `deferEnd`), and |
| 1324 | 1330 | `runDefers` emits a call to it at every way out of its statement list: |
| @@ -1467,14 +1467,18 @@ | ||
| 1467 | 1467 | return nil; |
| 1468 | 1468 | end; |
| 1469 | 1469 | |
| 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. *) | |
| 1472 | 1472 | proc matchBegin*(var x: Item): (I64, ERROR); |
| 1473 | 1473 | var p: I64; |
| 1474 | 1474 | begin |
| 1475 | 1475 | 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; | |
| 1476 | 1480 | 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))); | |
| 1478 | 1482 | end; |
| 1479 | 1483 | if x.ro then try copyToTemp(x); end; (* its arms' names stand for a copy's fields *) |
| 1480 | 1484 | try address(x); |
| @@ -1492,6 +1496,17 @@ | ||
| 1492 | 1496 | Gen.pushVar(a, 8, false, Gen.kWord); |
| 1493 | 1497 | Gen.pushConst(Gen.kWord, tag); |
| 1494 | 1498 | 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; | |
| 1495 | 1510 | end; |
| 1496 | 1511 | |
| 1497 | 1512 | (* To body if the integer in slot p is from lo to hi: less its lo, it is |
| @@ -1848,6 +1848,43 @@ | ||
| 1848 | 1848 | return nil; |
| 1849 | 1849 | end; |
| 1850 | 1850 | |
| 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 | + | |
| 1851 | 1888 | const |
| 1852 | 1889 | MinTable = 4; (* fewer labels are tested one by one *) |
| 1853 | 1890 | TableDensity = 8; (* a table has at most this many entries to a label *) |
| @@ -1896,13 +1933,14 @@ | ||
| 1896 | 1933 | proc matchStatement(start: Diag.Pos; ind: INT): ERROR; |
| 1897 | 1934 | var |
| 1898 | 1935 | 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; | |
| 1900 | 1937 | begin |
| 1901 | 1938 | Scan.next; |
| 1902 | 1939 | try expression(x, nil); |
| 1903 | 1940 | if x.typ^.form = Sym.fConst then try Item.coerce(x, Sym.tI64); end; |
| 1904 | 1941 | p := try Item.matchBegin(x); |
| 1905 | 1942 | t := x.typ; |
| 1943 | + isStr := t^.form = Sym.fSlice; (* matchBegin made a string a slice *) | |
| 1906 | 1944 | try expect(Scan.sOf); |
| 1907 | 1945 | hasElse := false; |
| 1908 | 1946 | all := true; |
| @@ -1926,6 +1964,8 @@ | ||
| 1926 | 1964 | hasElse := true; |
| 1927 | 1965 | elsif t^.form = Sym.fInt then |
| 1928 | 1966 | try intArm(t, ls, endL); |
| 1967 | + elsif isStr then | |
| 1968 | + try strArm(p, ls, endL); | |
| 1929 | 1969 | else |
| 1930 | 1970 | first := ls.n; |
| 1931 | 1971 | L := Gen.NoLabel; |
| @@ -1990,6 +2030,8 @@ | ||
| 1990 | 2030 | ends := all; |
| 1991 | 2031 | if (t^.form = Sym.fInt) and not hasElse then |
| 1992 | 2032 | 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:"); | |
| 1993 | 2035 | end; |
| 1994 | 2036 | if not hasElse then |
| 1995 | 2037 | c := t^.fields; |
| @@ -6,10 +6,13 @@ | ||
| 6 | 6 | set -e |
| 7 | 7 | b=build/src |
| 8 | 8 | # 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" | |
| 11 | 14 | newer() { |
| 12 | - case " $newer " in *" $1 "*) return 0 ;; esac | |
| 15 | + for g in $newer; do [ $g = $1 ] && return 0; done | |
| 13 | 16 | return 1 |
| 14 | 17 | } |
| 15 | 18 | mkdir -p $b |
| @@ -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 | |
| 2 | 2 | 5 | match x + 1.0 of end; |
| 3 | 3 | | ^^^^^^^ |
| 4 | 4 | raz: 1 error |
| @@ -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 | |
| 2 | 2 | 4 | match n of |
| 3 | 3 | | ^ |
| 4 | 4 | raz: 1 error |
| @@ -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 |
| @@ -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 |
| @@ -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 |