| | @@ -6,8 +6,9 @@ |
| 6 | 6 | |
| 7 | 7 | ## IMMINENT - APPROVED: what a call does with its arguments, written at the call |
| 8 | 8 | |
| 9 | | -**Status.** Approved by the owner, 2026-10-16, for the next thaw. It |
| 10 | | -is three parts, made in one thaw so that programs break once: |
| 9 | +**Status.** Approved by the owner, 2026-10-16, for the next thaw, and |
| 10 | +revised on 2026-10-17 after a review of how it reads in real code. It |
| 11 | +is five parts, made in one thaw so that programs break once: |
| 11 | 12 | |
| 12 | 13 | 1. **Marks at the call** (this section): an argument for a `var` |
| 13 | 14 | parameter is written `var x`, and one for a `keep` parameter |
| | @@ -16,6 +17,12 @@ |
| 16 | 17 | `ADR` that stands at nearly every call to the library goes. |
| 17 | 18 | 3. **`ret` and `keep`** (below): a heading says what a procedure does |
| 18 | 19 | with what it is given, and the compiler holds the body to it. |
| 20 | +4. **A `const` parameter** (below): a record or array only read is |
| 21 | + passed in place, unmarked, so that `var` at a call always means a |
| 22 | + change. |
| 23 | +5. **A constant holds a variant** (below): so that a table a procedure |
| 24 | + keeps, as `Opt.init`'s, can be a constant, not a literal in the |
| 25 | + frame. |
| 19 | 26 | |
| 20 | 27 | Nothing is built until the owner starts the milestone. The report |
| 21 | 28 | changes first, as always; the documentation listed below comes with |
| | @@ -53,13 +60,21 @@ |
| 53 | 60 | | Heading | The procedure | At the call | Example | |
| 54 | 61 | |---|---|---|---| |
| 55 | 62 | | no mark | reads it while the call runs, then lets go | nothing | `Str.eq(a, b)` | |
| 63 | +| `const x` | reads the caller's variable in place, without a copy, and doesn't change it | nothing | `Opt.at(p)` | |
| 56 | 64 | | `var x` | may change the caller's variable | `var x` | `Buf.add(var b, c)` | |
| 57 | | -| `keep x` | may store it where it outlives the call | `keep x` | `Names.put(var table, keep name, 1)` | |
| 65 | +| `keep x` | may store it where it outlives the call | `keep x` | `Names.put(var table, keep name, keep 1)` | |
| 58 | 66 | | `ret x` | its results may point into it | nothing | `s := Str.trim(line[0..n])` | |
| 59 | 67 | | `^T` | keeps a pointer, or takes one for C, an allocator, or what may be `nil` | `ADR(x)`, or a pointer it holds | `Thread.spawn(work, ^U8(ADR(job)))` | |
| 60 | 68 | |
| 61 | | -`var` is in the language now; `keep` and `ret` come with part 3. The |
| 62 | | -table goes into the report (8), raz(7) and the tutorial as it is here. |
| 69 | +A slice given to an unmarked parameter is a value, two words, but the |
| 70 | +elements it points at are the caller's: the procedure may write them, |
| 71 | +and nothing at the call says so. `Sys.read(fd, line)` fills `line`, |
| 72 | +`COPY(d, s)` fills `d`. A slice is a view of the caller's array, and |
| 73 | +lending it is what it is for; the table says so in a note under it. |
| 74 | + |
| 75 | +`var` is in the language now; `keep` and `ret` come with part 3, `const` |
| 76 | +with part 4. The table, and the note, go into the report (8), raz(7) |
| 77 | +and the tutorial as they are here. |
| 63 | 78 | |
| 64 | 79 | ### The rules |
| 65 | 80 | |
| | @@ -70,11 +85,13 @@ |
| 70 | 85 | another module's, of a procedure value, of a generic module's |
| 71 | 86 | instance. |
| 72 | 87 | 2. **`keep` at the call.** An argument for a `keep` parameter is |
| 73 | | - written `keep` and the argument, when the argument's type carries a |
| 74 | | - slice (part 3, "Words"). For a type that carries none it isn't |
| 75 | | - written, and writing it is an error: `Map(INT, INT).put(var m, 5, 7)` |
| 76 | | - keeps nothing that could end. In a generic module's instance the |
| 77 | | - argument's type is the instance's. |
| 88 | + written `keep` and the argument, whatever its type, as `var` is: |
| 89 | + `Counts.put(var counts, keep 5, keep 7)`. The map does keep the 5. |
| 90 | + Where the argument can't end, an `INT` or a literal, nothing is |
| 91 | + checked, but the mark stands. So the mark follows the heading alone: |
| 92 | + a generic module's body reads the same in every instance, as |
| 93 | + `Bag(T)` calling `L.push(var l, keep x)` must, and a call never |
| 94 | + hangs on fields of a record it can't see. |
| 78 | 95 | 3. **Both.** An argument for a `keep var` parameter is written |
| 79 | 96 | `keep var x`, in the heading's order. |
| 80 | 97 | 4. **`ret`** is never written at the call. It is about the result, |
| | @@ -87,12 +104,21 @@ |
| 87 | 104 | or `keep` parameters, and so take none. |
| 88 | 105 | 6. **What follows `keep`.** `keep` is a mark, not a name, when the |
| 89 | 106 | token after it is a name, a number, a string or character literal, |
| 90 | | - or `{`. Otherwise it is the name `keep`: `keep[i]`, `keep(x)` and |
| 91 | | - `keep^` are what they are today. An array literal is never the |
| 92 | | - argument of a `keep` parameter, since it lives in the frame |
| 93 | | - (part 3), so `keep [` is never needed. |
| 94 | | -7. **Procedure types** say `var` and `keep` as headings do (5.9), so a |
| 95 | | - call through a procedure value is marked as a call by name. |
| 107 | + `{`, `[`, `var`, `-`, `not` or `~`: anything that starts an |
| 108 | + argument and can't follow a name in one. Before `(`, `^`, `.`, `,`, |
| 109 | + `)` or an operator between two operands it is the name `keep`: |
| 110 | + `keep(x)`, `keep^`, `keep.f` and `keep + 1` are what they are today. |
| 111 | + An argument that starts with `(` is written without it: `keep a + b`, |
| 112 | + not `keep (a + b)`. `keep [` is the mark before an array literal or |
| 113 | + the empty slice, `keep []`; an array literal lives in the frame |
| 114 | + (part 3), so a kept one is an error unless it is a constant's. |
| 115 | +7. **Procedure types** say `const`, `var` and `keep` as headings do |
| 116 | + (5.9), so a call through a procedure value is marked as a call by |
| 117 | + name. |
| 118 | +8. **`const`** is never written at the call: the procedure neither |
| 119 | + changes nor keeps what it is given. Its argument is any expression |
| 120 | + of the parameter's type; a variable is passed in place, and anything |
| 121 | + else in a temporary of the statement (part 4). |
| 96 | 122 | |
| 97 | 123 | ``` |
| 98 | 124 | Arguments = "(" [ Argument { "," Argument } ] ")" . |
| | @@ -101,7 +127,7 @@ |
| 101 | 127 | |
| 102 | 128 | Each rule looks at the call and the heading only, so a one-pass |
| 103 | 129 | compiler makes them as it reads the call: perhaps 40 lines, beside the |
| 104 | | -250 to 400 of part 3. |
| 130 | +250 to 400 of part 3, the 30 to 60 of part 4 and the 20 of part 5. |
| 105 | 131 | |
| 106 | 132 | ### Examples |
| 107 | 133 | |
| | @@ -184,9 +210,11 @@ |
| 184 | 210 | (* import Names := Map([]U8, INT, Str.hash, Str.eq); |
| 185 | 211 | table: Names.Map and heap: Heap's allocator, module variables *) |
| 186 | 212 | proc count(name: []U8); |
| 213 | +var k: []U8; ok: BOOL; |
| 187 | 214 | begin |
| 188 | | - _ := Names.put(var table, keep Str.copy(heap, name), 1); (* a copy, on the heap *) |
| 189 | | - _ := Names.put(var table, keep "main", 1); (* a literal never ends *) |
| 215 | + k, ok := Str.copy(heap, name); (* a copy, on the heap *) |
| 216 | + if ok then _ := Names.put(var table, keep k, keep 1); end; |
| 217 | + _ := Names.put(var table, keep "main", keep 1); (* a literal never ends *) |
| 190 | 218 | end; |
| 191 | 219 | |
| 192 | 220 | proc lookup(name: []U8): INT; |
| | @@ -197,13 +225,55 @@ |
| 197 | 225 | end; |
| 198 | 226 | ``` |
| 199 | 227 | |
| 200 | | -**A map that keeps nothing that can end:** |
| 228 | +**A map that keeps nothing that can end.** The marks stand, as the |
| 229 | +heading says, though nothing is checked: |
| 201 | 230 | |
| 202 | 231 | ``` |
| 203 | 232 | (* import Counts := Map(INT, INT, hashInt, eqInt) *) |
| 204 | | -_ := Counts.put(var counts, 5, 7); (* INTs carry no slice: no keep *) |
| 233 | +_ := Counts.put(var counts, keep 5, keep 7); |
| 234 | +``` |
| 235 | + |
| 236 | +**A generic module calling another.** One body serves every instance: |
| 237 | + |
| 238 | +``` |
| 239 | +module Bag(T); |
| 240 | +import Mem, L := List(T); |
| 241 | +var l: L.List; |
| 242 | + |
| 243 | +proc add*(keep x: T); |
| 244 | +begin |
| 245 | + _ := L.push(var l, keep x); |
| 246 | +end; |
| 247 | +``` |
| 248 | + |
| 249 | +**A table a procedure keeps**, a constant (part 5), as `Sh`'s becomes: |
| 250 | + |
| 251 | +``` |
| 252 | +const Rows: []Opt.Row = [ |
| 253 | + Opt.usage("", "Sh [-c script | file]"), |
| 254 | + Opt.option("", 'c', "command", "script", "run script, not standard input")]; |
| 255 | +... |
| 256 | +Opt.init(var p, keep "Sh", keep Rows); |
| 257 | +while Opt.next(var p) do ... end; |
| 258 | +if Opt.at(p) > 1 then ... end; (* at only reads: const, no mark *) |
| 259 | +``` |
| 260 | + |
| 261 | +Today's `Opt.init(ADR(p), "Sh", [...])`, with the literal in the call, |
| 262 | +is an error once `init` is `keep table`: the literal lives in the |
| 263 | +frame, and `keep` can't say "as long as `p`" (part 3, "False alarms"). |
| 264 | + |
| 265 | +**A large record only read**, by `const` (part 4), from `tools/Pretty.raz`: |
| 266 | + |
| 205 | 267 | ``` |
| 268 | +proc is(const sc: Scanner; s: []U8): BOOL; (* Scan's *) |
| 206 | 269 | |
| 270 | +if Scan.is(sc, "-") or Scan.is(sc, "+") then ... end; |
| 271 | +if Scan.among(Scan.text(sc), binaryWords) then ... end; |
| 272 | +``` |
| 273 | + |
| 274 | +Today each of these is `ADR(sc)`; by `var` each would say `var sc`, |
| 275 | +and none changes it. |
| 276 | + |
| 207 | 277 | **A list of names a procedure keeps, passed on.** The parameter is |
| 208 | 278 | `keep` in turn, so the promise travels up: |
| 209 | 279 | |
| | @@ -251,6 +321,13 @@ |
| 251 | 321 | p := Sys.mmap(size); (* bytes, not a variable *) |
| 252 | 322 | ``` |
| 253 | 323 | |
| 324 | +**A slice's elements, lent unmarked:** |
| 325 | + |
| 326 | +``` |
| 327 | +n := Sys.read(fd, line); (* fills line[0..n] *) |
| 328 | +Str.lower(word); (* writes word's bytes *) |
| 329 | +``` |
| 330 | + |
| 254 | 331 | **`ret`, unmarked:** |
| 255 | 332 | |
| 256 | 333 | ``` |
| | @@ -266,8 +343,11 @@ |
| 266 | 343 | Sh.raz:94:21: b: write `var b`: Buf.add may change it |
| 267 | 344 | Sh.raz:115:23: b: Buf.bytes only reads it: no `var` |
| 268 | 345 | Count.raz:12:27: name: write `keep name`: Names.put keeps it |
| 269 | | -Count.raz:14:31: 5: INT carries no slice: no `keep` |
| 346 | +Count.raz:14:34: 7: write `keep 7`: Counts.put keeps it |
| 270 | 347 | Count.raz:9:27: key[0..4]: kept by Names.put, but it points into this procedure's frame |
| 348 | +Sh.raz:60:28: [...]: kept by Opt.init, but an array literal lives in this procedure's frame: make it a constant |
| 349 | +Pretty.raz:123:19: sc: Scan.is only reads it (const): no `var` |
| 350 | +Scan.raz:41:5: sc: a const parameter can't be changed |
| 271 | 351 | Sh.raz:88:14: ADR(b): Buf.init takes `var b`, not a pointer |
| 272 | 352 | Sh.raz:151:14: p: Buf.add takes `var p^`, not a pointer |
| 273 | 353 | ``` |
| | @@ -284,8 +364,14 @@ |
| 284 | 364 | into `var x`: the same count, shorter, and true; |
| 285 | 365 | - about 580 calls of our own procedures with `var` parameters, unmarked |
| 286 | 366 | today: each gains `var`, about one line in 40; |
| 287 | | -- `keep`: a few dozen calls (`Map.put`, `List.push`, `Opt.init`, and |
| 288 | | - procedures that pass a kept slice on). |
| 367 | +- `keep`: a few dozen calls (`Map.put`, `List.push`, `Vec.push`, |
| 368 | + `Opt.init`, `Opt.scope`, and procedures that pass a kept value on). |
| 369 | + Since it follows the heading, it stands also before values that can't |
| 370 | + end, `keep 1` in `Names.put(var table, keep name, keep 1)`: a word |
| 371 | + each, the price of one rule for every instance; |
| 372 | +- `const`: none at the call, and some 40 `var`s and `ADR`s fewer, where |
| 373 | + a large record is only read (`Opt`, `Scan` in the tools, `Item` in |
| 374 | + `src/`). |
| 289 | 375 | |
| 290 | 376 | So most of the marks stand where an `ADR` stands today. What is new is |
| 291 | 377 | the 580, and those are the calls, like `utf8(cur.pos)`, that change a |
| | @@ -305,9 +391,11 @@ |
| 305 | 391 | The thaw is not done until each of these is: |
| 306 | 392 | |
| 307 | 393 | - **The report, `doc/raz.md`.** 8: the grammar above; "Parameters" |
| 308 | | - becomes "Parameters and arguments", with the table, rules 1 to 7, and |
| 309 | | - the swap and map examples; `keep` and `ret` from part 3. 5.9: |
| 310 | | - procedure types with marks. 7.3: `ret`. 10: the promise, whose |
| 394 | + becomes "Parameters and arguments", with the table and its note on |
| 395 | + slices, rules 1 to 8, and the swap, map and `const` examples; `keep` |
| 396 | + and `ret` from part 3, `const` from part 4. 4: a constant may hold a |
| 397 | + variant (part 5), with `Opt`'s table as the example. 5.9: procedure |
| 398 | + types with marks. 7.3: `ret`. 10: the promise, whose |
| 311 | 399 | exceptions shrink to `ADR`, C, code procedures and `check off`. |
| 312 | 400 | 1, "A first look": its example marks its calls. Every example in the |
| 313 | 401 | report is marked. About two pages; the report stays under 40 (31 |
| | @@ -323,7 +411,8 @@ |
| 323 | 411 | and says why. A new section after 6, "WHAT A CALL DOES WITH ITS |
| 324 | 412 | ARGUMENTS": the table; a whole program, with its output, that counts |
| 325 | 413 | the words of its input in a `Map([]U8, INT)` with a `Buf`, using |
| 326 | | - `var`, `keep` and `ret`, each said in a sentence where it stands; |
| 414 | + `var`, `keep`, `ret` and `const`, each said in a sentence where it |
| 415 | + stands, and taking its options from a constant table of `Opt`; |
| 327 | 416 | then "What the compiler stops", the four mistakes of the messages |
| 328 | 417 | above, each with its message. Section 16, "FROM PASCAL AND C TO RAZ", |
| 329 | 418 | gains: Pascal's `var` is unmarked at the call, raz's is marked, as C#'s |
| | @@ -333,8 +422,10 @@ |
| 333 | 422 | - **Volume 2, `doc/raz-lib.md`, and raz-lib(3)**: every signature of |
| 334 | 423 | part 2, each module's example marked, and a note in each changed |
| 335 | 424 | module of what changed. |
| 336 | | -- **`STANDARDS.md`**: "Parameters: a value, `var`, or a pointer" says |
| 337 | | - that `ADR` no longer marks what is kept, `keep` does; "The library |
| 425 | +- **`STANDARDS.md`**: "Parameters: a value, `var`, or a pointer" becomes |
| 426 | + "a value, `const`, `var`, or a pointer": a large record or array only |
| 427 | + read is `const`, not `var`, so that `var` at a call always means a |
| 428 | + change; `ADR` no longer marks what is kept, `keep` does; "The library |
| 338 | 429 | until a thaw" goes. |
| 339 | 430 | - **`doc/compiler.md`**: where the marks are read and checked, the |
| 340 | 431 | checks of part 3 and the set each value carries, with the code |
| | @@ -368,10 +459,15 @@ |
| 368 | 459 | 3. The library's signatures (part 2), and every caller in this |
| 369 | 460 | repository mended. |
| 370 | 461 | 4. `ret` and `keep` in headings and their checks (part 3), and the |
| 371 | | - library marked (`Str.trim`, `Map.put`, `List.push`, `Opt.init`, ...). |
| 372 | | -5. The tutorial, volume 2, raz-lib(3), `STANDARDS.md`, |
| 462 | + library marked (`Str.trim`, `Map.put`, `List.push`, `Vec.push`, |
| 463 | + `Opt.init`, `Opt.scope`, ...). |
| 464 | +5. `const` parameters (part 4), and the library's and `src/`'s large |
| 465 | + records only read moved to it. |
| 466 | +6. Constants holding variants (part 5), and `Sh`'s and `Opt`'s tables |
| 467 | + made constants. |
| 468 | +7. The tutorial, volume 2, raz-lib(3), `STANDARDS.md`, |
| 373 | 469 | `doc/compiler.md`, `README.md`. |
| 374 | | -6. The tests: `conform/`, `test/err`, `test/msg`, `test/pretty`, the |
| 470 | +8. The tests: `conform/`, `test/err`, `test/msg`, `test/pretty`, the |
| 375 | 471 | editors. |
| 376 | 472 | |
| 377 | 473 | The examples of parts 2 and 3 are written as the whole thaw makes |
| | @@ -394,13 +490,16 @@ |
| 394 | 490 | `done`; `Buf.init`, `room`, `add`, `str`, `spare`, `cut`, `reset`, |
| 395 | 491 | `done`, and the `Buf` that `Files.load`, `Files.read`, `Fmt.write`, |
| 396 | 492 | `Str.join` and `Str.replace` write into; every procedure of `List`; |
| 397 | | - `Lock.take`, `give`; `Map.init`, `put`, `remove`, `done`; every |
| 398 | | - procedure of `Opt` (a `Parser` is many words, so even those that |
| 399 | | - only read it); `Sys.openDir`, `nextEntry`, `closeDir`; |
| 493 | + `Lock.take`, `give`; `Map.init`, `put`, `remove`, `done`; the |
| 494 | + procedures of `Opt` that change the `Parser` (`init`, `next`, |
| 495 | + `operand`, `scope`, ...); `Sys.openDir`, `nextEntry`, `closeDir`; |
| 400 | 496 | `Thread.lock`, `unlock`; `Vec.init`, `room`, `push`, `pop`, `add`, |
| 401 | 497 | `tryPush`, `tryAdd`, `resize`, `reset`, `done`. |
| 402 | 498 | - **To a value** (it only reads a record of a few words): `Buf.bytes`, |
| 403 | 499 | `Vec.all`, `Map.get`, `Map.next`, `Sys.entryKind`. |
| 500 | +- **To `const`** (it only reads a record of many words; part 4): |
| 501 | + `Opt.at`, and whichever other procedure of `Opt` a reading shows only |
| 502 | + reads the `Parser`. |
| 404 | 503 | - **Staying pointers**: an allocator, `^Mem.Allocator`, everywhere |
| 405 | 504 | (`NEW` takes one, and `^Arena(a)` needs one); `Mem.pages`, `unmap`, |
| 406 | 505 | `Sys.mmap`, `munmap`, `mapFile`, which are bytes, not variables; |
| | @@ -486,8 +585,12 @@ |
| 486 | 585 | value parameter's array, or an array literal used as a slice; |
| 487 | 586 | - **a parameter `p`**: what `p` lends. For a value parameter, that is |
| 488 | 587 | what its slices point at, which is the caller's business, not this |
| 489 | | - frame. For a `var` parameter, it is also the caller's variable |
| 490 | | - itself: `h[0..8]` of `var h: Hex` points into `h`. |
| 588 | + frame. For a `var` or `const` parameter whose type holds an array, |
| 589 | + it is also the caller's variable itself: `h[0..8]` of `var h: Hex` |
| 590 | + points into `h`. One whose type holds no array, as a `Scanner`, lends |
| 591 | + only what its slices point at, as a value parameter does, so |
| 592 | + `return Scan.text(n)` of a local `n: Scanner` is not frame-bound |
| 593 | + when `n.src` isn't. |
| 491 | 594 | - **nowhere that ends**: module variables, constants, string literals, |
| 492 | 595 | and anything reached through a pointer. |
| 493 | 596 | |
| | @@ -544,15 +647,16 @@ |
| 544 | 647 | end; |
| 545 | 648 | ``` |
| 546 | 649 | |
| 547 | | -`ret` is allowed on a parameter that carries a slice, or on a `var` |
| 548 | | -parameter whose type holds an array (it lends the caller's variable), |
| 549 | | -of a procedure with a result that carries a slice. It is an error |
| 550 | | -elsewhere: |
| 551 | | - |
| 552 | | -- `ret n: INT`, `ret a: [4]U8`: nothing to lend. A value array is the |
| 553 | | - procedure's own copy, and a slice of it can never be returned. |
| 554 | | -- `ret` on a procedure whose results carry no slice: nothing to lend |
| 555 | | - it to. |
| 650 | +`ret` lends something when the parameter carries a slice, or is a |
| 651 | +`var` or `const` parameter whose type holds an array (it lends the |
| 652 | +caller's variable), of a procedure with a result that carries a slice. |
| 653 | +Elsewhere, `ret n: INT`, `ret a: [4]U8` or on a procedure whose results |
| 654 | +carry no slice, it lends nothing, and is allowed: a mark is leave to |
| 655 | +do something, not a claim that it is done. Two cases need that. A |
| 656 | +generic module's parameter may be any type, and a procedure with two |
| 657 | +bodies, one for each target, has one heading: `Sys.nextEntry`'s `Dir` |
| 658 | +holds the entry's bytes in an array on Linux, where `ret` lends them, |
| 659 | +and a C pointer over C, where it lends nothing. |
| 556 | 660 | |
| 557 | 661 | With several results, `ret` covers them all: in `cut(ret s: []U8; |
| 558 | 662 | sep: []U8): ([]U8, []U8, BOOL)` both slices may point into `s`. With |
| | @@ -563,7 +667,7 @@ |
| 563 | 667 | #### `keep` |
| 564 | 668 | |
| 565 | 669 | ``` |
| 566 | | -proc put(m: ^Map; keep k: K; keep v: V): BOOL; |
| 670 | +proc put(var m: Map; keep k: K; keep v: V): BOOL; |
| 567 | 671 | ``` |
| 568 | 672 | |
| 569 | 673 | `keep k` says: what `k` points at may outlive the call; the procedure |
| | @@ -576,18 +680,20 @@ |
| 576 | 680 | (* import Names := Map([]U8, INT, Str.hash, Str.eq); |
| 577 | 681 | table: Names.Map and heap: Heap's allocator, module variables *) |
| 578 | 682 | proc count(name: []U8); |
| 579 | | -var key: [32]U8; |
| 683 | +var key: [32]U8; k: []U8; ok: BOOL; |
| 580 | 684 | begin |
| 581 | | - _ := Names.put(var table, keep name, 1); (* an error: name isn't keep here *) |
| 582 | | - _ := Names.put(var table, keep key[0..4], 1); (* an error: frame-bound to a keep *) |
| 583 | | - _ := Names.put(var table, keep "main", 1); (* fine: a literal never ends *) |
| 584 | | - _ := Names.put(var table, keep Str.copy(heap, name), 1); (* fine: copied to the heap *) |
| 685 | + _ := Names.put(var table, keep name, keep 1); (* an error: name isn't keep here *) |
| 686 | + _ := Names.put(var table, keep key[0..4], keep 1); (* an error: frame-bound to a keep *) |
| 687 | + _ := Names.put(var table, keep "main", keep 1); (* fine: a literal never ends *) |
| 688 | + k, ok := Str.copy(heap, name); |
| 689 | + if ok then _ := Names.put(var table, keep k, keep 1); end; (* fine: copied to the heap *) |
| 585 | 690 | end; |
| 586 | 691 | ``` |
| 587 | 692 | |
| 588 | | -`keep` is allowed on a parameter that carries a slice or is a `var` |
| 589 | | -parameter whose type holds an array; on another, `keep n: INT`, it |
| 590 | | -is an error, as `ret` is. |
| 693 | +`keep` lends something on a parameter that carries a slice, or a |
| 694 | +`var` parameter whose type holds an array; on another, `keep n: INT`, |
| 695 | +it lends nothing and is allowed, as `ret` is. At the call it is |
| 696 | +written all the same (part 1, rule 2). |
| 591 | 697 | |
| 592 | 698 | One exception keeps the common loop free of marks: a `var` parameter |
| 593 | 699 | may be given a value that points only where it already pointed, so |
| | @@ -610,12 +716,14 @@ |
| 610 | 716 | 1. `return e`, each result: `e` must not be frame-bound, and every |
| 611 | 717 | parameter it points at must be `ret`. |
| 612 | 718 | 2. A store into a place that outlives the call, `M.x := e`, |
| 613 | | - `p^.f := e`, or `v := e` for a `var` parameter `v`: `e` must not be |
| 719 | + `p^.f := e`, or `v := e` for a `var` parameter `v` or a part of it |
| 720 | + (`v.f := e`, `v[i] := e`): `e` must not be |
| 614 | 721 | frame-bound, and every parameter it points at must be `keep`, but |
| 615 | 722 | for the exception above. `COPY` into such a place is no such store: |
| 616 | 723 | it copies bytes, not where they point. |
| 617 | 724 | 3. An argument for a call's `keep` parameter, as 2. |
| 618 | | -4. A heading's marks are allowed where it says so above. |
| 725 | +4. A `const` parameter, or a part of it, is never assigned, given to |
| 726 | + a `var` parameter, nor to `ADR` (part 4). |
| 619 | 727 | |
| 620 | 728 | Checks 1 to 3 on a local variable's value wait for the procedure's |
| 621 | 729 | `end`, when everything assigned to it is known. |
| | @@ -623,15 +731,18 @@ |
| 623 | 731 | #### Procedure values, generic modules, and the rest |
| 624 | 732 | |
| 625 | 733 | **Procedure types** carry the marks, `proc(ret s: []U8): []U8`. A |
| 626 | | -procedure may be given where its type has the same marks or more, |
| 627 | | -since a mark promises the caller less: a procedure without `ret` can |
| 628 | | -stand where `ret` was allowed, not the other way. |
| 734 | +procedure with fewer `ret` and `keep` marks than a procedure type may |
| 735 | +stand where that type is wanted, since it does less than the type |
| 736 | +allows: a procedure without `ret` can stand where `ret` was allowed, |
| 737 | +not the other way. `const` and `var` must match, as `var` must now |
| 738 | +(5.9): they change how the argument is passed. |
| 629 | 739 | |
| 630 | 740 | **Generic modules.** A mark is allowed on a parameter whose type is a |
| 631 | 741 | module parameter, `keep v: T`, whatever `T` will be. The body is |
| 632 | 742 | checked in each instance: `Map(INT, INT)`'s `put` would pass without |
| 633 | 743 | `keep`, `Map([]U8, INT)`'s wouldn't, so the library marks its generic |
| 634 | | -modules for the instance that needs it. |
| 744 | +modules for the instance that needs it. Calls write the marks the |
| 745 | +heading has, in every instance (part 1, rule 2). |
| 635 | 746 | |
| 636 | 747 | **`extern` procedures** take no marks: C is outside the promise of |
| 637 | 748 | section 10 already. **Code procedures** take them, and are trusted: |
| | @@ -651,14 +762,15 @@ |
| 651 | 762 | `examples/Sh.raz` does |
| 652 | 763 | |
| 653 | 764 | ``` |
| 654 | | - Opt.init(var p, "Sh", [Opt.usage(...), Opt.option(...)]); |
| 765 | + Opt.init(var p, keep "Sh", keep [Opt.usage(...), Opt.option(...)]); |
| 655 | 766 | ``` |
| 656 | 767 | |
| 657 | | - `Opt.init` keeps its table in `p^`, so it needs `keep table`, and |
| 768 | + `Opt.init` keeps its table in `p`, so it needs `keep table`, and |
| 658 | 769 | the array literal is frame-bound: an error, though `p` is a local |
| 659 | | - and dies with the literal. `keep` can't say "as long as `p^`"; |
| 770 | + and dies with the literal. `keep` can't say "as long as `p`"; |
| 660 | 771 | saying that needs lifetimes on records, Rust's whole apparatus, which |
| 661 | | - is not proposed. The way out: the table in a module variable. |
| 772 | + is not proposed. The way out: the table a constant, which part 5 |
| 773 | + allows, as part 1's example shows. |
| 662 | 774 | - **A local variable used twice.** One slice variable that holds a |
| 663 | 775 | slice of a local buffer early on and a result to return later is |
| 664 | 776 | frame-bound throughout. Two variables cure it. |
| | @@ -670,9 +782,11 @@ |
| 670 | 782 | Every procedure, in the library or a program, that returns or keeps |
| 671 | 783 | what a parameter points at stops compiling until it is marked: a |
| 672 | 784 | thaw. In the library, by a search of the headings: `Str.trim`, |
| 673 | | -`trimLeft`, `trimRight`, `cut` and `field` (`ret`); `Map.put` and |
| 674 | | -`List.push` (`keep`); `Opt.init` (`keep`), and `Sh`'s call of it, as |
| 675 | | -above. Not marked: `Str.copy` (its result is the allocator's), |
| 785 | +`trimLeft`, `trimRight`, `cut` and `field` (`ret`); `Map.put`, |
| 786 | +`List.push`, `Vec.push` and `Vec.tryPush` (`keep`: they store a `T`); |
| 787 | +`Opt.init` (`keep name` and `keep table`) and `Opt.scope` (`keep |
| 788 | +name`), and `Sh`'s call of `init`, as above; `Sys.nextEntry` (`ret`, |
| 789 | +lending on Linux only). Not marked: `Str.copy` (its result is the allocator's), |
| 676 | 790 | `Sys.env` and `readLink` (their own buffer), `Buf.str` and |
| 677 | 791 | `Fmt.write` (they copy), `Map.get`. In sauce, `Svec.push` and `add` |
| 678 | 792 | (`keep`), and what returns a slice of its argument. |
| | @@ -694,16 +808,79 @@ |
| 694 | 808 | procedure values) take years; the narrow form here leaves record |
| 695 | 809 | lifetimes out, and so the first false alarm above, on purpose. |
| 696 | 810 | |
| 697 | | -## A `const` parameter |
| 811 | +### Part 4: a `const` parameter |
| 812 | + |
| 813 | +``` |
| 814 | +proc is*(const sc: Scanner; s: []U8): BOOL; |
| 815 | +proc text*(ret const sc: Scanner): []U8; |
| 816 | +``` |
| 817 | + |
| 818 | +A `const` parameter is the caller's variable itself, as a `var` one |
| 819 | +is, and the procedure may only read it: it may not assign it or a |
| 820 | +part of it, give it to a `var` parameter, nor take its `ADR`. Nothing |
| 821 | +is copied, whatever its size. Its argument is any expression of the |
| 822 | +parameter's type: a variable is passed in place; anything else, a |
| 823 | +call's result, a literal, an expression, goes in a temporary of the |
| 824 | +statement, and its address is passed. |
| 825 | + |
| 826 | +It is passed by its address always, so what it means is plain: if the |
| 827 | +variable changes while the procedure runs, through a `var` parameter, |
| 828 | +a pointer or another thread, the procedure sees the change. |
| 829 | +`f(var x, x)`, with `f`'s second parameter `const`, reads `x` as it now |
| 830 | +is. (Ada leaves the choice of copy or address to the compiler, and a |
| 831 | +program that depends on it is wrong; raz fixes the address, which is |
| 832 | +the cheaper and the one a reader expects.) |
| 698 | 833 | |
| 699 | | -`proc what(const x: Item): []U8;`: the callee may not change `x` (nor |
| 700 | | -take its address), so the caller may pass the address of a record or |
| 701 | | -array without a copy, and the program means the same. |
| 834 | +It is never marked at the call: the procedure neither changes nor |
| 835 | +keeps what it is given (part 1, rule 8). With it, `STANDARDS.md` says: |
| 836 | +a record or array only read is a value when it is a few words, and |
| 837 | +`const` when it is more; `var` is for what is changed. So `var` at a |
| 838 | +call always means a change, and the marks of part 1 stay true. |
| 839 | + |
| 840 | +A `const` parameter whose type holds an array lends the caller's |
| 841 | +variable to `ret`, as a `var` one does (part 3, "Words"). A procedure |
| 842 | +type says `const` where its procedure does, and two signatures are |
| 843 | +alike only if their `const`s are (5.9). A code procedure receives a |
| 844 | +`const` parameter's address in a register, as a `var` one's; `extern` |
| 845 | +procedures take none. |
| 702 | 846 | |
| 703 | 847 | Met in porting the compiler to raz, where the seed's `const x: TItem` |
| 704 | 848 | (about 130 bytes) became a value parameter, copied at every call. |
| 705 | 849 | Measured once the port built: not what stands out, the compiler's |
| 706 | | -profile is flat (`Scan.tokPos` is 9%). |
| 850 | +profile is flat (`Scan.tokPos` is 9%). Met again here, where `Opt`'s |
| 851 | +`Parser` and the tools' `Scanner`, many words, are only read by many |
| 852 | +procedures, and `var` for them would be a mark that isn't true. |
| 853 | + |
| 854 | +Costs: the report, 8 and 5.9; the compiler, 30 to 60 lines: the word |
| 855 | +in a heading, passing as `var` does, the temporary, and the checks |
| 856 | +above. The seed knows `const` parameters in Pascal, not in raz; `src/` |
| 857 | +uses them after the new seed, as it does the marks. |
| 858 | + |
| 859 | +### Part 5: a constant holds a variant |
| 860 | + |
| 861 | +Today a typed constant holds no variant (4: "no variant and no other |
| 862 | +pointer"), and `src/Parse.raz` says "a constant holds no variant". So a |
| 863 | +table of `Opt.Row`s, a variant, can't be a constant, and part 3 makes |
| 864 | +the literal table at `Opt.init`'s call an error. The plain place for |
| 865 | +such a table is a constant: |
| 866 | + |
| 867 | +``` |
| 868 | +const Rows: []Opt.Row = [ |
| 869 | + Opt.usage("", "Sh [-c script | file]"), |
| 870 | + Opt.option("", 'c', "command", "script", "run script, not standard input"), |
| 871 | + Opt.sub("", "run", "run the file named")]; |
| 872 | +``` |
| 873 | + |
| 874 | +A variant's case built from constants, `Opt.usage("", "...")`, or a |
| 875 | +case without fields, `dot`, is a constant of the variant's type, and a |
| 876 | +variant may stand among a constant's elements and fields as a record |
| 877 | +may. Its bytes are as 5.6 lays them out: the case's number, then its |
| 878 | +fields, the rest zero. Nothing else changes: a constant is still read |
| 879 | +only, and a `match` on one works as on a variable. |
| 880 | + |
| 881 | +Costs: the report, 4, one line; the compiler, the variant's branch of |
| 882 | +the constant writer in `Parse.raz`, which writes the tag and the |
| 883 | +case's fields as it writes a record's: about 20 lines. |
| 707 | 884 | |
| 708 | 885 | ## A minus sign after an operator |
| 709 | 886 | |