Docs, tutorial and tour for strings; the compiler follows report 2026-10.25 The tour's strings lesson takes strings apart with Str.cut and Str.field, shows raw strings, indented and not, and walks UTF-8 with Str.next; the variants lesson matches strings; the memory lesson builds with Str.join and Str.replace; the advanced tour reads numbers with Str.cut, Str.toHex, Math.toReal and Str.fromInt. The tutorial, raz(7), the README and the home page say the same. examples/Settings reads settings from an indented raw string, Sh picks its builtins by a match on a string, and Words takes its text apart with Str.field; tools/Doc uses Str.trim, and reads \(ga in a page as a backquote, as the site's builder does. The editors' colour test has a raw string.
| @@ -78,7 +78,9 @@ | ||
| 78 | 78 | |
| 79 | 79 | ## Where things stand |
| 80 | 80 | |
| 81 | -v0.12.0, report 2026-10.24, is the latest release. `src/` is the | |
| 81 | +v0.12.0, report 2026-10.24, is the latest release; `main` has report | |
| 82 | +2026-10.25, the thaw for strings (raw strings, `match` on a string, | |
| 83 | +`Str`'s new procedures, `Math.toReal`), not yet released. `src/` is the | |
| 82 | 84 | compiler in raz, built by itself (Self, done); the seed in `seed/` is |
| 83 | 85 | frozen, and v0.9.1 was the last release it built. The compiler takes |
| 84 | 86 | 1.4 times the Pascal one's time; `make kernels` measures `test/bench` |
| @@ -150,6 +150,9 @@ | ||
| 150 | 150 | size, in the order written. `CAS`, `XCHG`, `XADD`, `FENCE`. |
| 151 | 151 | - `and`, `or` and `not` for `BOOL`s; `&`, `|`, `xor` and `~` for bits; |
| 152 | 152 | `=` on arrays and strings. |
| 153 | +- Strings are `[]U8`, in UTF-8; raw strings between backquotes run over | |
| 154 | + lines and keep the indent of the code around them; `match` takes a | |
| 155 | + string. | |
| 153 | 156 | - Several results, `ERROR` values compared by identity, `defer`. |
| 154 | 157 | - Modules with bodies; generic modules with type and procedure |
| 155 | 158 | arguments, `Map(K, V; hash, eq)`. |
| @@ -158,7 +161,7 @@ | ||
| 158 | 161 | - `extern` procedures and variables from C; code procedures written in |
| 159 | 162 | the target's assembly language; `SYSCALL`. |
| 160 | 163 | |
| 161 | -The report, `doc/raz.md` and `doc/raz-lib.md`, says all of it in about 2,100 lines. | |
| 164 | +The report, `doc/raz.md` and `doc/raz-lib.md`, says all of it in about 2,300 lines. | |
| 162 | 165 | |
| 163 | 166 | ## The library |
| 164 | 167 |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Reading the raz compiler |
| 2 | 2 | |
| 3 | -The compiler is eleven files of raz, about 10,800 lines of code (the | |
| 3 | +The compiler is eleven files of raz, about 10,900 lines of code (the | |
| 4 | 4 | budget is a soft 10,000, never past 11,000). It is its own compiler: the |
| 5 | 5 | frozen Pascal compiler in `seed/` builds `src/` the first time, and |
| 6 | 6 | after that a raz builds `src/` (`make fixpoint` checks that it makes |
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | .\" Every whole module in a literal block here is a program that make |
| 3 | 3 | .\" check compiles and runs, against test/man/Name.expect; see |
| 4 | 4 | .\" tools/Doc.raz. |
| 5 | -.Dd October 12, 2026 | |
| 5 | +.Dd October 14, 2026 | |
| 6 | 6 | .Dt RAZ-TUTORIAL 7 |
| 7 | 7 | .Os |
| 8 | 8 | .Sh NAME |
| @@ -308,7 +308,7 @@ | ||
| 308 | 308 | end; |
| 309 | 309 | |
| 310 | 310 | proc main*; |
| 311 | -var a: [5]INT; i: INT; s: []U8; name: [8]U8; | |
| 311 | +var a: [5]INT; i: INT; s, val: []U8; name: [8]U8; ok: BOOL; | |
| 312 | 312 | begin |
| 313 | 313 | for i := 0 to 4 do a[i] := i * i; end; |
| 314 | 314 | Out.int(sum(a)); Out.ln; (* the array, as a slice *) |
| @@ -317,9 +317,15 @@ | ||
| 317 | 317 | Out.str(s[7..LEN(s)]); Out.ln; |
| 318 | 318 | if s[0..5] = "hello" then Out.str("it says hello\en"); end; |
| 319 | 319 | Out.int(Str.find(s, "world")); Out.ln; |
| 320 | + _, val, ok := Str.cut(" size = 42 ", "="); | |
| 321 | + if ok then Out.str(Str.trim(val)); Out.ln; end; | |
| 320 | 322 | name := "raz"; (* a copy, the program's own *) |
| 321 | 323 | name[0] := 'R'; |
| 322 | 324 | Out.str(name[0..3]); Out.ln; |
| 325 | + Out.str(\(ga | |
| 326 | + C:\etmp\e "as it stands" | |
| 327 | + over lines | |
| 328 | + \(ga); | |
| 323 | 329 | end; |
| 324 | 330 | |
| 325 | 331 | end Slices. |
| @@ -330,7 +336,10 @@ | ||
| 330 | 336 | world |
| 331 | 337 | it says hello |
| 332 | 338 | 7 |
| 339 | +42 | |
| 333 | 340 | Raz |
| 341 | +C:\etmp\e "as it stands" | |
| 342 | + over lines | |
| 334 | 343 | .Ed |
| 335 | 344 | .Pp |
| 336 | 345 | .Cm [5]INT |
| @@ -358,8 +367,15 @@ | ||
| 358 | 367 | and |
| 359 | 368 | .Cm # ; |
| 360 | 369 | .Cm Str |
| 361 | -has the rest: ordering, finding, hashing, UTF-8 and reading a number. | |
| 370 | +has the rest: ordering, finding, trimming, cutting a string into | |
| 371 | +parts, which are slices of it, UTF-8, and numbers to and from text. | |
| 362 | 372 | .Pp |
| 373 | +A string between backquotes is raw: no escapes, and it may run over | |
| 374 | +lines. | |
| 375 | +When the opening backquote ends its line and the closing one has | |
| 376 | +only blanks before it, those blanks come off every line, so the text | |
| 377 | +stands at the indent of the code around it. | |
| 378 | +.Pp | |
| 363 | 379 | A |
| 364 | 380 | .Cm for |
| 365 | 381 | loop takes its bounds once, and never steps past the last: with an |
| @@ -740,6 +756,15 @@ | ||
| 740 | 756 | end; |
| 741 | 757 | end; |
| 742 | 758 | |
| 759 | +proc colour(name: []U8): Colour; | |
| 760 | +begin | |
| 761 | + match name of | |
| 762 | + | "red", "rose": return red; | |
| 763 | + | "green": return green; | |
| 764 | + | else: return blue; | |
| 765 | + end; | |
| 766 | +end; | |
| 767 | + | |
| 743 | 768 | proc main*; |
| 744 | 769 | var shapes: [3]Shape; i: INT; warm: Colours; c: Colour; |
| 745 | 770 | begin |
| @@ -753,6 +778,7 @@ | ||
| 753 | 778 | for c := red to blue do |
| 754 | 779 | if c in warm then Out.int(I64(c)); Out.ln; end; |
| 755 | 780 | end; |
| 781 | + Out.int(I64(colour("rose"))); Out.int(I64(colour("green"))); Out.ln; | |
| 756 | 782 | end; |
| 757 | 783 | |
| 758 | 784 | end Shapes. |
| @@ -764,6 +790,7 @@ | ||
| 764 | 790 | digit letter other |
| 765 | 791 | 0 |
| 766 | 792 | 1 |
| 793 | +01 | |
| 767 | 794 | .Ed |
| 768 | 795 | .Pp |
| 769 | 796 | An enumeration is a list of names, in order; a |
| @@ -786,7 +813,8 @@ | ||
| 786 | 813 | comes last. |
| 787 | 814 | On an integer, |
| 788 | 815 | .Cm match |
| 789 | -takes constants and ranges, and must have an | |
| 816 | +takes constants and ranges, and on a string, string constants; both | |
| 817 | +must have an | |
| 790 | 818 | .Cm "| else:" . |
| 791 | 819 | .Sh 12. GENERIC MODULES |
| 792 | 820 | A module may take types, and the procedures it needs on them, as |
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | .\" raz(7) - the raz language at a glance |
| 2 | -.Dd October 12, 2026 | |
| 2 | +.Dd October 14, 2026 | |
| 3 | 3 | .Dt RAZ 7 |
| 4 | 4 | .Os |
| 5 | 5 | .Sh NAME |
| @@ -45,6 +45,11 @@ | ||
| 45 | 45 | .It Strings |
| 46 | 46 | .Cm \(dqhello\en\(dq , |
| 47 | 47 | its UTF-8 bytes, ending on its line. |
| 48 | +.It Raw strings | |
| 49 | +.Cm \(gaC:\etmp\(ga , | |
| 50 | +the text between backquotes as it stands, no escapes, over lines; if | |
| 51 | +the opening backquote ends its line and the closing one has only | |
| 52 | +blanks before it, those blanks come off every line. | |
| 48 | 53 | .It Escapes |
| 49 | 54 | .Cm \en \et \er \e0 \e\e \e' \e\(dq \eu{E9} , |
| 50 | 55 | and in strings |
| @@ -432,6 +437,10 @@ | ||
| 432 | 437 | enumeration, an arm for each name; on an integer, arms of constants |
| 433 | 438 | and ranges, |
| 434 | 439 | .Cm "| '0' .. '9':" , |
| 440 | +and an | |
| 441 | +.Cm "| else:" ; | |
| 442 | +on a string, arms of string constants, | |
| 443 | +.Cm "| \(dqadd\(dq, \(dqplus\(dq:" , | |
| 435 | 444 | and an |
| 436 | 445 | .Cm "| else:" , |
| 437 | 446 | which any |
| @@ -24,9 +24,6 @@ | ||
| 24 | 24 | and may be indented with the code around them (section 2), and |
| 25 | 25 | `match` on a string (section 7.4); it breaks no program. |
| 26 | 26 | |
| 27 | -The compiler follows version 2026-10.24 until it has what 2026-10.25 | |
| 28 | -adds. | |
| 29 | - | |
| 30 | 27 | The language is frozen, softly, from this version on: it changes only |
| 31 | 28 | to mend a defect, and its library grows only by what breaks no program |
| 32 | 29 | written for it. What anyone wishes it had waits on a list, for a later |
| @@ -0,0 +1,4 @@ | ||
| 1 | +line 5: colour = "green" | |
| 2 | +line 6: depth | |
| 3 | +raz \ settings: 80 by 24, at 1.5 | |
| 4 | +5 characters in naïve |
| @@ -0,0 +1,65 @@ | ||
| 1 | +(* Strings: reads settings from a text of its own, written as a raw | |
| 2 | + string that keeps the indent of the code around it. Each line is a | |
| 3 | + name, =, and a value; # starts a comment. Str takes the lines apart | |
| 4 | + without copying them, and a match on a string picks what each name | |
| 5 | + sets. *) | |
| 6 | +module Settings; | |
| 7 | +import Out, Fmt, Str, Math; | |
| 8 | + | |
| 9 | +const | |
| 10 | + Text = ` | |
| 11 | + # a window | |
| 12 | + title = raz \ settings | |
| 13 | + width = 80 | |
| 14 | + Height=24 | |
| 15 | + scale = 1.5 | |
| 16 | + colour = "green" | |
| 17 | + depth | |
| 18 | + `; | |
| 19 | + | |
| 20 | +var | |
| 21 | + title: []U8; | |
| 22 | + width, height: I64; | |
| 23 | + scale: F64; | |
| 24 | + | |
| 25 | +(* One setting: false if the name isn't known, or its value is wrong. *) | |
| 26 | +proc setting(name, value: []U8): BOOL; | |
| 27 | +var ok: BOOL; lower: [16]U8; | |
| 28 | +begin | |
| 29 | + if LEN(name) > LEN(lower) then return false; end; | |
| 30 | + COPY(lower, name); | |
| 31 | + Str.lower(lower[0..LEN(name)]); | |
| 32 | + ok := true; | |
| 33 | + match lower[0..LEN(name)] of | |
| 34 | + | "title": title := value; | |
| 35 | + | "width": width, ok := Str.toInt(value); | |
| 36 | + | "height": height, ok := Str.toInt(value); | |
| 37 | + | "scale": scale, ok := Math.toReal(value); | |
| 38 | + | else: ok := false; | |
| 39 | + end; | |
| 40 | + return ok; | |
| 41 | +end; | |
| 42 | + | |
| 43 | +proc main*; | |
| 44 | +var line, rest, name, value: []U8; more, found: BOOL; n: INT; | |
| 45 | +begin | |
| 46 | + rest := Text; | |
| 47 | + more := true; | |
| 48 | + while more do | |
| 49 | + line, rest, more := Str.cut(rest, "\n"); | |
| 50 | + line, _, _ := Str.cut(line, "#"); | |
| 51 | + line := Str.trim(line); | |
| 52 | + if LEN(line) > 0 then | |
| 53 | + INC(n); | |
| 54 | + name, value, found := Str.cut(line, "="); | |
| 55 | + if not found or not setting(Str.trim(name), Str.trim(value)) then | |
| 56 | + Fmt.print("line {}: {}\n", [Fmt.int(n), Fmt.str(line)]); | |
| 57 | + end; | |
| 58 | + end; | |
| 59 | + end; | |
| 60 | + Fmt.print("{}: {} by {}, at {}\n", [Fmt.str(title), Fmt.int(width), Fmt.int(height), | |
| 61 | + Fmt.real(scale, 1)]); | |
| 62 | + Out.int(Str.chars("naïve")); Out.str(" characters in naïve\n"); | |
| 63 | +end; | |
| 64 | + | |
| 65 | +end Settings. |
| @@ -208,21 +208,22 @@ | ||
| 208 | 208 | proc builtin(c: ^Command): BOOL; |
| 209 | 209 | var dir: []U8; n: I64; pid, _code: INT; ok: BOOL; _st: Sys.Status; |
| 210 | 210 | begin |
| 211 | - if c^.args[0] = "cd" then | |
| 211 | + match c^.args[0] of | |
| 212 | + | "cd": | |
| 212 | 213 | if c^.n > 1 then dir := c^.args[1]; else dir := Sys.env("HOME"); end; |
| 213 | 214 | status := 0; |
| 214 | 215 | if Sys.setWorkDir(dir) < 0 then say("cd: can't go to {}", [Fmt.str(dir)]); status := 1; end; |
| 215 | - elsif c^.args[0] = "wait" then | |
| 216 | + | "wait": | |
| 216 | 217 | repeat pid, _st, _code := Sys.wait(-1, true); until pid < 0; |
| 217 | 218 | status := 0; |
| 218 | - elsif c^.args[0] = "exit" then | |
| 219 | + | "exit": | |
| 219 | 220 | n := status; |
| 220 | 221 | if c^.n > 1 then |
| 221 | 222 | n, ok := Str.toInt(c^.args[1]); |
| 222 | 223 | if not ok then say("exit: {} isn't a number", [Fmt.str(c^.args[1])]); n := 2; end; |
| 223 | 224 | end; |
| 224 | 225 | Rt.exit(INT(n)); |
| 225 | - else | |
| 226 | + | else: | |
| 226 | 227 | return false; |
| 227 | 228 | end; |
| 228 | 229 | return true; |
| @@ -1,5 +1,6 @@ | ||
| 1 | -(* Generic modules: counts the words of a text in a Map from []U8 to | |
| 2 | - INT, hashed and compared by Str's procedures, keeps them in order of | |
| 1 | +(* Generic modules: counts the words of a text, taken apart by | |
| 2 | + Str.field, in a Map from []U8 to INT, hashed and compared by Str's | |
| 3 | + procedures, keeps them in order of | |
| 3 | 4 | first sight in a List([]U8), and prints them with Fmt. *) |
| 4 | 5 | module Words; |
| 5 | 6 | import Fmt, Str, Heap, Count := Map([]U8, INT, Str.hash, Str.eq), Names := List([]U8); |
| @@ -9,20 +10,17 @@ | ||
| 9 | 10 | proc main*; |
| 10 | 11 | var |
| 11 | 12 | counts: Count.Map; order: Names.List; n: ^Names.Node; |
| 12 | - i, start, k, total: INT; found, _ok: BOOL; w: []U8; | |
| 13 | + k, total: INT; found, _ok: BOOL; w, rest: []U8; | |
| 13 | 14 | begin |
| 14 | 15 | Count.init(ADR(counts), Heap.heap); |
| 15 | 16 | Names.init(ADR(order), Heap.heap); |
| 16 | - start := 0; | |
| 17 | - for i := 0 to LEN(Text) do | |
| 18 | - if (i = LEN(Text)) or (Text[i] = ' ') then | |
| 19 | - w := Text[start..i]; | |
| 20 | - INC(total); | |
| 21 | - k, found := Count.get(ADR(counts), w); | |
| 22 | - if not found then _ok := Names.push(ADR(order), w); end; | |
| 23 | - _ok := Count.put(ADR(counts), w, k + 1); | |
| 24 | - start := i + 1; | |
| 25 | - end; | |
| 17 | + w, rest := Str.field(Text); | |
| 18 | + while LEN(w) > 0 do | |
| 19 | + INC(total); | |
| 20 | + k, found := Count.get(ADR(counts), w); | |
| 21 | + if not found then _ok := Names.push(ADR(order), w); end; | |
| 22 | + _ok := Count.put(ADR(counts), w, k + 1); | |
| 23 | + w, rest := Str.field(rest); | |
| 26 | 24 | end; |
| 27 | 25 | Names.reverse(ADR(order)); |
| 28 | 26 | n := order.head; |
| @@ -57,7 +57,7 @@ | ||
| 57 | 57 | |
| 58 | 58 | TOKEN = re.compile(r""" |
| 59 | 59 | (?P<comment>\(\*.*?\*\)) | |
| 60 | - (?P<string>"(?:\\.|[^"\\\n])*"|'(?:\\.|[^'\\\n])*') | | |
| 60 | + (?P<string>"(?:\\.|[^"\\\n])*"|'(?:\\.|[^'\\\n])*'|`[^`]*`) | | |
| 61 | 61 | (?P<number>\b\d[0-9A-Fa-f]*(?:\.\d+)?(?:E[-+]?\d+)?H?\b) | |
| 62 | 62 | (?P<word>[A-Za-z_][A-Za-z_0-9]*) |
| 63 | 63 | """, re.X | re.S) |
| @@ -92,8 +92,9 @@ | ||
| 92 | 92 | blocks.append("\n".join(cur) + "\n") |
| 93 | 93 | cur = None |
| 94 | 94 | elif cur is not None: |
| 95 | - # roff writes a backslash as \e and a dot at a line's start as \&. | |
| 96 | - cur.append(line.replace("\\&", "").replace("\\e", "\\")) | |
| 95 | + # roff writes a backslash as \e, a backquote as \(ga and a dot | |
| 96 | + # at a line's start as \&. | |
| 97 | + cur.append(line.replace("\\&", "").replace("\\(ga", "`").replace("\\e", "\\")) | |
| 97 | 98 | return blocks |
| 98 | 99 | |
| 99 | 100 | def tutorial_module(name): |
| @@ -171,33 +171,38 @@ | ||
| 171 | 171 | |
| 172 | 172 | ## Numbers and text |
| 173 | 173 | |
| 174 | -`Str.toInt` reads a decimal number with an optional sign and nothing else, and says whether it was one. `Fmt` goes the other way. `Str.encode` and `Str.decode` move between characters and UTF-8, as the first tour showed. | |
| 174 | +`Str.toInt` reads a decimal number with an optional sign and nothing else, and says whether it was one; `Str.toHex` reads hexadecimal digits, and `Math.toReal` a real, as a constant is written but with `e` for `E` too, to the nearest `F64` exactly. `Str.fromInt` goes the other way into an array of 20 bytes, with no allocator, and `Fmt` does the rest. `Str.encode` and `Str.decode` move between characters and UTF-8, as the first tour showed. | |
| 175 | 175 | |
| 176 | 176 | ```raz Numbers run |
| 177 | 177 | module Numbers; |
| 178 | -import Out, Str; | |
| 178 | +import Out, Str, Math, Fmt; | |
| 179 | 179 | |
| 180 | 180 | (* the sum of the numbers in a comma separated list, or -1 if any isn't *) |
| 181 | 181 | proc total(s: []U8): I64; |
| 182 | -var start, i: INT; sum, n: I64; ok: BOOL; | |
| 182 | +var part: []U8; sum, n: I64; ok, more: BOOL; | |
| 183 | 183 | begin |
| 184 | - start := 0; | |
| 185 | - for i := 0 to LEN(s) do | |
| 186 | - if (i = LEN(s)) or (s[i] = ',') then | |
| 187 | - n, ok := Str.toInt(s[start..i]); | |
| 188 | - if not ok then return -1; end; | |
| 189 | - sum := sum + n; | |
| 190 | - start := i + 1; | |
| 191 | - end; | |
| 184 | + more := true; | |
| 185 | + while more do | |
| 186 | + part, s, more := Str.cut(s, ","); | |
| 187 | + n, ok := Str.toInt(part); | |
| 188 | + if not ok then return -1; end; | |
| 189 | + sum := sum + n; | |
| 192 | 190 | end; |
| 193 | 191 | return sum; |
| 194 | 192 | end; |
| 195 | 193 | |
| 196 | 194 | proc main*; |
| 195 | +var u: U64; x: F64; ok: BOOL; digits: [20]U8; k: INT; | |
| 197 | 196 | begin |
| 198 | 197 | Out.int(total("10,20,-5,+7")); Out.ln; |
| 199 | 198 | Out.int(total("10,x,20")); Out.ln; |
| 200 | 199 | Out.int(total("10,,20")); Out.ln; |
| 200 | + u, ok := Str.toHex("ff00"); | |
| 201 | + if ok then Out.hex(u, 1); Out.ln; end; | |
| 202 | + x, ok := Math.toReal("-1.5e3"); | |
| 203 | + if ok then Fmt.print("{}\n", [Fmt.real(x, 1)]); end; | |
| 204 | + digits, k := Str.fromInt(-9223372036854775807 - 1); | |
| 205 | + Out.str(digits[0..k]); Out.ln; | |
| 201 | 206 | end; |
| 202 | 207 | |
| 203 | 208 | end Numbers. |
| @@ -92,18 +92,15 @@ | ||
| 92 | 92 | |
| 93 | 93 | proc main*; |
| 94 | 94 | var |
| 95 | - m: Count.Map; entries: [32]Entry; i, start, k, n, found: INT; | |
| 96 | - w: []U8; key: []U8; v: INT; | |
| 95 | + m: Count.Map; entries: [32]Entry; i, k, n, found: INT; | |
| 96 | + w, rest: []U8; key: []U8; v: INT; | |
| 97 | 97 | begin |
| 98 | 98 | Count.init(ADR(m), Heap.heap); |
| 99 | - start := 0; | |
| 100 | - for i := 0 to LEN(Text) do | |
| 101 | - if (i = LEN(Text)) or (Text[i] = ' ') then | |
| 102 | - w := Text[start..i]; | |
| 103 | - k, _ := Count.get(ADR(m), w); | |
| 104 | - _ := Count.put(ADR(m), w, k + 1); | |
| 105 | - start := i + 1; | |
| 106 | - end; | |
| 99 | + w, rest := Str.field(Text); | |
| 100 | + while LEN(w) > 0 do | |
| 101 | + k, _ := Count.get(ADR(m), w); | |
| 102 | + _ := Count.put(ADR(m), w, k + 1); | |
| 103 | + w, rest := Str.field(rest); | |
| 107 | 104 | end; |
| 108 | 105 | n := 0; |
| 109 | 106 | found, key, v := Count.next(ADR(m), 0); |
| @@ -39,6 +39,7 @@ | ||
| 39 | 39 | <li>Integers of 8 to 64 bits, signed and unsigned, which wrap; <code>BOOL</code>, <code>F32</code>, <code>F64</code>. Nothing converts unless it is written.</li> |
| 40 | 40 | <li>Arrays, checked slices, pointers, records laid out as C lays them out, packed records, enumerations, sets, and variants with <code>match</code>.</li> |
| 41 | 41 | <li>Every index and slice is checked; a failure says where it was, with the values, and the calls under way.</li> |
| 42 | + <li>Strings of UTF-8 bytes, raw strings over lines that keep the code's indent, and <code>match</code> on a string.</li> | |
| 42 | 43 | <li>Every variable starts at zero. Behaviour is defined everywhere but through pointers.</li> |
| 43 | 44 | <li>No garbage collector: memory comes from an allocator the program chooses and passes.</li> |
| 44 | 45 | <li>Several results, <code>ERROR</code> values and <code>defer</code>, in place of exceptions.</li> |
| @@ -37,7 +37,7 @@ | ||
| 37 | 37 | |
| 38 | 38 | ## What `Out` can write |
| 39 | 39 | |
| 40 | -`Out` is the library's buffered output. It writes strings, single characters, numbers in decimal or hexadecimal, and a line feed. A string is written as it is, with `\n` for a line feed, `\t` for a tab, `\"` and `\\`. | |
| 40 | +`Out` is the library's buffered output. It writes strings, single characters, numbers in decimal or hexadecimal, and a line feed. A string is written as it is, with `\n` for a line feed, `\t` for a tab, `\"` and `\\`; or between backquotes, as a raw string with no escapes at all, which may run over lines (lesson 6). | |
| 41 | 41 | |
| 42 | 42 | ```raz Numbers run |
| 43 | 43 | module Numbers; |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | title: Arrays, slices and strings |
| 2 | 2 | part: Set |
| 3 | -summary: Five in a row, a window onto them, bytes in UTF-8, and checks on every index. | |
| 3 | +summary: Five in a row, a window onto them, bytes in UTF-8, raw strings over lines, and checks on every index. | |
| 4 | 4 | --- |
| 5 | 5 | ```raz Slices run |
| 6 | 6 | ``` |
| @@ -90,7 +90,7 @@ | ||
| 90 | 90 | |
| 91 | 91 | ## Strings |
| 92 | 92 | |
| 93 | -A string is a slice of bytes, `[]U8`, in UTF-8, and a string constant's bytes are constant. A `[8]U8` made from one is a copy you can change. Arrays and slices of integers compare with `=` and `#`, so strings do too. `Str` has the rest: ordering, finding, hashing, prefixes, UTF-8 and reading a number. | |
| 93 | +A string is a slice of bytes, `[]U8`, in UTF-8, and a string constant's bytes are constant. A `[8]U8` made from one is a copy you can change. Arrays and slices of integers compare with `=` and `#`, so strings do too. `Str` has the rest: ordering, finding, hashing, prefixes and suffixes, trimming, taking apart, ASCII case, UTF-8, and numbers to and from text. | |
| 94 | 94 | |
| 95 | 95 | ```raz Words run |
| 96 | 96 | module Words; |
| @@ -104,8 +104,12 @@ | ||
| 104 | 104 | if s[0..5] = "hello" then Out.str("it says hello\n"); end; |
| 105 | 105 | Out.int(Str.find(s, "world")); Out.ln; (* 7 *) |
| 106 | 106 | Out.int(Str.find(s, "xyz")); Out.ln; (* -1 *) |
| 107 | + Out.int(Str.findLast("abcabc", "bc")); Out.ln; (* 4 *) | |
| 107 | 108 | if Str.hasPrefix(s, "hell") then Out.str("prefix\n"); end; |
| 109 | + if Str.hasSuffix(s, "world") then Out.str("suffix\n"); end; | |
| 108 | 110 | Out.int(Str.cmp("apple", "banana")); Out.ln; (* -1 *) |
| 111 | + if Str.eqFold("RAZ", "raz") then Out.str("the same, but for case\n"); end; | |
| 112 | + Out.str("["); Out.str(Str.trim(" padded \t\n")); Out.str("]\n"); | |
| 109 | 113 | |
| 110 | 114 | n, ok := Str.toInt("-1234"); |
| 111 | 115 | if ok then Out.int(n + 1); Out.ln; end; |
| @@ -115,47 +119,115 @@ | ||
| 115 | 119 | name := "raz"; (* a copy, the program's own *) |
| 116 | 120 | name[0] := 'R'; |
| 117 | 121 | Out.str(name[0..3]); Out.ln; |
| 122 | + Str.upper(name[0..3]); (* in place: name is a variable *) | |
| 123 | + Out.str(name[0..3]); Out.ln; | |
| 118 | 124 | end; |
| 119 | 125 | |
| 120 | 126 | end Words. |
| 121 | 127 | ``` |
| 122 | 128 | |
| 123 | -## UTF-8 | |
| 129 | +## Taking a string apart | |
| 124 | 130 | |
| 125 | -A character is an `I32`, its code point. `Str.decode` reads the first character of a string and says how many bytes it took; `Str.encode` goes the other way. Byte length and character count are different things, and the program is the one that says which it wants. | |
| 131 | +`Str.cut` splits a string at the first place a separator is: what comes before it, what comes after, and whether it was there at all. `Str.field` gives the first word, past any spaces, and the rest. Neither copies anything: every part is a slice of the string it came from, so taking a line or a file apart costs no memory. | |
| 126 | 132 | |
| 127 | -```raz Runes run | |
| 128 | -module Runes; | |
| 133 | +```raz Parts run | |
| 134 | +module Parts; | |
| 129 | 135 | import Out, Str; |
| 130 | 136 | |
| 131 | -(* the characters of s, not its bytes *) | |
| 132 | -proc chars(s: []U8): INT; | |
| 133 | -var i, n, size: INT; | |
| 137 | +const Text = "name = raz\nkind = language\nyear = 2026\n"; | |
| 138 | + | |
| 139 | +proc main*; | |
| 140 | +var line, rest, key, val, w: []U8; more, ok: BOOL; | |
| 134 | 141 | begin |
| 135 | - i := 0; | |
| 136 | - n := 0; | |
| 137 | - while i < LEN(s) do | |
| 138 | - _, size := Str.decode(s[i..LEN(s)]); | |
| 139 | - INC(i, size); | |
| 140 | - INC(n); | |
| 142 | + (* line by line, then each line at its = *) | |
| 143 | + line, rest, more := Str.cut(Text, "\n"); | |
| 144 | + while more do | |
| 145 | + key, val, ok := Str.cut(line, "="); | |
| 146 | + if ok then | |
| 147 | + Out.str(Str.trim(key)); Out.str(" is "); Out.str(Str.trim(val)); Out.ln; | |
| 148 | + end; | |
| 149 | + line, rest, more := Str.cut(rest, "\n"); | |
| 141 | 150 | end; |
| 142 | - return n; | |
| 151 | + | |
| 152 | + (* word by word *) | |
| 153 | + w, rest := Str.field(" the quick brown fox "); | |
| 154 | + while LEN(w) > 0 do | |
| 155 | + Out.str("<"); Out.str(w); Out.str(">"); | |
| 156 | + w, rest := Str.field(rest); | |
| 157 | + end; | |
| 158 | + Out.ln; | |
| 143 | 159 | end; |
| 144 | 160 | |
| 161 | +end Parts. | |
| 162 | +``` | |
| 163 | + | |
| 164 | +When `Str.cut` finds no separator it gives the whole string, an empty one and `false`, so the loop above ends after the last line that has a line feed. Building a string goes the other way, in a `Buf`: `Str.join` adds parts with a separator between them, and `Str.replace` adds a string with every match of one text replaced by another (lesson 9 shows `Buf`). | |
| 165 | + | |
| 166 | +## Raw strings | |
| 167 | + | |
| 168 | +A string between backquotes is raw: its bytes are the text as it stands, with no escapes, so a backslash or a double quote is itself, and it may run over lines. When the opening backquote ends its line and the closing one has only spaces and tabs before it, those blanks come off the start of every line between, and the text keeps the indent of the code it sits in. Each line ends in a line feed, the last one too. | |
| 169 | + | |
| 170 | +```raz Raw run | |
| 171 | +module Raw; | |
| 172 | +import Out; | |
| 173 | + | |
| 174 | +const Path = `C:\Users\raz\"notes".txt`; | |
| 175 | + | |
| 176 | +proc usage; | |
| 177 | +begin | |
| 178 | + Out.str(` | |
| 179 | + usage: tool [-v] file ... | |
| 180 | + -v say what is done | |
| 181 | + file read this, not standard input | |
| 182 | + `); | |
| 183 | +end; | |
| 184 | + | |
| 145 | 185 | proc main*; |
| 146 | -var s: []U8; c: I32; size: INT; bytes: [4]U8; k: INT; | |
| 147 | 186 | begin |
| 187 | + Out.str(Path); Out.ln; | |
| 188 | + usage; | |
| 189 | + Out.str(`a raw string on one line, \n and all`); Out.ln; | |
| 190 | +end; | |
| 191 | + | |
| 192 | +end Raw. | |
| 193 | +``` | |
| 194 | + | |
| 195 | +A line of an indented raw string that doesn't start with the closing line's blanks is an error, so a slip in the indent is caught where it is made, not printed. A raw string can't hold a backquote; join one in with `+` and an ordinary string that holds it. | |
| 196 | + | |
| 197 | +## UTF-8 | |
| 198 | + | |
| 199 | +A character is an `I32`, its code point. A string stays bytes; UTF-8 is how `Str` reads them. `Str.next` walks a string a character at a time, giving each one and where the next starts, -1 at the end. `Str.decode` reads the first character of a string and says how many bytes it took, `Str.decodeLast` the last, and `Str.encode` goes the other way. `Str.chars` counts characters and `Str.valid` says whether a string is UTF-8 at all; a byte that doesn't start a character rightly reads as FFFDH, the character that stands for a wrong one. Byte length and character count are different things, and the program is the one that says which it wants. | |
| 200 | + | |
| 201 | +```raz Runes run | |
| 202 | +module Runes; | |
| 203 | +import Out, Str; | |
| 204 | + | |
| 205 | +proc main*; | |
| 206 | +var s: []U8; c: I32; i, size: INT; bytes: [4]U8; k: INT; | |
| 207 | +begin | |
| 148 | 208 | s := "caf\u{E9} \u{3BB}"; |
| 149 | 209 | Out.int(LEN(s)); Out.str(" bytes, "); |
| 150 | - Out.int(chars(s)); Out.str(" characters\n"); | |
| 151 | - c, size := Str.decode(s[3..LEN(s)]); | |
| 152 | - Out.int(I64(c)); Out.str(" took "); Out.int(size); Out.str(" bytes\n"); | |
| 210 | + Out.int(Str.chars(s)); Out.str(" characters\n"); | |
| 211 | + | |
| 212 | + (* each character, as its code point *) | |
| 213 | + c, i := Str.next(s, 0); | |
| 214 | + while i >= 0 do | |
| 215 | + Out.int(I64(c)); Out.str(" "); | |
| 216 | + c, i := Str.next(s, i); | |
| 217 | + end; | |
| 218 | + Out.ln; | |
| 219 | + | |
| 220 | + c, size := Str.decodeLast(s); | |
| 221 | + Out.int(I64(c)); Out.str(" is last, in "); Out.int(size); Out.str(" bytes\n"); | |
| 153 | 222 | bytes, k := Str.encode(955); |
| 154 | 223 | Out.str(bytes[0..k]); Out.ln; |
| 224 | + if not Str.valid(s[0..4]) then Out.str("cut in the middle of a character\n"); end; | |
| 155 | 225 | end; |
| 156 | 226 | |
| 157 | 227 | end Runes. |
| 158 | 228 | ``` |
| 229 | + | |
| 230 | +`Str` knows UTF-8 and no more of Unicode: its case is ASCII's, and it doesn't say how wide a character is on a terminal or which spellings of a text are the same. Those need tables bigger than the rest of the library. | |
| 159 | 231 | |
| 160 | 232 | ## Loops over lengths |
| 161 | 233 |
| @@ -163,3 +163,43 @@ | ||
| 163 | 163 | |
| 164 | 164 | end Classify. |
| 165 | 165 | ``` |
| 166 | + | |
| 167 | +## Matching strings | |
| 168 | + | |
| 169 | +On a string, `match` takes string constants, several to an arm if need be, and compares them as `=` does. It too must have an `| else:`. It is how a program reads a command, an option or a keyword without a chain of `elsif`s. | |
| 170 | + | |
| 171 | +```raz Commands run | |
| 172 | +module Commands; | |
| 173 | +import Out; | |
| 174 | + | |
| 175 | +type Op = (none, add, sub, show); | |
| 176 | + | |
| 177 | +proc op(word: []U8): Op; | |
| 178 | +begin | |
| 179 | + match word of | |
| 180 | + | "add", "plus", "+": return add; | |
| 181 | + | "sub", "minus", "-": return sub; | |
| 182 | + | "show": return show; | |
| 183 | + | else: return none; | |
| 184 | + end; | |
| 185 | +end; | |
| 186 | + | |
| 187 | +proc main*; | |
| 188 | +var total: INT; words: [6][]U8; i: INT; | |
| 189 | +begin | |
| 190 | + words := ["add", "plus", "sub", "show", "times", "-"]; | |
| 191 | + for i := 0 to 5 do | |
| 192 | + match op(words[i]) of | |
| 193 | + | add: INC(total); | |
| 194 | + | sub: DEC(total); | |
| 195 | + | show: Out.int(total); Out.ln; | |
| 196 | + | none: Out.str(words[i]); Out.str("?\n"); | |
| 197 | + end; | |
| 198 | + end; | |
| 199 | + Out.int(total); Out.ln; | |
| 200 | +end; | |
| 201 | + | |
| 202 | +end Commands. | |
| 203 | +``` | |
| 204 | + | |
| 205 | +Two labels that are the same string are an error, as two arms for one case are. |
| @@ -95,7 +95,7 @@ | ||
| 95 | 95 | |
| 96 | 96 | ```raz Build run |
| 97 | 97 | module Build; |
| 98 | -import Out, Buf, Heap; | |
| 98 | +import Out, Buf, Heap, Str; | |
| 99 | 99 | |
| 100 | 100 | proc main*; |
| 101 | 101 | var b: Buf.Buf; i: INT; |
| @@ -109,10 +109,18 @@ | ||
| 109 | 109 | end; |
| 110 | 110 | Out.str(Buf.bytes(ADR(b))); Out.ln; |
| 111 | 111 | Out.int(b.len); Out.str(" bytes\n"); |
| 112 | + Buf.reset(ADR(b)); | |
| 113 | + Str.join(ADR(b), ["red", "green", "blue"], ", "); | |
| 114 | + Out.str(Buf.bytes(ADR(b))); Out.ln; | |
| 115 | + Buf.reset(ADR(b)); | |
| 116 | + Str.replace(ADR(b), "a-b-c", "-", " and "); | |
| 117 | + Out.str(Buf.bytes(ADR(b))); Out.ln; | |
| 112 | 118 | end; |
| 113 | 119 | |
| 114 | 120 | end Build. |
| 115 | 121 | ``` |
| 122 | + | |
| 123 | +`Str.join` and `Str.replace` build into a `Buf` too: the parts with a separator between them, or a string with one text replaced by another wherever it is. | |
| 116 | 124 | |
| 117 | 125 | ## Pages |
| 118 | 126 |
| @@ -29,7 +29,7 @@ | ||
| 29 | 29 | import Sys, Out, Version, Files, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; |
| 30 | 30 | |
| 31 | 31 | const |
| 32 | - Report = "2026-10.24"; (* the version of doc/raz.md it follows *) | |
| 32 | + Report = "2026-10.25"; (* the version of doc/raz.md it follows *) | |
| 33 | 33 | MemfdCreate = 319; (* the system call *) |
| 34 | 34 | MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) |
| 35 | 35 | Execve = 59; |
| @@ -10,7 +10,8 @@ | ||
| 10 | 10 | # so are those whose message says what match takes. |
| 11 | 11 | newer="conform/run/RawStrings.raz conform/err/ScanRawOpen.raz conform/err/ScanRawIndent.raz |
| 12 | 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" | |
| 13 | + conform/err/MatchStrVar.raz conform/err/MatchReal.raz conform/err/AtMatch.raz | |
| 14 | + examples/Sh.raz examples/Settings.raz" | |
| 14 | 15 | newer() { |
| 15 | 16 | for g in $newer; do [ $g = $1 ] && return 0; done |
| 16 | 17 | return 1 |
| @@ -5,8 +5,14 @@ | ||
| 5 | 5 | 3 39 string |
| 6 | 6 | 3 49 string |
| 7 | 7 | 3 53 string |
| 8 | -4 8 type | |
| 9 | -4 17 type | |
| 10 | -6 5 builtin | |
| 11 | -6 19 constant | |
| 12 | -5 1 keyword | |
| 8 | +4 1 keyword | |
| 9 | +4 16 string | |
| 10 | +4 17 string | |
| 11 | +5 14 string | |
| 12 | +6 1 string | |
| 13 | +6 5 string | |
| 14 | +7 8 type | |
| 15 | +7 17 type | |
| 16 | +9 5 builtin | |
| 17 | +9 19 constant | |
| 18 | +8 1 keyword |
| @@ -1,6 +1,9 @@ | ||
| 1 | 1 | (* outer (* nested *) still comment *) |
| 2 | 2 | module Colours; |
| 3 | 3 | const big = 0FFH; small = 2.5E-3; q = '\''; s = "a\"b"; |
| 4 | +const raw = `C:\`; | |
| 5 | + lines = `begin | |
| 6 | +end (* x *)`; | |
| 4 | 7 | var x: I64; ok: BOOL; |
| 5 | 8 | begin |
| 6 | 9 | INC(x); ok := true; |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | " Checks each token of Colours.expect has the colour group it should. |
| 2 | 2 | let s:kinds = {'razComment': 'comment', 'razKeyword': 'keyword', 'razNumber': 'number', |
| 3 | - \ 'razString': 'string', 'razChar': 'string', 'razType': 'type', | |
| 3 | + \ 'razString': 'string', 'razRawString': 'string', 'razChar': 'string', 'razType': 'type', | |
| 4 | 4 | \ 'razBuiltin': 'builtin', 'razConstant': 'constant'} |
| 5 | 5 | for s:want in readfile('test/editors/Colours.expect') |
| 6 | 6 | let [s:l, s:c, s:k] = split(s:want) |
| @@ -306,20 +306,9 @@ | ||
| 306 | 306 | end; |
| 307 | 307 | end; |
| 308 | 308 | |
| 309 | -(* line, without the spaces at its ends *) | |
| 310 | -proc trim(line: []U8): []U8; | |
| 311 | -var i, j: INT; | |
| 312 | -begin | |
| 313 | - i := 0; | |
| 314 | - j := LEN(line); | |
| 315 | - while (i < j) and Scan.isSpace(line[i]) do INC(i); end; | |
| 316 | - while (j > i) and Scan.isSpace(line[j - 1]) do DEC(j); end; | |
| 317 | - return line[i..j]; | |
| 318 | -end; | |
| 319 | - | |
| 320 | 309 | (* line, from the report or a manual page's literal block, added to b |
| 321 | - as the program has it: in a page, \e is a backslash and \& is | |
| 322 | - nothing. *) | |
| 310 | + as the program has it: in a page, \e is a backslash, \(ga a | |
| 311 | + backquote and \& nothing. *) | |
| 323 | 312 | proc addLine(b: ^Buf.Buf; line: []U8; page: BOOL); |
| 324 | 313 | var i: INT; |
| 325 | 314 | begin |
| @@ -328,6 +317,9 @@ | ||
| 328 | 317 | if page and (line[i] = '\\') and (i + 1 < LEN(line)) and (line[i + 1] = 'e') then |
| 329 | 318 | Buf.add(b, '\\'); |
| 330 | 319 | INC(i); |
| 320 | + elsif page and Str.hasPrefix(line[i..LEN(line)], "\\(ga") then | |
| 321 | + Buf.add(b, '`'); | |
| 322 | + INC(i, 3); | |
| 331 | 323 | elsif page and (line[i] = '\\') and (i + 1 < LEN(line)) and (line[i + 1] = '&') then |
| 332 | 324 | INC(i); |
| 333 | 325 | else Buf.add(b, line[i]); |
| @@ -368,7 +360,7 @@ | ||
| 368 | 360 | k := i; |
| 369 | 361 | while (k < LEN(s)) and (s[k] # 10) do INC(k); end; |
| 370 | 362 | line := s[i..k]; |
| 371 | - fence := not page and (trim(line) = "```"); | |
| 363 | + fence := not page and (Str.trim(line) = "```"); | |
| 372 | 364 | if inBlock and (fence or page and (line = ".Ed")) then |
| 373 | 365 | if whole and Str.hasPrefix(first, "module ") and Str.hasPrefix(last, "end ") then |
| 374 | 366 | name := first[7..LEN(first)]; |
| @@ -391,10 +383,10 @@ | ||
| 391 | 383 | whole := true; |
| 392 | 384 | elsif inBlock then |
| 393 | 385 | addLine(ADR(prog), line, page); |
| 394 | - if trim(line) = "..." then whole := false; end; | |
| 395 | - if LEN(trim(line)) > 0 then | |
| 396 | - if LEN(first) = 0 then first := trim(line); end; | |
| 397 | - last := trim(line); | |
| 386 | + if Str.trim(line) = "..." then whole := false; end; | |
| 387 | + if LEN(Str.trim(line)) > 0 then | |
| 388 | + if LEN(first) = 0 then first := Str.trim(line); end; | |
| 389 | + last := Str.trim(line); | |
| 398 | 390 | end; |
| 399 | 391 | end; |
| 400 | 392 | i := k + 1; |