raz / commit 2c7e6ede

commit
2c7e6ede260992778ad02e1db1300a5fb9c7f4da
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
a65f701e
Report 2026-10.25: a thaw for strings

Raw strings between backquotes, with no escapes, which may run over
lines; match on a string, with string constants as labels. Str gains
eqFold, findLast, hasSuffix, trim, trimLeft, trimRight, cut, field,
lower, upper, join, replace, decodeLast, valid, chars, toHex and
fromInt; Math gains toReal. The report only, for review; the compiler
follows 2026-10.24 until the code comes.

patch · log

5 files changed, +139 -31

CLAUDE.md modified +1 -1

@@ -95,7 +95,7 @@
9595 defect, and the library grows only by what breaks no program written
9696 for it. A wish for more, the owner's or yours, goes in
9797 `PROPOSALS.md`, for a later thaw; it isn't built. Thaws happen only by the
98-owner's word (v0.6.0, and reports 2026-10.13 to 10.23).
98+owner's word (v0.6.0, reports 2026-10.13 to 10.23, and 10.25 for strings).
9999
100100 The parser reaches the machine only through `Gen.raz`. Keep
101101 `doc/compiler.md`'s table of lines and its routine names true when the

PROPOSALS.md modified +0 -17

@@ -15,23 +15,6 @@
1515 Measured once the port built: not what stands out, the compiler's
1616 profile is flat (`Scan.tokPos` is 9%).
1717
18-## Text that runs over lines, or comes from a file
19-
20-A string ends on the line it starts on, so a long text, a program's
21-help or a script it carries, is written as one constant of many
22-strings joined by `+`, a `\n` at the end of each. Two ways out, either
23-enough:
24-
25-- a string that may run over lines, its line breaks part of it;
26-- a constant whose value is a file's bytes, read when the module is
27- compiled, found as a module is: `const Prelude = file "prelude.fs";`.
28- The second keeps the text a file of its own, which an editor and
29- other tools see as what it is.
30-
31-Met in tery, a Forth in raz, whose prelude of 270 lines of Forth is
32-made into a module of 270 strings by `sed` in its Makefile, every `\`
33-and `"` escaped.
34-
3518 ## Dereferencing a conversion
3619
3720 `^I64(ADR(mem[a]))^` is an error today ("a conversion can't be

doc/raz-lib.md (history) modified +76 -3

@@ -19,7 +19,7 @@
1919 | `Mem` | `Allocator`, the interface of every allocator, and pages from the system |
2020 | `Heap` | a general allocator, `Heap.heap`, safe to call from several threads; freeing a block twice, or one it never gave out, stops the program |
2121 | `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator |
22-| `Str` | operations on `[]U8` strings: comparing, hashing, finding, UTF-8, reading a number |
22+| `Str` | operations on `[]U8` strings: comparing, hashing, finding, trimming and cutting, case, joining and replacing, UTF-8, numbers to and from text |
2323 | `Bytes` | searches of bytes, many at a time: `index`, `last`, `count` and `find` in a `[]U8`; one file per target where the target has instructions for it |
2424 | `Buf` | a growable byte buffer |
2525 | `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])`, or into a `Buf` with `Fmt.write` |
@@ -404,13 +404,30 @@
404404 module Str;
405405 proc cmp*(a, b: []U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *)
406406 proc eq*(a, b: []U8): BOOL;
407+proc eqFold*(a, b: []U8): BOOL; (* eq, an ASCII letter's capital and small the same *)
407408 proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); (* s's bytes in memory from a; false if a has none *)
408409 proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *)
409410 proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *)
411+proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *)
410412 proc hasPrefix*(s, p: []U8): BOOL;
413+proc hasSuffix*(s, x: []U8): BOOL;
414+proc trim*(s: []U8): []U8; (* s without the spaces at either end *)
415+proc trimLeft*(s: []U8): []U8; (* s without the spaces at its start *)
416+proc trimRight*(s: []U8): []U8; (* s without the spaces at its end *)
417+proc cut*(s, sep: []U8): ([]U8, []U8, BOOL); (* what comes before the first sep, and after it *)
418+proc field*(s: []U8): ([]U8, []U8); (* the first run of bytes in s that aren't spaces, and what follows it *)
419+proc lower*(s: []U8); (* s's ASCII capitals made small, in place *)
420+proc upper*(s: []U8); (* s's ASCII small letters made capitals, in place *)
421+proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *)
422+proc replace*(b: ^Buf.Buf; s, old, new: []U8); (* s added to b, with new for each old in it *)
411423 proc encode*(c: I32): ([4]U8, INT); (* c in UTF-8: its bytes, and how many *)
412424 proc decode*(s: []U8): (I32, INT); (* the character s starts with, and its length in bytes *)
425+proc decodeLast*(s: []U8): (I32, INT); (* the character s ends with, and its length in bytes *)
426+proc valid*(s: []U8): BOOL; (* s is UTF-8, every character rightly spelled *)
427+proc chars*(s: []U8): INT; (* the characters in s, as decode reads them *)
413428 proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *)
429+proc toHex*(s: []U8): (U64, BOOL); (* s, in hexadecimal; false if it isn't a number a U64 holds *)
430+proc fromInt*(n: I64): ([20]U8, INT); (* n in decimal: its bytes, and how many *)
414431 proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *)
415432 proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *)
416433 proc putLE*(s: []U8; v: U64); (* v into s, the lowest byte first *)
@@ -481,6 +498,7 @@
481498 proc bits*(x: F64): U64; (* the bits of x, as IEEE 754 lays them out *)
482499 proc fromBits*(u: U64): F64; (* the F64 whose bits are u *)
483500 proc mul*(a, b: U64): (U64, U64); (* a times b: the low 64 bits of the product, and the high *)
501+proc toReal*(s: []U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn't one *)
484502 ```
485503
486504 A `Buf`, a `List` and a `Map` take their memory from the allocator
@@ -493,8 +511,56 @@
493511 10FFFFH, in one to four bytes; a number that isn't one becomes FFFDH,
494512 the character that stands for a wrong one. `decode` reads the first
495513 character of `s`, and gives FFFDH and 1 for a byte that doesn't start
496-one rightly, and 0 and 0 for an empty `s`. `toInt` reads a decimal
497-number, with a `-` or `+` before it if any, and nothing else.
514+one rightly, and 0 and 0 for an empty `s`: a continuation, a byte
515+followed by too few continuations, a surrogate, a character beyond
516+10FFFFH or one spelled in more bytes than it needs. `decodeLast` reads
517+the last character in the same way, to walk a string backwards.
518+`valid` says that `decode` finds no such byte in `s`, and `chars`
519+counts the characters, each such byte one. A string is walked a
520+character at a time so:
521+
522+```
523+i := 0;
524+while i < LEN(s) do
525+ c, n := Str.decode(s[i..LEN(s)]);
526+ ...
527+ i := i + n;
528+end;
529+```
530+
531+A string is bytes, and `Str` takes no more of Unicode than UTF-8:
532+`lower`, `upper` and `eqFold` change and match ASCII letters alone,
533+and nothing says how wide a character is on a terminal, nor which
534+spellings of a text are the same.
535+
536+A space, to `trim`, `trimLeft`, `trimRight` and `field`, is a blank, a
537+tab, a line feed, a carriage return, a vertical tab or a form feed.
538+`cut` gives what comes before the first `sep` in `s`, what comes after
539+it, and `true`; or `s`, an empty string and `false` if `s` has no
540+`sep`. `field` passes the spaces at the start of `s` and gives the
541+bytes up to the next space or the end, and the rest of `s` from there;
542+two empty strings if `s` holds only spaces. They take a string apart
543+without memory of their own, each part a slice of `s`:
544+
545+```
546+line, rest, more := Str.cut(text, "\n"); (* the first line, and the others *)
547+key, val, ok := Str.cut(line, "=");
548+w, rest := Str.field(line); (* the words of a line *)
549+while LEN(w) > 0 do
550+ Out.str(w); Out.ln;
551+ w, rest := Str.field(rest);
552+end;
553+```
554+
555+`lower` and `upper` write `s`'s bytes, so they take a variable's, not
556+a string literal's (section 5.10). `join` and `replace` add to a
557+`Buf` as `Buf.str` does; an empty `old` adds `s` as it is.
558+
559+`toInt` reads a decimal number, with a `-` or `+` before it if any,
560+and nothing else. `toHex` reads hexadecimal digits, capitals or not,
561+and nothing else: no sign, `0x` or `H`. `fromInt` writes a number as
562+`Out.int` does, with a `-` if it is negative, its bytes the first of
563+the 20 it gives.
498564
499565 `le` and `be` read an unsigned number of up to 8 bytes in a fixed
500566 order, little-endian or big-endian, whatever the machine's own, and
@@ -505,6 +571,13 @@
505571 len := U32(Str.le(header[4..8]));
506572 Str.putBE(packet[0..2], U64(port));
507573 ```
574+
575+`Math.toReal` reads a real as a constant is written (section 2), with
576+a `-` or `+` before it if any, its point and the digits after it, or
577+its exponent, left out if it has none, and `e` taken as `E`: `2`,
578+`-0.5` and `1.5e-3` are reals to it. It gives the `F64` nearest the
579+number, and `false` for anything else, or a number too big for an
580+`F64`.
508581
509582 `Map.next` walks a map: from 0, it gives the next entry and the number
510583 to pass for the one after it, and -1 when there are no more. A walk

doc/raz.md (history) modified +55 -10

@@ -13,13 +13,19 @@
1313 This report defines the language. It is short on purpose. Whatever it
1414 doesn't allow, raz doesn't have.
1515
16-This is version 2026-10.24 of the report; section 11 says where each
16+This is version 2026-10.25 of the report; section 11 says where each
1717 version's changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose,
1818 for a language anyone can build again. They break programs written
1919 for 2026-10.10 and before: for 2026-10.9, say, the report of compiler
2020 0.5.0, the last release before them; and 2026-10.12 breaks code
2121 procedures written for 2026-10.11. 2026-10.20 is a third, for `try`
22-(section 7.8); it breaks a program that has a name `try`.
22+(section 7.8); it breaks a program that has a name `try`. 2026-10.25
23+is a fourth, for strings: raw strings, which may run over lines
24+(section 2), and `match` on a string (section 7.4); it breaks no
25+program.
26+
27+The compiler follows version 2026-10.24 until it has what 2026-10.25
28+adds.
2329
2430 The language is frozen, softly, from this version on: it changes only
2531 to mend a defect, and its library grows only by what breaks no program
@@ -107,8 +113,28 @@
107113 (zero), `\\`, `\'`, `\"`, `\u{H...}` (the code point with hexadecimal
108114 value H..., up to 10FFFFH, in a string its UTF-8) and, in strings only, `\xHH` (the
109115 byte with hexadecimal value HH, which needn't make valid UTF-8). In
110-an escape, the hexadecimal digits may be capitals or not. A character
111-or a string ends on the line it starts on.
116+an escape, the hexadecimal digits may be capitals or not. A character,
117+or a string in double quotes, ends on the line it starts on.
118+
119+**Raw strings.** ``` `C:\tmp\"a b"` ``` is a string written between
120+backquotes: its bytes are the text between them as it stands, with no
121+escapes, so `\` and `"` are themselves. A raw string may run over
122+lines, and each line break in it is a line feed, part of the string; a
123+carriage return in it is left out, and so is a line break right after
124+the opening backquote, so that a long text may start on a line of its
125+own:
126+
127+```
128+const Usage = `
129+usage: tool [-v] file ...
130+ -v say what is done
131+`;
132+```
133+
134+is `"usage: tool [-v] file ...\n -v say what is done\n"`. A raw
135+string can't hold a backquote; a text that needs one joins it with
136+`+`, as ``"`"``. Everywhere else a raw string is a string, as one in
137+double quotes is.
112138
113139 Messages give the line and the column, counted in characters
114140 (section 10).
@@ -905,8 +931,8 @@
905931 end;
906932 ```
907933
908-`match` takes a variant, an enumeration or an integer type (a `U8`
909-included); other types are an error. It runs the arm of the case `s` holds. An arm may name the case's
934+`match` takes a variant, an enumeration, an integer type (a `U8`
935+included) or a string, a `[]U8`; other types are an error. It runs the arm of the case `s` holds. An arm may name the case's
910936 fields, in order, all of them; the names stand for those fields of `s`
911937 itself, so assigning to one changes `s`; if `s` isn't a variable, they
912938 stand for the fields of a copy. An arm may also leave the fields
@@ -943,9 +969,26 @@
943969 both included (unlike a slice, which leaves `hi` out; `lo` may not be
944970 above `hi`). Each must fit the integer's type, and no value may be in
945971 two labels. An integer
946-`match` must have an `| else:` arm, which may be empty. Whether an arm
972+`match` must have an `| else:` arm, which may be empty.
973+
974+`match` on a string, a `[]U8` or what converts to one (section 5.10),
975+runs the arm one of whose labels it equals, as `=` compares strings
976+(section 6.5):
977+
978+```
979+match word of
980+| "add", "plus": op := add;
981+| "sub": op := sub;
982+| else: op := none;
983+end;
984+```
985+
986+A label is a string constant, and no two labels may be equal; there
987+are no ranges. A `match` on a string must have an `| else:` arm, which
988+may be empty. Whether an arm
947989 names a case or gives labels is set by the type matched: `| Max:` is a
948-case of a variant or an enumeration, and a constant for an integer.
990+case of a variant or an enumeration, and a constant for an integer
991+or a string.
949992
950993 ### 7.5 Order of evaluation
951994
@@ -1429,7 +1472,8 @@
14291472 | digit { hexDigit } "H"
14301473 | digit { digit } "." digit { digit } [ "E" [ "+" | "-" ] digit { digit } ] .
14311474 char = "'" ( character | escape ) "'" .
1432-string = '"' { character | escape | "\x" anyHex anyHex } '"' .
1475+string = '"' { character | escape | "\x" anyHex anyHex } '"'
1476+ | "`" { rawChar } "`" .
14331477 escape = "\n" | "\t" | "\r" | "\0" | "\\" | "\'" | '\"'
14341478 | "\u{" anyHex { anyHex } "}" .
14351479 anyHex = hexDigit | "a" .. "f" .
@@ -1517,7 +1561,8 @@
15171561 ```
15181562
15191563 `character` is any character but a line break, and in a string any
1520-but `"` and `\`, in a character any but `'` and `\`. A procedure
1564+but `"` and `\`, in a character any but `'` and `\`. `rawChar` is any
1565+character but `` ` ``, line breaks included. A procedure
15211566 argument of a generic module (section 3) is written as a `QualIdent`,
15221567 which is a `Type` here. `...` comes last in a `Signature`, and only in
15231568 an `extern`. An `Instruction` is written in the target's assembly

doc/report-history.md (history) modified +7 -0

@@ -134,3 +134,10 @@
134134 `sendFds` and `receiveFds`; and memory shared through a file,
135135 `memFile` and `mapFile`. `Thread` works in a program that uses C,
136136 and no longer says `link static` (volume 2).
137+- **2026-10.25.** A thaw, for strings. A raw string, between
138+ backquotes, has no escapes and may run over lines (section 2 and
139+ appendix B); `match` takes a string, with string constants as labels
140+ (section 7.4). It breaks no program. `Str` gains `eqFold`,
141+ `findLast`, `hasSuffix`, `trim`, `trimLeft`, `trimRight`, `cut`,
142+ `field`, `lower`, `upper`, `join`, `replace`, `decodeLast`, `valid`,
143+ `chars`, `toHex` and `fromInt`; `Math` gains `toReal` (volume 2).