site: v0.15.0 The tarball and its SHA-256; the report's PDFs. Collections gains Vec, and Freq keeps its entries in one; Files gains modes, links and a lock (openMode, linkInfo, Info.ino, lock, syncAll); the slices tour says a slice of a local can't be returned.
| @@ -1,8 +1,8 @@ | ||
| 1 | -title: Lists, maps and sorting | |
| 1 | +title: Lists, arrays, maps and sorting | |
| 2 | 2 | part: The library |
| 3 | -summary: List(T), Map(K, V) and Sort(T), put together in a word-frequency program. | |
| 3 | +summary: List(T), Vec(T), Map(K, V) and Sort(T), put together in a word-frequency program. | |
| 4 | 4 | --- |
| 5 | -The three generic modules are instantiated like any other generic module, with the types and procedures they need. All three take their memory from an allocator you give them; none of them allocates anything you didn't ask for. | |
| 5 | +The four generic modules are instantiated like any other generic module, with the types and procedures they need. All four take their memory from an allocator you give them; none of them allocates anything you didn't ask for. | |
| 6 | 6 | |
| 7 | 7 | ## `List(T)` |
| 8 | 8 | |
| @@ -29,6 +29,30 @@ | ||
| 29 | 29 | end Pile. |
| 30 | 30 | ``` |
| 31 | 31 | |
| 32 | +## `Vec(T)` | |
| 33 | + | |
| 34 | +A growable array: one block of elements that doubles when it is full, for what a program learns the length of only as it reads. `push` adds at the end, `add` adds a zero element and gives a pointer to fill it in place, `resize` cuts it short or grows it with zeros. `all` gives the elements as a slice, indexed and checked as any slice is; the slice is good until the array next grows. Running out of memory stops the program, as with `Buf`: an element dropped silently would be wrong data. `room` asks first, for a program that can do better. | |
| 35 | + | |
| 36 | +```raz Squares run | |
| 37 | +module Squares; | |
| 38 | +import Out, Heap, Ints := Vec(INT); | |
| 39 | + | |
| 40 | +proc main*; | |
| 41 | +var v: Ints.Vec; i: INT; s: []INT; | |
| 42 | +begin | |
| 43 | + Ints.init(ADR(v), Heap.heap); | |
| 44 | + defer Ints.done(ADR(v)); | |
| 45 | + for i := 1 to 1000 do Ints.push(ADR(v), i * i); end; | |
| 46 | + s := Ints.all(ADR(v)); | |
| 47 | + Out.int(v.len); Out.str(" squares, the last "); Out.int(s[LEN(s) - 1]); Out.ln; | |
| 48 | + Ints.resize(ADR(v), 3); | |
| 49 | + for i := 0 to v.len - 1 do Out.int(Ints.all(ADR(v))[i]); Out.char(' '); end; | |
| 50 | + Out.ln; | |
| 51 | +end; | |
| 52 | + | |
| 53 | +end Squares. | |
| 54 | +``` | |
| 55 | + | |
| 32 | 56 | ## `Map(K, V; hash, eq)` |
| 33 | 57 | |
| 34 | 58 | A hash map. Its key's hash and equality are procedure arguments, so any type can be a key. `put` replaces what a key maps to; `get` says what it maps to and whether it maps to anything; `remove` says whether the key was there. `next` walks the map: from 0, it gives the next entry and the number to pass for the one after, and -1 when there are no more. |
| @@ -71,12 +95,13 @@ | ||
| 71 | 95 | |
| 72 | 96 | ## Putting them together |
| 73 | 97 | |
| 74 | -Counting words, then listing them by count and then alphabetically: a `Map` for the counts, an array of records to sort, and `Sort` with a two-level `less`. | |
| 98 | +Counting words, then listing them by count and then alphabetically: a `Map` for the counts, a `Vec` of records to sort, however many words there are, and `Sort` with a two-level `less`. | |
| 75 | 99 | |
| 76 | 100 | ```raz Freq run |
| 77 | 101 | module Freq; |
| 78 | 102 | import Fmt, Str, Heap, |
| 79 | 103 | Count := Map([]U8, INT, Str.hash, Str.eq), |
| 104 | + Entries := Vec(Entry), | |
| 80 | 105 | Rank := Sort(Entry, before); |
| 81 | 106 | |
| 82 | 107 | type Entry = record word: []U8; n: INT; end; |
| @@ -92,8 +117,8 @@ | ||
| 92 | 117 | |
| 93 | 118 | proc main*; |
| 94 | 119 | var |
| 95 | - m: Count.Map; entries: [32]Entry; i, k, n, found: INT; | |
| 96 | - w, rest: []U8; key: []U8; v: INT; | |
| 120 | + m: Count.Map; entries: Entries.Vec; i, k, found: INT; | |
| 121 | + w, rest: []U8; key: []U8; v: INT; all: []Entry; | |
| 97 | 122 | begin |
| 98 | 123 | Count.init(ADR(m), Heap.heap); |
| 99 | 124 | w, rest := Str.field(Text); |
| @@ -102,17 +127,16 @@ | ||
| 102 | 127 | _ := Count.put(ADR(m), w, k + 1); |
| 103 | 128 | w, rest := Str.field(rest); |
| 104 | 129 | end; |
| 105 | - n := 0; | |
| 130 | + Entries.init(ADR(entries), Heap.heap); | |
| 106 | 131 | found, key, v := Count.next(ADR(m), 0); |
| 107 | 132 | while found >= 0 do |
| 108 | - entries[n].word := key; | |
| 109 | - entries[n].n := v; | |
| 110 | - INC(n); | |
| 133 | + Entries.push(ADR(entries), {word: key, n: v}); | |
| 111 | 134 | found, key, v := Count.next(ADR(m), found); |
| 112 | 135 | end; |
| 113 | - Rank.sort(entries[0..n]); | |
| 114 | - for i := 0 to n - 1 do | |
| 115 | - Fmt.print("{} {}\n", [Fmt.int(entries[i].n), Fmt.str(entries[i].word)]); | |
| 136 | + all := Entries.all(ADR(entries)); | |
| 137 | + Rank.sort(all); | |
| 138 | + for i := 0 to LEN(all) - 1 do | |
| 139 | + Fmt.print("{} {}\n", [Fmt.int(all[i].n), Fmt.str(all[i].word)]); | |
| 116 | 140 | end; |
| 117 | 141 | end; |
| 118 | 142 | |
| @@ -123,4 +147,4 @@ | ||
| 123 | 147 | |
| 124 | 148 | ## Choosing |
| 125 | 149 | |
| 126 | -Use a `List` when you push and pop at one end and walk it in order. Use a `Map` when you look things up by key. Use an array and `Sort` when you know the size and want an order. When none of them fits, they are each under a hundred lines of raz in `lib/`, and the advanced tour's allocator lesson shows what is underneath. | |
| 150 | +Use a `List` when you push and pop at one end and walk it in order. Use a `Vec` when you add at the end and index, and don't know how many will come; an array when you do. Use a `Map` when you look things up by key. Use `Sort` on either's slice when you want an order. When none of them fits, they are each under a hundred lines of raz in `lib/`, and the advanced tour's allocator lesson shows what is underneath. |
| @@ -136,6 +136,44 @@ | ||
| 136 | 136 | end Tree. |
| 137 | 137 | ``` |
| 138 | 138 | |
| 139 | +## Modes, links, and a lock | |
| 140 | + | |
| 141 | +`Sys.openMode` is `open` with the permissions a file it makes should have, less those the umask takes away: 1EDH (755) for a script, 124H (444) for a file never to be written. `Sys.linkInfo` looks at a symbolic link itself, where `info` follows it, so a link whose target is gone is still seen. `Info.ino` is the file's inode: a file renamed over another has a new one, though its size and time may match, which is how a program knows a file it cached is still the same. | |
| 142 | + | |
| 143 | +`Sys.lock` takes the lock on an open file, which one open file holds at a time; another that asks for it waits, or with `wait` false gets -11 at once. The lock goes when the file is closed or the program ends, however it ends, so it can't go stale as a lock file can. `Sys.syncAll` puts everything written to a file system on the disk at once, before the one file that names it all is written: a database's or a version control system's commit. | |
| 144 | + | |
| 145 | +```raz Store run | |
| 146 | +module Store; | |
| 147 | +import Fmt, Sys; | |
| 148 | + | |
| 149 | +proc main*; | |
| 150 | +var fd, other: INT; named, target: Sys.Info; | |
| 151 | +begin | |
| 152 | + fd := Sys.openMode("run.sh", {Sys.writing, Sys.creating, Sys.truncating}, 1EDH); | |
| 153 | + _ := Sys.write(fd, "echo hi\n"); | |
| 154 | + _ := Sys.syncAll(fd); | |
| 155 | + Sys.close(fd); | |
| 156 | + target, _ := Sys.info("run.sh"); | |
| 157 | + Fmt.print("its owner may run it: {}\n", [Fmt.bool(target.mode & 40H # 0)]); | |
| 158 | + | |
| 159 | + _ := Sys.remove("current"); | |
| 160 | + _ := Sys.symlink("run.sh", "current"); | |
| 161 | + named, _ := Sys.linkInfo("current"); | |
| 162 | + Fmt.print("a link: {}, of {} bytes\n", [Fmt.bool(named.symlink), Fmt.int(named.size)]); | |
| 163 | + named, _ := Sys.info("current"); | |
| 164 | + Fmt.print("the same file: {}\n", [Fmt.bool(named.ino = target.ino)]); | |
| 165 | + | |
| 166 | + fd := Sys.openRead("run.sh"); | |
| 167 | + other := Sys.openRead("run.sh"); | |
| 168 | + Fmt.print("{} {}\n", [Fmt.int(Sys.lock(fd, false)), Fmt.int(Sys.lock(other, false))]); (* -11: held *) | |
| 169 | + Sys.close(fd); | |
| 170 | + Fmt.print("{}\n", [Fmt.int(Sys.lock(other, false))]); (* free again *) | |
| 171 | + Sys.close(other); | |
| 172 | +end; | |
| 173 | + | |
| 174 | +end Store. | |
| 175 | +``` | |
| 176 | + | |
| 139 | 177 | ## A command line, from a table |
| 140 | 178 | |
| 141 | 179 | A program describes its options once, in a table of `Opt.Row`s: its synopsis lines, its subcommands and its options. The help and the error messages are made from the table. The parser reads by the rules of the C library's `getopt` with GNU's long options, so `-vn3`, `-n 3`, `--count=3` and `--count 3` all work, and `--` ends the options. |
| @@ -68,6 +68,8 @@ | ||
| 68 | 68 | |
| 69 | 69 | Every index and every slice bound is checked, so a slice can never reach past what it was made from; the lesson on failures shows what that looks like. |
| 70 | 70 | |
| 71 | +A slice of a local array is good only while its procedure runs: the array is gone once it returns. So returning one is an error, `return a[0..n]` or `return a` with `a` a local, a value parameter or an array literal. Take the array as a `var` parameter instead, and the slice is of the caller's array; or fill a buffer the caller gives. | |
| 72 | + | |
| 71 | 73 | ## `COPY` |
| 72 | 74 | |
| 73 | 75 | `COPY(d, s)` copies the elements of `s` to the start of `d`, as one block, and is right when they overlap. `LEN(s)` must be at most `LEN(d)`. |
| @@ -1 +1 @@ | ||
| 1 | -/download/latest /download/raz-0.14.0-linux-x86_64.tar.gz 302 | |
| 1 | +/download/latest /download/raz-0.15.0-linux-x86_64.tar.gz 302 |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html" aria-current="page">Writing an allocator</a></li> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html" aria-current="page">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -240,7 +240,7 @@ | ||
| 240 | 240 | FF00 |
| 241 | 241 | -1500.0 |
| 242 | 242 | -9223372036854775808 |
| 243 | -</code></pre><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="fmt.html">← Output and formatting</a><a href="index.html">Contents</a><a class="next" href="collections.html">Lists, maps and sorting →</a></div></article></div> | |
| 243 | +</code></pre><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="fmt.html">← Output and formatting</a><a href="index.html">Contents</a><a class="next" href="collections.html">Lists, arrays, maps and sorting →</a></div></article></div> | |
| 244 | 244 | </main> |
| 245 | 245 | <footer>raz is under the ISC license.</footer> |
| 246 | 246 | </body> |
| @@ -3,10 +3,10 @@ | ||
| 3 | 3 | <head> |
| 4 | 4 | <meta charset="utf-8"> |
| 5 | 5 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 6 | -<title>Lists, maps and sorting - More Raz</title> | |
| 6 | +<title>Lists, arrays, maps and sorting - More Raz</title> | |
| 7 | 7 | <meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux."> |
| 8 | 8 | <meta name="theme-color" content="#E07A5F"> |
| 9 | -<meta property="og:title" content="Lists, maps and sorting - More Raz"> | |
| 9 | +<meta property="og:title" content="Lists, arrays, maps and sorting - More Raz"> | |
| 10 | 10 | <meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report."> |
| 11 | 11 | <meta property="og:image" content="https://raz.lucelad.workers.dev/social.png"> |
| 12 | 12 | <meta name="twitter:card" content="summary_large_image"> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html" aria-current="page">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html" aria-current="page">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -43,8 +43,8 @@ | ||
| 43 | 43 | <li><a href="ordering.html">Ordering and publication</a></li> |
| 44 | 44 | <li><a href="lockfree.html">A lock-free stack</a></li> |
| 45 | 45 | <li><a href="ring.html">A ring buffer between threads</a></li> |
| 46 | -</ol></aside><article><p class="kicker">The library · lesson 3 of 18</p><h1>Lists, maps and sorting</h1> | |
| 47 | -<p>The three generic modules are instantiated like any other generic module, with the types and procedures they need. All three take their memory from an allocator you give them; none of them allocates anything you didn't ask for.</p> | |
| 46 | +</ol></aside><article><p class="kicker">The library · lesson 3 of 18</p><h1>Lists, arrays, maps and sorting</h1> | |
| 47 | +<p>The four generic modules are instantiated like any other generic module, with the types and procedures they need. All four take their memory from an allocator you give them; none of them allocates anything you didn't ask for.</p> | |
| 48 | 48 | <h2><code>List(T)</code></h2> |
| 49 | 49 | <p>A singly linked list with <code>push</code> at the front, <code>pop</code> from the front, and the nodes exported, so a program walks it itself. Both can fail if the allocator is out of room, and say so.</p> |
| 50 | 50 | <div class="file"><span>Pile.raz</span></div><pre class="code"><code><span class="kw">module</span> Pile; |
| @@ -68,6 +68,28 @@ | ||
| 68 | 68 | <pre class="output"><code>1 4 9 16 25 |
| 69 | 69 | 1 popped, 4 left |
| 70 | 70 | </code></pre> |
| 71 | +<h2><code>Vec(T)</code></h2> | |
| 72 | +<p>A growable array: one block of elements that doubles when it is full, for what a program learns the length of only as it reads. <code>push</code> adds at the end, <code>add</code> adds a zero element and gives a pointer to fill it in place, <code>resize</code> cuts it short or grows it with zeros. <code>all</code> gives the elements as a slice, indexed and checked as any slice is; the slice is good until the array next grows. Running out of memory stops the program, as with <code>Buf</code>: an element dropped silently would be wrong data. <code>room</code> asks first, for a program that can do better.</p> | |
| 73 | +<div class="file"><span>Squares.raz</span></div><pre class="code"><code><span class="kw">module</span> Squares; | |
| 74 | +<span class="kw">import</span> Out, Heap, Ints := Vec(<span class="ty">INT</span>); | |
| 75 | + | |
| 76 | +<span class="kw">proc</span> main*; | |
| 77 | +<span class="kw">var</span> v: Ints.Vec; i: <span class="ty">INT</span>; s: []<span class="ty">INT</span>; | |
| 78 | +<span class="kw">begin</span> | |
| 79 | + Ints.init(<span class="ty">ADR</span>(v), Heap.heap); | |
| 80 | + <span class="kw">defer</span> Ints.done(<span class="ty">ADR</span>(v)); | |
| 81 | + <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">1000</span> <span class="kw">do</span> Ints.push(<span class="ty">ADR</span>(v), i * i); <span class="kw">end</span>; | |
| 82 | + s := Ints.all(<span class="ty">ADR</span>(v)); | |
| 83 | + Out.int(v.len); Out.str(<span class="string">" squares, the last "</span>); Out.int(s[<span class="ty">LEN</span>(s) - <span class="number">1</span>]); Out.ln; | |
| 84 | + Ints.resize(<span class="ty">ADR</span>(v), <span class="number">3</span>); | |
| 85 | + <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> v.len - <span class="number">1</span> <span class="kw">do</span> Out.int(Ints.all(<span class="ty">ADR</span>(v))[i]); Out.char(<span class="string">' '</span>); <span class="kw">end</span>; | |
| 86 | + Out.ln; | |
| 87 | +<span class="kw">end</span>; | |
| 88 | + | |
| 89 | +<span class="kw">end</span> Squares.</code></pre> | |
| 90 | +<pre class="output"><code>1000 squares, the last 1000000 | |
| 91 | +1 4 9 | |
| 92 | +</code></pre> | |
| 71 | 93 | <h2><code>Map(K, V; hash, eq)</code></h2> |
| 72 | 94 | <p>A hash map. Its key's hash and equality are procedure arguments, so any type can be a key. <code>put</code> replaces what a key maps to; <code>get</code> says what it maps to and whether it maps to anything; <code>remove</code> says whether the key was there. <code>next</code> walks the map: from 0, it gives the next entry and the number to pass for the one after, and -1 when there are no more.</p> |
| 73 | 95 | <div class="file"><span>Ages.raz</span></div><pre class="code"><code><span class="kw">module</span> Ages; |
| @@ -106,10 +128,11 @@ | ||
| 106 | 128 | <h2><code>Sort(T; less)</code></h2> |
| 107 | 129 | <p>An in-place sort of a slice, by a <code>less</code> procedure, in <code>n log n</code> at most and with no memory of its own. Elements equal by <code>less</code> may change places.</p> |
| 108 | 130 | <h2>Putting them together</h2> |
| 109 | -<p>Counting words, then listing them by count and then alphabetically: a <code>Map</code> for the counts, an array of records to sort, and <code>Sort</code> with a two-level <code>less</code>.</p> | |
| 131 | +<p>Counting words, then listing them by count and then alphabetically: a <code>Map</code> for the counts, a <code>Vec</code> of records to sort, however many words there are, and <code>Sort</code> with a two-level <code>less</code>.</p> | |
| 110 | 132 | <div class="file"><span>Freq.raz</span></div><pre class="code"><code><span class="kw">module</span> Freq; |
| 111 | 133 | <span class="kw">import</span> Fmt, Str, Heap, |
| 112 | 134 | Count := Map([]<span class="ty">U8</span>, <span class="ty">INT</span>, Str.hash, Str.eq), |
| 135 | + Entries := Vec(Entry), | |
| 113 | 136 | Rank := Sort(Entry, before); |
| 114 | 137 | |
| 115 | 138 | <span class="kw">type</span> Entry = <span class="kw">record</span> word: []<span class="ty">U8</span>; n: <span class="ty">INT</span>; <span class="kw">end</span>; |
| @@ -125,8 +148,8 @@ | ||
| 125 | 148 | |
| 126 | 149 | <span class="kw">proc</span> main*; |
| 127 | 150 | <span class="kw">var</span> |
| 128 | - m: Count.Map; entries: [<span class="number">32</span>]Entry; i, k, n, found: <span class="ty">INT</span>; | |
| 129 | - w, rest: []<span class="ty">U8</span>; key: []<span class="ty">U8</span>; v: <span class="ty">INT</span>; | |
| 151 | + m: Count.Map; entries: Entries.Vec; i, k, found: <span class="ty">INT</span>; | |
| 152 | + w, rest: []<span class="ty">U8</span>; key: []<span class="ty">U8</span>; v: <span class="ty">INT</span>; all: []Entry; | |
| 130 | 153 | <span class="kw">begin</span> |
| 131 | 154 | Count.init(<span class="ty">ADR</span>(m), Heap.heap); |
| 132 | 155 | w, rest := Str.field(Text); |
| @@ -135,17 +158,16 @@ | ||
| 135 | 158 | _ := Count.put(<span class="ty">ADR</span>(m), w, k + <span class="number">1</span>); |
| 136 | 159 | w, rest := Str.field(rest); |
| 137 | 160 | <span class="kw">end</span>; |
| 138 | - n := <span class="number">0</span>; | |
| 161 | + Entries.init(<span class="ty">ADR</span>(entries), Heap.heap); | |
| 139 | 162 | found, key, v := Count.next(<span class="ty">ADR</span>(m), <span class="number">0</span>); |
| 140 | 163 | <span class="kw">while</span> found >= <span class="number">0</span> <span class="kw">do</span> |
| 141 | - entries[n].word := key; | |
| 142 | - entries[n].n := v; | |
| 143 | - <span class="ty">INC</span>(n); | |
| 164 | + Entries.push(<span class="ty">ADR</span>(entries), {word: key, n: v}); | |
| 144 | 165 | found, key, v := Count.next(<span class="ty">ADR</span>(m), found); |
| 145 | 166 | <span class="kw">end</span>; |
| 146 | - Rank.sort(entries[<span class="number">0</span>..n]); | |
| 147 | - <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> n - <span class="number">1</span> <span class="kw">do</span> | |
| 148 | - Fmt.print(<span class="string">"{} {}\n"</span>, [Fmt.int(entries[i].n), Fmt.str(entries[i].word)]); | |
| 167 | + all := Entries.all(<span class="ty">ADR</span>(entries)); | |
| 168 | + Rank.sort(all); | |
| 169 | + <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(all) - <span class="number">1</span> <span class="kw">do</span> | |
| 170 | + Fmt.print(<span class="string">"{} {}\n"</span>, [Fmt.int(all[i].n), Fmt.str(all[i].word)]); | |
| 149 | 171 | <span class="kw">end</span>; |
| 150 | 172 | <span class="kw">end</span>; |
| 151 | 173 | |
| @@ -161,7 +183,7 @@ | ||
| 161 | 183 | </code></pre> |
| 162 | 184 | <p>The keys here are slices of the constant text, so they outlive the map; a program whose keys come from a buffer it will reuse copies them first with <code>Str.copy</code>.</p> |
| 163 | 185 | <h2>Choosing</h2> |
| 164 | -<p>Use a <code>List</code> when you push and pop at one end and walk it in order. Use a <code>Map</code> when you look things up by key. Use an array and <code>Sort</code> when you know the size and want an order. When none of them fits, they are each under a hundred lines of raz in <code>lib/</code>, and the advanced tour's allocator lesson shows what is underneath.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="bytes.html">← Strings and bytes</a><a href="index.html">Contents</a><a class="next" href="files.html">Files and the command line →</a></div></article></div> | |
| 186 | +<p>Use a <code>List</code> when you push and pop at one end and walk it in order. Use a <code>Vec</code> when you add at the end and index, and don't know how many will come; an array when you do. Use a <code>Map</code> when you look things up by key. Use <code>Sort</code> on either's slice when you want an order. When none of them fits, they are each under a hundred lines of raz in <code>lib/</code>, and the advanced tour's allocator lesson shows what is underneath.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="bytes.html">← Strings and bytes</a><a href="index.html">Contents</a><a class="next" href="files.html">Files and the command line →</a></div></article></div> | |
| 165 | 187 | </main> |
| 166 | 188 | <footer>raz is under the ISC license.</footer> |
| 167 | 189 | </body> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html" aria-current="page">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -175,6 +175,44 @@ | ||
| 175 | 175 | b.txt, 2 bytes |
| 176 | 176 | src/ |
| 177 | 177 | </code></pre> |
| 178 | +<h2>Modes, links, and a lock</h2> | |
| 179 | +<p><code>Sys.openMode</code> is <code>open</code> with the permissions a file it makes should have, less those the umask takes away: 1EDH (755) for a script, 124H (444) for a file never to be written. <code>Sys.linkInfo</code> looks at a symbolic link itself, where <code>info</code> follows it, so a link whose target is gone is still seen. <code>Info.ino</code> is the file's inode: a file renamed over another has a new one, though its size and time may match, which is how a program knows a file it cached is still the same.</p> | |
| 180 | +<p><code>Sys.lock</code> takes the lock on an open file, which one open file holds at a time; another that asks for it waits, or with <code>wait</code> false gets -11 at once. The lock goes when the file is closed or the program ends, however it ends, so it can't go stale as a lock file can. <code>Sys.syncAll</code> puts everything written to a file system on the disk at once, before the one file that names it all is written: a database's or a version control system's commit.</p> | |
| 181 | +<div class="file"><span>Store.raz</span></div><pre class="code"><code><span class="kw">module</span> Store; | |
| 182 | +<span class="kw">import</span> Fmt, Sys; | |
| 183 | + | |
| 184 | +<span class="kw">proc</span> main*; | |
| 185 | +<span class="kw">var</span> fd, other: <span class="ty">INT</span>; named, target: Sys.Info; | |
| 186 | +<span class="kw">begin</span> | |
| 187 | + fd := Sys.openMode(<span class="string">"run.sh"</span>, {Sys.writing, Sys.creating, Sys.truncating}, <span class="number">1EDH</span>); | |
| 188 | + _ := Sys.write(fd, <span class="string">"echo hi\n"</span>); | |
| 189 | + _ := Sys.syncAll(fd); | |
| 190 | + Sys.close(fd); | |
| 191 | + target, _ := Sys.info(<span class="string">"run.sh"</span>); | |
| 192 | + Fmt.print(<span class="string">"its owner may run it: {}\n"</span>, [Fmt.bool(target.mode & <span class="number">40H</span> # <span class="number">0</span>)]); | |
| 193 | + | |
| 194 | + _ := Sys.remove(<span class="string">"current"</span>); | |
| 195 | + _ := Sys.symlink(<span class="string">"run.sh"</span>, <span class="string">"current"</span>); | |
| 196 | + named, _ := Sys.linkInfo(<span class="string">"current"</span>); | |
| 197 | + Fmt.print(<span class="string">"a link: {}, of {} bytes\n"</span>, [Fmt.bool(named.symlink), Fmt.int(named.size)]); | |
| 198 | + named, _ := Sys.info(<span class="string">"current"</span>); | |
| 199 | + Fmt.print(<span class="string">"the same file: {}\n"</span>, [Fmt.bool(named.ino = target.ino)]); | |
| 200 | + | |
| 201 | + fd := Sys.openRead(<span class="string">"run.sh"</span>); | |
| 202 | + other := Sys.openRead(<span class="string">"run.sh"</span>); | |
| 203 | + Fmt.print(<span class="string">"{} {}\n"</span>, [Fmt.int(Sys.lock(fd, <span class="ty">false</span>)), Fmt.int(Sys.lock(other, <span class="ty">false</span>))]); <span class="comment">(* -11: held *)</span> | |
| 204 | + Sys.close(fd); | |
| 205 | + Fmt.print(<span class="string">"{}\n"</span>, [Fmt.int(Sys.lock(other, <span class="ty">false</span>))]); <span class="comment">(* free again *)</span> | |
| 206 | + Sys.close(other); | |
| 207 | +<span class="kw">end</span>; | |
| 208 | + | |
| 209 | +<span class="kw">end</span> Store.</code></pre> | |
| 210 | +<pre class="output"><code>its owner may run it: true | |
| 211 | +a link: true, of 6 bytes | |
| 212 | +the same file: true | |
| 213 | +0 -11 | |
| 214 | +0 | |
| 215 | +</code></pre> | |
| 178 | 216 | <h2>A command line, from a table</h2> |
| 179 | 217 | <p>A program describes its options once, in a table of <code>Opt.Row</code>s: its synopsis lines, its subcommands and its options. The help and the error messages are made from the table. The parser reads by the rules of the C library's <code>getopt</code> with GNU's long options, so <code>-vn3</code>, <code>-n 3</code>, <code>--count=3</code> and <code>--count 3</code> all work, and <code>--</code> ends the options.</p> |
| 180 | 218 | <div class="file"><span>Tool.raz</span></div><pre class="code"><code><span class="kw">module</span> Tool; |
| @@ -229,7 +267,7 @@ | ||
| 229 | 267 | tool: no words; see tool -h |
| 230 | 268 | status 2</code></pre> |
| 231 | 269 | <p><code>-h</code> and <code>--help</code> are <code>Opt</code>'s: the program never sees them. A mistake on the command line is a sentence on standard error with the program's name first, then the synopsis, and status 2. Nothing in <code>Tool</code> wrote any of that.</p> |
| 232 | -<p>A mistake in the table itself, such as an option given twice, is the program's, and <code>init</code> says so and ends with status 1 before it reads a word.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="collections.html">← Lists, maps and sorting</a><a href="index.html">Contents</a><a class="next" href="processes.html">Time, randomness and other programs →</a></div></article></div> | |
| 270 | +<p>A mistake in the table itself, such as an option given twice, is the program's, and <code>init</code> says so and ends with status 1 before it reads a word.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="collections.html">← Lists, arrays, maps and sorting</a><a href="index.html">Contents</a><a class="next" href="processes.html">Time, randomness and other programs →</a></div></article></div> | |
| 233 | 271 | </main> |
| 234 | 272 | <footer>raz is under the ISC license.</footer> |
| 235 | 273 | </body> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html" aria-current="page">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -31,7 +31,7 @@ | ||
| 31 | 31 | <h2>The library</h2><ol start='1'> |
| 32 | 32 | <li><a href="fmt.html">Output and formatting</a> <span>Out's buffer, Fmt's arguments, formatting into a Buf, and laying out columns.</span></li> |
| 33 | 33 | <li><a href="bytes.html">Strings and bytes</a> <span>Copying, hashing and ordering strings, searching bytes many at a time, and reading and writing binary formats.</span></li> |
| 34 | -<li><a href="collections.html">Lists, maps and sorting</a> <span>List(T), Map(K, V) and Sort(T), put together in a word-frequency program.</span></li> | |
| 34 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a> <span>List(T), Vec(T), Map(K, V) and Sort(T), put together in a word-frequency program.</span></li> | |
| 35 | 35 | <li><a href="files.html">Files and the command line</a> <span>Whole files with Files, descriptors and directories with Sys, and options with Opt.</span></li> |
| 36 | 36 | <li><a href="processes.html">Time, randomness and other programs</a> <span>Clocks, sleeping, random bytes, and running a program with its output piped back.</span></li> |
| 37 | 37 | <li><a href="allocators.html">Writing an allocator</a> <span>Mem.Allocator is two procedures; make one over a fixed array, and one that counts.</span></li> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html" aria-current="page">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
| @@ -52,7 +52,7 @@ | ||
| 52 | 52 | Sys.raz |
| 53 | 53 | Thread.linux-x86_64.raz |
| 54 | 54 | <span class="cmd">$ cd $RAZLIB && grep -c SYSCALL Sys.linux-x86_64.raz Sys.raz</span> |
| 55 | -Sys.linux-x86_64.raz:93 | |
| 55 | +Sys.linux-x86_64.raz:97 | |
| 56 | 56 | Sys.raz:0</code></pre> |
| 57 | 57 | <h2>A module of your own</h2> |
| 58 | 58 | <p>Here is a module with two files. <code>Clock.linux-x86_64.raz</code> is for this target; <code>Clock.raz</code> is what any other target would use. Both say what they are in <code>Name</code>, and both make <code>ticks</code>.</p> |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | <div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'> |
| 22 | 22 | <li><a href="fmt.html">Output and formatting</a></li> |
| 23 | 23 | <li><a href="bytes.html">Strings and bytes</a></li> |
| 24 | -<li><a href="collections.html">Lists, maps and sorting</a></li> | |
| 24 | +<li><a href="collections.html">Lists, arrays, maps and sorting</a></li> | |
| 25 | 25 | <li><a href="files.html">Files and the command line</a></li> |
| 26 | 26 | <li><a href="processes.html">Time, randomness and other programs</a></li> |
| 27 | 27 | <li><a href="allocators.html">Writing an allocator</a></li> |
Binary file; the patch has it.
| @@ -0,0 +1 @@ | ||
| 1 | +e10a7ae85fc10032c8cc7b2b36bfe023581f2412b3fc3f1a6f4193411f562eb3 raz-0.15.0-linux-x86_64.tar.gz |
| @@ -42,7 +42,7 @@ | ||
| 42 | 42 | <div class="facts"> |
| 43 | 43 | <span><b>~10,700</b> lines of compiler</span> |
| 44 | 44 | <span><b>~500K</b> lines compiled a second</span> |
| 45 | - <span><b>1</b> report, <b>28</b> pages</span> | |
| 45 | + <span><b>1</b> report, <b>29</b> pages</span> | |
| 46 | 46 | <span><b>static</b> executables, no run time to install</span> |
| 47 | 47 | </div> |
| 48 | 48 | |
| @@ -109,28 +109,28 @@ | ||
| 109 | 109 | <p>The language is defined by <a href="spec.html">the report</a>, which is |
| 110 | 110 | short on purpose: whatever it doesn’t allow, raz doesn’t have. |
| 111 | 111 | Its second volume describes the library. Both are on one page, and each is |
| 112 | -a PDF: <a href="raz.pdf">the language</a> (28 pages) and | |
| 112 | +a PDF: <a href="raz.pdf">the language</a> (29 pages) and | |
| 113 | 113 | <a href="raz-lib.pdf">the library</a>.</p> |
| 114 | 114 | |
| 115 | 115 | <h2 id="download">Download</h2> |
| 116 | -<p>raz 0.14.0 is a tarball for x86-64 Linux with the compiler, <code>raz</code>, | |
| 116 | +<p>raz 0.15.0 is a tarball for x86-64 Linux with the compiler, <code>raz</code>, | |
| 117 | 117 | its library, <code>lib/</code>, which it finds beside itself, the formatter |
| 118 | 118 | and the documentation tool as sources in <code>tools/</code>, and the manual |
| 119 | 119 | pages. It needs nothing else installed.</p> |
| 120 | 120 | <div class="cta"> |
| 121 | - <a class="btn primary" href="download/raz-0.14.0-linux-x86_64.tar.gz">Download raz 0.14.0</a> | |
| 122 | - <a class="btn" href="download/raz-0.14.0-linux-x86_64.tar.gz.sha256">SHA-256</a> | |
| 121 | + <a class="btn primary" href="download/raz-0.15.0-linux-x86_64.tar.gz">Download raz 0.15.0</a> | |
| 122 | + <a class="btn" href="download/raz-0.15.0-linux-x86_64.tar.gz.sha256">SHA-256</a> | |
| 123 | 123 | </div> |
| 124 | -<pre class="code"><code>$ tar xzf raz-0.14.0-linux-x86_64.tar.gz | |
| 125 | -$ ln -s $PWD/raz-0.14.0-linux-x86_64/raz ~/.local/bin/raz | |
| 124 | +<pre class="code"><code>$ tar xzf raz-0.15.0-linux-x86_64.tar.gz | |
| 125 | +$ ln -s $PWD/raz-0.15.0-linux-x86_64/raz ~/.local/bin/raz | |
| 126 | 126 | $ raz run Hello.raz</code></pre> |
| 127 | 127 | <p>The link works since raz finds <code>lib/</code> beside its real path. The |
| 128 | 128 | manual pages are in <code>man/</code>: <code>man ./man/raz-tutorial.7</code>. |
| 129 | 129 | The address <code>download/latest</code> always leads to the newest release, |
| 130 | -and <code>download/raz-0.14.0-linux-x86_64.tar.gz</code> to this one, for | |
| 131 | -good. Earlier releases: <a href="download/raz-0.13.0-linux-x86_64.tar.gz">0.13.0</a>, <a href="download/raz-0.12.0-linux-x86_64.tar.gz">0.12.0</a>, <a href="download/raz-0.11.1-linux-x86_64.tar.gz">0.11.1</a>, <a href="download/raz-0.11.0-linux-x86_64.tar.gz">0.11.0</a>, <a href="download/raz-0.10.1-linux-x86_64.tar.gz">0.10.1</a>, <a href="download/raz-0.10.0-linux-x86_64.tar.gz">0.10.0</a>, <a href="download/raz-0.9.1-linux-x86_64.tar.gz">0.9.1</a>, <a href="download/raz-0.9.0-linux-x86_64.tar.gz">0.9.0</a>, <a href="download/raz-0.8.0-linux-x86_64.tar.gz">0.8.0</a>, <a href="download/raz-0.7.1-linux-x86_64.tar.gz">0.7.1</a>; each has its SHA-256 beside it, with | |
| 130 | +and <code>download/raz-0.15.0-linux-x86_64.tar.gz</code> to this one, for | |
| 131 | +good. Earlier releases: <a href="download/raz-0.14.0-linux-x86_64.tar.gz">0.14.0</a>, <a href="download/raz-0.13.0-linux-x86_64.tar.gz">0.13.0</a>, <a href="download/raz-0.12.0-linux-x86_64.tar.gz">0.12.0</a>, <a href="download/raz-0.11.1-linux-x86_64.tar.gz">0.11.1</a>, <a href="download/raz-0.11.0-linux-x86_64.tar.gz">0.11.0</a>, <a href="download/raz-0.10.1-linux-x86_64.tar.gz">0.10.1</a>, <a href="download/raz-0.10.0-linux-x86_64.tar.gz">0.10.0</a>, <a href="download/raz-0.9.1-linux-x86_64.tar.gz">0.9.1</a>, <a href="download/raz-0.9.0-linux-x86_64.tar.gz">0.9.0</a>, <a href="download/raz-0.8.0-linux-x86_64.tar.gz">0.8.0</a>, <a href="download/raz-0.7.1-linux-x86_64.tar.gz">0.7.1</a>; each has its SHA-256 beside it, with | |
| 132 | 132 | <code>.sha256</code> added to its name.</p> |
| 133 | -<p class="sha">SHA-256 <code>a410faef783d33ccd84f85b0788f7f06e2e9f682bd6c8945e8729fce52d8abba</code></p> | |
| 133 | +<p class="sha">SHA-256 <code>e10a7ae85fc10032c8cc7b2b36bfe023581f2412b3fc3f1a6f4193411f562eb3</code></p> | |
| 134 | 134 | |
| 135 | 135 | </main> |
| 136 | 136 | <footer>raz is under the ISC license.</footer> |
Binary file; the patch has it.
Binary file; the patch has it.
| @@ -38,7 +38,7 @@ | ||
| 38 | 38 | <p>raz is a small systems language, first made for x86-64 Linux. A raz program can lay a record over a device’s registers, read and write a device’s memory exactly as written, take part in lock-free algorithms, make system calls and call C libraries. It has no garbage collector and no hidden run time: what the program does is what its text says.</p> |
| 39 | 39 | <p>This is the first volume of the report: the language, what an implementation chooses, and the grammar. The second, <code>doc/raz-lib.md</code>, is the library, which sections 3 and 10 and appendix A name.</p> |
| 40 | 40 | <p>This report defines the language. It is short on purpose. Whatever it doesn’t allow, raz doesn’t have.</p> |
| 41 | -<p>This is version 2026-10.26 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 <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program.</p> | |
| 41 | +<p>This is version 2026-10.27 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 <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program. 2026-10.27 mends a defect: returning a slice of the procedure’s own frame is an error (section 7.3), which breaks only a program that read memory already gone.</p> | |
| 42 | 42 | <p>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 written for it. What anyone wishes it had waits on a list, for a later thaw.</p> |
| 43 | 43 | <h1 id="a-first-look">1. A first look</h1> |
| 44 | 44 | <pre><code>module Hello; |
| @@ -528,6 +528,12 @@ | ||
| 528 | 528 | <p><code>break</code> ends the innermost <code>while</code>, <code>repeat</code> or <code>for</code> it stands in, and the program goes on after it. There is no <code>goto</code>.</p> |
| 529 | 529 | <h2 id="return">7.3 Return</h2> |
| 530 | 530 | <p><code>return e;</code> ends the procedure with the result <code>e</code>; <code>return a, b;</code> with several results; <code>return f(x);</code> with the several results of <code>f</code>, if they are the procedure’s own (section 8); <code>return;</code> from a procedure without results. A procedure with results that runs off its end returns zeros, each result as a variable of its type starts (section 4).</p> |
| 531 | +<p>A slice returned must not point into the procedure’s own frame, which is gone once it returns. It is an error to return, as a slice, a local array, a value parameter that is an array, an array literal, or an array a call returns, or a slice of one, a field or an element of one included:</p> | |
| 532 | +<pre><code>proc short(h: Hex): []U8; (* Hex = [64]U8 *) | |
| 533 | +begin | |
| 534 | + return h[0..8]; (* an error: h is the procedure's copy *) | |
| 535 | +end;</code></pre> | |
| 536 | +<p>A slice of a <code>var</code> parameter, of a module variable, or of what a pointer points at may be returned: none of them is this procedure’s frame. A slice of a frame that leaves it some other way isn’t caught, and is the exception of section 10: one held in a local slice variable, or in a record, that is returned; one stored in a module variable or through a pointer; and one that came back from a call, as the slice of a <code>var</code> parameter does to the caller whose local it is.</p> | |
| 531 | 537 | <h2 id="match">7.4 Match</h2> |
| 532 | 538 | <pre><code>match s of |
| 533 | 539 | | circle(r): area := 3.14159 * r * r; |
| @@ -694,7 +700,7 @@ | ||
| 694 | 700 | proc exit*(status: INT); |
| 695 | 701 | proc trace*; (* the calls under way, on standard error *)</code></pre> |
| 696 | 702 | <p><code>Rt.trace</code> says on standard error the calls under way, from the one that calls it out, each with its variables, as a program that stops does.</p> |
| 697 | -<p><strong>Defined behaviour.</strong> Every variable starts at zero, integers wrap, and indexing arrays and slices is checked, so a program’s behaviour is defined by this report, with one exception: memory reached other than through a checked variable. Dereferencing a stale or made-up pointer, indexing through a pointer, an index out of range in a module that says <code>check off;</code>, writing a string’s bytes, a code procedure that breaks its rules, and C code can read or write any memory, and what follows isn’t defined. Dereferencing <code>nil</code>, dividing by zero, calling a <code>nil</code> procedure and running out of stack stop the program, on every target, with status 2, as a failed check does. Before it stops, the program prints to standard error where it was, as file, line and column, and why, as far as it can tell: a <code>nil</code> pointer, a <code>nil</code> procedure, a stack overflow, a bad address or a division fault; then the calls under way, each with its variables of a basic, enumeration, set or pointer type. Whether the machine catches these or the compiled code checks for them is the implementation’s choice (appendix A); that they stop the program isn’t.</p> | |
| 703 | +<p><strong>Defined behaviour.</strong> Every variable starts at zero, integers wrap, and indexing arrays and slices is checked, so a program’s behaviour is defined by this report, with one exception: memory reached other than through a checked variable. Dereferencing a stale or made-up pointer, indexing through a pointer, a slice kept after the call whose frame holds what it points at (section 7.3), an index out of range in a module that says <code>check off;</code>, writing a string’s bytes, a code procedure that breaks its rules, and C code can read or write any memory, and what follows isn’t defined. Dereferencing <code>nil</code>, dividing by zero, calling a <code>nil</code> procedure and running out of stack stop the program, on every target, with status 2, as a failed check does. Before it stops, the program prints to standard error where it was, as file, line and column, and why, as far as it can tell: a <code>nil</code> pointer, a <code>nil</code> procedure, a stack overflow, a bad address or a division fault; then the calls under way, each with its variables of a basic, enumeration, set or pointer type. Whether the machine catches these or the compiled code checks for them is the implementation’s choice (appendix A); that they stop the program isn’t.</p> | |
| 698 | 704 | <p><strong>Separate compilation.</strong> An importer depends only on a module’s interface: what it exports, as an importer sees it, never its source or its code. An implementation that compiles modules on its own makes the same program as if every module were compiled together (appendix A).</p> |
| 699 | 705 | <p><strong>Errors.</strong> A program that breaks a rule of this report that says it is an error isn’t a raz program, and an implementation must reject it: say what is wrong and make no executable. Such a rule is one whose breach would leave the program with no meaning this report can give: a mistake of types, a name that isn’t declared or is declared twice, an assignment to what is read-only, a call with the wrong number of arguments, a type name or a procedure with no result used as a value, type arguments given to a module that isn’t generic, or too many, an import that can’t be found, a procedure declared <code>forward</code> and never defined, a module that imports itself. A fault that is only a lint isn’t an error: a name declared and never used, or a result that a call standing on its own drops (<code>Sys.write(fd, s);</code>, where <code>_ := Sys.write(fd, s);</code> says it is meant). A program with one is valid, and an implementation may warn of it. The text of its messages, how many it reports and which warnings it gives are its own (appendix A).</p> |
| 700 | 706 | <h1 id="versions">11. Versions</h1> |
| @@ -715,7 +721,7 @@ | ||
| 715 | 721 | <li><strong>Messages and warnings.</strong> The text of the compiler’s messages, how many errors it reports before it stops, and which warnings it gives, <code>-Werror</code> included (section 10). That a program that is an error in this report is rejected isn’t free; the words that say so are. So is what a program that fails says on standard error: conformance is judged by a program’s standard output and exit status, never by its standard error.</li> |
| 716 | 722 | <li><strong>The command line.</strong> How the compiler is run, its options, and where its library lives (sections 1 and 3).</li> |
| 717 | 723 | </ul> |
| 718 | -<p><strong>The core.</strong> A target may leave out what the system under it can’t give: <code>Sys</code> past writing, ending the program and getting memory, <code>Thread</code>, <code>link</code> and <code>extern</code>. What it must have, besides the whole language, reals aside, is the core of the library: <code>Rt</code>, <code>Out</code>, <code>Mem</code>, <code>Arena</code>, <code>Lock</code>, <code>Str</code> and <code>Buf</code>, and of <code>Sys</code> what they need. A program that keeps to the core runs on every target. The rest of the library (<code>Heap</code>, <code>Fmt</code>, <code>List</code>, <code>Map</code>, <code>Sort</code>, <code>Math</code>, <code>Files</code>, <code>Opt</code> and the rest of <code>Sys</code>) is the standard library: a target may leave a module out, and one that has it gives what <code>doc/raz-lib.md</code> says. <code>conform/</code> tests the core only.</p> | |
| 724 | +<p><strong>The core.</strong> A target may leave out what the system under it can’t give: <code>Sys</code> past writing, ending the program and getting memory, <code>Thread</code>, <code>link</code> and <code>extern</code>. What it must have, besides the whole language, reals aside, is the core of the library: <code>Rt</code>, <code>Out</code>, <code>Mem</code>, <code>Arena</code>, <code>Lock</code>, <code>Str</code> and <code>Buf</code>, and of <code>Sys</code> what they need. A program that keeps to the core runs on every target. The rest of the library (<code>Heap</code>, <code>Fmt</code>, <code>List</code>, <code>Map</code>, <code>Vec</code>, <code>Sort</code>, <code>Math</code>, <code>Files</code>, <code>Opt</code> and the rest of <code>Sys</code>) is the standard library: a target may leave a module out, and one that has it gives what <code>doc/raz-lib.md</code> says. <code>conform/</code> tests the core only.</p> | |
| 719 | 725 | <h1 id="appendix-b.-the-grammar">Appendix B. The grammar</h1> |
| 720 | 726 | <p>The whole syntax of raz, gathered from the sections above, with the symbols of section 2. <code>{ x }</code> is any number of <code>x</code>, <code>[ x ]</code> is <code>x</code> or nothing, and <code>( x | y )</code> is one of them.</p> |
| 721 | 727 | <pre><code>ident = letter { letter | digit } . |
| @@ -830,7 +836,7 @@ | ||
| 830 | 836 | </tr> |
| 831 | 837 | <tr> |
| 832 | 838 | <td><code>Sys</code></td> |
| 833 | -<td>system calls: <code>open</code>, <code>openRead</code>, <code>create</code>, <code>read</code>, <code>write</code>, <code>seek</code>, <code>position</code>, <code>truncate</code>, <code>sync</code>, <code>close</code>, <code>mmap</code>, <code>munmap</code>, <code>isTerminal</code>, <code>yield</code>, <code>exit</code>; files and directories, <code>rename</code>, <code>remove</code>, <code>removeDir</code>, <code>makeDir</code>, <code>chmod</code>, <code>symlink</code>, <code>readLink</code>, <code>info</code>, <code>infoOf</code>, <code>openDir</code>, <code>nextEntry</code>, <code>closeDir</code>; the terminal, <code>rawMode</code>, <code>restoreMode</code>, <code>termSize</code>, <code>ready</code>; the environment, <code>env</code>, <code>workDir</code>, <code>setWorkDir</code>; programs, pipes and signals; time, <code>clock</code>, <code>now</code>, <code>sleep</code>; <code>random</code>; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and <code>Sys.raz</code> over the C library for the others</td> | |
| 839 | +<td>system calls: <code>open</code>, <code>openMode</code>, <code>openRead</code>, <code>create</code>, <code>read</code>, <code>write</code>, <code>seek</code>, <code>position</code>, <code>truncate</code>, <code>sync</code>, <code>syncAll</code>, <code>lock</code>, <code>close</code>, <code>mmap</code>, <code>munmap</code>, <code>isTerminal</code>, <code>yield</code>, <code>exit</code>; files and directories, <code>rename</code>, <code>remove</code>, <code>removeDir</code>, <code>makeDir</code>, <code>chmod</code>, <code>symlink</code>, <code>readLink</code>, <code>info</code>, <code>linkInfo</code>, <code>infoOf</code>, <code>openDir</code>, <code>nextEntry</code>, <code>closeDir</code>; the terminal, <code>rawMode</code>, <code>restoreMode</code>, <code>termSize</code>, <code>ready</code>; the environment, <code>env</code>, <code>workDir</code>, <code>setWorkDir</code>; programs, pipes and signals; time, <code>clock</code>, <code>now</code>, <code>sleep</code>; <code>random</code>; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and <code>Sys.raz</code> over the C library for the others</td> | |
| 834 | 840 | </tr> |
| 835 | 841 | <tr> |
| 836 | 842 | <td><code>Out</code></td> |
| @@ -885,6 +891,10 @@ | ||
| 885 | 891 | <td>a generic hash map, <code>Map(K, V; hash, eq)</code></td> |
| 886 | 892 | </tr> |
| 887 | 893 | <tr> |
| 894 | +<td><code>Vec</code></td> | |
| 895 | +<td>a generic growable array, <code>Vec(T)</code>, indexed as a slice</td> | |
| 896 | +</tr> | |
| 897 | +<tr> | |
| 888 | 898 | <td><code>Sort</code></td> |
| 889 | 899 | <td>sorting a slice in place, <code>Sort(T; less)</code></td> |
| 890 | 900 | </tr> |
| @@ -991,6 +1001,7 @@ | ||
| 991 | 1001 | dir*: BOOL; (* a directory *) |
| 992 | 1002 | file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) |
| 993 | 1003 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| 1004 | + ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) | |
| 994 | 1005 | end; |
| 995 | 1006 | Dir* = record ... end; (* an open directory; its fields are the target's *) |
| 996 | 1007 | Program* = record |
| @@ -1010,12 +1021,15 @@ | ||
| 1010 | 1021 | proc write*(fd: INT; s: []U8): INT; (* the bytes written *) |
| 1011 | 1022 | proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) |
| 1012 | 1023 | proc open*(path: []U8; how: Accesses): INT; (* its fd *) |
| 1024 | +proc openMode*(path: []U8; how: Accesses; mode: INT): INT; (* open, a file it makes given mode *) | |
| 1013 | 1025 | proc openRead*(path: []U8): INT; (* open(path, {reading}) *) |
| 1014 | 1026 | proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *) |
| 1015 | 1027 | proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; (* the new position *) |
| 1016 | 1028 | proc position*(fd: INT): I64; (* where the next read or write is *) |
| 1017 | 1029 | proc truncate*(fd: INT; size: I64): INT; (* the file made size bytes long *) |
| 1018 | 1030 | proc sync*(fd: INT): INT; (* what was written to it, on the disk *) |
| 1031 | +proc syncAll*(fd: INT): INT; (* all that was written to its file system, on the disk *) | |
| 1032 | +proc lock*(fd: INT; wait: BOOL): INT; (* the file's lock, held until fd is closed *) | |
| 1019 | 1033 | proc close*(fd: INT); |
| 1020 | 1034 | proc isTerminal*(fd: INT): BOOL; |
| 1021 | 1035 | proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) |
| @@ -1031,6 +1045,7 @@ | ||
| 1031 | 1045 | proc symlink*(target, path: []U8): INT; (* path made a link to target *) |
| 1032 | 1046 | proc readLink*(path: []U8): ([]U8, INT); (* what the link names, good until the next call *) |
| 1033 | 1047 | proc info*(path: []U8): (Info, INT); (* following a symbolic link *) |
| 1048 | +proc linkInfo*(path: []U8): (Info, INT); (* the link itself *) | |
| 1034 | 1049 | proc infoOf*(fd: INT): (Info, INT); (* the open file's *) |
| 1035 | 1050 | proc space*(path: []U8): (I64, I64, INT); (* the bytes free and in all on path's file system *) |
| 1036 | 1051 | |
| @@ -1089,7 +1104,7 @@ | ||
| 1089 | 1104 | proc mapFile*(fd, size: INT): ^U8; (* the first size bytes of fd, shared; munmap gives them back; or nil *)</code></pre> |
| 1090 | 1105 | <p><code>info</code> follows a symbolic link: <code>size</code>, <code>dir</code> and <code>file</code> are those of what it names, and <code>symlink</code> says the path was a link, so that a program walking a tree can choose not to follow one back up it. A link to nothing is <code>-ENOENT</code>, as a missing file is. <code>file</code> lets a walk open only what holds data: opening a pipe would wait for a program to write to it.</p> |
| 1091 | 1106 | <p><code>nextEntry</code> gives every name in the directory except <code>.</code> and <code>..</code>, in no particular order. <code>rawMode</code> turns off what the terminal does with keys before the program reads them (echo, line editing, signals, flow control), and NL to CR NL on output. A program that turns it on restores it before it ends: <code>Rt.atExit</code> is the place.</p> |
| 1092 | -<p><strong>Files.</strong> <code>open</code> gives a file for what <code>how</code> asks: <code>reading</code>, <code>writing</code> or both; <code>creating</code> it if it is missing, readable by all and writable by its owner; with <code>exclusive</code>, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; <code>truncating</code> it to nothing; <code>appending</code>, each write going to its end, however other programs write it too. <code>seek</code> moves to <code>pos</code> from the start of the file, or from its end if <code>fromEnd</code>, where a negative <code>pos</code> reads back from the end; <code>position</code> says where the file is, without moving. <code>truncate</code> makes the file <code>size</code> bytes long, cutting it or adding zeros, and leaves the position where it was. <code>sync</code> returns once what was written to the file is on the disk, as a program needs before it says a file is saved. <code>infoOf</code> is <code>info</code> of an open file: its size is what it holds now, however it was written. <code>Info.mtime</code> is what a program checks to see whether a file changed under it; <code>Info.mode</code> holds the nine bits of <code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for a program, 1A4H (644) for data.</p> | |
| 1107 | +<p><strong>Files.</strong> <code>open</code> gives a file for what <code>how</code> asks: <code>reading</code>, <code>writing</code> or both; <code>creating</code> it if it is missing, readable by all and writable by its owner; with <code>exclusive</code>, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; <code>truncating</code> it to nothing; <code>appending</code>, each write going to its end, however other programs write it too. <code>openMode</code> is <code>open</code> that gives a file it makes the bits <code>mode</code>, less those the umask takes away: 1EDH (755) for a program, 124H (444) for a file never to be written; <code>open</code> gives 1A4H (644). <code>seek</code> moves to <code>pos</code> from the start of the file, or from its end if <code>fromEnd</code>, where a negative <code>pos</code> reads back from the end; <code>position</code> says where the file is, without moving. <code>truncate</code> makes the file <code>size</code> bytes long, cutting it or adding zeros, and leaves the position where it was. <code>sync</code> returns once what was written to the file is on the disk, as a program needs before it says a file is saved; <code>syncAll</code> returns once everything written to the file system that holds the file is on the disk, at once rather than a file at a time, and may flush other file systems too. <code>lock</code> takes the file’s lock, which one open file holds at a time: another that asks for it waits if <code>wait</code>, and otherwise fails with -11 (EAGAIN). The lock goes when the file is closed or the program ends, however it ends, so it never goes stale as a file made with <code>exclusive</code> does. <code>infoOf</code> is <code>info</code> of an open file: its size is what it holds now, however it was written; <code>linkInfo</code> is <code>info</code> of a symbolic link itself, not what it names, so that <code>symlink</code> is true, <code>dir</code> and <code>file</code> false, and <code>size</code> the length of what it names, which need not be there. <code>Info.mtime</code> is what a program checks to see whether a file changed under it, and <code>Info.ino</code> whether it is still the same file, not one renamed over it; 0 on a target whose files have none. <code>Info.mode</code> holds the nine bits of <code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for a program, 1A4H (644) for data.</p> | |
| 1093 | 1108 | <p><strong>Programs.</strong> <code>openPty</code> makes a pseudo-terminal, a terminal that a program, a terminal emulator, plays the screen and the keyboard of: what it writes to the fd is typed, and what the programs on the terminal write it reads.</p> |
| 1094 | 1109 | <p><code>spawn</code> starts the program at <code>p.path</code>, with <code>p.args</code> as its arguments, the first being its name. Its environment is this program’s with each <code>NAME=value</code> of <code>p.env</code> added or put in place of the old value, or, if <code>p.onlyEnv</code>, <code>p.env</code> alone. It starts in <code>p.dir</code>. Its standard input, output and error are <code>p.fds</code>; a 0 gives it this program’s own, so <code>{path: "/bin/ls", args: ["ls"]}</code> runs <code>ls</code> as a shell runs it, with every other field zero. It gets no other open file, every signal does what it does at first, and none is blocked. If <code>p.tty</code> is a pseudo-terminal’s fd, the program starts a session on that terminal: it is its controlling terminal, and its standard input, output and error, and <code>p.fds</code> and <code>p.group</code> are not used.</p> |
| 1095 | 1110 | <p><code>p.group</code> puts the program in a process group, the programs that a shell’s job is made of: -1 makes it the first of a new group, whose number is its process id, and the other programs of the job join that number. The terminal gives its keys, and the signals of Ctrl-C and Ctrl-Z, to one group, the one in its foreground: <code>p.foreground</code> puts the program’s group there before it starts, and <code>foreground</code> puts any group there, as a shell does to take the terminal back. <code>setGroup</code> moves a program to a group, as the shell does for each program it starts, for whichever of the two runs first; a shell runs in a group of its own, <code>setGroup(0, 0)</code>.</p> |
| @@ -1141,6 +1156,7 @@ | ||
| 1141 | 1156 | proc room*(b: ^Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *) |
| 1142 | 1157 | proc add*(b: ^Buf; c: U8); |
| 1143 | 1158 | proc str*(b: ^Buf; s: []U8); |
| 1159 | +proc spare*(b: ^Buf; n: INT): []U8; (* room for n more bytes, and those bytes, to fill *) | |
| 1144 | 1160 | proc bytes*(b: ^Buf): []U8; (* what it holds, until it next grows *) |
| 1145 | 1161 | proc reset*(b: ^Buf); (* emptied, its memory kept *) |
| 1146 | 1162 | proc done*(b: ^Buf); (* its memory given back *) |
| @@ -1176,6 +1192,17 @@ | ||
| 1176 | 1192 | proc get*(m: ^Map; k: K): (V, BOOL); (* what k maps to, and whether it maps to anything *) |
| 1177 | 1193 | proc next*(m: ^Map; i: INT): (INT, K, V); (* the entry after i *) |
| 1178 | 1194 | |
| 1195 | +module Vec(T); | |
| 1196 | +type Vec* = record len*: INT; ... end; | |
| 1197 | +proc init*(v: ^Vec; a: ^Mem.Allocator); | |
| 1198 | +proc room*(v: ^Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *) | |
| 1199 | +proc push*(v: ^Vec; x: T); (* x added at the end *) | |
| 1200 | +proc add*(v: ^Vec): ^T; (* a new element at the end, zero, to fill in place *) | |
| 1201 | +proc resize*(v: ^Vec; n: INT); (* n elements: cut short, or the new ones zero *) | |
| 1202 | +proc all*(v: ^Vec): []T; (* the elements, until it next grows *) | |
| 1203 | +proc reset*(v: ^Vec); (* emptied, its memory kept *) | |
| 1204 | +proc done*(v: ^Vec); (* its memory given back *) | |
| 1205 | + | |
| 1179 | 1206 | module Sort(T; less: proc(a, b: T): BOOL); |
| 1180 | 1207 | proc sort*(s: []T); |
| 1181 | 1208 | |
| @@ -1201,7 +1228,7 @@ | ||
| 1201 | 1228 | proc fromBits*(u: U64): F64; (* the F64 whose bits are u *) |
| 1202 | 1229 | proc mul*(a, b: U64): (U64, U64); (* a times b: the low 64 bits of the product, and the high *) |
| 1203 | 1230 | proc toReal*(s: []U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn't one *)</code></pre> |
| 1204 | -<p>A <code>Buf</code>, a <code>List</code> and a <code>Map</code> take their memory from the allocator <code>init</code> gives them. <code>Fmt</code>’s format takes the next argument at each <code>{}</code>; <code>{{</code> and <code>}}</code> stand for <code>{</code> and <code>}</code>. A <code>Map</code> hashes and compares its keys with the procedures it is given: <code>import Count := Map([]U8, INT, Str.hash, Str.eq);</code>.</p> | |
| 1231 | +<p>A <code>Buf</code>, a <code>List</code>, a <code>Map</code> and a <code>Vec</code> take their memory from the allocator <code>init</code> gives them. <code>Buf.add</code>, <code>str</code> and <code>spare</code> stop the program, as a failed check does, if the allocator has no room; <code>room</code> asks first. A program reads into a <code>Buf</code> through <code>spare</code>, then adds what it read to <code>len</code>: <code>s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n > 0 then INC(b^.len, n); end;</code>. A <code>Vec</code> is <code>Buf</code> for any <code>T</code>: one block, which doubles when it is full, so <code>push</code> costs the same on average however long it grows. <code>all</code> gives the elements as a slice, indexed and checked as any is, and <code>v.len</code> is their number; the slice, and what <code>add</code> gives, are good until the array next grows. <code>push</code>, <code>add</code> and <code>resize</code> stop the program, as <code>Buf.add</code> does, if the allocator has no room; <code>room</code> asks first: <code>import Lines := Vec([]U8);</code>. <code>Fmt</code>’s format takes the next argument at each <code>{}</code>; <code>{{</code> and <code>}}</code> stand for <code>{</code> and <code>}</code>. A <code>Map</code> hashes and compares its keys with the procedures it is given: <code>import Count := Map([]U8, INT, Str.hash, Str.eq);</code>.</p> | |
| 1205 | 1232 | <p><code>Str</code> reads and writes UTF-8. <code>encode</code> writes a character, of at most 10FFFFH, in one to four bytes; a number that isn’t one becomes FFFDH, the character that stands for a wrong one. <code>decode</code> reads the first character of <code>s</code>, and gives FFFDH and 1 for a byte that doesn’t start one rightly, and 0 and 0 for an empty <code>s</code>: a continuation, a byte followed by too few continuations, a surrogate, a character beyond 10FFFFH or one spelled in more bytes than it needs. <code>decodeLast</code> reads the last character in the same way, to walk a string backwards. <code>valid</code> says that <code>decode</code> finds no such byte in <code>s</code>, and <code>chars</code> counts the characters, each such byte one. <code>next</code> walks a string a character at a time, as <code>decode</code> reads them: from 0, it gives the character at <code>i</code> and where the one after it starts, and 0 and -1 when <code>i</code> is at the end, or past it:</p> |
| 1206 | 1233 | <pre><code>c, i := Str.next(s, 0); |
| 1207 | 1234 | while i >= 0 do |
| @@ -143,6 +143,7 @@ | ||
| 143 | 143 | |
| 144 | 144 | </code></pre> |
| 145 | 145 | <p>Every index and every slice bound is checked, so a slice can never reach past what it was made from; the lesson on failures shows what that looks like.</p> |
| 146 | +<p>A slice of a local array is good only while its procedure runs: the array is gone once it returns. So returning one is an error, <code>return a[0..n]</code> or <code>return a</code> with <code>a</code> a local, a value parameter or an array literal. Take the array as a <code>var</code> parameter instead, and the slice is of the caller's array; or fill a buffer the caller gives.</p> | |
| 146 | 147 | <h2><code>COPY</code></h2> |
| 147 | 148 | <p><code>COPY(d, s)</code> copies the elements of <code>s</code> to the start of <code>d</code>, as one block, and is right when they overlap. <code>LEN(s)</code> must be at most <code>LEN(d)</code>.</p> |
| 148 | 149 | <div class="file"><span>Moves.raz</span></div><pre class="code"><code><span class="kw">module</span> Moves; |