From 2c7e6ede260992778ad02e1db1300a5fb9c7f4da Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Wed, 14 Oct 2026 21:58:43 +0000 Subject: [PATCH] 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. --- diff --git a/CLAUDE.md b/CLAUDE.md index 23161b3..006efdb 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -95,7 +95,7 @@ defect, and the library grows only by what breaks no program written for it. A wish for more, the owner's or yours, goes in `PROPOSALS.md`, for a later thaw; it isn't built. Thaws happen only by the -owner's word (v0.6.0, and reports 2026-10.13 to 10.23). +owner's word (v0.6.0, reports 2026-10.13 to 10.23, and 10.25 for strings). The parser reaches the machine only through `Gen.raz`. Keep `doc/compiler.md`'s table of lines and its routine names true when the diff --git a/PROPOSALS.md b/PROPOSALS.md index c0e2565..c9db06d 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -15,23 +15,6 @@ Measured once the port built: not what stands out, the compiler's profile is flat (`Scan.tokPos` is 9%). -## Text that runs over lines, or comes from a file - -A string ends on the line it starts on, so a long text, a program's -help or a script it carries, is written as one constant of many -strings joined by `+`, a `\n` at the end of each. Two ways out, either -enough: - -- a string that may run over lines, its line breaks part of it; -- a constant whose value is a file's bytes, read when the module is - compiled, found as a module is: `const Prelude = file "prelude.fs";`. - The second keeps the text a file of its own, which an editor and - other tools see as what it is. - -Met in tery, a Forth in raz, whose prelude of 270 lines of Forth is -made into a module of 270 strings by `sed` in its Makefile, every `\` -and `"` escaped. - ## Dereferencing a conversion `^I64(ADR(mem[a]))^` is an error today ("a conversion can't be diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 15f182b..5d88524 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -19,7 +19,7 @@ | `Mem` | `Allocator`, the interface of every allocator, and pages from the system | | `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 | | `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator | -| `Str` | operations on `[]U8` strings: comparing, hashing, finding, UTF-8, reading a number | +| `Str` | operations on `[]U8` strings: comparing, hashing, finding, trimming and cutting, case, joining and replacing, UTF-8, numbers to and from text | | `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 | | `Buf` | a growable byte buffer | | `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 @@ module Str; proc cmp*(a, b: []U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *) proc eq*(a, b: []U8): BOOL; +proc eqFold*(a, b: []U8): BOOL; (* eq, an ASCII letter's capital and small the same *) proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); (* s's bytes in memory from a; false if a has none *) proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *) proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *) +proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *) proc hasPrefix*(s, p: []U8): BOOL; +proc hasSuffix*(s, x: []U8): BOOL; +proc trim*(s: []U8): []U8; (* s without the spaces at either end *) +proc trimLeft*(s: []U8): []U8; (* s without the spaces at its start *) +proc trimRight*(s: []U8): []U8; (* s without the spaces at its end *) +proc cut*(s, sep: []U8): ([]U8, []U8, BOOL); (* what comes before the first sep, and after it *) +proc field*(s: []U8): ([]U8, []U8); (* the first run of bytes in s that aren't spaces, and what follows it *) +proc lower*(s: []U8); (* s's ASCII capitals made small, in place *) +proc upper*(s: []U8); (* s's ASCII small letters made capitals, in place *) +proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *) +proc replace*(b: ^Buf.Buf; s, old, new: []U8); (* s added to b, with new for each old in it *) proc encode*(c: I32): ([4]U8, INT); (* c in UTF-8: its bytes, and how many *) proc decode*(s: []U8): (I32, INT); (* the character s starts with, and its length in bytes *) +proc decodeLast*(s: []U8): (I32, INT); (* the character s ends with, and its length in bytes *) +proc valid*(s: []U8): BOOL; (* s is UTF-8, every character rightly spelled *) +proc chars*(s: []U8): INT; (* the characters in s, as decode reads them *) proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *) +proc toHex*(s: []U8): (U64, BOOL); (* s, in hexadecimal; false if it isn't a number a U64 holds *) +proc fromInt*(n: I64): ([20]U8, INT); (* n in decimal: its bytes, and how many *) proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *) proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *) proc putLE*(s: []U8; v: U64); (* v into s, the lowest byte first *) @@ -481,6 +498,7 @@ proc bits*(x: F64): U64; (* the bits of x, as IEEE 754 lays them out *) proc fromBits*(u: U64): F64; (* the F64 whose bits are u *) proc mul*(a, b: U64): (U64, U64); (* a times b: the low 64 bits of the product, and the high *) +proc toReal*(s: []U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn't one *) ``` A `Buf`, a `List` and a `Map` take their memory from the allocator @@ -493,8 +511,56 @@ 10FFFFH, in one to four bytes; a number that isn't one becomes FFFDH, the character that stands for a wrong one. `decode` reads the first character of `s`, and gives FFFDH and 1 for a byte that doesn't start -one rightly, and 0 and 0 for an empty `s`. `toInt` reads a decimal -number, with a `-` or `+` before it if any, and nothing else. +one rightly, and 0 and 0 for an empty `s`: a continuation, a byte +followed by too few continuations, a surrogate, a character beyond +10FFFFH or one spelled in more bytes than it needs. `decodeLast` reads +the last character in the same way, to walk a string backwards. +`valid` says that `decode` finds no such byte in `s`, and `chars` +counts the characters, each such byte one. A string is walked a +character at a time so: + +``` +i := 0; +while i < LEN(s) do + c, n := Str.decode(s[i..LEN(s)]); + ... + i := i + n; +end; +``` + +A string is bytes, and `Str` takes no more of Unicode than UTF-8: +`lower`, `upper` and `eqFold` change and match ASCII letters alone, +and nothing says how wide a character is on a terminal, nor which +spellings of a text are the same. + +A space, to `trim`, `trimLeft`, `trimRight` and `field`, is a blank, a +tab, a line feed, a carriage return, a vertical tab or a form feed. +`cut` gives what comes before the first `sep` in `s`, what comes after +it, and `true`; or `s`, an empty string and `false` if `s` has no +`sep`. `field` passes the spaces at the start of `s` and gives the +bytes up to the next space or the end, and the rest of `s` from there; +two empty strings if `s` holds only spaces. They take a string apart +without memory of their own, each part a slice of `s`: + +``` +line, rest, more := Str.cut(text, "\n"); (* the first line, and the others *) +key, val, ok := Str.cut(line, "="); +w, rest := Str.field(line); (* the words of a line *) +while LEN(w) > 0 do + Out.str(w); Out.ln; + w, rest := Str.field(rest); +end; +``` + +`lower` and `upper` write `s`'s bytes, so they take a variable's, not +a string literal's (section 5.10). `join` and `replace` add to a +`Buf` as `Buf.str` does; an empty `old` adds `s` as it is. + +`toInt` reads a decimal number, with a `-` or `+` before it if any, +and nothing else. `toHex` reads hexadecimal digits, capitals or not, +and nothing else: no sign, `0x` or `H`. `fromInt` writes a number as +`Out.int` does, with a `-` if it is negative, its bytes the first of +the 20 it gives. `le` and `be` read an unsigned number of up to 8 bytes in a fixed order, little-endian or big-endian, whatever the machine's own, and @@ -505,6 +571,13 @@ len := U32(Str.le(header[4..8])); Str.putBE(packet[0..2], U64(port)); ``` + +`Math.toReal` reads a real as a constant is written (section 2), with +a `-` or `+` before it if any, its point and the digits after it, or +its exponent, left out if it has none, and `e` taken as `E`: `2`, +`-0.5` and `1.5e-3` are reals to it. It gives the `F64` nearest the +number, and `false` for anything else, or a number too big for an +`F64`. `Map.next` walks a map: from 0, it gives the next entry and the number to pass for the one after it, and -1 when there are no more. A walk diff --git a/doc/raz.md b/doc/raz.md index c0ec3c8..cdaa30f 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -13,13 +13,19 @@ This report defines the language. It is short on purpose. Whatever it doesn't allow, raz doesn't have. -This is version 2026-10.24 of the report; section 11 says where each +This is version 2026-10.25 of the report; section 11 says where each version's changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for `try` -(section 7.8); it breaks a program that has a name `try`. +(section 7.8); it breaks a program that has a name `try`. 2026-10.25 +is a fourth, for strings: raw strings, which may run over lines +(section 2), and `match` on a string (section 7.4); it breaks no +program. + +The compiler follows version 2026-10.24 until it has what 2026-10.25 +adds. The language is frozen, softly, from this version on: it changes only to mend a defect, and its library grows only by what breaks no program @@ -107,8 +113,28 @@ (zero), `\\`, `\'`, `\"`, `\u{H...}` (the code point with hexadecimal value H..., up to 10FFFFH, in a string its UTF-8) and, in strings only, `\xHH` (the byte with hexadecimal value HH, which needn't make valid UTF-8). In -an escape, the hexadecimal digits may be capitals or not. A character -or a string ends on the line it starts on. +an escape, the hexadecimal digits may be capitals or not. A character, +or a string in double quotes, ends on the line it starts on. + +**Raw strings.** ``` `C:\tmp\"a b"` ``` is a string written between +backquotes: its bytes are the text between them as it stands, with no +escapes, so `\` and `"` are themselves. A raw string may run over +lines, and each line break in it is a line feed, part of the string; a +carriage return in it is left out, and so is a line break right after +the opening backquote, so that a long text may start on a line of its +own: + +``` +const Usage = ` +usage: tool [-v] file ... + -v say what is done +`; +``` + +is `"usage: tool [-v] file ...\n -v say what is done\n"`. A raw +string can't hold a backquote; a text that needs one joins it with +`+`, as ``"`"``. Everywhere else a raw string is a string, as one in +double quotes is. Messages give the line and the column, counted in characters (section 10). @@ -905,8 +931,8 @@ end; ``` -`match` takes a variant, an enumeration or an integer type (a `U8` -included); other types are an error. It runs the arm of the case `s` holds. An arm may name the case's +`match` takes a variant, an enumeration, an integer type (a `U8` +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 fields, in order, all of them; the names stand for those fields of `s` itself, so assigning to one changes `s`; if `s` isn't a variable, they stand for the fields of a copy. An arm may also leave the fields @@ -943,9 +969,26 @@ both included (unlike a slice, which leaves `hi` out; `lo` may not be above `hi`). Each must fit the integer's type, and no value may be in two labels. An integer -`match` must have an `| else:` arm, which may be empty. Whether an arm +`match` must have an `| else:` arm, which may be empty. + +`match` on a string, a `[]U8` or what converts to one (section 5.10), +runs the arm one of whose labels it equals, as `=` compares strings +(section 6.5): + +``` +match word of +| "add", "plus": op := add; +| "sub": op := sub; +| else: op := none; +end; +``` + +A label is a string constant, and no two labels may be equal; there +are no ranges. A `match` on a string must have an `| else:` arm, which +may be empty. Whether an arm names a case or gives labels is set by the type matched: `| Max:` is a -case of a variant or an enumeration, and a constant for an integer. +case of a variant or an enumeration, and a constant for an integer +or a string. ### 7.5 Order of evaluation @@ -1429,7 +1472,8 @@ | digit { hexDigit } "H" | digit { digit } "." digit { digit } [ "E" [ "+" | "-" ] digit { digit } ] . char = "'" ( character | escape ) "'" . -string = '"' { character | escape | "\x" anyHex anyHex } '"' . +string = '"' { character | escape | "\x" anyHex anyHex } '"' + | "`" { rawChar } "`" . escape = "\n" | "\t" | "\r" | "\0" | "\\" | "\'" | '\"' | "\u{" anyHex { anyHex } "}" . anyHex = hexDigit | "a" .. "f" . @@ -1517,7 +1561,8 @@ ``` `character` is any character but a line break, and in a string any -but `"` and `\`, in a character any but `'` and `\`. A procedure +but `"` and `\`, in a character any but `'` and `\`. `rawChar` is any +character but `` ` ``, line breaks included. A procedure argument of a generic module (section 3) is written as a `QualIdent`, which is a `Type` here. `...` comes last in a `Signature`, and only in an `extern`. An `Instruction` is written in the target's assembly diff --git a/doc/report-history.md b/doc/report-history.md index 22f8f54..f91dd90 100644 --- a/doc/report-history.md +++ b/doc/report-history.md @@ -134,3 +134,10 @@ `sendFds` and `receiveFds`; and memory shared through a file, `memFile` and `mapFile`. `Thread` works in a program that uses C, and no longer says `link static` (volume 2). +- **2026-10.25.** A thaw, for strings. A raw string, between + backquotes, has no escapes and may run over lines (section 2 and + appendix B); `match` takes a string, with string constants as labels + (section 7.4). It breaks no program. `Str` gains `eqFold`, + `findLast`, `hasSuffix`, `trim`, `trimLeft`, `trimRight`, `cut`, + `field`, `lower`, `upper`, `join`, `replace`, `decodeLast`, `valid`, + `chars`, `toHex` and `fromInt`; `Math` gains `toReal` (volume 2).