Report 10.22.6: two volumes, the language (doc/raz.md) and the library (doc/raz-lib.md)
| @@ -104,7 +104,7 @@ | ||
| 104 | 104 | @rloc -c src/*.raz |
| 105 | 105 | |
| 106 | 106 | # raz-lib(3), the library's manual page, made by tools/Doc.raz from |
| 107 | -# its sources, in the order of the report's section 11 | |
| 107 | +# its sources, in the order of doc/raz-lib.md | |
| 108 | 108 | LIBDOC = Rt Mem Heap Arena Lock Thread.linux-x86_64 Out Sys.linux-x86_64 \ |
| 109 | 109 | Str Bytes.x86_64 Buf Fmt List Map Sort Math Files Opt C |
| 110 | 110 | |
| @@ -118,8 +118,8 @@ | ||
| 118 | 118 | # CI has neither |
| 119 | 119 | pdf: build/raz.pdf |
| 120 | 120 | |
| 121 | -build/raz.pdf: doc/raz.md doc/report.css | build | |
| 122 | - pandoc doc/raz.md -s --toc --toc-depth=1 --shift-heading-level-by=-1 \ | |
| 121 | +build/raz.pdf: doc/raz.md doc/raz-lib.md doc/report.css | build | |
| 122 | + pandoc doc/raz.md doc/raz-lib.md -s --toc --toc-depth=1 --shift-heading-level-by=-1 \ | |
| 123 | 123 | --syntax-highlighting=none -c ../doc/report.css \ |
| 124 | 124 | -o build/raz.html |
| 125 | 125 | chromium --headless --no-pdf-header-footer --print-to-pdf=$@ build/raz.html 2>/dev/null |
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | Work goes by milestones, each a release. A milestone says what it is |
| 4 | 4 | for, what is in it and when it is done; its steps are commits, each |
| 5 | 5 | stopping for the owner's review. A finished milestone is cut from this |
| 6 | -file: git keeps the history, and the report's section 12 the language's. | |
| 6 | +file: git keeps the history, and the report's history, `doc/report-history.md`, the language's. | |
| 7 | 7 | |
| 8 | 8 | v0.9.1, the latest release, is tagged by the owner's word. All before |
| 9 | 9 | v0.7.1 is approved by the owner; v0.7.1, v0.8.0, v0.9.0 and v0.9.1 wait |
| @@ -33,7 +33,7 @@ | ||
| 33 | 33 | |
| 34 | 34 | | Path | What | |
| 35 | 35 | |---|---| |
| 36 | -| `doc/raz.md` | The language report: the definition of raz | | |
| 36 | +| `doc/raz.md`, `doc/raz-lib.md` | The report: the language (volume 1) and the library (volume 2) | | |
| 37 | 37 | | `doc/compiler.md` | A guide to reading the compiler, file by file | |
| 38 | 38 | | `doc/raz.1`, `doc/raz.7`, `doc/raz-tutorial.7` | The manual pages: the command, the language at a glance, a tour; `make man` makes the library's, raz-lib(3), from `lib/` | |
| 39 | 39 | | `src/` | The compiler, in raz | |
| @@ -157,7 +157,7 @@ | ||
| 157 | 157 | - `extern` procedures and variables from C; code procedures written in |
| 158 | 158 | the target's assembly language; `SYSCALL`. |
| 159 | 159 | |
| 160 | -The report, `doc/raz.md`, says all of it in about 2,000 lines. | |
| 160 | +The report, `doc/raz.md` and `doc/raz-lib.md`, says all of it in about 2,100 lines. | |
| 161 | 161 | |
| 162 | 162 | ## The library |
| 163 | 163 |
| @@ -0,0 +1,618 @@ | ||
| 1 | +## The library, volume 2 | |
| 2 | + | |
| 3 | +The second volume of the report on raz. The first, `doc/raz.md`, defines | |
| 4 | +the language; this one defines the modules that come with it, and | |
| 5 | +refers to the first's sections by number, as "section 6.8". Only the | |
| 6 | +core binds every implementation (appendix A of the first volume); the | |
| 7 | +rest is the standard library. | |
| 8 | + | |
| 9 | +Modules written in raz, in the compiler's `lib` directory: | |
| 10 | + | |
| 11 | +| Module | What | | |
| 12 | +|---|---| | |
| 13 | +| `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail | | |
| 14 | +| `Sys` | system calls: `open`, `openRead`, `create`, `read`, `write`, `seek`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `symlink`, `readLink`, `info`, `openDir`, `nextEntry`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `termSize`, `ready`; the environment, `env`, `workDir`, `setWorkDir`; programs, pipes and signals; time, `clock`, `now`, `sleep`; `random`; and sockets. One file per target, and `Sys.raz` over the C library for the others | | |
| 15 | +| `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | | |
| 16 | +| `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | | |
| 17 | +| `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | | |
| 18 | +| `Thread` | threads for a program without C: `spawn`, `join`, and `Mutex`; it says `link static` | | |
| 19 | +| `Mem` | `Allocator`, the interface of every allocator, and pages from the system | | |
| 20 | +| `Heap` | a general allocator, `Heap.heap`, safe to call from several threads; freeing a block twice, or one it never gave out, stops the program | | |
| 21 | +| `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator | | |
| 22 | +| `Str` | operations on `[]U8` strings: comparing, hashing, finding, UTF-8, reading a number | | |
| 23 | +| `Bytes` | searches of bytes, many at a time: `index`, `last`, `count` and `find` in a `[]U8`; one file per target where the target has instructions for it | | |
| 24 | +| `Buf` | a growable byte buffer | | |
| 25 | +| `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])`, or into a `Buf` with `Fmt.write` | | |
| 26 | +| `List` | a generic singly linked list, `List(T)` | | |
| 27 | +| `Map` | a generic hash map, `Map(K, V; hash, eq)` | | |
| 28 | +| `Sort` | sorting a slice in place, `Sort(T; less)` | | |
| 29 | +| `Math` | roots, powers, logarithms, rounding and trigonometry, and `pi`, for `F64` | | |
| 30 | +| `Files` | an argument as a string, and a whole file read or written | | |
| 31 | +| `Opt` | a command line read through one table, by `getopt`'s rules with long options; its help and its mistakes | | |
| 32 | + | |
| 33 | +Lengths, counts, file descriptors, process ids and statuses are | |
| 34 | +`INT`s, and a character is an `I32`. What may pass an `INT` on a small | |
| 35 | +target is an `I64`: a file's size and position, a time in nanoseconds, | |
| 36 | +and a number read from text. | |
| 37 | + | |
| 38 | +What follows is each module's interface, as `tools/Doc.raz` prints it | |
| 39 | +from the module's file; `...` stands for fields that are the module's | |
| 40 | +own. A module's `main`, where it has one, is its test, and is left | |
| 41 | +out here. | |
| 42 | + | |
| 43 | +``` | |
| 44 | +module Rt; | |
| 45 | +const Full*: ERROR = "too many procedures to run at exit"; | |
| 46 | +proc atExit*(p: proc): ERROR; (* p run when the program ends by exit *) | |
| 47 | +proc exit*(status: INT); (* the atExit procedures, the last first, then the end *) | |
| 48 | +proc stop*(msg: []U8); (* msg on standard error, then exit with status 2 *) | |
| 49 | +proc trace*; (* the calls under way, and their variables, on standard error *) | |
| 50 | +``` | |
| 51 | + | |
| 52 | +`Rt` also holds what the compiler's checks call when they fail, and | |
| 53 | +the handlers of the signals that stop a program (section 10). `exit` | |
| 54 | +takes each procedure off the list before it runs it, so it runs once | |
| 55 | +even if it fails or calls `exit` itself; in a program that uses C, it | |
| 56 | +ends through C's `exit`, so that C flushes its buffers too. | |
| 57 | + | |
| 58 | +``` | |
| 59 | +module Mem; | |
| 60 | +type | |
| 61 | + Allocator* = record | |
| 62 | + alloc*: proc(a: ^Allocator; size, align: INT): ^U8; (* nil if out of room *) | |
| 63 | + free*: proc(a: ^Allocator; p: ^U8); | |
| 64 | + end; | |
| 65 | +const Page* = 4096; | |
| 66 | +proc pages*(size: INT): ^U8; (* size bytes, in whole pages, zeroed; or nil *) | |
| 67 | +proc unmap*(p: ^U8; size: INT); (* pages given back *) | |
| 68 | +proc alloc*(a: ^Allocator; size, align: INT): ^U8; (* a^.alloc(a, size, align) *) | |
| 69 | +proc free*(a: ^Allocator; p: ^U8); (* a^.free(a, p) *) | |
| 70 | + | |
| 71 | +module Heap; | |
| 72 | +var heap*: ^Mem.Allocator; | |
| 73 | + | |
| 74 | +module Arena; | |
| 75 | +type Arena* = record | |
| 76 | + base*: Mem.Allocator; (* first, so that ^Arena(a) finds the rest *) | |
| 77 | + ... | |
| 78 | +end; | |
| 79 | +proc init*(ar: ^Arena; size: INT): BOOL; (* an arena of size bytes; false if the system has none *) | |
| 80 | +proc reset*(ar: ^Arena); (* every allocation forgotten; the memory used again *) | |
| 81 | +proc done*(ar: ^Arena); (* its memory given back *) | |
| 82 | +``` | |
| 83 | + | |
| 84 | +`NEW(p, a)` calls `a^.alloc`. `Heap` is the general | |
| 85 | +allocator. An arena hands out memory from | |
| 86 | +one block: its `free` does nothing, and `reset` forgets everything. | |
| 87 | + | |
| 88 | +``` | |
| 89 | +module Lock; | |
| 90 | +type Lock* = record ... end; | |
| 91 | +proc take*(l: ^Lock); | |
| 92 | +proc give*(l: ^Lock); | |
| 93 | + | |
| 94 | +module Thread; | |
| 95 | +const Failed*: ERROR = "can't start a thread"; | |
| 96 | +type | |
| 97 | + Thread* = record ... end; | |
| 98 | + Mutex* = record ... end; (* zero is free *) | |
| 99 | +proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); (* p(arg) in a new thread *) | |
| 100 | +proc join*(t: ^Thread); (* waits for t to end, then frees its stack *) | |
| 101 | +proc lock*(m: ^Mutex); | |
| 102 | +proc unlock*(m: ^Mutex); | |
| 103 | +``` | |
| 104 | + | |
| 105 | +A `Lock` is for short sections: a thread that finds it taken tries | |
| 106 | +again, giving up the processor now and then. A `Mutex` sleeps. Each of | |
| 107 | +`Thread`'s threads runs on a stack of its own, with a page below it | |
| 108 | +that can't be touched, so that running off the stack stops the program. | |
| 109 | +A program that uses C makes its threads with C's `pthread_create`, and | |
| 110 | +so can't use `Thread`. | |
| 111 | + | |
| 112 | +``` | |
| 113 | +module Out; | |
| 114 | +proc str*(s: []U8); | |
| 115 | +proc char*(c: U8); | |
| 116 | +proc ln*; (* a line feed *) | |
| 117 | +proc int*(n: I64); (* n in decimal *) | |
| 118 | +proc hex*(n: U64; digits: INT); (* n in hexadecimal, digits digits at least *) | |
| 119 | +proc flush*; | |
| 120 | +``` | |
| 121 | + | |
| 122 | +**Out and C's stdio.** `Out` keeps a buffer of `Sys.outSize` bytes, | |
| 123 | +4 KB on Linux. It writes it out | |
| 124 | +when it is full, at each line feed when standard output is a terminal, | |
| 125 | +at `Out.flush`, and when the program ends by `Rt.exit`. A lock keeps | |
| 126 | +what each call writes together when threads write at once. C's `stdio` | |
| 127 | +keeps a buffer of its own for the same standard output. A program that | |
| 128 | +writes with both flushes one before writing with the other, or the | |
| 129 | +text comes out of order: | |
| 130 | + | |
| 131 | +``` | |
| 132 | +module Both; | |
| 133 | +import Out, C; (* C: printf, fflush and C's FILE *stdout *) | |
| 134 | + | |
| 135 | +proc main*; | |
| 136 | +begin | |
| 137 | + Out.str("from raz\n"); | |
| 138 | + Out.flush; | |
| 139 | + C.printf("from C\n"); | |
| 140 | + C.fflush(C.stdout); | |
| 141 | + Out.str("from raz again\n"); | |
| 142 | +end; | |
| 143 | + | |
| 144 | +end Both. | |
| 145 | +``` | |
| 146 | + | |
| 147 | +**Sys** gives every target the same procedures, so that a program | |
| 148 | +that needs only these runs wherever raz does: through system calls on | |
| 149 | +Linux, through the C library elsewhere. A result that can fail is a | |
| 150 | +number, minus an errno, as on Linux; 0 is success. | |
| 151 | + | |
| 152 | +``` | |
| 153 | +module Sys; | |
| 154 | +const | |
| 155 | + stdin* = 0; stdout* = 1; stderr* = 2; | |
| 156 | + outSize* = 4096; (* the bytes Out keeps; each target has its own *) | |
| 157 | + (* signals, by their numbers on Linux; each target has its own *) | |
| 158 | + sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; | |
| 159 | + sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; | |
| 160 | + sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28; | |
| 161 | + | |
| 162 | +type | |
| 163 | + Access* = (reading, writing, creating, exclusive, truncating, appending); | |
| 164 | + Accesses* = set of Access; | |
| 165 | + Info* = record | |
| 166 | + size*: I64; (* in bytes *) | |
| 167 | + mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *) | |
| 168 | + mode*: INT; (* its permission bits, as chmod takes them: 1EDH is rwxr-xr-x *) | |
| 169 | + dir*: BOOL; (* a directory *) | |
| 170 | + file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) | |
| 171 | + symlink*: BOOL; (* the path itself is a symbolic link *) | |
| 172 | + end; | |
| 173 | + Dir* = record ... end; (* an open directory; its fields are the target's *) | |
| 174 | + Program* = record | |
| 175 | + path*: []U8; | |
| 176 | + args*: [][]U8; (* the first is its name *) | |
| 177 | + env*: [][]U8; (* NAME=value, each added to this program's environment *) | |
| 178 | + onlyEnv*: BOOL; (* env is its whole environment *) | |
| 179 | + dir*: []U8; (* its working directory; "" for this program's *) | |
| 180 | + fds*: [3]INT; (* its standard input, output and error; 0 for this program's own *) | |
| 181 | + group*: INT; (* the process group it joins: 0 for this program's, -1 a new one it leads *) | |
| 182 | + foreground*: BOOL; (* its group takes the terminal on standard input *) | |
| 183 | + tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *) | |
| 184 | + end; | |
| 185 | + Status* = (running, exited, killed, stopped); | |
| 186 | + | |
| 187 | +proc write*(fd: INT; s: []U8): INT; (* the bytes written *) | |
| 188 | +proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) | |
| 189 | +proc open*(path: []U8; how: Accesses): INT; (* its fd *) | |
| 190 | +proc openRead*(path: []U8): INT; (* open(path, {reading}) *) | |
| 191 | +proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *) | |
| 192 | +proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; (* the new position *) | |
| 193 | +proc close*(fd: INT); | |
| 194 | +proc isTerminal*(fd: INT): BOOL; | |
| 195 | +proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) | |
| 196 | +proc munmap*(p: ^U8; size: INT); | |
| 197 | +proc yield*; (* the processor given to another thread *) | |
| 198 | +proc exit*(status: INT); (* the program ended at once, without Rt's atExit *) | |
| 199 | + | |
| 200 | +proc rename*(old, new: []U8): INT; (* replaces what new names *) | |
| 201 | +proc remove*(path: []U8): INT; (* a file, not a directory *) | |
| 202 | +proc removeDir*(path: []U8): INT; (* an empty directory *) | |
| 203 | +proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *) | |
| 204 | +proc chmod*(path: []U8; mode: INT): INT; | |
| 205 | +proc symlink*(target, path: []U8): INT; (* path made a link to target *) | |
| 206 | +proc readLink*(path: []U8): ([]U8, INT); (* what the link names, good until the next call *) | |
| 207 | +proc info*(path: []U8): (Info, INT); (* following a symbolic link *) | |
| 208 | +proc space*(path: []U8): (I64, I64, INT); (* the bytes free and in all on path's file system *) | |
| 209 | + | |
| 210 | +proc openDir*(d: ^Dir; path: []U8): INT; | |
| 211 | +proc nextEntry*(d: ^Dir): ([]U8, BOOL); (* a name, good until the next call; false at the end *) | |
| 212 | +proc closeDir*(d: ^Dir); | |
| 213 | + | |
| 214 | +proc rawMode*(fd: INT): BOOL; (* the terminal on fd: no echo, no lines, no signals *) | |
| 215 | +proc restoreMode*(fd: INT); (* as rawMode found it *) | |
| 216 | +proc termSize*(fd: INT): (INT, INT); (* columns and rows; 80 and 24 if unknown *) | |
| 217 | +proc ready*(fd, ms: INT): BOOL; (* something to read within ms, or ever if ms < 0 *) | |
| 218 | +proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *) | |
| 219 | +proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *) | |
| 220 | +proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *) | |
| 221 | + | |
| 222 | +proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) | |
| 223 | +proc workDir*: ([]U8, INT); (* the working directory, good until the next call *) | |
| 224 | +proc setWorkDir*(path: []U8): INT; | |
| 225 | + | |
| 226 | +proc pipe*: (INT, INT); (* the fd to read and the fd to write; or minus an errno and -1 *) | |
| 227 | +proc openPty*: INT; (* a pseudo-terminal: the fd of its controlling side *) | |
| 228 | +proc setTermSize*(fd, cols, rows: INT): INT; (* the size its programs are told of *) | |
| 229 | +proc spawn*(p: Program): INT; (* a program started: its process id *) | |
| 230 | +proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *) | |
| 231 | +proc kill*(pid, signal: INT): INT; | |
| 232 | +proc pid*: INT; (* this program's process id *) | |
| 233 | +proc setGroup*(pid, group: INT): INT; (* pid 0 is this program; group 0 a new one pid leads *) | |
| 234 | +proc foreground*(fd, group: INT): INT; (* the terminal on fd gives its keys to group *) | |
| 235 | +proc foregroundOf*(fd: INT): INT; (* the group the terminal on fd gives its keys to *) | |
| 236 | + | |
| 237 | +proc catch*(sigs: []INT): INT; (* sigs caught: an fd to wait on *) | |
| 238 | +proc caught*(fd: INT): INT; (* the next signal caught, or 0 if none is waiting *) | |
| 239 | +proc ignore*(sig: INT; yes: BOOL): INT; (* sig ignored; or, if not yes, back to what it does at first *) | |
| 240 | + | |
| 241 | +proc clock*: I64; (* nanoseconds, on a clock that only goes forward *) | |
| 242 | +proc now*: I64; (* nanoseconds since 1970-01-01, UTC *) | |
| 243 | +proc sleep*(ms: INT); | |
| 244 | +proc random*(b: []U8); (* b filled with random bytes *) | |
| 245 | + | |
| 246 | +proc connect*(path: []U8): INT; (* a stream socket, connected to the Unix socket at path: its fd *) | |
| 247 | +proc connectTcp*(addr: []U8; port: INT): INT; (* addr an IPv4 or IPv6 address, as numbers *) | |
| 248 | +proc connectUdp*(addr: []U8; port: INT): INT; | |
| 249 | +proc listen*(path: []U8): INT; (* a Unix socket made at path, taking connections *) | |
| 250 | +proc listenTcp*(addr: []U8; port: INT): INT; | |
| 251 | +proc accept*(fd: INT): INT; (* the next connection, waiting for one *) | |
| 252 | +proc send*(fd: INT; s: []U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *) | |
| 253 | +``` | |
| 254 | + | |
| 255 | +`info` follows a symbolic link: `size`, `dir` and `file` are those of | |
| 256 | +what it names, and `symlink` says the path was a link, so that a program walking | |
| 257 | +a tree can choose not to follow one back up it. A link to nothing is | |
| 258 | +`-ENOENT`, as a missing file is. `file` lets a walk open only what | |
| 259 | +holds data: opening a pipe would wait for a program to write to it. | |
| 260 | + | |
| 261 | +`nextEntry` gives every name in the directory except `.` and `..`, in | |
| 262 | +no particular order. `rawMode` turns off what the terminal does with | |
| 263 | +keys before the program reads them (echo, line editing, signals, flow | |
| 264 | +control), and NL to CR NL on output. A program that turns it on | |
| 265 | +restores it before it ends: `Rt.atExit` is the place. | |
| 266 | + | |
| 267 | +**Files.** `open` gives a file for what `how` asks: `reading`, | |
| 268 | +`writing` or both; `creating` it if it is missing, readable by all and | |
| 269 | +writable by its owner; with `exclusive`, failing with -17 (EEXIST) if | |
| 270 | +it is there, as a program does that makes a lock or a file of its | |
| 271 | +own; `truncating` it to nothing; `appending`, each write going to its | |
| 272 | +end, however other programs write it too. `seek` moves to `pos` from | |
| 273 | +the start of the file, or from its end if `fromEnd`, where a negative | |
| 274 | +`pos` reads back from the end. `Info.mtime` is what a program checks | |
| 275 | +to see whether a file changed under it; `Info.mode` holds the nine | |
| 276 | +bits of `rwxrwxrwx`, and `chmod` sets them, 1EDH (755) for a program, | |
| 277 | +1A4H (644) for data. | |
| 278 | + | |
| 279 | +**Programs.** `openPty` makes a pseudo-terminal, a terminal that a | |
| 280 | +program, a terminal emulator, plays the screen and the keyboard of: | |
| 281 | +what it writes to the fd is typed, and what the programs on the | |
| 282 | +terminal write it reads. | |
| 283 | + | |
| 284 | +`spawn` starts the program at `p.path`, with `p.args` as its | |
| 285 | +arguments, the first being its name. Its environment is this | |
| 286 | +program's with each `NAME=value` of `p.env` added or put in place of | |
| 287 | +the old value, or, if `p.onlyEnv`, `p.env` alone. It starts in | |
| 288 | +`p.dir`. Its standard input, output and error are `p.fds`; a 0 gives | |
| 289 | +it this program's own, so `{path: "/bin/ls", args: ["ls"]}` runs | |
| 290 | +`ls` as a shell runs it, with every other field zero. It gets no | |
| 291 | +other open file, every signal does what it does at first, and none | |
| 292 | +is blocked. If `p.tty` is a pseudo-terminal's fd, the program starts a | |
| 293 | +session on that terminal: it is its controlling terminal, and its | |
| 294 | +standard input, output and error, and `p.fds` and `p.group` are not | |
| 295 | +used. | |
| 296 | + | |
| 297 | +`p.group` puts the program in a process group, the programs that a | |
| 298 | +shell's job is made of: -1 makes it the first of a new group, whose | |
| 299 | +number is its process id, and the other programs of the job join that | |
| 300 | +number. The terminal gives its keys, and the signals of Ctrl-C and | |
| 301 | +Ctrl-Z, to one group, the one in its foreground: `p.foreground` puts | |
| 302 | +the program's group there before it starts, and `foreground` puts any | |
| 303 | +group there, as a shell does to take the terminal back. `setGroup` | |
| 304 | +moves a program to a group, as the shell does for each program it | |
| 305 | +starts, for whichever of the two runs first; a shell runs in a group | |
| 306 | +of its own, `setGroup(0, 0)`. | |
| 307 | + | |
| 308 | +`wait` waits for the program `pid` to change, or for any of this | |
| 309 | +program's if `pid` is -1, and says which one, and how: `exited`, with | |
| 310 | +its exit status; `killed`, with the number of the signal that ended | |
| 311 | +it; or `stopped`, by Ctrl-Z or a signal, with the signal's number. | |
| 312 | +Without `block` it doesn't wait, and is 0 and `running` if no program | |
| 313 | +changed. A program that has nothing to wait for is -10 (ECHILD). | |
| 314 | +`kill` sends a program a signal, `sigTerm` to end it; to a group, if | |
| 315 | +`pid` is minus its number. | |
| 316 | + | |
| 317 | +**Signals.** `catch` takes the signals `sigs` away from what they | |
| 318 | +would do, ending the program most of them, and gives an fd that is | |
| 319 | +ready to read when one comes, so that a program waits for signals with | |
| 320 | +`ready` and `readyOf` as it waits for keys or a server. `caught` gives | |
| 321 | +each signal that came, in turn. A later `catch` adds to the signals | |
| 322 | +caught and gives the same fd. Catching `sigKill` or `sigStop`, or the | |
| 323 | +signals `Rt` reports, SIGSEGV and SIGFPE, is -22 (EINVAL). `ignore` | |
| 324 | +makes a signal do nothing, as a shell ignores `sigTtou` so that it can | |
| 325 | +take its terminal back, and `ignore(sig, false)` gives it back what it | |
| 326 | +does at first. A program that `spawn` starts begins with neither. | |
| 327 | + | |
| 328 | +**Time.** `clock` is for measuring how long something takes: it never | |
| 329 | +goes back, and says nothing of the date. `now` is the date and time, | |
| 330 | +which a person or the network may set back. `sleep` waits `ms` | |
| 331 | +milliseconds. | |
| 332 | + | |
| 333 | +**Sockets.** `connect` is for talking to a server on the same machine, | |
| 334 | +an X server at `/tmp/.X11-unix/X0` say; `connectTcp` for one across | |
| 335 | +the network, given its address as numbers, `"127.0.0.1"` or `"::1"`: | |
| 336 | +turning a name into an address is a library's work over these. | |
| 337 | +`connectUdp` gives a socket of datagrams, each `send` one message to | |
| 338 | +that address and each `read` one message from it, as a program asks a | |
| 339 | +name server. What a socket receives comes by `read`, what it sends goes | |
| 340 | +by `send`, and `close` ends it. `send` is `write` for a socket, except | |
| 341 | +when the other end has closed it: `write` would stop the program with | |
| 342 | +the signal SIGPIPE, where `send` returns -32 (EPIPE), so that a | |
| 343 | +program learns its server has gone and can end as it wants. | |
| 344 | +`listen` and `listenTcp` are the other side: a server's socket, on | |
| 345 | +`path`, which must not be there yet, or on `port` at `addr`, | |
| 346 | +`"0.0.0.0"` or `"::"` for every address the machine has; `accept` | |
| 347 | +gives each connection that comes, as a socket of its own. | |
| 348 | + | |
| 349 | +`readyOf` waits on several file descriptors at once, as a program does | |
| 350 | +that listens to a server and a terminal both; a descriptor at its end, | |
| 351 | +or failed, counts as ready, since a read of it won't wait. It looks at | |
| 352 | +the first 64 of `fds`, and `connect` and `listen` take a path of at | |
| 353 | +most 107 bytes, as `struct sockaddr_un` holds; a longer one is -36 | |
| 354 | +(ENAMETOOLONG). | |
| 355 | + | |
| 356 | +A program that both reads from a descriptor and writes much to it, as | |
| 357 | +a terminal writes what is pasted to its program while it reads what | |
| 358 | +the program writes back, must not wait on either: the program may be | |
| 359 | +waiting for its own writes to be read. `setBlocking(fd, false)` has a | |
| 360 | +read or a write on `fd` that would wait return -11 (EAGAIN) at once, | |
| 361 | +a write having written what there was room for; `readyFor` waits, as | |
| 362 | +`readyOf` does, for something to read on any of `reads` or for room to | |
| 363 | +write on any of `writes`. A descriptor in both is given once, when it | |
| 364 | +is ready for either; the 64 it looks at are `reads`, then `writes`. | |
| 365 | + | |
| 366 | +**Strings, buffers and collections.** | |
| 367 | + | |
| 368 | +``` | |
| 369 | +module Str; | |
| 370 | +proc cmp*(a, b: []U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *) | |
| 371 | +proc eq*(a, b: []U8): BOOL; | |
| 372 | +proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); (* s's bytes in memory from a; false if a has none *) | |
| 373 | +proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *) | |
| 374 | +proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *) | |
| 375 | +proc hasPrefix*(s, p: []U8): BOOL; | |
| 376 | +proc encode*(c: I32): ([4]U8, INT); (* c in UTF-8: its bytes, and how many *) | |
| 377 | +proc decode*(s: []U8): (I32, INT); (* the character s starts with, and its length in bytes *) | |
| 378 | +proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *) | |
| 379 | +proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *) | |
| 380 | +proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *) | |
| 381 | +proc putLE*(s: []U8; v: U64); (* v into s, the lowest byte first *) | |
| 382 | +proc putBE*(s: []U8; v: U64); (* v into s, the highest byte first *) | |
| 383 | + | |
| 384 | +module Buf; | |
| 385 | +type Buf* = record len*: INT; ... end; | |
| 386 | +proc init*(b: ^Buf; a: ^Mem.Allocator); | |
| 387 | +proc room*(b: ^Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *) | |
| 388 | +proc add*(b: ^Buf; c: U8); | |
| 389 | +proc str*(b: ^Buf; s: []U8); | |
| 390 | +proc bytes*(b: ^Buf): []U8; (* what it holds, until it next grows *) | |
| 391 | +proc reset*(b: ^Buf); (* emptied, its memory kept *) | |
| 392 | +proc done*(b: ^Buf); (* its memory given back *) | |
| 393 | + | |
| 394 | +module Fmt; | |
| 395 | +type Arg* = variant | |
| 396 | + int(i: I64); | |
| 397 | + uint(u: U64); | |
| 398 | + hex(h: U64); | |
| 399 | + str(s: []U8); | |
| 400 | + char(c: U8); | |
| 401 | + bool(b: BOOL); | |
| 402 | + real(r: F64; places: INT); | |
| 403 | +end; | |
| 404 | +proc print*(f: []U8; args: []Arg); (* to standard output *) | |
| 405 | +proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); (* added to b *) | |
| 406 | + | |
| 407 | +module List(T); | |
| 408 | +type | |
| 409 | + Node* = record next*: ^Node; val*: T; end; | |
| 410 | + List* = record head*: ^Node; len*: INT; ... end; | |
| 411 | +proc init*(l: ^List; a: ^Mem.Allocator); | |
| 412 | +proc push*(l: ^List; v: T): BOOL; (* v at the front; false if the allocator has no room *) | |
| 413 | +proc pop*(l: ^List): (T, BOOL); (* the front value, taken off; false if the list is empty *) | |
| 414 | +proc reverse*(l: ^List); | |
| 415 | +proc clear*(l: ^List); | |
| 416 | + | |
| 417 | +module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); | |
| 418 | +type Map* = record len*: INT; ... end; | |
| 419 | +proc init*(m: ^Map; a: ^Mem.Allocator); | |
| 420 | +proc put*(m: ^Map; k: K; v: V): BOOL; (* k now maps to v; false if there was no room *) | |
| 421 | +proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn't there *) | |
| 422 | +proc get*(m: ^Map; k: K): (V, BOOL); (* what k maps to, and whether it maps to anything *) | |
| 423 | +proc next*(m: ^Map; i: INT): (INT, K, V); (* the entry after i *) | |
| 424 | + | |
| 425 | +module Sort(T; less: proc(a, b: T): BOOL); | |
| 426 | +proc sort*(s: []T); | |
| 427 | + | |
| 428 | +module Math; | |
| 429 | +const pi* = 3.141592653589793; | |
| 430 | +proc sqrt*(x: F64): F64; | |
| 431 | +proc pow*(x, y: F64): F64; (* x to the power y *) | |
| 432 | +proc floor*(x: F64): F64; | |
| 433 | +proc ceil*(x: F64): F64; | |
| 434 | +proc round*(x: F64): F64; (* to the nearest whole number; a half away from 0 *) | |
| 435 | +proc exp*(x: F64): F64; | |
| 436 | +proc ln*(x: F64): F64; | |
| 437 | +proc log2*(x: F64): F64; | |
| 438 | +proc log10*(x: F64): F64; | |
| 439 | +proc sin*(x: F64): F64; | |
| 440 | +proc cos*(x: F64): F64; | |
| 441 | +proc tan*(x: F64): F64; | |
| 442 | +proc asin*(x: F64): F64; (* from -pi/2 to pi/2 *) | |
| 443 | +proc acos*(x: F64): F64; (* from 0 to pi *) | |
| 444 | +proc atan*(x: F64): F64; (* from -pi/2 to pi/2 *) | |
| 445 | +proc atan2*(y, x: F64): F64; (* the angle of (x, y) from the x axis, from -pi to pi *) | |
| 446 | +``` | |
| 447 | + | |
| 448 | +A `Buf`, a `List` and a `Map` take their memory from the allocator | |
| 449 | +`init` gives them. `Fmt`'s format takes the next argument at each | |
| 450 | +`{}`; `{{` and `}}` stand for `{` and `}`. A `Map` hashes and | |
| 451 | +compares its keys with the procedures it is given: | |
| 452 | +`import Count := Map([]U8, INT, Str.hash, Str.eq);`. | |
| 453 | + | |
| 454 | +`Str` reads and writes UTF-8. `encode` writes a character, of at most | |
| 455 | +10FFFFH, in one to four bytes; a number that isn't one becomes FFFDH, | |
| 456 | +the character that stands for a wrong one. `decode` reads the first | |
| 457 | +character of `s`, and gives FFFDH and 1 for a byte that doesn't start | |
| 458 | +one rightly, and 0 and 0 for an empty `s`. `toInt` reads a decimal | |
| 459 | +number, with a `-` or `+` before it if any, and nothing else. | |
| 460 | + | |
| 461 | +`le` and `be` read an unsigned number of up to 8 bytes in a fixed | |
| 462 | +order, little-endian or big-endian, whatever the machine's own, and | |
| 463 | +`putLE` and `putBE` write one; more than 8 bytes stops the program. | |
| 464 | +They are how a file format or a network protocol is read and written: | |
| 465 | + | |
| 466 | +``` | |
| 467 | +len := U32(Str.le(header[4..8])); | |
| 468 | +Str.putBE(packet[0..2], U64(port)); | |
| 469 | +``` | |
| 470 | + | |
| 471 | +`Map.next` walks a map: from 0, it gives the next entry and the number | |
| 472 | +to pass for the one after it, and -1 when there are no more. A walk | |
| 473 | +sees each entry once if the map doesn't change during it: | |
| 474 | + | |
| 475 | +``` | |
| 476 | +i, k, v := Names.next(ADR(names), 0); | |
| 477 | +while i >= 0 do | |
| 478 | + Fmt.print("{} is {}\n", [Fmt.str(k), Fmt.int(v)]); | |
| 479 | + i, k, v := Names.next(ADR(names), i); | |
| 480 | +end; | |
| 481 | +``` | |
| 482 | + | |
| 483 | +`Sort.sort` puts `s` in the order `less` gives, in place, without | |
| 484 | +memory of its own, in time `n log n` at most; two elements equal by | |
| 485 | +`less` may change places. `Math`'s procedures are as C's `math.h` | |
| 486 | +has them, to within the last bit or so. `Fmt.real(x, places)` writes | |
| 487 | +`x` with `places` digits after the point, at most 9, rounded to the | |
| 488 | +nearest; from 1e15 on, with an exponent, as `1.50e17`; and `nan`, | |
| 489 | +`inf` and `-inf` as themselves. | |
| 490 | + | |
| 491 | +**Files and Opt** are what most programs need of their command line. | |
| 492 | + | |
| 493 | +``` | |
| 494 | +module Files; | |
| 495 | +proc arg*(i: INT): []U8; (* argument i; 0 is the program's name *) | |
| 496 | +proc read*(path: []U8; b: ^Buf.Buf): BOOL; (* the whole file added to b *) | |
| 497 | +proc write*(path, data: []U8): BOOL; (* the file made to hold data *) | |
| 498 | + | |
| 499 | +module Opt; | |
| 500 | +type | |
| 501 | + Row* = variant | |
| 502 | + usage(scope, line: []U8); (* a synopsis line, without "usage: " *) | |
| 503 | + sub(scope, name, help: []U8); (* a subcommand *) | |
| 504 | + option(scope: []U8; letter: U8; long, value, help: []U8); | |
| 505 | + end; | |
| 506 | + Parser* = record | |
| 507 | + letter*: U8; (* the option just read *) | |
| 508 | + value*: []U8; (* its value; "" for a flag *) | |
| 509 | + long*: BOOL; (* it was written by its long name *) | |
| 510 | + ... | |
| 511 | + end; | |
| 512 | +proc init*(p: ^Parser; name: []U8; table: []Row); (* the arguments from 1 on, by table *) | |
| 513 | +proc next*(p: ^Parser): BOOL; (* the next option; false when they end *) | |
| 514 | +proc operand*(p: ^Parser): ([]U8, BOOL); (* the next operand; false when there are no more *) | |
| 515 | +proc scope*(p: ^Parser; name: []U8); (* subcommand name's options, from the word after it *) | |
| 516 | +proc number*(p: ^Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *) | |
| 517 | +proc fail*(p: ^Parser; text: []U8); (* the program's own mistake *) | |
| 518 | +``` | |
| 519 | + | |
| 520 | +`Opt` reads a command line by the rules of the C library's `getopt`, | |
| 521 | +with GNU's long options, so that a program's options behave as every | |
| 522 | +other Unix program's. The program describes itself once, in a table of | |
| 523 | +`Row`s: its synopsis lines, its subcommands and its options. The help | |
| 524 | +and the messages for mistakes are made from the table, so they can't | |
| 525 | +drift from it; the program's test of `p.letter` is the only other | |
| 526 | +place an option appears. The parser is a record the program owns, and | |
| 527 | +the table a slice it keeps as long as the parser: a literal written in | |
| 528 | +the call to `init` lives as long as the procedure that makes it. | |
| 529 | + | |
| 530 | +A row's `scope` is `""` for the program itself, or a subcommand's | |
| 531 | +name: `"start"`, or `"remote.add"` for a subcommand of `remote`. An | |
| 532 | +option has a `letter`, a `long` name, or both. One with a long name | |
| 533 | +only has a key of its own below `'!'` for a letter, so that the | |
| 534 | +program can tell it from the others. With a `value`, the name the | |
| 535 | +help and the messages give it, the option takes a value; with `""` it | |
| 536 | +is a flag. | |
| 537 | + | |
| 538 | +``` | |
| 539 | +const wait = 1; (* the key of --wait, which has no letter *) | |
| 540 | +... | |
| 541 | +Opt.init(ADR(p), "keep", [ | |
| 542 | + Opt.usage("", "keep [-v] [--wait secs] start ..."), | |
| 543 | + Opt.sub("", "start", "start a job"), | |
| 544 | + Opt.option("", 'v', "verbose", "", "say more"), | |
| 545 | + Opt.option("", wait, "wait", "secs", "give the job secs to start"), | |
| 546 | + Opt.usage("start", "keep start [-b label] name command [argument ...]"), | |
| 547 | + Opt.option("start", 'b', "bar", "label", "show the job in the bar as label")]); | |
| 548 | +while Opt.next(ADR(p)) do | |
| 549 | + if p.letter = 'v' then verbose := true; | |
| 550 | + elsif p.letter = wait then secs := Opt.number(ADR(p), 1, 600); | |
| 551 | + end; | |
| 552 | +end; | |
| 553 | +w, ok := Opt.operand(ADR(p)); | |
| 554 | +if not ok or (w # "start") then Opt.fail(ADR(p), "no command; see keep -h"); end; | |
| 555 | +Opt.scope(ADR(p), "start"); | |
| 556 | +while Opt.next(ADR(p)) do | |
| 557 | + if p.letter = 'b' then bar := p.value; end; | |
| 558 | +end; | |
| 559 | +job, ok := Opt.operand(ADR(p)); | |
| 560 | +``` | |
| 561 | + | |
| 562 | +The rules are `getopt`'s: | |
| 563 | + | |
| 564 | +- Flags may share a dash: `-lw` is `-l -w`. A letter's value is the | |
| 565 | + rest of its word, `-bdev`, or the whole next word, `-b dev`, even one | |
| 566 | + that starts with a dash or is empty; in a group, the first letter | |
| 567 | + that takes a value takes the rest, `-qn12`. | |
| 568 | +- A long option is written in full, with its value after a `=` or in | |
| 569 | + the next word: `--bar=dev`, `--bar dev`. An abbreviation is unknown, | |
| 570 | + not guessed, so that adding an option never breaks a script. A value | |
| 571 | + given to a flag, `--follow=x`, is a mistake. | |
| 572 | +- `--` ends the options, and isn't an operand; `-` alone is one. The | |
| 573 | + options end at the first operand, so in `keep start site python -m | |
| 574 | + http.server` the `-m` is python's. | |
| 575 | +- Each scope has its own options. An option given twice is read twice. | |
| 576 | + | |
| 577 | +`number` reads the value as `Str.toInt` does. `operand` reads what | |
| 578 | +options are left first, and the program doesn't see them. | |
| 579 | + | |
| 580 | +`-h` or `--help` among a scope's options prints its help on standard | |
| 581 | +output and ends the program with status 0. `init` and `scope` answer | |
| 582 | +it, looking through the scope's options before `next` gives one, so | |
| 583 | +that the program has done nothing yet: `keep -v -h -x` prints the | |
| 584 | +help, and the program never sees `-v`, nor the mistake. The help is | |
| 585 | +the scope's synopsis lines, a blank line, and its subcommands, its | |
| 586 | +options and the help itself, each with its line of help: | |
| 587 | + | |
| 588 | +``` | |
| 589 | +usage: keep start [-b label] name command [argument ...] | |
| 590 | + | |
| 591 | + -b, --bar label show the job in the bar as label | |
| 592 | + -h, --help print this and exit | |
| 593 | +``` | |
| 594 | + | |
| 595 | +A scope with several synopsis lines has the others indented under the | |
| 596 | +first; one with none, `usage:` and its words, as `usage: keep start`. | |
| 597 | + | |
| 598 | +A mistake on the command line is a sentence on standard error, the | |
| 599 | +program's name first, then the scope's synopsis lines, as the help | |
| 600 | +begins, and status 2: | |
| 601 | + | |
| 602 | +``` | |
| 603 | +keep: unknown option -x | |
| 604 | +usage: keep start [-b label] name command [argument ...] | |
| 605 | +``` | |
| 606 | + | |
| 607 | +The sentences are `unknown option -x` (or `--name`, up to its `=`), | |
| 608 | +`-b needs a label`, `--follow takes no value` and `--wait wants a | |
| 609 | +number from 1 to 600, not '0'`. `fail` writes the program's own | |
| 610 | +mistakes the same way, but alone, so that the sentence can say where | |
| 611 | +the help is: `keep: no command; see keep -h`. | |
| 612 | + | |
| 613 | +A mistake in the table is the program's, not its user's. `init` checks | |
| 614 | +the table before it reads a word, and ends the program with status 1: | |
| 615 | +`keep: option table, row 3: the option is in the scope already`. The | |
| 616 | +others are an option with neither a letter nor a long name, a long | |
| 617 | +name with `=` in it, a letter `-`, and `-h` or `--help` in the table, | |
| 618 | +since they are `Opt`'s. |
| @@ -6,10 +6,14 @@ | ||
| 6 | 6 | make system calls and call C libraries. It has no garbage collector and |
| 7 | 7 | no hidden run time: what the program does is what its text says. |
| 8 | 8 | |
| 9 | +This is the first volume of the report: the language, what an | |
| 10 | +implementation chooses, and the grammar. The second, `doc/raz-lib.md`, | |
| 11 | +is the library, which sections 3 and 10 and appendix A name. | |
| 12 | + | |
| 9 | 13 | This report defines the language. It is short on purpose. Whatever it |
| 10 | 14 | doesn't allow, raz doesn't have. |
| 11 | 15 | |
| 12 | -This is version 2026-10.21 of the report; section 12 says where each | |
| 16 | +This is version 2026-10.21 of the report; section 11 says where each | |
| 13 | 17 | version's changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, |
| 14 | 18 | for a language anyone can build again. They break programs written |
| 15 | 19 | for 2026-10.10 and before: for 2026-10.9, say, the report of compiler |
| @@ -233,7 +237,7 @@ | ||
| 233 | 237 | |
| 234 | 238 | **Names are flat.** The names of a program's modules are one space, |
| 235 | 239 | with no packages or paths. So the names of the library's modules |
| 236 | -(section 11) are kept for it: a program or another library doesn't | |
| 240 | +(the library, `doc/raz-lib.md`) are kept for it: a program or another library doesn't | |
| 237 | 241 | name a module of its own `Arena`, `Buf`, `C`, `Files`, `Fmt`, `Heap`, |
| 238 | 242 | `List`, `Lock`, `Map`, `Math`, `Mem`, `Opt`, `Out`, `Rt`, `Sort`, |
| 239 | 243 | `Str`, `Sys` or `Thread`, nor any name the library takes later. The |
| @@ -447,7 +451,7 @@ | ||
| 447 | 451 | The bytes of a value of more than one byte are in the target's order |
| 448 | 452 | (appendix A). A file or a message in an order of its own is read and |
| 449 | 453 | written a byte at a time, as `Str.le`, `Str.be`, `Str.putLE` and |
| 450 | -`Str.putBE` do (section 11). | |
| 454 | +`Str.putBE` do (the library, `doc/raz-lib.md`). | |
| 451 | 455 | |
| 452 | 456 | ### 5.6 Variants |
| 453 | 457 | |
| @@ -795,7 +799,7 @@ | ||
| 795 | 799 | |
| 796 | 800 | `NEW(p, a)` calls `a^.alloc(a, SIZE(T), A)`, where `A` is the |
| 797 | 801 | alignment of `T`, and zeroes the bytes it gets if they aren't `nil`. |
| 798 | -`Mem.Allocator` (section 11) is the one type of the library the | |
| 802 | +`Mem.Allocator` (the library, `doc/raz-lib.md`) is the one type of the library the | |
| 799 | 803 | language names. |
| 800 | 804 | |
| 801 | 805 | `COPY` is a statement, as `INC` is. It copies the bytes of `LEN(s)` |
| @@ -1326,622 +1330,9 @@ | ||
| 1326 | 1330 | valid, and an implementation may warn of it. The text of its messages, |
| 1327 | 1331 | how many it reports and which warnings it gives are its own (appendix |
| 1328 | 1332 | A). |
| 1329 | - | |
| 1330 | -## 11. The library | |
| 1331 | - | |
| 1332 | -Modules written in raz, in the compiler's `lib` directory: | |
| 1333 | - | |
| 1334 | -| Module | What | | |
| 1335 | -|---|---| | |
| 1336 | -| `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail | | |
| 1337 | -| `Sys` | system calls: `open`, `openRead`, `create`, `read`, `write`, `seek`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `symlink`, `readLink`, `info`, `openDir`, `nextEntry`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `termSize`, `ready`; the environment, `env`, `workDir`, `setWorkDir`; programs, pipes and signals; time, `clock`, `now`, `sleep`; `random`; and sockets. One file per target, and `Sys.raz` over the C library for the others | | |
| 1338 | -| `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | | |
| 1339 | -| `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | | |
| 1340 | -| `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | | |
| 1341 | -| `Thread` | threads for a program without C: `spawn`, `join`, and `Mutex`; it says `link static` | | |
| 1342 | -| `Mem` | `Allocator`, the interface of every allocator, and pages from the system | | |
| 1343 | -| `Heap` | a general allocator, `Heap.heap`, safe to call from several threads; freeing a block twice, or one it never gave out, stops the program | | |
| 1344 | -| `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator | | |
| 1345 | -| `Str` | operations on `[]U8` strings: comparing, hashing, finding, UTF-8, reading a number | | |
| 1346 | -| `Bytes` | searches of bytes, many at a time: `index`, `last`, `count` and `find` in a `[]U8`; one file per target where the target has instructions for it | | |
| 1347 | -| `Buf` | a growable byte buffer | | |
| 1348 | -| `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])`, or into a `Buf` with `Fmt.write` | | |
| 1349 | -| `List` | a generic singly linked list, `List(T)` | | |
| 1350 | -| `Map` | a generic hash map, `Map(K, V; hash, eq)` | | |
| 1351 | -| `Sort` | sorting a slice in place, `Sort(T; less)` | | |
| 1352 | -| `Math` | roots, powers, logarithms, rounding and trigonometry, and `pi`, for `F64` | | |
| 1353 | -| `Files` | an argument as a string, and a whole file read or written | | |
| 1354 | -| `Opt` | a command line read through one table, by `getopt`'s rules with long options; its help and its mistakes | | |
| 1355 | - | |
| 1356 | -Lengths, counts, file descriptors, process ids and statuses are | |
| 1357 | -`INT`s, and a character is an `I32`. What may pass an `INT` on a small | |
| 1358 | -target is an `I64`: a file's size and position, a time in nanoseconds, | |
| 1359 | -and a number read from text. | |
| 1360 | - | |
| 1361 | -What follows is each module's interface, as `tools/Doc.raz` prints it | |
| 1362 | -from the module's file; `...` stands for fields that are the module's | |
| 1363 | -own. A module's `main`, where it has one, is its test, and is left | |
| 1364 | -out here. | |
| 1365 | - | |
| 1366 | -``` | |
| 1367 | -module Rt; | |
| 1368 | -const Full*: ERROR = "too many procedures to run at exit"; | |
| 1369 | -proc atExit*(p: proc): ERROR; (* p run when the program ends by exit *) | |
| 1370 | -proc exit*(status: INT); (* the atExit procedures, the last first, then the end *) | |
| 1371 | -proc stop*(msg: []U8); (* msg on standard error, then exit with status 2 *) | |
| 1372 | -proc trace*; (* the calls under way, and their variables, on standard error *) | |
| 1373 | -``` | |
| 1374 | - | |
| 1375 | -`Rt` also holds what the compiler's checks call when they fail, and | |
| 1376 | -the handlers of the signals that stop a program (section 10). `exit` | |
| 1377 | -takes each procedure off the list before it runs it, so it runs once | |
| 1378 | -even if it fails or calls `exit` itself; in a program that uses C, it | |
| 1379 | -ends through C's `exit`, so that C flushes its buffers too. | |
| 1380 | - | |
| 1381 | -``` | |
| 1382 | -module Mem; | |
| 1383 | -type | |
| 1384 | - Allocator* = record | |
| 1385 | - alloc*: proc(a: ^Allocator; size, align: INT): ^U8; (* nil if out of room *) | |
| 1386 | - free*: proc(a: ^Allocator; p: ^U8); | |
| 1387 | - end; | |
| 1388 | -const Page* = 4096; | |
| 1389 | -proc pages*(size: INT): ^U8; (* size bytes, in whole pages, zeroed; or nil *) | |
| 1390 | -proc unmap*(p: ^U8; size: INT); (* pages given back *) | |
| 1391 | -proc alloc*(a: ^Allocator; size, align: INT): ^U8; (* a^.alloc(a, size, align) *) | |
| 1392 | -proc free*(a: ^Allocator; p: ^U8); (* a^.free(a, p) *) | |
| 1393 | - | |
| 1394 | -module Heap; | |
| 1395 | -var heap*: ^Mem.Allocator; | |
| 1396 | 1333 | |
| 1397 | -module Arena; | |
| 1398 | -type Arena* = record | |
| 1399 | - base*: Mem.Allocator; (* first, so that ^Arena(a) finds the rest *) | |
| 1400 | - ... | |
| 1401 | -end; | |
| 1402 | -proc init*(ar: ^Arena; size: INT): BOOL; (* an arena of size bytes; false if the system has none *) | |
| 1403 | -proc reset*(ar: ^Arena); (* every allocation forgotten; the memory used again *) | |
| 1404 | -proc done*(ar: ^Arena); (* its memory given back *) | |
| 1405 | -``` | |
| 1406 | - | |
| 1407 | -`NEW(p, a)` calls `a^.alloc`. `Heap` is the general | |
| 1408 | -allocator. An arena hands out memory from | |
| 1409 | -one block: its `free` does nothing, and `reset` forgets everything. | |
| 1410 | - | |
| 1411 | -``` | |
| 1412 | -module Lock; | |
| 1413 | -type Lock* = record ... end; | |
| 1414 | -proc take*(l: ^Lock); | |
| 1415 | -proc give*(l: ^Lock); | |
| 1416 | - | |
| 1417 | -module Thread; | |
| 1418 | -const Failed*: ERROR = "can't start a thread"; | |
| 1419 | -type | |
| 1420 | - Thread* = record ... end; | |
| 1421 | - Mutex* = record ... end; (* zero is free *) | |
| 1422 | -proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); (* p(arg) in a new thread *) | |
| 1423 | -proc join*(t: ^Thread); (* waits for t to end, then frees its stack *) | |
| 1424 | -proc lock*(m: ^Mutex); | |
| 1425 | -proc unlock*(m: ^Mutex); | |
| 1426 | -``` | |
| 1427 | - | |
| 1428 | -A `Lock` is for short sections: a thread that finds it taken tries | |
| 1429 | -again, giving up the processor now and then. A `Mutex` sleeps. Each of | |
| 1430 | -`Thread`'s threads runs on a stack of its own, with a page below it | |
| 1431 | -that can't be touched, so that running off the stack stops the program. | |
| 1432 | -A program that uses C makes its threads with C's `pthread_create`, and | |
| 1433 | -so can't use `Thread`. | |
| 1434 | - | |
| 1435 | -``` | |
| 1436 | -module Out; | |
| 1437 | -proc str*(s: []U8); | |
| 1438 | -proc char*(c: U8); | |
| 1439 | -proc ln*; (* a line feed *) | |
| 1440 | -proc int*(n: I64); (* n in decimal *) | |
| 1441 | -proc hex*(n: U64; digits: INT); (* n in hexadecimal, digits digits at least *) | |
| 1442 | -proc flush*; | |
| 1443 | -``` | |
| 1444 | - | |
| 1445 | -**Out and C's stdio.** `Out` keeps a buffer of `Sys.outSize` bytes, | |
| 1446 | -4 KB on Linux. It writes it out | |
| 1447 | -when it is full, at each line feed when standard output is a terminal, | |
| 1448 | -at `Out.flush`, and when the program ends by `Rt.exit`. A lock keeps | |
| 1449 | -what each call writes together when threads write at once. C's `stdio` | |
| 1450 | -keeps a buffer of its own for the same standard output. A program that | |
| 1451 | -writes with both flushes one before writing with the other, or the | |
| 1452 | -text comes out of order: | |
| 1453 | - | |
| 1454 | -``` | |
| 1455 | -module Both; | |
| 1456 | -import Out, C; (* C: printf, fflush and C's FILE *stdout *) | |
| 1457 | - | |
| 1458 | -proc main*; | |
| 1459 | -begin | |
| 1460 | - Out.str("from raz\n"); | |
| 1461 | - Out.flush; | |
| 1462 | - C.printf("from C\n"); | |
| 1463 | - C.fflush(C.stdout); | |
| 1464 | - Out.str("from raz again\n"); | |
| 1465 | -end; | |
| 1466 | - | |
| 1467 | -end Both. | |
| 1468 | -``` | |
| 1469 | - | |
| 1470 | -**Sys** gives every target the same procedures, so that a program | |
| 1471 | -that needs only these runs wherever raz does: through system calls on | |
| 1472 | -Linux, through the C library elsewhere. A result that can fail is a | |
| 1473 | -number, minus an errno, as on Linux; 0 is success. | |
| 1474 | - | |
| 1475 | -``` | |
| 1476 | -module Sys; | |
| 1477 | -const | |
| 1478 | - stdin* = 0; stdout* = 1; stderr* = 2; | |
| 1479 | - outSize* = 4096; (* the bytes Out keeps; each target has its own *) | |
| 1480 | - (* signals, by their numbers on Linux; each target has its own *) | |
| 1481 | - sigHup* = 1; sigInt* = 2; sigQuit* = 3; sigKill* = 9; sigPipe* = 13; | |
| 1482 | - sigTerm* = 15; sigChld* = 17; sigCont* = 18; sigStop* = 19; | |
| 1483 | - sigTstp* = 20; sigTtin* = 21; sigTtou* = 22; sigWinch* = 28; | |
| 1484 | - | |
| 1485 | -type | |
| 1486 | - Access* = (reading, writing, creating, exclusive, truncating, appending); | |
| 1487 | - Accesses* = set of Access; | |
| 1488 | - Info* = record | |
| 1489 | - size*: I64; (* in bytes *) | |
| 1490 | - mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *) | |
| 1491 | - mode*: INT; (* its permission bits, as chmod takes them: 1EDH is rwxr-xr-x *) | |
| 1492 | - dir*: BOOL; (* a directory *) | |
| 1493 | - file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) | |
| 1494 | - symlink*: BOOL; (* the path itself is a symbolic link *) | |
| 1495 | - end; | |
| 1496 | - Dir* = record ... end; (* an open directory; its fields are the target's *) | |
| 1497 | - Program* = record | |
| 1498 | - path*: []U8; | |
| 1499 | - args*: [][]U8; (* the first is its name *) | |
| 1500 | - env*: [][]U8; (* NAME=value, each added to this program's environment *) | |
| 1501 | - onlyEnv*: BOOL; (* env is its whole environment *) | |
| 1502 | - dir*: []U8; (* its working directory; "" for this program's *) | |
| 1503 | - fds*: [3]INT; (* its standard input, output and error; 0 for this program's own *) | |
| 1504 | - group*: INT; (* the process group it joins: 0 for this program's, -1 a new one it leads *) | |
| 1505 | - foreground*: BOOL; (* its group takes the terminal on standard input *) | |
| 1506 | - tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *) | |
| 1507 | - end; | |
| 1508 | - Status* = (running, exited, killed, stopped); | |
| 1509 | - | |
| 1510 | -proc write*(fd: INT; s: []U8): INT; (* the bytes written *) | |
| 1511 | -proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) | |
| 1512 | -proc open*(path: []U8; how: Accesses): INT; (* its fd *) | |
| 1513 | -proc openRead*(path: []U8): INT; (* open(path, {reading}) *) | |
| 1514 | -proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *) | |
| 1515 | -proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; (* the new position *) | |
| 1516 | -proc close*(fd: INT); | |
| 1517 | -proc isTerminal*(fd: INT): BOOL; | |
| 1518 | -proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) | |
| 1519 | -proc munmap*(p: ^U8; size: INT); | |
| 1520 | -proc yield*; (* the processor given to another thread *) | |
| 1521 | -proc exit*(status: INT); (* the program ended at once, without Rt's atExit *) | |
| 1522 | - | |
| 1523 | -proc rename*(old, new: []U8): INT; (* replaces what new names *) | |
| 1524 | -proc remove*(path: []U8): INT; (* a file, not a directory *) | |
| 1525 | -proc removeDir*(path: []U8): INT; (* an empty directory *) | |
| 1526 | -proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *) | |
| 1527 | -proc chmod*(path: []U8; mode: INT): INT; | |
| 1528 | -proc symlink*(target, path: []U8): INT; (* path made a link to target *) | |
| 1529 | -proc readLink*(path: []U8): ([]U8, INT); (* what the link names, good until the next call *) | |
| 1530 | -proc info*(path: []U8): (Info, INT); (* following a symbolic link *) | |
| 1531 | -proc space*(path: []U8): (I64, I64, INT); (* the bytes free and in all on path's file system *) | |
| 1532 | - | |
| 1533 | -proc openDir*(d: ^Dir; path: []U8): INT; | |
| 1534 | -proc nextEntry*(d: ^Dir): ([]U8, BOOL); (* a name, good until the next call; false at the end *) | |
| 1535 | -proc closeDir*(d: ^Dir); | |
| 1536 | - | |
| 1537 | -proc rawMode*(fd: INT): BOOL; (* the terminal on fd: no echo, no lines, no signals *) | |
| 1538 | -proc restoreMode*(fd: INT); (* as rawMode found it *) | |
| 1539 | -proc termSize*(fd: INT): (INT, INT); (* columns and rows; 80 and 24 if unknown *) | |
| 1540 | -proc ready*(fd, ms: INT): BOOL; (* something to read within ms, or ever if ms < 0 *) | |
| 1541 | -proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *) | |
| 1542 | -proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *) | |
| 1543 | -proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *) | |
| 1544 | - | |
| 1545 | -proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) | |
| 1546 | -proc workDir*: ([]U8, INT); (* the working directory, good until the next call *) | |
| 1547 | -proc setWorkDir*(path: []U8): INT; | |
| 1548 | - | |
| 1549 | -proc pipe*: (INT, INT); (* the fd to read and the fd to write; or minus an errno and -1 *) | |
| 1550 | -proc openPty*: INT; (* a pseudo-terminal: the fd of its controlling side *) | |
| 1551 | -proc setTermSize*(fd, cols, rows: INT): INT; (* the size its programs are told of *) | |
| 1552 | -proc spawn*(p: Program): INT; (* a program started: its process id *) | |
| 1553 | -proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *) | |
| 1554 | -proc kill*(pid, signal: INT): INT; | |
| 1555 | -proc pid*: INT; (* this program's process id *) | |
| 1556 | -proc setGroup*(pid, group: INT): INT; (* pid 0 is this program; group 0 a new one pid leads *) | |
| 1557 | -proc foreground*(fd, group: INT): INT; (* the terminal on fd gives its keys to group *) | |
| 1558 | -proc foregroundOf*(fd: INT): INT; (* the group the terminal on fd gives its keys to *) | |
| 1559 | - | |
| 1560 | -proc catch*(sigs: []INT): INT; (* sigs caught: an fd to wait on *) | |
| 1561 | -proc caught*(fd: INT): INT; (* the next signal caught, or 0 if none is waiting *) | |
| 1562 | -proc ignore*(sig: INT; yes: BOOL): INT; (* sig ignored; or, if not yes, back to what it does at first *) | |
| 1563 | - | |
| 1564 | -proc clock*: I64; (* nanoseconds, on a clock that only goes forward *) | |
| 1565 | -proc now*: I64; (* nanoseconds since 1970-01-01, UTC *) | |
| 1566 | -proc sleep*(ms: INT); | |
| 1567 | -proc random*(b: []U8); (* b filled with random bytes *) | |
| 1568 | - | |
| 1569 | -proc connect*(path: []U8): INT; (* a stream socket, connected to the Unix socket at path: its fd *) | |
| 1570 | -proc connectTcp*(addr: []U8; port: INT): INT; (* addr an IPv4 or IPv6 address, as numbers *) | |
| 1571 | -proc connectUdp*(addr: []U8; port: INT): INT; | |
| 1572 | -proc listen*(path: []U8): INT; (* a Unix socket made at path, taking connections *) | |
| 1573 | -proc listenTcp*(addr: []U8; port: INT): INT; | |
| 1574 | -proc accept*(fd: INT): INT; (* the next connection, waiting for one *) | |
| 1575 | -proc send*(fd: INT; s: []U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *) | |
| 1576 | -``` | |
| 1577 | - | |
| 1578 | -`info` follows a symbolic link: `size`, `dir` and `file` are those of | |
| 1579 | -what it names, and `symlink` says the path was a link, so that a program walking | |
| 1580 | -a tree can choose not to follow one back up it. A link to nothing is | |
| 1581 | -`-ENOENT`, as a missing file is. `file` lets a walk open only what | |
| 1582 | -holds data: opening a pipe would wait for a program to write to it. | |
| 1583 | - | |
| 1584 | -`nextEntry` gives every name in the directory except `.` and `..`, in | |
| 1585 | -no particular order. `rawMode` turns off what the terminal does with | |
| 1586 | -keys before the program reads them (echo, line editing, signals, flow | |
| 1587 | -control), and NL to CR NL on output. A program that turns it on | |
| 1588 | -restores it before it ends: `Rt.atExit` is the place. | |
| 1334 | +## 11. Versions | |
| 1589 | 1335 | |
| 1590 | -**Files.** `open` gives a file for what `how` asks: `reading`, | |
| 1591 | -`writing` or both; `creating` it if it is missing, readable by all and | |
| 1592 | -writable by its owner; with `exclusive`, failing with -17 (EEXIST) if | |
| 1593 | -it is there, as a program does that makes a lock or a file of its | |
| 1594 | -own; `truncating` it to nothing; `appending`, each write going to its | |
| 1595 | -end, however other programs write it too. `seek` moves to `pos` from | |
| 1596 | -the start of the file, or from its end if `fromEnd`, where a negative | |
| 1597 | -`pos` reads back from the end. `Info.mtime` is what a program checks | |
| 1598 | -to see whether a file changed under it; `Info.mode` holds the nine | |
| 1599 | -bits of `rwxrwxrwx`, and `chmod` sets them, 1EDH (755) for a program, | |
| 1600 | -1A4H (644) for data. | |
| 1601 | - | |
| 1602 | -**Programs.** `openPty` makes a pseudo-terminal, a terminal that a | |
| 1603 | -program, a terminal emulator, plays the screen and the keyboard of: | |
| 1604 | -what it writes to the fd is typed, and what the programs on the | |
| 1605 | -terminal write it reads. | |
| 1606 | - | |
| 1607 | -`spawn` starts the program at `p.path`, with `p.args` as its | |
| 1608 | -arguments, the first being its name. Its environment is this | |
| 1609 | -program's with each `NAME=value` of `p.env` added or put in place of | |
| 1610 | -the old value, or, if `p.onlyEnv`, `p.env` alone. It starts in | |
| 1611 | -`p.dir`. Its standard input, output and error are `p.fds`; a 0 gives | |
| 1612 | -it this program's own, so `{path: "/bin/ls", args: ["ls"]}` runs | |
| 1613 | -`ls` as a shell runs it, with every other field zero. It gets no | |
| 1614 | -other open file, every signal does what it does at first, and none | |
| 1615 | -is blocked. If `p.tty` is a pseudo-terminal's fd, the program starts a | |
| 1616 | -session on that terminal: it is its controlling terminal, and its | |
| 1617 | -standard input, output and error, and `p.fds` and `p.group` are not | |
| 1618 | -used. | |
| 1619 | - | |
| 1620 | -`p.group` puts the program in a process group, the programs that a | |
| 1621 | -shell's job is made of: -1 makes it the first of a new group, whose | |
| 1622 | -number is its process id, and the other programs of the job join that | |
| 1623 | -number. The terminal gives its keys, and the signals of Ctrl-C and | |
| 1624 | -Ctrl-Z, to one group, the one in its foreground: `p.foreground` puts | |
| 1625 | -the program's group there before it starts, and `foreground` puts any | |
| 1626 | -group there, as a shell does to take the terminal back. `setGroup` | |
| 1627 | -moves a program to a group, as the shell does for each program it | |
| 1628 | -starts, for whichever of the two runs first; a shell runs in a group | |
| 1629 | -of its own, `setGroup(0, 0)`. | |
| 1630 | - | |
| 1631 | -`wait` waits for the program `pid` to change, or for any of this | |
| 1632 | -program's if `pid` is -1, and says which one, and how: `exited`, with | |
| 1633 | -its exit status; `killed`, with the number of the signal that ended | |
| 1634 | -it; or `stopped`, by Ctrl-Z or a signal, with the signal's number. | |
| 1635 | -Without `block` it doesn't wait, and is 0 and `running` if no program | |
| 1636 | -changed. A program that has nothing to wait for is -10 (ECHILD). | |
| 1637 | -`kill` sends a program a signal, `sigTerm` to end it; to a group, if | |
| 1638 | -`pid` is minus its number. | |
| 1639 | - | |
| 1640 | -**Signals.** `catch` takes the signals `sigs` away from what they | |
| 1641 | -would do, ending the program most of them, and gives an fd that is | |
| 1642 | -ready to read when one comes, so that a program waits for signals with | |
| 1643 | -`ready` and `readyOf` as it waits for keys or a server. `caught` gives | |
| 1644 | -each signal that came, in turn. A later `catch` adds to the signals | |
| 1645 | -caught and gives the same fd. Catching `sigKill` or `sigStop`, or the | |
| 1646 | -signals `Rt` reports, SIGSEGV and SIGFPE, is -22 (EINVAL). `ignore` | |
| 1647 | -makes a signal do nothing, as a shell ignores `sigTtou` so that it can | |
| 1648 | -take its terminal back, and `ignore(sig, false)` gives it back what it | |
| 1649 | -does at first. A program that `spawn` starts begins with neither. | |
| 1650 | - | |
| 1651 | -**Time.** `clock` is for measuring how long something takes: it never | |
| 1652 | -goes back, and says nothing of the date. `now` is the date and time, | |
| 1653 | -which a person or the network may set back. `sleep` waits `ms` | |
| 1654 | -milliseconds. | |
| 1655 | - | |
| 1656 | -**Sockets.** `connect` is for talking to a server on the same machine, | |
| 1657 | -an X server at `/tmp/.X11-unix/X0` say; `connectTcp` for one across | |
| 1658 | -the network, given its address as numbers, `"127.0.0.1"` or `"::1"`: | |
| 1659 | -turning a name into an address is a library's work over these. | |
| 1660 | -`connectUdp` gives a socket of datagrams, each `send` one message to | |
| 1661 | -that address and each `read` one message from it, as a program asks a | |
| 1662 | -name server. What a socket receives comes by `read`, what it sends goes | |
| 1663 | -by `send`, and `close` ends it. `send` is `write` for a socket, except | |
| 1664 | -when the other end has closed it: `write` would stop the program with | |
| 1665 | -the signal SIGPIPE, where `send` returns -32 (EPIPE), so that a | |
| 1666 | -program learns its server has gone and can end as it wants. | |
| 1667 | -`listen` and `listenTcp` are the other side: a server's socket, on | |
| 1668 | -`path`, which must not be there yet, or on `port` at `addr`, | |
| 1669 | -`"0.0.0.0"` or `"::"` for every address the machine has; `accept` | |
| 1670 | -gives each connection that comes, as a socket of its own. | |
| 1671 | - | |
| 1672 | -`readyOf` waits on several file descriptors at once, as a program does | |
| 1673 | -that listens to a server and a terminal both; a descriptor at its end, | |
| 1674 | -or failed, counts as ready, since a read of it won't wait. It looks at | |
| 1675 | -the first 64 of `fds`, and `connect` and `listen` take a path of at | |
| 1676 | -most 107 bytes, as `struct sockaddr_un` holds; a longer one is -36 | |
| 1677 | -(ENAMETOOLONG). | |
| 1678 | - | |
| 1679 | -A program that both reads from a descriptor and writes much to it, as | |
| 1680 | -a terminal writes what is pasted to its program while it reads what | |
| 1681 | -the program writes back, must not wait on either: the program may be | |
| 1682 | -waiting for its own writes to be read. `setBlocking(fd, false)` has a | |
| 1683 | -read or a write on `fd` that would wait return -11 (EAGAIN) at once, | |
| 1684 | -a write having written what there was room for; `readyFor` waits, as | |
| 1685 | -`readyOf` does, for something to read on any of `reads` or for room to | |
| 1686 | -write on any of `writes`. A descriptor in both is given once, when it | |
| 1687 | -is ready for either; the 64 it looks at are `reads`, then `writes`. | |
| 1688 | - | |
| 1689 | -**Strings, buffers and collections.** | |
| 1690 | - | |
| 1691 | -``` | |
| 1692 | -module Str; | |
| 1693 | -proc cmp*(a, b: []U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *) | |
| 1694 | -proc eq*(a, b: []U8): BOOL; | |
| 1695 | -proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); (* s's bytes in memory from a; false if a has none *) | |
| 1696 | -proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *) | |
| 1697 | -proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *) | |
| 1698 | -proc hasPrefix*(s, p: []U8): BOOL; | |
| 1699 | -proc encode*(c: I32): ([4]U8, INT); (* c in UTF-8: its bytes, and how many *) | |
| 1700 | -proc decode*(s: []U8): (I32, INT); (* the character s starts with, and its length in bytes *) | |
| 1701 | -proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *) | |
| 1702 | -proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *) | |
| 1703 | -proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *) | |
| 1704 | -proc putLE*(s: []U8; v: U64); (* v into s, the lowest byte first *) | |
| 1705 | -proc putBE*(s: []U8; v: U64); (* v into s, the highest byte first *) | |
| 1706 | - | |
| 1707 | -module Buf; | |
| 1708 | -type Buf* = record len*: INT; ... end; | |
| 1709 | -proc init*(b: ^Buf; a: ^Mem.Allocator); | |
| 1710 | -proc room*(b: ^Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *) | |
| 1711 | -proc add*(b: ^Buf; c: U8); | |
| 1712 | -proc str*(b: ^Buf; s: []U8); | |
| 1713 | -proc bytes*(b: ^Buf): []U8; (* what it holds, until it next grows *) | |
| 1714 | -proc reset*(b: ^Buf); (* emptied, its memory kept *) | |
| 1715 | -proc done*(b: ^Buf); (* its memory given back *) | |
| 1716 | - | |
| 1717 | -module Fmt; | |
| 1718 | -type Arg* = variant | |
| 1719 | - int(i: I64); | |
| 1720 | - uint(u: U64); | |
| 1721 | - hex(h: U64); | |
| 1722 | - str(s: []U8); | |
| 1723 | - char(c: U8); | |
| 1724 | - bool(b: BOOL); | |
| 1725 | - real(r: F64; places: INT); | |
| 1726 | -end; | |
| 1727 | -proc print*(f: []U8; args: []Arg); (* to standard output *) | |
| 1728 | -proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); (* added to b *) | |
| 1729 | - | |
| 1730 | -module List(T); | |
| 1731 | -type | |
| 1732 | - Node* = record next*: ^Node; val*: T; end; | |
| 1733 | - List* = record head*: ^Node; len*: INT; ... end; | |
| 1734 | -proc init*(l: ^List; a: ^Mem.Allocator); | |
| 1735 | -proc push*(l: ^List; v: T): BOOL; (* v at the front; false if the allocator has no room *) | |
| 1736 | -proc pop*(l: ^List): (T, BOOL); (* the front value, taken off; false if the list is empty *) | |
| 1737 | -proc reverse*(l: ^List); | |
| 1738 | -proc clear*(l: ^List); | |
| 1739 | - | |
| 1740 | -module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); | |
| 1741 | -type Map* = record len*: INT; ... end; | |
| 1742 | -proc init*(m: ^Map; a: ^Mem.Allocator); | |
| 1743 | -proc put*(m: ^Map; k: K; v: V): BOOL; (* k now maps to v; false if there was no room *) | |
| 1744 | -proc remove*(m: ^Map; k: K): BOOL; (* false if k wasn't there *) | |
| 1745 | -proc get*(m: ^Map; k: K): (V, BOOL); (* what k maps to, and whether it maps to anything *) | |
| 1746 | -proc next*(m: ^Map; i: INT): (INT, K, V); (* the entry after i *) | |
| 1747 | - | |
| 1748 | -module Sort(T; less: proc(a, b: T): BOOL); | |
| 1749 | -proc sort*(s: []T); | |
| 1750 | - | |
| 1751 | -module Math; | |
| 1752 | -const pi* = 3.141592653589793; | |
| 1753 | -proc sqrt*(x: F64): F64; | |
| 1754 | -proc pow*(x, y: F64): F64; (* x to the power y *) | |
| 1755 | -proc floor*(x: F64): F64; | |
| 1756 | -proc ceil*(x: F64): F64; | |
| 1757 | -proc round*(x: F64): F64; (* to the nearest whole number; a half away from 0 *) | |
| 1758 | -proc exp*(x: F64): F64; | |
| 1759 | -proc ln*(x: F64): F64; | |
| 1760 | -proc log2*(x: F64): F64; | |
| 1761 | -proc log10*(x: F64): F64; | |
| 1762 | -proc sin*(x: F64): F64; | |
| 1763 | -proc cos*(x: F64): F64; | |
| 1764 | -proc tan*(x: F64): F64; | |
| 1765 | -proc asin*(x: F64): F64; (* from -pi/2 to pi/2 *) | |
| 1766 | -proc acos*(x: F64): F64; (* from 0 to pi *) | |
| 1767 | -proc atan*(x: F64): F64; (* from -pi/2 to pi/2 *) | |
| 1768 | -proc atan2*(y, x: F64): F64; (* the angle of (x, y) from the x axis, from -pi to pi *) | |
| 1769 | -``` | |
| 1770 | - | |
| 1771 | -A `Buf`, a `List` and a `Map` take their memory from the allocator | |
| 1772 | -`init` gives them. `Fmt`'s format takes the next argument at each | |
| 1773 | -`{}`; `{{` and `}}` stand for `{` and `}`. A `Map` hashes and | |
| 1774 | -compares its keys with the procedures it is given: | |
| 1775 | -`import Count := Map([]U8, INT, Str.hash, Str.eq);`. | |
| 1776 | - | |
| 1777 | -`Str` reads and writes UTF-8. `encode` writes a character, of at most | |
| 1778 | -10FFFFH, in one to four bytes; a number that isn't one becomes FFFDH, | |
| 1779 | -the character that stands for a wrong one. `decode` reads the first | |
| 1780 | -character of `s`, and gives FFFDH and 1 for a byte that doesn't start | |
| 1781 | -one rightly, and 0 and 0 for an empty `s`. `toInt` reads a decimal | |
| 1782 | -number, with a `-` or `+` before it if any, and nothing else. | |
| 1783 | - | |
| 1784 | -`le` and `be` read an unsigned number of up to 8 bytes in a fixed | |
| 1785 | -order, little-endian or big-endian, whatever the machine's own, and | |
| 1786 | -`putLE` and `putBE` write one; more than 8 bytes stops the program. | |
| 1787 | -They are how a file format or a network protocol is read and written: | |
| 1788 | - | |
| 1789 | -``` | |
| 1790 | -len := U32(Str.le(header[4..8])); | |
| 1791 | -Str.putBE(packet[0..2], U64(port)); | |
| 1792 | -``` | |
| 1793 | - | |
| 1794 | -`Map.next` walks a map: from 0, it gives the next entry and the number | |
| 1795 | -to pass for the one after it, and -1 when there are no more. A walk | |
| 1796 | -sees each entry once if the map doesn't change during it: | |
| 1797 | - | |
| 1798 | -``` | |
| 1799 | -i, k, v := Names.next(ADR(names), 0); | |
| 1800 | -while i >= 0 do | |
| 1801 | - Fmt.print("{} is {}\n", [Fmt.str(k), Fmt.int(v)]); | |
| 1802 | - i, k, v := Names.next(ADR(names), i); | |
| 1803 | -end; | |
| 1804 | -``` | |
| 1805 | - | |
| 1806 | -`Sort.sort` puts `s` in the order `less` gives, in place, without | |
| 1807 | -memory of its own, in time `n log n` at most; two elements equal by | |
| 1808 | -`less` may change places. `Math`'s procedures are as C's `math.h` | |
| 1809 | -has them, to within the last bit or so. `Fmt.real(x, places)` writes | |
| 1810 | -`x` with `places` digits after the point, at most 9, rounded to the | |
| 1811 | -nearest; from 1e15 on, with an exponent, as `1.50e17`; and `nan`, | |
| 1812 | -`inf` and `-inf` as themselves. | |
| 1813 | - | |
| 1814 | -**Files and Opt** are what most programs need of their command line. | |
| 1815 | - | |
| 1816 | -``` | |
| 1817 | -module Files; | |
| 1818 | -proc arg*(i: INT): []U8; (* argument i; 0 is the program's name *) | |
| 1819 | -proc read*(path: []U8; b: ^Buf.Buf): BOOL; (* the whole file added to b *) | |
| 1820 | -proc write*(path, data: []U8): BOOL; (* the file made to hold data *) | |
| 1821 | - | |
| 1822 | -module Opt; | |
| 1823 | -type | |
| 1824 | - Row* = variant | |
| 1825 | - usage(scope, line: []U8); (* a synopsis line, without "usage: " *) | |
| 1826 | - sub(scope, name, help: []U8); (* a subcommand *) | |
| 1827 | - option(scope: []U8; letter: U8; long, value, help: []U8); | |
| 1828 | - end; | |
| 1829 | - Parser* = record | |
| 1830 | - letter*: U8; (* the option just read *) | |
| 1831 | - value*: []U8; (* its value; "" for a flag *) | |
| 1832 | - long*: BOOL; (* it was written by its long name *) | |
| 1833 | - ... | |
| 1834 | - end; | |
| 1835 | -proc init*(p: ^Parser; name: []U8; table: []Row); (* the arguments from 1 on, by table *) | |
| 1836 | -proc next*(p: ^Parser): BOOL; (* the next option; false when they end *) | |
| 1837 | -proc operand*(p: ^Parser): ([]U8, BOOL); (* the next operand; false when there are no more *) | |
| 1838 | -proc scope*(p: ^Parser; name: []U8); (* subcommand name's options, from the word after it *) | |
| 1839 | -proc number*(p: ^Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *) | |
| 1840 | -proc fail*(p: ^Parser; text: []U8); (* the program's own mistake *) | |
| 1841 | -``` | |
| 1842 | - | |
| 1843 | -`Opt` reads a command line by the rules of the C library's `getopt`, | |
| 1844 | -with GNU's long options, so that a program's options behave as every | |
| 1845 | -other Unix program's. The program describes itself once, in a table of | |
| 1846 | -`Row`s: its synopsis lines, its subcommands and its options. The help | |
| 1847 | -and the messages for mistakes are made from the table, so they can't | |
| 1848 | -drift from it; the program's test of `p.letter` is the only other | |
| 1849 | -place an option appears. The parser is a record the program owns, and | |
| 1850 | -the table a slice it keeps as long as the parser: a literal written in | |
| 1851 | -the call to `init` lives as long as the procedure that makes it. | |
| 1852 | - | |
| 1853 | -A row's `scope` is `""` for the program itself, or a subcommand's | |
| 1854 | -name: `"start"`, or `"remote.add"` for a subcommand of `remote`. An | |
| 1855 | -option has a `letter`, a `long` name, or both. One with a long name | |
| 1856 | -only has a key of its own below `'!'` for a letter, so that the | |
| 1857 | -program can tell it from the others. With a `value`, the name the | |
| 1858 | -help and the messages give it, the option takes a value; with `""` it | |
| 1859 | -is a flag. | |
| 1860 | - | |
| 1861 | -``` | |
| 1862 | -const wait = 1; (* the key of --wait, which has no letter *) | |
| 1863 | -... | |
| 1864 | -Opt.init(ADR(p), "keep", [ | |
| 1865 | - Opt.usage("", "keep [-v] [--wait secs] start ..."), | |
| 1866 | - Opt.sub("", "start", "start a job"), | |
| 1867 | - Opt.option("", 'v', "verbose", "", "say more"), | |
| 1868 | - Opt.option("", wait, "wait", "secs", "give the job secs to start"), | |
| 1869 | - Opt.usage("start", "keep start [-b label] name command [argument ...]"), | |
| 1870 | - Opt.option("start", 'b', "bar", "label", "show the job in the bar as label")]); | |
| 1871 | -while Opt.next(ADR(p)) do | |
| 1872 | - if p.letter = 'v' then verbose := true; | |
| 1873 | - elsif p.letter = wait then secs := Opt.number(ADR(p), 1, 600); | |
| 1874 | - end; | |
| 1875 | -end; | |
| 1876 | -w, ok := Opt.operand(ADR(p)); | |
| 1877 | -if not ok or (w # "start") then Opt.fail(ADR(p), "no command; see keep -h"); end; | |
| 1878 | -Opt.scope(ADR(p), "start"); | |
| 1879 | -while Opt.next(ADR(p)) do | |
| 1880 | - if p.letter = 'b' then bar := p.value; end; | |
| 1881 | -end; | |
| 1882 | -job, ok := Opt.operand(ADR(p)); | |
| 1883 | -``` | |
| 1884 | - | |
| 1885 | -The rules are `getopt`'s: | |
| 1886 | - | |
| 1887 | -- Flags may share a dash: `-lw` is `-l -w`. A letter's value is the | |
| 1888 | - rest of its word, `-bdev`, or the whole next word, `-b dev`, even one | |
| 1889 | - that starts with a dash or is empty; in a group, the first letter | |
| 1890 | - that takes a value takes the rest, `-qn12`. | |
| 1891 | -- A long option is written in full, with its value after a `=` or in | |
| 1892 | - the next word: `--bar=dev`, `--bar dev`. An abbreviation is unknown, | |
| 1893 | - not guessed, so that adding an option never breaks a script. A value | |
| 1894 | - given to a flag, `--follow=x`, is a mistake. | |
| 1895 | -- `--` ends the options, and isn't an operand; `-` alone is one. The | |
| 1896 | - options end at the first operand, so in `keep start site python -m | |
| 1897 | - http.server` the `-m` is python's. | |
| 1898 | -- Each scope has its own options. An option given twice is read twice. | |
| 1899 | - | |
| 1900 | -`number` reads the value as `Str.toInt` does. `operand` reads what | |
| 1901 | -options are left first, and the program doesn't see them. | |
| 1902 | - | |
| 1903 | -`-h` or `--help` among a scope's options prints its help on standard | |
| 1904 | -output and ends the program with status 0. `init` and `scope` answer | |
| 1905 | -it, looking through the scope's options before `next` gives one, so | |
| 1906 | -that the program has done nothing yet: `keep -v -h -x` prints the | |
| 1907 | -help, and the program never sees `-v`, nor the mistake. The help is | |
| 1908 | -the scope's synopsis lines, a blank line, and its subcommands, its | |
| 1909 | -options and the help itself, each with its line of help: | |
| 1910 | - | |
| 1911 | -``` | |
| 1912 | -usage: keep start [-b label] name command [argument ...] | |
| 1913 | - | |
| 1914 | - -b, --bar label show the job in the bar as label | |
| 1915 | - -h, --help print this and exit | |
| 1916 | -``` | |
| 1917 | - | |
| 1918 | -A scope with several synopsis lines has the others indented under the | |
| 1919 | -first; one with none, `usage:` and its words, as `usage: keep start`. | |
| 1920 | - | |
| 1921 | -A mistake on the command line is a sentence on standard error, the | |
| 1922 | -program's name first, then the scope's synopsis lines, as the help | |
| 1923 | -begins, and status 2: | |
| 1924 | - | |
| 1925 | -``` | |
| 1926 | -keep: unknown option -x | |
| 1927 | -usage: keep start [-b label] name command [argument ...] | |
| 1928 | -``` | |
| 1929 | - | |
| 1930 | -The sentences are `unknown option -x` (or `--name`, up to its `=`), | |
| 1931 | -`-b needs a label`, `--follow takes no value` and `--wait wants a | |
| 1932 | -number from 1 to 600, not '0'`. `fail` writes the program's own | |
| 1933 | -mistakes the same way, but alone, so that the sentence can say where | |
| 1934 | -the help is: `keep: no command; see keep -h`. | |
| 1935 | - | |
| 1936 | -A mistake in the table is the program's, not its user's. `init` checks | |
| 1937 | -the table before it reads a word, and ends the program with status 1: | |
| 1938 | -`keep: option table, row 3: the option is in the scope already`. The | |
| 1939 | -others are an option with neither a letter nor a long name, a long | |
| 1940 | -name with `=` in it, a letter `-`, and `-h` or `--help` in the table, | |
| 1941 | -since they are `Opt`'s. | |
| 1942 | - | |
| 1943 | -## 12. Versions | |
| 1944 | - | |
| 1945 | 1336 | A version of this report is named by the month it was made in and a |
| 1946 | 1337 | count: `2026-10.0`, then `2026-10.1` for the next change that month. |
| 1947 | 1338 | `raz --version` says which version the compiler follows. |
| @@ -1985,7 +1376,7 @@ | ||
| 1985 | 1376 | it, destination first. Which instructions it knows is the |
| 1986 | 1377 | implementation's; `db` gives any others. |
| 1987 | 1378 | - **Sizes in the library.** How many bytes `Out` keeps before it |
| 1988 | - writes them, `Sys.outSize` (section 11): 4 KB on Linux, fewer on a | |
| 1379 | + writes them, `Sys.outSize` (the library volume): 4 KB on Linux, fewer on a | |
| 1989 | 1380 | small machine. |
| 1990 | 1381 | - **The executable.** Its format, and the names and line numbers it |
| 1991 | 1382 | carries for a debugger (section 10). |
| @@ -2011,7 +1402,7 @@ | ||
| 2011 | 1402 | that keeps to the core runs on every target. The rest of the library |
| 2012 | 1403 | (`Heap`, `Fmt`, `List`, `Map`, `Sort`, `Math`, `Files`, `Opt` and the |
| 2013 | 1404 | rest of `Sys`) is the standard library: a target may leave a module |
| 2014 | -out, and one that has it gives what section 11 says. `conform/` tests | |
| 1405 | +out, and one that has it gives what `doc/raz-lib.md` says. `conform/` tests | |
| 2015 | 1406 | the core only. |
| 2016 | 1407 | |
| 2017 | 1408 | ## Appendix B. The grammar |
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | (* A command line read through one table, by the rules of the C |
| 2 | - library's getopt with GNU's long options (report, section 11). The | |
| 2 | + library's getopt with GNU's long options (report, volume 2). The | |
| 3 | 3 | program describes its synopsis lines, subcommands and options once, |
| 4 | 4 | in a slice of Row; the help and the messages for mistakes are made |
| 5 | 5 | from it, so they can't drift from it. |
| @@ -305,7 +305,8 @@ | ||
| 305 | 305 | if not shutil.which("pandoc"): |
| 306 | 306 | sys.exit("pandoc makes the report's page: apt-get install pandoc") |
| 307 | 307 | p = subprocess.run( |
| 308 | - ["pandoc", os.path.join(ROOT, "doc", "raz.md"), "-s", "--template", | |
| 308 | + ["pandoc", os.path.join(ROOT, "doc", "raz.md"), | |
| 309 | + os.path.join(ROOT, "doc", "raz-lib.md"), "-s", "--template", | |
| 309 | 310 | os.path.join(HERE, "content", "spec.tmpl"), "--toc", "--toc-depth=1", |
| 310 | 311 | "--shift-heading-level-by=-1", "--no-highlight", "-t", "html", |
| 311 | 312 | # wide enough that no row of a table is "long": pandoc would |
| @@ -152,7 +152,7 @@ | ||
| 152 | 152 | # the report's examples that are whole modules: each compiles, and |
| 153 | 153 | # each with a main runs and ends with status 0 |
| 154 | 154 | rm -rf $out/report; mkdir -p $out/report |
| 155 | - for name in $($out/Doc examples doc/raz.md $out/report); do | |
| 155 | + for name in $($out/Doc examples doc/raz.md $out/report) $($out/Doc examples doc/raz-lib.md $out/report); do | |
| 156 | 156 | if ! $raz -o $out/report/$name $out/report/$name.raz 2>$out/report/$name.cerr; then |
| 157 | 157 | echo "FAIL report example $name: $(cat $out/report/$name.cerr)"; fail=$((fail + 1)) |
| 158 | 158 | elif grep -q '^proc main\*' $out/report/$name.raz && ! $out/report/$name > /dev/null 2>&1; then |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | module Opt; |
| 2 | 2 | A command line read through one table, by the rules of the C |
| 3 | - library's getopt with GNU's long options (report, section 11). The | |
| 3 | + library's getopt with GNU's long options (report, volume 2). The | |
| 4 | 4 | program describes its synopsis lines, subcommands and options once, |
| 5 | 5 | in a slice of Row; the help and the messages for mistakes are made |
| 6 | 6 | from it, so they can't drift from it. |
| @@ -425,7 +425,7 @@ | ||
| 425 | 425 | control(".Ic main ,"); |
| 426 | 426 | control("where it has one, is its test, and is left out."); |
| 427 | 427 | control(".Pp"); |
| 428 | - control("The report, section 11, says what the library promises, and which"); | |
| 428 | + control("The report's second volume says what the library promises, and which"); | |
| 429 | 429 | control("of its modules every target has."); |
| 430 | 430 | control(".Sh MODULES"); |
| 431 | 431 | for i := 2 to ARGC() - 1 do document(Files.arg(i)); end; |
| @@ -436,6 +436,8 @@ | ||
| 436 | 436 | control(".Xr raz-tutorial 7 ;"); |
| 437 | 437 | control("the report,"); |
| 438 | 438 | control(".Pa doc/raz.md ,"); |
| 439 | + control("and its second volume,"); | |
| 440 | + control(".Pa doc/raz-lib.md ,"); | |
| 439 | 441 | control("in raz's sources."); |
| 440 | 442 | end; |
| 441 | 443 |