| | @@ -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.25 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.</p> |
| 41 | +<p>This is version 2026-10.26 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program.</p> |
| 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; |
| | @@ -207,7 +207,7 @@ |
| 207 | 207 | </tr> |
| 208 | 208 | </tbody> |
| 209 | 209 | </table> |
| 210 | | -<p><code>INT</code> is the everyday integer: lengths, indexes and sizes are <code>INT</code>s. It is as big as an address, so it is the integer a pointer converts to (section 6.7), and a word the atomic built-ins work on (section 6.8). <code>INT</code> and <code>I64</code> are two types, even where they have the same size: an <code>INT</code> goes silently into an <code>I64</code> (section 5.10), but the two don’t mix in an expression (section 6.1), and an <code>I64</code> becomes an <code>INT</code> only by <code>INT(x)</code>, which may drop bits on a smaller target. A type bigger than the largest <code>INT</code> in bytes is an error.</p> |
| 210 | +<p><code>INT</code> is the everyday integer: lengths, indexes and sizes are <code>INT</code>s. It is as big as an address, so it is the integer a pointer converts to (section 6.7), and a word the atomic built-ins work on (section 6.8). <code>INT</code> and <code>I64</code> are two types, even where they have the same size: an <code>INT</code> goes silently into an <code>I64</code> wherever it meets one, in an expression too (sections 5.10 and 6.1), and an <code>I64</code> becomes an <code>INT</code> only by <code>INT(x)</code>, which may drop bits on a smaller target. A type bigger than the largest <code>INT</code> in bytes is an error.</p> |
| 211 | 211 | <p><code>F32</code> and <code>F64</code> exist only on a target that has reals (appendix A). On one that hasn’t, a real literal, and a use of <code>F32</code>, <code>F64</code> or of a name whose type is made of them, is an error; a declaration that no used code names isn’t, so a module like <code>Fmt</code> keeps one interface on every target.</p> |
| 212 | 212 | <p>Each is aligned as the target’s C aligns it: to its size on x86-64. So are <code>ERROR</code> (section 5.8), pointers and procedures, each as big as an address; a slice is aligned as an address is.</p> |
| 213 | 213 | <h2 id="arrays">5.2 Arrays</h2> |
| | @@ -266,7 +266,7 @@ |
| 266 | 266 | <li><code>A</code> is <code>INT</code> and <code>B</code> is <code>I64</code>, or <code>A</code> is <code>UINT</code> and <code>B</code> is <code>U64</code>, which loses nothing on any target.</li> |
| 267 | 267 | </ul> |
| 268 | 268 | <p>A string’s bytes are constant. A <code>[]U8</code> or <code>^U8</code> made from a string literal, or a slice of a string constant, points at them, and writing through it isn’t defined: on a target with read-only memory, it may stop the program as a bad address (section 10). A <code>[n]U8</code> made from a string is a copy, the program’s to change.</p> |
| 269 | | -<p>Nothing else converts silently. In particular, there is no other conversion between integer types, between integers and reals, or between <code>F32</code> and <code>F64</code>, without a written conversion (section 6.7). Results passed on whole need the same types exactly: in <code>a, b := f(x)</code> and in <code>return f(x)</code>, an <code>INT</code> result doesn’t pass up as an <code>I64</code>. Elsewhere, <code>return n, n</code> included, an <code>INT</code> widens as above; the bounds of a <code>for</code> and the step of <code>INC</code> and <code>DEC</code> take the type of the variable they work on, and an <code>INT</code> is not widened to it.</p> |
| 269 | +<p>Nothing else converts silently. In particular, there is no other conversion between integer types, between integers and reals, or between <code>F32</code> and <code>F64</code>, without a written conversion (section 6.7). An <code>INT</code> widens to <code>I64</code>, and a <code>UINT</code> to <code>U64</code>, wherever its value goes where an <code>I64</code> is wanted: in <code>a, b := f(x)</code> and <code>return f(x)</code> as in any other assignment, to the bounds of a <code>for</code> and the step of <code>INC</code> and <code>DEC</code> whose variable is an <code>I64</code>, and in an expression, where it meets an <code>I64</code> (section 6.1). The types themselves stay two: a <code>^INT</code> isn’t a <code>^I64</code>, an <code>INT</code> variable isn’t passed to a <code>var</code> parameter of type <code>I64</code>, and a procedure returning an <code>INT</code> isn’t of a procedure type returning an <code>I64</code>.</p> |
| 270 | 270 | <h1 id="expressions">6. Expressions</h1> |
| 271 | 271 | <pre><code>Expr = Conjunct { "or" Conjunct } . |
| 272 | 272 | Conjunct = Relation { "and" Relation } . |
| | @@ -285,12 +285,12 @@ |
| 285 | 285 | Element = Expr | ident ":" Expr . |
| 286 | 286 | Designator = QualIdent { Selector } . |
| 287 | 287 | Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]" |
| 288 | | - | "(" [ Expr { "," Expr } ] ")" .</code></pre> |
| 288 | + | "(" [ Expr { "," Expr } ] ")" | "." "(" Type ")" .</code></pre> |
| 289 | 289 | <p>The operators bind in the order of the grammar: <code>not</code>, <code>~</code> and <code>try</code> (section 7.8) most tightly, then the multiplying operators, then the adding ones, then the relations, then <code>and</code>, then <code>or</code>. So <code>a + b * c = d</code> is <code>(a + (b * c)) = d</code>, <code>x & 0FH = 0</code> is <code>(x & 0FH) = 0</code>, and <code>i < n and a[i] # 0 or done</code> is <code>((i < n) and (a[i] # 0)) or done</code>. A unary minus applies to the first term: <code>-a * b</code> is <code>-(a * b)</code>.</p> |
| 290 | 290 | <p><code>and</code>, <code>or</code> and <code>not</code> are for <code>BOOL</code>s only; <code>&</code>, <code>|</code>, <code>xor</code> and <code>~</code> are for integers only.</p> |
| 291 | | -<p>A pointer to a record isn’t the record: its fields are <code>p^.f</code>, never <code>p.f</code>, and the elements of a pointer to an array are <code>p^[i]</code>. A statement starts with a name or <code>try</code>, so <code>^T(x)^ := v</code> isn’t one.</p> |
| 291 | +<p>A pointer to a record isn’t the record: its fields are <code>p^.f</code>, never <code>p.f</code>, and the elements of a pointer to an array are <code>p^[i]</code>. A statement starts with a name or <code>try</code>, so <code>^T(x)^ := v</code> isn’t one; a view, <code>mem[a].(I64) := v</code> (section 6.9), says it without a pointer.</p> |
| 292 | 292 | <h2 id="integers">6.1 Integers</h2> |
| 293 | | -<p>Both operands of <code>+ - * div mod & | xor</code>, and of a relation, have the same integer type, or one is an untyped constant that takes the other’s type, which it must fit. So do <code>v</code> and <code>k</code> in <code>INC(v, k)</code> and <code>DEC(v, k)</code>. The result has that type and wraps around at its width: in <code>U8</code>, <code>200 + 100</code> is <code>44</code>. A unary minus wraps too: for <code>x</code> of an unsigned type, <code>-x</code> is <code>0 - x</code>. Two integer types are never mixed, whatever their sizes: with <code>i: I64</code>, <code>i < LEN(s)</code> is an error, and <code>i</code> should be an <code>INT</code>.</p> |
| 293 | +<p>Both operands of <code>+ - * div mod & | xor</code>, and of a relation, have the same integer type, or one is an untyped constant that takes the other’s type, which it must fit, or one is an <code>INT</code> and the other an <code>I64</code> (a <code>UINT</code> and a <code>U64</code>), and the <code>INT</code> widens to <code>I64</code> (section 5.10). So do <code>v</code> and <code>k</code> in <code>INC(v, k)</code> and <code>DEC(v, k)</code>, where only <code>k</code> widens: an <code>I64</code> <code>k</code> isn’t added to an <code>INT</code> <code>v</code>. The result has that type and wraps around at its width: in <code>U8</code>, <code>200 + 100</code> is <code>44</code>. A unary minus wraps too: for <code>x</code> of an unsigned type, <code>-x</code> is <code>0 - x</code>. Other integer types are never mixed, whatever their sizes: with <code>i: I32</code>, <code>i < LEN(s)</code> is an error, and <code>i</code> should be an <code>INT</code>. With <code>pos: I64</code>, <code>pos + LEN(s)</code> is an <code>I64</code>.</p> |
| 294 | 294 | <p>Where the report asks for an integer and names no type, as an index, a slice’s bound, a shift’s count, a pointer’s offset <code>i</code> in <code>p + i</code>, or a conversion’s operand, any integer type will do, and its value is taken. On a target whose <code>INT</code> is narrower, a pointer’s offset is cut to an <code>INT</code>.</p> |
| 295 | 295 | <p><code>SIZE(T)</code>, and <code>LEN</code> of an array or a string constant, are untyped constants, like a literal.</p> |
| 296 | 296 | <p><code>div</code> rounds toward zero and <code>mod</code> takes the sign of the dividend, as in C: <code>-7 div 2</code> is <code>-3</code>, <code>-7 mod 2</code> is <code>-1</code>. Dividing by zero, and dividing the smallest value of a signed type by <code>-1</code>, stop the program (section 10), whatever the type and the target.</p> |
| | @@ -468,7 +468,35 @@ |
| 468 | 468 | <p><code>NEW(p, a)</code> calls <code>a^.alloc(a, SIZE(T), A)</code>, where <code>A</code> is the alignment of <code>T</code>, and zeroes the bytes it gets if they aren’t <code>nil</code>. <code>Mem.Allocator</code> (the library, <code>doc/raz-lib.md</code>) is the one type of the library the language names.</p> |
| 469 | 469 | <p><code>COPY</code> is a statement, as <code>INC</code> is. It copies the bytes of <code>LEN(s)</code> elements as one block, as an assignment of an array does, and is right when <code>d</code> and <code>s</code> overlap, as in <code>COPY(a[1..n], a[0..n - 1])</code>. It is checked as a slice is: past <code>LEN(d)</code>, it stops the program, unless the module says <code>check off;</code>.</p> |
| 470 | 470 | <p><code>INC</code> and <code>DEC</code> evaluate <code>v</code> once, so <code>INC(a[f(i)])</code> calls <code>f</code> once. They read <code>v</code> and then write it: two accesses, even through a <code>^VOLATILE</code> pointer, and not atomic; <code>XADD</code> is the atomic one.</p> |
| 471 | | -<p>For <code>CAS</code>, <code>XCHG</code> and <code>XADD</code>, <code>p</code> is a <code>^T</code> or <code>^VOLATILE T</code> where <code>T</code> is <code>INT</code>, <code>UINT</code> or a pointer type (<code>INT</code> or <code>UINT</code> for <code>XADD</code>), on every target. They are sequentially consistent, on every target; how a machine without atomic instructions makes them so is the implementation’s choice (appendix A).</p> |
| 471 | +<p>For <code>CAS</code>, <code>XCHG</code> and <code>XADD</code>, <code>p</code> is a <code>^T</code> or <code>^VOLATILE T</code> where <code>T</code> is <code>INT</code>, <code>UINT</code> or a pointer type (<code>INT</code> or <code>UINT</code> for <code>XADD</code>), on every target. They are sequentially consistent, on every target; how a machine without atomic instructions makes them so is the implementation’s choice (appendix A). ### 6.9 Views</p> |
| 472 | +<p>A view reads and writes the bytes of a variable as a value of another type. <code>v.(T)</code>, with <code>v</code> a variable, is the <code>T</code> whose bytes start where <code>v</code> starts:</p> |
| 473 | +<pre><code>var mem: [4096]U8; n: I64; x: F64; bits: U64; |
| 474 | + |
| 475 | +mem[16].(I64) := -1; (* the 8 bytes mem[16] to mem[23] now hold -1 *) |
| 476 | +n := mem[16].(I64); (* and are read back as an I64: n is -1 *) |
| 477 | +bits := x.(U64); (* the 8 bytes of x, as a U64 *)</code></pre> |
| 478 | +<p>A view is a variable, not a value: <code>mem[16].(I64)</code> is made of the 8 bytes <code>mem[16]</code> to <code>mem[23]</code>, as <code>p^</code> is the variable <code>p</code> points at. Reading it reads those bytes; assigning it writes them, and so changes <code>mem</code>. Nothing is copied to make it. It stands wherever a variable may: on the left of <code>:=</code>, as the variable of <code>INC</code>, and with selectors after it:</p> |
| 479 | +<pre><code>type Header = record magic: U32; count: U32; end; |
| 480 | +var buf: [512]U8; k: U32; |
| 481 | + |
| 482 | +k := buf[0].(Header).count; (* bytes 4 to 7 of buf, as a U32 *) |
| 483 | +buf[0].(Header).count := 0; |
| 484 | +INC(mem[a].(I64), 8);</code></pre> |
| 485 | +<p><strong>A view isn’t a conversion.</strong> A conversion works on a value, a view on bytes. With <code>x: F64</code> holding 1.5, <code>I64(x)</code> is 1, the number rounded toward zero, while <code>x.(I64)</code> is 3FF8000000000000H, the bits that hold 1.5. With <code>n: I64</code> holding 258, <code>U8(n)</code> is 2, the number cut to its low byte, while <code>n.(U8)</code> is the first byte of <code>n</code> in memory, which is the low one only on a target that puts its low byte first (appendix A). When the program means a number, it wants a conversion; when it means the bytes as they lie, a view.</p> |
| 486 | +<p><strong>The types.</strong> <code>T</code> is an integer type, a real type, a pointer type, or a record or array made of these alone, at any depth: the types for which any bytes at all are a value. <code>BOOL</code>, an enumeration, a set, a slice, a variant, a procedure type and <code>ERROR</code> are left out, since bytes that are none of their values would make the program mean nothing. To read one of them from bytes, view the integer and convert it: <code>Colour(mem[a])</code>. <code>v</code> may be of any type, and is a variable: a name of one, a field or element of one, or what a pointer points at; not a constant, nor the result of a call or of an operator.</p> |
| 487 | +<p><strong>Where its bytes lie.</strong> The <code>T</code> lies within the variable <code>v</code> is part of:</p> |
| 488 | +<ul> |
| 489 | +<li>for an element of an array or a slice, <code>a[i].(T)</code>, from that element to the end of <code>a</code>: <code>mem[4088].(I64)</code> is the last 8 bytes of <code>mem</code>, and <code>mem[4090].(I64)</code> would run 2 bytes past its end;</li> |
| 490 | +<li>for anything else, within <code>v</code> itself: <code>x.(U64)</code> is allowed only if <code>x</code> has at least 8 bytes.</li> |
| 491 | +</ul> |
| 492 | +<p>This is checked, as an index is: by the compiler when it can tell, otherwise while the program runs, where a view that runs past the end is a failed check (section 6.5), unless the module says <code>check off;</code>. An element reached through a pointer, <code>p[i].(T)</code>, is checked no more than <code>p[i]</code> is, since a pointer has no length.</p> |
| 493 | +<p><strong>Any address.</strong> The <code>T</code> may start at any byte: <code>mem[3].(I64)</code> is an <code>I64</code> whose first byte is <code>mem[3]</code>. As a field of a packed record is (section 5.5), it is read and written on every target, in parts where the machine needs that; and, as such a field, a view has no address: <code>ADR</code> of it, a <code>var</code> argument of it and a slice of it are errors. Copy it out to a variable, and back in. A view of a volatile variable is an error too, since its access can’t be promised as one (section 7.6): a device’s registers are reached through a <code>^VOLATILE</code> pointer to a record of them.</p> |
| 494 | +<p><strong>Byte order.</strong> The bytes are in the target’s order (appendix A). A file or a message whose format fixes an order of its own is read with <code>Str.le</code>, <code>Str.be</code> and the like (section 5.5), if the program is to read it right on every target.</p> |
| 495 | +<p><strong>Before views.</strong> A program used to make a pointer for each access, and may still: <code>mem[a].(I64) := n</code> does what</p> |
| 496 | +<pre><code>p := ^I64(ADR(mem[a])); |
| 497 | +p^ := n;</code></pre> |
| 498 | +<p>does, but with <code>a</code> checked against the length of <code>mem</code>, no pointer variable, and no address of a word that may be misaligned.</p> |
| 499 | +<p><code>v</code> is evaluated once, and its check made, before the bytes are read or written.</p> |
| 472 | 500 | <h1 id="statements">7. Statements</h1> |
| 473 | 501 | <pre><code>Statements = { Statement ";" } . |
| 474 | 502 | Statement = Designator [ ":=" Expr ] |
| | @@ -495,7 +523,7 @@ |
| 495 | 523 | <p>The blank, <code>_</code>, drops what would be assigned to it: <code>_ := e;</code> evaluates <code>e</code> and drops it, and <code>n, _ := f(x);</code> keeps the first result and drops the second. What is dropped is still evaluated as any expression is: its calls are made, and a volatile variable in it is read (section 7.6). A result dropped by a call standing on its own is a lint an implementation may warn of (section 10); one dropped through <code>_</code> says it is meant.</p> |
| 496 | 524 | <h2 id="conditions-and-loops">7.2 Conditions and loops</h2> |
| 497 | 525 | <p><code>if</code>, <code>while</code> and <code>repeat</code> take <code>BOOL</code> expressions. <code>repeat</code> runs its body at least once and stops when the condition is true.</p> |
| 498 | | -<p><code>for i := a to b by s do ... end;</code> sets <code>i</code> to <code>a</code>, <code>a + s</code>, … while <code>i <= b</code> (or <code>i >= b</code> if <code>s</code> is negative). <code>i</code> is a variable of an integer or enumeration type, named by its name alone; <code>a</code> and then <code>b</code> are evaluated once, before the first round; <code>s</code> is a constant other than zero, and 1 if left out. <code>a</code> and <code>b</code> have <code>i</code>’s type, or are untyped constants that take it, as with <code>+</code> (section 6.1): so in <code>for i := 0 to LEN(s) - 1</code>, <code>i</code> must be an <code>INT</code>.</p> |
| 526 | +<p><code>for i := a to b by s do ... end;</code> sets <code>i</code> to <code>a</code>, <code>a + s</code>, … while <code>i <= b</code> (or <code>i >= b</code> if <code>s</code> is negative). <code>i</code> is a variable of an integer or enumeration type, named by its name alone; <code>a</code> and then <code>b</code> are evaluated once, before the first round; <code>s</code> is a constant other than zero, and 1 if left out. <code>a</code> and <code>b</code> have <code>i</code>’s type, or are untyped constants that take it, or are <code>INT</code>s that widen to an <code>I64</code> <code>i</code>, as with <code>+</code> (section 6.1): so in <code>for i := 0 to LEN(s) - 1</code>, <code>i</code> is an <code>INT</code> or an <code>I64</code>.</p> |
| 499 | 527 | <p>The loop never steps past <code>b</code>, so <code>i</code> can’t wrap: afterwards <code>i</code> holds the last value it took, or <code>a</code> if the body never ran. The body may assign <code>i</code>. Precisely, for a positive <code>s</code>: at the end of a round, if <code>i <= b</code> and <code>b - i < s</code>, the loop ends with <code>i</code> as it is; otherwise <code>i</code> becomes <code>i + s</code>, wrapping, and the loop goes on while <code>i <= b</code>. A negative <code>s</code> is the same, the other way round.</p> |
| 500 | 528 | <p><code>break</code> ends the innermost <code>while</code>, <code>repeat</code> or <code>for</code> it stands in, and the program goes on after it. There is no <code>goto</code>.</p> |
| 501 | 529 | <h2 id="return">7.3 Return</h2> |
| | @@ -751,7 +779,7 @@ |
| 751 | 779 | Element = Expr | ident ":" Expr . |
| 752 | 780 | Designator = QualIdent { Selector } . |
| 753 | 781 | Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]" |
| 754 | | - | "(" [ Expr { "," Expr } ] ")" . |
| 782 | + | "(" [ Expr { "," Expr } ] ")" | "." "(" Type ")" . |
| 755 | 783 | |
| 756 | 784 | Statements = { Statement ";" } . |
| 757 | 785 | Statement = Designator [ ":=" Expr ] |
| | @@ -802,7 +830,7 @@ |
| 802 | 830 | </tr> |
| 803 | 831 | <tr> |
| 804 | 832 | <td><code>Sys</code></td> |
| 805 | | -<td>system calls: <code>open</code>, <code>openRead</code>, <code>create</code>, <code>read</code>, <code>write</code>, <code>seek</code>, <code>close</code>, <code>mmap</code>, <code>munmap</code>, <code>isTerminal</code>, <code>yield</code>, <code>exit</code>; files and directories, <code>rename</code>, <code>remove</code>, <code>removeDir</code>, <code>makeDir</code>, <code>chmod</code>, <code>symlink</code>, <code>readLink</code>, <code>info</code>, <code>openDir</code>, <code>nextEntry</code>, <code>closeDir</code>; the terminal, <code>rawMode</code>, <code>restoreMode</code>, <code>termSize</code>, <code>ready</code>; the environment, <code>env</code>, <code>workDir</code>, <code>setWorkDir</code>; programs, pipes and signals; time, <code>clock</code>, <code>now</code>, <code>sleep</code>; <code>random</code>; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and <code>Sys.raz</code> over the C library for the others</td> |
| 833 | +<td>system calls: <code>open</code>, <code>openRead</code>, <code>create</code>, <code>read</code>, <code>write</code>, <code>seek</code>, <code>position</code>, <code>truncate</code>, <code>sync</code>, <code>close</code>, <code>mmap</code>, <code>munmap</code>, <code>isTerminal</code>, <code>yield</code>, <code>exit</code>; files and directories, <code>rename</code>, <code>remove</code>, <code>removeDir</code>, <code>makeDir</code>, <code>chmod</code>, <code>symlink</code>, <code>readLink</code>, <code>info</code>, <code>infoOf</code>, <code>openDir</code>, <code>nextEntry</code>, <code>closeDir</code>; the terminal, <code>rawMode</code>, <code>restoreMode</code>, <code>termSize</code>, <code>ready</code>; the environment, <code>env</code>, <code>workDir</code>, <code>setWorkDir</code>; programs, pipes and signals; time, <code>clock</code>, <code>now</code>, <code>sleep</code>; <code>random</code>; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and <code>Sys.raz</code> over the C library for the others</td> |
| 806 | 834 | </tr> |
| 807 | 835 | <tr> |
| 808 | 836 | <td><code>Out</code></td> |
| | @@ -985,6 +1013,9 @@ |
| 985 | 1013 | proc openRead*(path: []U8): INT; (* open(path, {reading}) *) |
| 986 | 1014 | proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *) |
| 987 | 1015 | proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; (* the new position *) |
| 1016 | +proc position*(fd: INT): I64; (* where the next read or write is *) |
| 1017 | +proc truncate*(fd: INT; size: I64): INT; (* the file made size bytes long *) |
| 1018 | +proc sync*(fd: INT): INT; (* what was written to it, on the disk *) |
| 988 | 1019 | proc close*(fd: INT); |
| 989 | 1020 | proc isTerminal*(fd: INT): BOOL; |
| 990 | 1021 | proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *) |
| | @@ -1000,6 +1031,7 @@ |
| 1000 | 1031 | proc symlink*(target, path: []U8): INT; (* path made a link to target *) |
| 1001 | 1032 | proc readLink*(path: []U8): ([]U8, INT); (* what the link names, good until the next call *) |
| 1002 | 1033 | proc info*(path: []U8): (Info, INT); (* following a symbolic link *) |
| 1034 | +proc infoOf*(fd: INT): (Info, INT); (* the open file's *) |
| 1003 | 1035 | proc space*(path: []U8): (I64, I64, INT); (* the bytes free and in all on path's file system *) |
| 1004 | 1036 | |
| 1005 | 1037 | proc openDir*(d: ^Dir; path: []U8): INT; |
| | @@ -1057,7 +1089,7 @@ |
| 1057 | 1089 | proc mapFile*(fd, size: INT): ^U8; (* the first size bytes of fd, shared; munmap gives them back; or nil *)</code></pre> |
| 1058 | 1090 | <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> |
| 1059 | 1091 | <p><code>nextEntry</code> gives every name in the directory except <code>.</code> and <code>..</code>, in no particular order. <code>rawMode</code> turns off what the terminal does with keys before the program reads them (echo, line editing, signals, flow control), and NL to CR NL on output. A program that turns it on restores it before it ends: <code>Rt.atExit</code> is the place.</p> |
| 1060 | | -<p><strong>Files.</strong> <code>open</code> gives a file for what <code>how</code> asks: <code>reading</code>, <code>writing</code> or both; <code>creating</code> it if it is missing, readable by all and writable by its owner; with <code>exclusive</code>, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; <code>truncating</code> it to nothing; <code>appending</code>, each write going to its end, however other programs write it too. <code>seek</code> moves to <code>pos</code> from the start of the file, or from its end if <code>fromEnd</code>, where a negative <code>pos</code> reads back from the end. <code>Info.mtime</code> is what a program checks to see whether a file changed under it; <code>Info.mode</code> holds the nine bits of <code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for a program, 1A4H (644) for data.</p> |
| 1092 | +<p><strong>Files.</strong> <code>open</code> gives a file for what <code>how</code> asks: <code>reading</code>, <code>writing</code> or both; <code>creating</code> it if it is missing, readable by all and writable by its owner; with <code>exclusive</code>, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; <code>truncating</code> it to nothing; <code>appending</code>, each write going to its end, however other programs write it too. <code>seek</code> moves to <code>pos</code> from the start of the file, or from its end if <code>fromEnd</code>, where a negative <code>pos</code> reads back from the end; <code>position</code> says where the file is, without moving. <code>truncate</code> makes the file <code>size</code> bytes long, cutting it or adding zeros, and leaves the position where it was. <code>sync</code> returns once what was written to the file is on the disk, as a program needs before it says a file is saved. <code>infoOf</code> is <code>info</code> of an open file: its size is what it holds now, however it was written. <code>Info.mtime</code> is what a program checks to see whether a file changed under it; <code>Info.mode</code> holds the nine bits of <code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for a program, 1A4H (644) for data.</p> |
| 1061 | 1093 | <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> |
| 1062 | 1094 | <p><code>spawn</code> starts the program at <code>p.path</code>, with <code>p.args</code> as its arguments, the first being its name. Its environment is this program’s with each <code>NAME=value</code> of <code>p.env</code> added or put in place of the old value, or, if <code>p.onlyEnv</code>, <code>p.env</code> alone. It starts in <code>p.dir</code>. Its standard input, output and error are <code>p.fds</code>; a 0 gives it this program’s own, so <code>{path: "/bin/ls", args: ["ls"]}</code> runs <code>ls</code> as a shell runs it, with every other field zero. It gets no other open file, every signal does what it does at first, and none is blocked. If <code>p.tty</code> is a pseudo-terminal’s fd, the program starts a session on that terminal: it is its controlling terminal, and its standard input, output and error, and <code>p.fds</code> and <code>p.group</code> are not used.</p> |
| 1063 | 1095 | <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> |
| | @@ -1220,6 +1252,7 @@ |
| 1220 | 1252 | proc init*(p: ^Parser; name: []U8; table: []Row); (* the arguments from 1 on, by table *) |
| 1221 | 1253 | proc next*(p: ^Parser): BOOL; (* the next option; false when they end *) |
| 1222 | 1254 | proc operand*(p: ^Parser): ([]U8, BOOL); (* the next operand; false when there are no more *) |
| 1255 | +proc at*(p: ^Parser): INT; (* the argument operand would give next *) |
| 1223 | 1256 | proc scope*(p: ^Parser; name: []U8); (* subcommand name's options, from the word after it *) |
| 1224 | 1257 | proc number*(p: ^Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *) |
| 1225 | 1258 | proc fail*(p: ^Parser; text: []U8); (* the program's own mistake *)</code></pre> |
| | @@ -1253,7 +1286,9 @@ |
| 1253 | 1286 | <li><code>--</code> ends the options, and isn’t an operand; <code>-</code> alone is one. The options end at the first operand, so in <code>keep start site python -m http.server</code> the <code>-m</code> is python’s.</li> |
| 1254 | 1287 | <li>Each scope has its own options. An option given twice is read twice.</li> |
| 1255 | 1288 | </ul> |
| 1256 | | -<p><code>number</code> reads the value as <code>Str.toInt</code> does. <code>operand</code> reads what options are left first, and the program doesn’t see them.</p> |
| 1289 | +<p><code>number</code> reads the value as <code>Str.toInt</code> does. <code>operand</code> reads what options are left first, and the program doesn’t see them. So does <code>at</code>, which says where the operands are: the arguments from <code>operand</code>’s next on are <code>Files.arg(i)</code> for <code>i</code> from <code>at(p)</code> to <code>ARGC() - 1</code>, so that an interpreter hands a script its arguments as they are:</p> |
| 1290 | +<pre><code>script, ok := Opt.operand(ADR(p)); (* tery file arg ... *) |
| 1291 | +first := Opt.at(ADR(p)); (* file's own arguments, from here on *)</code></pre> |
| 1257 | 1292 | <p><code>-h</code> or <code>--help</code> among a scope’s options prints its help on standard output and ends the program with status 0. <code>init</code> and <code>scope</code> answer it, looking through the scope’s options before <code>next</code> gives one, so that the program has done nothing yet: <code>keep -v -h -x</code> prints the help, and the program never sees <code>-v</code>, nor the mistake. The help is the scope’s synopsis lines, a blank line, and its subcommands, its options and the help itself, each with its line of help:</p> |
| 1258 | 1293 | <pre><code>usage: keep start [-b label] name command [argument ...] |
| 1259 | 1294 | |