| | @@ -38,7 +38,7 @@ |
| 38 | 38 | <p>raz is a small systems language, first made for x86-64 Linux. A raz program can lay a record over a device’s registers, read and write a device’s memory exactly as written, take part in lock-free algorithms, make system calls and call C libraries. It has no garbage collector and no hidden run time: what the program does is what its text says.</p> |
| 39 | 39 | <p>This is the first volume of the report: the language, what an implementation chooses, and the grammar. The second, <code>doc/raz-lib.md</code>, is the library, which sections 3 and 10 and appendix A name.</p> |
| 40 | 40 | <p>This report defines the language. It is short on purpose. Whatever it doesn’t allow, raz doesn’t have.</p> |
| 41 | | -<p>This is version 2026-10.31 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program. 2026-10.27 mends a defect: returning a slice of the procedure’s own frame is an error (section 7.3), which breaks only a program that read memory already gone. 2026-10.28 mends another: an array a call returns lives to the end of its statement, and a slice of it kept past that is undefined (sections 8 and 10). 2026-10.29 is a sixth thaw: an array constant typed <code>[]T</code> takes its length from its literal (section 4); it breaks no program. 2026-10.30 mends a defect: <code>[]</code> used as a slice is the empty slice, which may be returned (sections 6.5 and 7.3); it breaks no program. 2026-10.31 is a seventh thaw: <code>INT</code> is at least 16 bits, and <code>U8</code>, <code>I8</code> and <code>I16</code> widen to it (sections 5.1, 5.10 and 6.1); an enumeration’s name may be written with its type, and must be where another declaration shares it (section 5.7); and <code>continue</code> (section 7.2), which breaks a program that has a name <code>continue</code>.</p> |
| 41 | +<p>This is version 2026-10.32 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program. 2026-10.27 mends a defect: returning a slice of the procedure’s own frame is an error (section 7.3), which breaks only a program that read memory already gone. 2026-10.28 mends another: an array a call returns lives to the end of its statement, and a slice of it kept past that is undefined (sections 8 and 10). 2026-10.29 is a sixth thaw: an array constant typed <code>[]T</code> takes its length from its literal (section 4); it breaks no program. 2026-10.30 mends a defect: <code>[]</code> used as a slice is the empty slice, which may be returned (sections 6.5 and 7.3); it breaks no program. 2026-10.31 is a seventh thaw: <code>INT</code> is at least 16 bits, and <code>U8</code>, <code>I8</code> and <code>I16</code> widen to it (sections 5.1, 5.10 and 6.1); an enumeration’s name may be written with its type, and must be where another declaration shares it (section 5.7); and <code>continue</code> (section 7.2), which breaks a program that has a name <code>continue</code>. 2026-10.32 is an eighth thaw: a constant typed <code>[]T</code> may be <code>[]</code>, the empty slice (section 4); it breaks no program.</p> |
| 42 | 42 | <p>The language is frozen, softly, from this version on: it changes only to mend a defect, and its library grows only by what breaks no program written for it. What anyone wishes it had waits on a list, for a later thaw.</p> |
| 43 | 43 | <h1 id="a-first-look">1. A first look</h1> |
| 44 | 44 | <pre><code>module Hello; |
| | @@ -139,16 +139,17 @@ |
| 139 | 139 | Line = Text + "\n"; |
| 140 | 140 | ... |
| 141 | 141 | w := Text[4..7]; (* "cat" *)</code></pre> |
| 142 | | -<p>A constant with a type is an <code>ERROR</code> (section 5.8), an array or a record, and the value of an array or record constant is a literal (section 6.5) of constants. Its elements and fields are integers, reals, <code>BOOL</code>s, enumerations, sets, <code>ERROR</code>s, procedures, <code>nil</code>, a string for a <code>[]U8</code>, an array constant for a slice of its element type, and arrays and records of these; no variant and no other pointer:</p> |
| 142 | +<p>A constant with a type is an <code>ERROR</code> (section 5.8), an array or a record, and the value of an array or record constant is a literal (section 6.5) of constants. Its elements and fields are integers, reals, <code>BOOL</code>s, enumerations, sets, <code>ERROR</code>s, procedures, <code>nil</code>, a string for a <code>[]U8</code>, an array constant or the empty slice for a slice of its element type, and arrays and records of these; no variant and no other pointer:</p> |
| 143 | 143 | <pre><code>const Primes: [5]I64 = [2, 3, 5, 7, 11]; |
| 144 | 144 | type Rule = record class: []U8; tag: INT; float: BOOL; end; |
| 145 | 145 | const Rules: [2]Rule = [{class: "firefox", tag: 2}, {class: "mpv", float: true}]; |
| 146 | 146 | ... |
| 147 | 147 | p := Primes[i];</code></pre> |
| 148 | 148 | <p>It is used as a variable of that type is, but can’t be changed: an array constant can be indexed and sliced and its <code>LEN</code> is <code>n</code>, a record constant’s fields can be read, and either can be assigned, compared as its type allows, and passed. Its parts are constant as a string’s bytes are (section 5.10). A part of it isn’t a constant, even at a constant index.</p> |
| 149 | | -<p>An array constant may be typed <code>[]T</code>, with no length: its length is then its literal’s, as a string constant’s is the string’s, and it is an array constant of that length, <code>[n]T</code>, in all else. Its literal has at least one element.</p> |
| 149 | +<p>An array constant may be typed <code>[]T</code>, with no length: its length is then its literal’s, as a string constant’s is the string’s, and it is an array constant of that length, <code>[n]T</code>, in all else. If its literal is <code>[]</code>, it is no array but the empty slice, as a slice variable starts: a value of type <code>[]T</code>, used as a slice is, and <code>LEN</code> of it is 0 but not a constant.</p> |
| 150 | 150 | <pre><code>const Wide: [][2]I32 = [[1100H, 115FH], [2E80H, 303EH], [0AC00H, 0D7A3H]]; |
| 151 | | - Count = LEN(Wide); (* 3, a constant *)</code></pre> |
| 151 | + Count = LEN(Wide); (* 3, a constant *) |
| 152 | + Narrow: [][2]I32 = []; (* the empty slice *)</code></pre> |
| 152 | 153 | <p><strong>Variables</strong> of a module live as long as the program; variables of a procedure live as long as its call. Both start at zero: every byte of them is zero. So a number starts as <code>0</code> (<code>0.0</code> for <code>F32</code> and <code>F64</code>), a <code>BOOL</code> as <code>false</code>, a pointer, procedure or <code>ERROR</code> as <code>nil</code>, a slice as the empty slice (<code>ptr</code> is <code>nil</code>, <code>len</code> is 0), a variant as its first case with its fields zero, a set as the empty set, and an enumeration as its first name. No variable is ever read before it has a value.</p> |
| 153 | 154 | <h1 id="types">5. Types</h1> |
| 154 | 155 | <pre><code>Type = QualIdent |
| | @@ -1044,6 +1045,7 @@ |
| 1044 | 1045 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| 1045 | 1046 | ino*: U64; (* its inode, which a rename keeps and a new file doesn't *) |
| 1046 | 1047 | dev*: U64; (* the file system it is on; with ino, it names the file *) |
| 1048 | + links*: U64; (* how many names it has: 1 for a file no hard link shares *) |
| 1047 | 1049 | end; |
| 1048 | 1050 | Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *) |
| 1049 | 1051 | Dir* = record ... end; (* an open directory; its fields are the target's *) |
| | @@ -1174,7 +1176,7 @@ |
| 1174 | 1176 | proc mapFile*(fd, size: INT): ^U8; (* the first size bytes of fd, shared; munmap gives them back; or nil *)</code></pre> |
| 1175 | 1177 | <p><code>info</code> follows a symbolic link: <code>size</code>, <code>dir</code> and <code>file</code> are those of what it names, and <code>symlink</code> says the path was a link, so that a program walking a tree can choose not to follow one back up it. A link to nothing is <code>-ENOENT</code>, as a missing file is. <code>file</code> lets a walk open only what holds data: opening a pipe would wait for a program to write to it.</p> |
| 1176 | 1178 | <p><code>nextEntry</code> gives every name in the directory except <code>.</code> and <code>..</code>, in no particular order. <code>entryKind</code> says what the name it gave last is, a file, a directory, a symbolic link (whatever it names) or something else, as the directory itself records it, so that a program walking a tree needn’t ask <code>linkInfo</code> of every name, a system call each; where the file system doesn’t record it, it is <code>unknown</code>, and <code>linkInfo</code> must be asked. <code>rawMode</code> turns off what the terminal does with keys before the program reads them (echo, line editing, signals, flow control), and NL to CR NL on output. A program that turns it on restores it before it ends: <code>Rt.atExit</code> is the place.</p> |
| 1177 | | -<p><strong>Files.</strong> <code>open</code> gives a file for what <code>how</code> asks: <code>reading</code>, <code>writing</code> or both; <code>creating</code> it if it is missing, readable by all and writable by its owner; with <code>exclusive</code>, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; <code>truncating</code> it to nothing; <code>appending</code>, each write going to its end, however other programs write it too. <code>openMode</code> is <code>open</code> that gives a file it makes the bits <code>mode</code>, less those the umask takes away: 1EDH (755) for a program, 124H (444) for a file never to be written; <code>open</code> gives 1A4H (644). <code>seek</code> moves to <code>pos</code> from the start of the file, or from its end if <code>fromEnd</code>, where a negative <code>pos</code> reads back from the end; <code>position</code> says where the file is, without moving. <code>truncate</code> makes the file <code>size</code> bytes long, cutting it or adding zeros, and leaves the position where it was. <code>sync</code> returns once what was written to the file is on the disk, as a program needs before it says a file is saved; <code>syncAll</code> returns once everything written to the file system that holds the file is on the disk, at once rather than a file at a time, and may flush other file systems too. <code>lock</code> takes the file’s lock, which one open file holds at a time: another that asks for it waits if <code>wait</code>, and otherwise fails with -11 (EAGAIN). The lock goes when the file is closed or the program ends, however it ends, so it never goes stale as a file made with <code>exclusive</code> does. <code>infoOf</code> is <code>info</code> of an open file: its size is what it holds now, however it was written; <code>linkInfo</code> is <code>info</code> of a symbolic link itself, not what it names, so that <code>symlink</code> is true, <code>dir</code> and <code>file</code> false, and <code>size</code> the length of what it names, which need not be there. <code>Info.mtime</code> is what a program checks to see whether a file changed under it, and <code>Info.ino</code> whether it is still the same file, not one renamed over it; 0 on a target whose files have none. <code>Info.dev</code> is the file system the file is on: two files are one only if both <code>dev</code> and <code>ino</code> agree, which is how a program following links finds that one has led it back into a directory it is already in. <code>Info.mode</code> holds the nine bits of <code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for a program, 1A4H (644) for data.</p> |
| 1179 | +<p><strong>Files.</strong> <code>open</code> gives a file for what <code>how</code> asks: <code>reading</code>, <code>writing</code> or both; <code>creating</code> it if it is missing, readable by all and writable by its owner; with <code>exclusive</code>, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; <code>truncating</code> it to nothing; <code>appending</code>, each write going to its end, however other programs write it too. <code>openMode</code> is <code>open</code> that gives a file it makes the bits <code>mode</code>, less those the umask takes away: 1EDH (755) for a program, 124H (444) for a file never to be written; <code>open</code> gives 1A4H (644). <code>seek</code> moves to <code>pos</code> from the start of the file, or from its end if <code>fromEnd</code>, where a negative <code>pos</code> reads back from the end; <code>position</code> says where the file is, without moving. <code>truncate</code> makes the file <code>size</code> bytes long, cutting it or adding zeros, and leaves the position where it was. <code>sync</code> returns once what was written to the file is on the disk, as a program needs before it says a file is saved; <code>syncAll</code> returns once everything written to the file system that holds the file is on the disk, at once rather than a file at a time, and may flush other file systems too. <code>lock</code> takes the file’s lock, which one open file holds at a time: another that asks for it waits if <code>wait</code>, and otherwise fails with -11 (EAGAIN). The lock goes when the file is closed or the program ends, however it ends, so it never goes stale as a file made with <code>exclusive</code> does. <code>infoOf</code> is <code>info</code> of an open file: its size is what it holds now, however it was written; <code>linkInfo</code> is <code>info</code> of a symbolic link itself, not what it names, so that <code>symlink</code> is true, <code>dir</code> and <code>file</code> false, and <code>size</code> the length of what it names, which need not be there. <code>Info.mtime</code> is what a program checks to see whether a file changed under it, and <code>Info.ino</code> whether it is still the same file, not one renamed over it; 0 on a target whose files have none. <code>Info.dev</code> is the file system the file is on: two files are one only if both <code>dev</code> and <code>ino</code> agree, which is how a program following links finds that one has led it back into a directory it is already in. <code>Info.links</code> is how many names the file has: more than 1 if a hard link shares it, so that writing to it writes to a file named elsewhere too; 0 on a target that doesn’t say. <code>Info.mode</code> holds the nine bits of <code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for a program, 1A4H (644) for data.</p> |
| 1178 | 1180 | <p><strong>Programs.</strong> <code>openPty</code> makes a pseudo-terminal, a terminal that a program, a terminal emulator, plays the screen and the keyboard of: what it writes to the fd is typed, and what the programs on the terminal write it reads.</p> |
| 1179 | 1181 | <p><code>spawn</code> starts the program at <code>p.path</code>, with <code>p.args</code> as its arguments, the first being its name. A <code>p.path</code> without a <code>/</code> is looked for as a shell looks for a command, and as C’s <code>execvp</code> does: in each directory of this program’s <code>PATH</code> in turn, an empty one being the working directory, or of <code>/bin:/usr/bin</code> if <code>PATH</code> is unset or empty; the first that runs is the program. Its environment is this program’s with each <code>NAME=value</code> of <code>p.env</code> added or put in place of the old value, or, if <code>p.onlyEnv</code>, <code>p.env</code> alone. It starts in <code>p.dir</code>. Its standard input, output and error are <code>p.fds</code>; a 0 gives it this program’s own, so <code>{path: "ls", args: ["ls"]}</code> runs <code>ls</code> as a shell runs it, with every other field zero. It gets no other open file but those of <code>p.more</code>, each at its own number, as a shell passes <code>3<file</code> on, every signal does what it does at first, and none is blocked. If <code>p.tty</code> is a pseudo-terminal’s fd, the program starts a session on that terminal: it is its controlling terminal, and its standard input, output and error, and <code>p.fds</code> and <code>p.group</code> are not used.</p> |
| 1180 | 1182 | <p><code>p.group</code> puts the program in a process group, the programs that a shell’s job is made of: -1 makes it the first of a new group, whose number is its process id, and the other programs of the job join that number. The terminal gives its keys, and the signals of Ctrl-C and Ctrl-Z, to one group, the one in its foreground: <code>p.foreground</code> puts the program’s group there before it starts, and <code>foreground</code> puts any group there, as a shell does to take the terminal back. <code>setGroup</code> moves a program to a group, as the shell does for each program it starts, for whichever of the two runs first; a shell runs in a group of its own, <code>setGroup(0, 0)</code>.</p> |
| | @@ -1280,6 +1282,7 @@ |
| 1280 | 1282 | proc init*(v: ^Vec; a: ^Mem.Allocator); |
| 1281 | 1283 | proc room*(v: ^Vec; n: INT): BOOL; (* room for n more; false if the allocator has none *) |
| 1282 | 1284 | proc push*(v: ^Vec; x: T); (* x added at the end *) |
| 1285 | +proc pop*(v: ^Vec): (T, BOOL); (* the last element, taken off; false if the array is empty *) |
| 1283 | 1286 | proc add*(v: ^Vec): ^T; (* a new element at the end, zero, to fill in place *) |
| 1284 | 1287 | proc tryPush*(v: ^Vec; x: T): BOOL; (* push; false, and v as it was, if there is no room *) |
| 1285 | 1288 | proc tryAdd*(v: ^Vec): (^T, BOOL); (* add; nil and false, and v as it was, if there is no room *) |
| | @@ -1313,7 +1316,7 @@ |
| 1313 | 1316 | proc fromBits*(u: U64): F64; (* the F64 whose bits are u *) |
| 1314 | 1317 | proc mul*(a, b: U64): (U64, U64); (* a times b: the low 64 bits of the product, and the high *) |
| 1315 | 1318 | proc toReal*(s: []U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn't one *)</code></pre> |
| 1316 | | -<p>A <code>Buf</code>, a <code>List</code>, a <code>Map</code> and a <code>Vec</code> take their memory from the allocator <code>init</code> gives them. <code>Buf.add</code>, <code>str</code> and <code>spare</code> stop the program, as a failed check does, if the allocator has no room; <code>room</code> asks first. A program reads into a <code>Buf</code> through <code>spare</code>, then adds what it read to <code>len</code>: <code>s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n > 0 then INC(b^.len, n); end;</code>. A <code>Vec</code> is <code>Buf</code> for any <code>T</code>: one block, which doubles when it is full, so <code>push</code> costs the same on average however long it grows. <code>all</code> gives the elements as a slice, indexed and checked as any is, and <code>v.len</code> is their number; the slice, and what <code>add</code> gives, are good until the array next grows. <code>push</code>, <code>add</code> and <code>resize</code> stop the program, as <code>Buf.add</code> does, if the allocator has no room; <code>room</code> asks first: <code>import Lines := Vec([]U8);</code>. <code>Fmt</code>’s format takes the next argument at each <code>{}</code>; <code>{{</code> and <code>}}</code> stand for <code>{</code> and <code>}</code>. A <code>Map</code> hashes and compares its keys with the procedures it is given: <code>import Count := Map([]U8, INT, Str.hash, Str.eq);</code>.</p> |
| 1319 | +<p>A <code>Buf</code>, a <code>List</code>, a <code>Map</code> and a <code>Vec</code> take their memory from the allocator <code>init</code> gives them. <code>Buf.add</code>, <code>str</code> and <code>spare</code> stop the program, as a failed check does, if the allocator has no room; <code>room</code> asks first. A program reads into a <code>Buf</code> through <code>spare</code>, then adds what it read to <code>len</code>: <code>s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n > 0 then INC(b^.len, n); end;</code>. A <code>Vec</code> is <code>Buf</code> for any <code>T</code>: one block, which doubles when it is full, so <code>push</code> costs the same on average however long it grows. <code>all</code> gives the elements as a slice, indexed and checked as any is, and <code>v.len</code> is their number; the slice, and what <code>add</code> gives, are good until the array next grows. <code>pop</code> takes the last element off, keeping the memory. <code>push</code>, <code>add</code> and <code>resize</code> stop the program, as <code>Buf.add</code> does, if the allocator has no room; <code>room</code> asks first: <code>import Lines := Vec([]U8);</code>. <code>Fmt</code>’s format takes the next argument at each <code>{}</code>; <code>{{</code> and <code>}}</code> stand for <code>{</code> and <code>}</code>. A <code>Map</code> hashes and compares its keys with the procedures it is given: <code>import Count := Map([]U8, INT, Str.hash, Str.eq);</code>.</p> |
| 1317 | 1320 | <p><code>Str</code> reads and writes UTF-8. <code>encode</code> writes a character, of at most 10FFFFH, in one to four bytes; a number that isn’t one becomes FFFDH, the character that stands for a wrong one. <code>decode</code> reads the first character of <code>s</code>, and gives FFFDH and 1 for a byte that doesn’t start one rightly, and 0 and 0 for an empty <code>s</code>: a continuation, a byte followed by too few continuations, a surrogate, a character beyond 10FFFFH or one spelled in more bytes than it needs. <code>decodeLast</code> reads the last character in the same way, to walk a string backwards. <code>valid</code> says that <code>decode</code> finds no such byte in <code>s</code>, and <code>chars</code> counts the characters, each such byte one. <code>next</code> walks a string a character at a time, as <code>decode</code> reads them: from 0, it gives the character at <code>i</code> and where the one after it starts, and 0 and -1 when <code>i</code> is at the end, or past it:</p> |
| 1318 | 1321 | <pre><code>c, i := Str.next(s, 0); |
| 1319 | 1322 | while i >= 0 do |