From c1d8e857b8708609f17d080bd7606e8b09105549 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 21:45:29 +0000 Subject: [PATCH] STANDARDS.md: a parameter by value, var or pointer; PROPOSALS.md: the library's by var A procedure takes what it reads as a value (or var, when large), what it changes and lets go as var, and only what it keeps as a pointer, so that ADR at a call means the callee keeps it. The library takes nearly everything by pointer though it keeps nothing; the proposal lists, for a thaw, what moves to var, what to a value, and what stays. --- diff --git a/CLAUDE.md b/CLAUDE.md index 01ee3d4..705aeea 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -77,7 +77,9 @@ and after, and in `doc/compiler.md`. - Style: clarity over cleverness; each unit opens with what it is, what it depends on and what it leaves out; comments say why. Match the - report's plain, short prose in documentation. + report's plain, short prose in documentation. `STANDARDS.md` says + how raz is written here: a parameter is a value, `var`, or, only + when it is kept, a pointer; no `ADR` where `var` serves. ## Where things stand diff --git a/PROPOSALS.md b/PROPOSALS.md index 60c7e1f..5b10775 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -550,3 +550,45 @@ Met in mosaico's `Mconfig`, where `MenuAction.run` and a status block's kind `run` met; the block's kind was renamed `command`. + +## The library's parameters by `var`, not by pointer + +Most of the library's procedures take what they work on as `^T`: +`Buf.str(ADR(b), s)`, `Vec.push(ADR(v), x)`, `Lock.take(ADR(l))`. None +keeps it past the call; each reads or changes it and returns. That is +what `var` is for (8), and with it the call is `Buf.str(b, s)`. As it +is, `ADR` stands at nearly every call a program makes to the library, +and says nothing: a reader can't tell the few places where a pointer +is kept from the many where it isn't. `STANDARDS.md` says how our own +code chooses; this would bring the library to it. + +The proposal, procedure by procedure, from reading each one: + +- **To `var`** (it changes what it is given): `Arena.init`, `reset`, + `done`; `Buf.init`, `room`, `add`, `str`, `spare`, `cut`, `reset`, + `done`, and the `Buf` that `Files.load`, `Files.read`, `Fmt.write`, + `Str.join` and `Str.replace` write into; every procedure of `List`; + `Lock.take`, `give`; `Map.init`, `put`, `remove`, `done`; every + procedure of `Opt` (a `Parser` is many words, so even those that + only read it); `Sys.openDir`, `nextEntry`, `closeDir`; + `Thread.lock`, `unlock`; `Vec.init`, `room`, `push`, `pop`, `add`, + `tryPush`, `tryAdd`, `resize`, `reset`, `done`. +- **To a value** (it only reads a record of a few words): `Buf.bytes`, + `Vec.all`, `Map.get`, `Map.next`, `Sys.entryKind`. +- **Staying pointers**: an allocator, `^Mem.Allocator`, everywhere + (`NEW` takes one, and `^Arena(a)` needs one); `Mem.pages`, `unmap`, + `Sys.mmap`, `munmap`, `mapFile`, which are bytes, not variables; + `Thread.spawn`'s `arg`, which is kept, and the `^Thread` it gives, + which `join` and `signal` take as the program holds it; `Rt.fail`, + which the compiler calls. + +What it costs: it breaks every program that calls these, `src/`, +rmacs, ravite, the tools and the library's own tests among them. The +mending is mechanical, `ADR(x)` to `x` at each such call, and `p` to +`p^` where a program holds the thing by pointer, and the compiler's +message for a `^T` given to a `var` can name it. The seed knows `var` +parameters, so `src/` and the library it imports can take them now. +The report doesn't change; volume 2 and `raz-lib.3` do. + +Met in rpg and rsqlite, whose `Query`, `Result` and `Stmt` went from +`^T` to `var` and to values, and whose own calls to `Buf` couldn't. diff --git a/STANDARDS.md b/STANDARDS.md new file mode 100644 index 0000000..bc93240 --- /dev/null +++ b/STANDARDS.md @@ -0,0 +1,56 @@ +# raz: standards + +How raz is written here: in `src/`, `lib/`, `tools/` and `examples/`, +and in the programs and libraries that use them (rpg, rsqlite, rmacs, +ravite, ...). The report says what a program may do; this says what we +do. New code keeps to it; old code comes to it when it is next changed, +or, for the library's interfaces, at a thaw (`PROPOSALS.md`). + +## Parameters: a value, `var`, or a pointer + +A procedure takes a variable it is given in one of three ways, by what +it does with it: + +- **It reads it**: a value, when the type is a few words at most (a + handle, a slice, a small record). A larger record or array that is + only read is `var`, since a value parameter is a copy (8; a `const` + parameter waits in `PROPOSALS.md`). +- **It changes it, and lets go of it when it returns**: `var`. +- **It keeps it**, in a record, a module variable or another thread, + past the call: a pointer, `^T`. + +So `ADR` at a call says the callee keeps what it is given. A reader +who sees `Pg.start(q, ADR(c), sql)` knows that `q` is changed and `c` +is kept, and that `c` must outlive `q`. + +``` +proc start*(var q: Query; c: ^Conn; sql: []U8); (* q changed; c kept in q *) +proc run*(q: Query; var r: Result): ERROR; (* q read; r changed *) +proc rows*(r: Result): INT; (* r read: one word *) +proc clear*(var r: Result); (* r changed *) +``` + +A pointer is right also where `var` can't serve: + +- **An allocator**, `^Mem.Allocator`: `NEW` takes one, and an + allocator such as `Arena` is reached through its first field, + `^Arena(a)`, which a `var` of exactly one type can't do. +- **What may be absent**: a pointer may be `nil`; a `var` can't. +- **What the program already holds by pointer**: what `NEW` or the + library hands out, such as `Thread.spawn`'s `^Thread`, is passed on + as it is, not as `p^`. +- **C**, and memory that is bytes, not a variable: `extern` + procedures, `Mem.pages`, `Sys.mmap`, a buffer's address. +- **A procedure value's argument** that holds what the procedure will + need, as `Thread.spawn`'s `arg: ^U8`, which is kept. + +Anything else that takes `^T` and neither keeps it nor needs one of the +above is a mistake, and its callers' `ADR`s are noise. + +## The library until a thaw + +The library's interfaces mostly take `^T` (`Buf`, `Vec`, `Map`, +`List`, `Lock`, `Opt`, ...), though none keeps what it is given. They +can't change while the freeze holds, since every caller would break; +`PROPOSALS.md` lists them for the next thaw. Until then a program +calls them with `ADR`, and its own procedures keep to this file.