| | @@ -16,16 +16,13 @@ |
| 16 | 16 | <link rel="stylesheet" href="style.css"> |
| 17 | 17 | </head> |
| 18 | 18 | <body> |
| 19 | | -<header><nav><a class="mark" href="index.html"><img src="logo.svg" width="26" height="26" alt="">raz</a><a href="tour/index.html">Raz, Set, Go</a><a href="advanced/index.html">More Raz</a><a href="spec.html" aria-current="page">Report</a><a href="index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header> |
| 19 | +<header><nav><a class="mark" href="index.html"><img src="logo.svg" width="26" height="26" alt="">raz</a><a href="tour/index.html">Raz, Set, Go</a><a href="advanced/index.html">More Raz</a><a href="spec.html" aria-current="page">Report</a><a href="index.html#download">Download</a></nav></header> |
| 20 | 20 | <main> |
| 21 | 21 | <div class="spec"><ul> |
| 22 | | -<li><a href="#a-first-look" id="toc-a-first-look">1. A first |
| 23 | | -look</a></li> |
| 24 | | -<li><a href="#words-and-symbols" id="toc-words-and-symbols">2. Words and |
| 25 | | -symbols</a></li> |
| 22 | +<li><a href="#a-first-look" id="toc-a-first-look">1. A first look</a></li> |
| 23 | +<li><a href="#words-and-symbols" id="toc-words-and-symbols">2. Words and symbols</a></li> |
| 26 | 24 | <li><a href="#modules" id="toc-modules">3. Modules</a></li> |
| 27 | | -<li><a href="#declarations" id="toc-declarations">4. |
| 28 | | -Declarations</a></li> |
| 25 | +<li><a href="#declarations" id="toc-declarations">4. Declarations</a></li> |
| 29 | 26 | <li><a href="#types" id="toc-types">5. Types</a></li> |
| 30 | 27 | <li><a href="#expressions" id="toc-expressions">6. Expressions</a></li> |
| 31 | 28 | <li><a href="#statements" id="toc-statements">7. Statements</a></li> |
| | @@ -34,29 +31,13 @@ |
| 34 | 31 | <li><a href="#programs" id="toc-programs">10. Programs</a></li> |
| 35 | 32 | <li><a href="#the-library" id="toc-the-library">11. The library</a></li> |
| 36 | 33 | <li><a href="#versions" id="toc-versions">12. Versions</a></li> |
| 37 | | -<li><a href="#appendix-a.-what-an-implementation-chooses" |
| 38 | | -id="toc-appendix-a.-what-an-implementation-chooses">Appendix A. What an |
| 39 | | -implementation chooses</a></li> |
| 40 | | -<li><a href="#appendix-b.-the-grammar" |
| 41 | | -id="toc-appendix-b.-the-grammar">Appendix B. The grammar</a></li> |
| 34 | +<li><a href="#appendix-a.-what-an-implementation-chooses" id="toc-appendix-a.-what-an-implementation-chooses">Appendix A. What an implementation chooses</a></li> |
| 35 | +<li><a href="#appendix-b.-the-grammar" id="toc-appendix-b.-the-grammar">Appendix B. The grammar</a></li> |
| 42 | 36 | </ul> |
| 43 | | -<p>raz is a small systems language, first made for x86-64 Linux. A raz |
| 44 | | -program can lay a record over a device’s registers, read and write a |
| 45 | | -device’s memory exactly as written, take part in lock-free algorithms, |
| 46 | | -make system calls and call C libraries. It has no garbage collector and |
| 47 | | -no hidden run time: what the program does is what its text says.</p> |
| 48 | | -<p>This report defines the language. It is short on purpose. Whatever it |
| 49 | | -doesn’t allow, raz doesn’t have.</p> |
| 50 | | -<p>This is version 2026-10.17 of the report; section 12 says what each |
| 51 | | -version changed. 2026-10.11 and 2026-10.12 are a thaw the owner chose, |
| 52 | | -for a language anyone can build again. They break programs written for |
| 53 | | -2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, |
| 54 | | -the last release before them; and 2026-10.12 breaks code procedures |
| 55 | | -written for 2026-10.11.</p> |
| 56 | | -<p>The language is frozen, softly, from this version on: it changes only |
| 57 | | -to mend a defect, and its library grows only by what breaks no program |
| 58 | | -written for it. What anyone wishes it had waits on a list, for a later |
| 59 | | -thaw.</p> |
| 37 | +<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> |
| 38 | +<p>This report defines the language. It is short on purpose. Whatever it doesn’t allow, raz doesn’t have.</p> |
| 39 | +<p>This is version 2026-10.19 of the report; section 12 says what each version changed. 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.</p> |
| 40 | +<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> |
| 60 | 41 | <h1 id="a-first-look">1. A first look</h1> |
| 61 | 42 | <pre><code>module Hello; |
| 62 | 43 | import Out; |
| | @@ -67,82 +48,27 @@ |
| 67 | 48 | end; |
| 68 | 49 | |
| 69 | 50 | end Hello.</code></pre> |
| 70 | | -<p>A program is a set of modules. The one named on the command line is |
| 71 | | -the main module, and it exports a procedure <code>main</code>; the |
| 72 | | -program starts there, once every module’s body has run (section 3). |
| 73 | | -Saved as <code>Hello.raz</code>, it is compiled with |
| 74 | | -<code>raz Hello.raz</code>, which writes the executable |
| 75 | | -<code>Hello</code>.</p> |
| 51 | +<p>A program is a set of modules. The one named on the command line is the main module, and it exports a procedure <code>main</code>; the program starts there, once every module’s body has run (section 3). Saved as <code>Hello.raz</code>, it is compiled with <code>raz Hello.raz</code>, which writes the executable <code>Hello</code>.</p> |
| 76 | 52 | <h1 id="words-and-symbols">2. Words and symbols</h1> |
| 77 | | -<p>A program is written in UTF-8. The compiler rejects a file that isn’t |
| 78 | | -valid UTF-8: a malformed or overlong sequence, a surrogate, or a code |
| 79 | | -point beyond 10FFFFH. Everything outside strings, characters and |
| 80 | | -comments is ASCII. Spaces, tabs and line breaks separate symbols and are |
| 81 | | -otherwise ignored.</p> |
| 82 | | -<p><strong>Identifiers</strong> are an ASCII letter or <code>_</code> |
| 83 | | -followed by letters, digits and <code>_</code>, up to 63 characters. |
| 84 | | -Case matters: <code>count</code> and <code>Count</code> are different |
| 85 | | -names. <code>_</code> alone isn’t a name: it is the blank, which stands |
| 86 | | -only to the left of <code>:=</code> (section 7.1).</p> |
| 87 | | -<p><strong>Reserved words</strong> are lowercase, except |
| 88 | | -<code>VOLATILE</code>:</p> |
| 53 | +<p>A program is written in UTF-8. The compiler rejects a file that isn’t valid UTF-8: a malformed or overlong sequence, a surrogate, or a code point beyond 10FFFFH. Everything outside strings, characters and comments is ASCII. Spaces, tabs and line breaks separate symbols and are otherwise ignored.</p> |
| 54 | +<p><strong>Identifiers</strong> are an ASCII letter or <code>_</code> followed by letters, digits and <code>_</code>, up to 63 characters. Case matters: <code>count</code> and <code>Count</code> are different names. <code>_</code> alone isn’t a name: it is the blank, which stands only to the left of <code>:=</code> (section 7.1).</p> |
| 55 | +<p><strong>Reserved words</strong> are lowercase, except <code>VOLATILE</code>:</p> |
| 89 | 56 | <pre><code>and begin break by code const defer div do else elsif end extern for |
| 90 | 57 | forward if import in link match mod module not of or packed proc record |
| 91 | 58 | repeat return set shl shr then to type until var variant VOLATILE while |
| 92 | 59 | xor</code></pre> |
| 93 | | -<p><code>static</code>, in <code>link static;</code> (section 3), isn’t |
| 94 | | -one.</p> |
| 95 | | -<p><strong>Predeclared names</strong> are the basic types (section 5.1), |
| 96 | | -<code>ERROR</code>, <code>true</code>, <code>false</code>, |
| 97 | | -<code>nil</code> and the built-in procedures (section 6.8). They belong |
| 98 | | -to a scope around every module, so a module or a procedure may declare |
| 99 | | -the same name for something else, which hides them there.</p> |
| 100 | | -<p><strong>Numbers.</strong> <code>123</code> is decimal. |
| 101 | | -<code>0FFH</code> is hexadecimal: it starts with a digit, its digits are |
| 102 | | -<code>0</code> to <code>9</code> and <code>A</code> to <code>F</code>, |
| 103 | | -and it ends in <code>H</code>, all in capitals. <code>1.5</code> and |
| 104 | | -<code>2.0E-3</code> are reals; a real always has a digit on both sides |
| 105 | | -of its point, and its exponent, if it has one, is <code>E</code>, a sign |
| 106 | | -if wanted, and digits. A decimal number above the largest |
| 107 | | -<code>I64</code>, or a hexadecimal one above the largest |
| 108 | | -<code>U64</code>, is an error.</p> |
| 109 | | -<p>An integer is an untyped constant, which takes the type it is used |
| 110 | | -as, up to the largest <code>I64</code>. A hexadecimal number above that, |
| 111 | | -such as <code>0FFFFFFFFFFFFFFFFH</code>, has type <code>U64</code>: so |
| 112 | | -<code>-1</code> never fits a <code>U64</code>, and the big number goes |
| 113 | | -nowhere else without a conversion. Arithmetic on untyped constants |
| 114 | | -doesn’t wrap: a result beyond the <code>I64</code>s, such as |
| 115 | | -<code>9223372036854775807 + 1</code>, is an error.</p> |
| 116 | | -<p>A real is an untyped constant too, which takes <code>F32</code> or |
| 117 | | -<code>F64</code>, whichever it is used as, rounded to the nearest value |
| 118 | | -of that type; where nothing says which, it is an <code>F64</code>. A |
| 119 | | -real never becomes an integer, nor an integer a real, without a |
| 120 | | -conversion: <code>x := 1;</code> is an error for <code>x: F64</code>, |
| 121 | | -and <code>x := 1.0;</code> is right.</p> |
| 122 | | -<p><strong>Characters.</strong> <code>'a'</code> is the code point of |
| 123 | | -one character, an integer: <code>'a'</code> is 97 and <code>'é'</code> |
| 124 | | -is 233.</p> |
| 125 | | -<p><strong>Strings.</strong> <code>"héllo\n"</code> is a sequence of |
| 126 | | -bytes, the UTF-8 of its text: <code>"é"</code> is the two bytes 0C3H, |
| 127 | | -0A9H. Both characters and strings take the escapes <code>\n</code> (line |
| 128 | | -feed), <code>\t</code> (tab), <code>\r</code> (carriage return), |
| 129 | | -<code>\0</code> (zero), <code>\\</code>, <code>\'</code>, |
| 130 | | -<code>\"</code>, <code>\u{H...}</code> (the code point with hexadecimal |
| 131 | | -value H…, in a string its UTF-8) and, in strings only, <code>\xHH</code> |
| 132 | | -(the byte with hexadecimal value HH, which needn’t make valid UTF-8). In |
| 133 | | -an escape, the hexadecimal digits may be capitals or not. A character or |
| 134 | | -a string ends on the line it starts on.</p> |
| 135 | | -<p>Messages give the line and the column, counted in characters (section |
| 136 | | -10).</p> |
| 137 | | -<p><strong>Comments</strong> are <code>(*</code> … <code>*)</code> and |
| 138 | | -nest. By convention, the comment just before an exported declaration is |
| 139 | | -its documentation, and the one before <code>module</code> is the |
| 140 | | -module’s.</p> |
| 60 | +<p><code>static</code>, in <code>link static;</code> (section 3), isn’t one.</p> |
| 61 | +<p><strong>Predeclared names</strong> are the basic types (section 5.1), <code>ERROR</code>, <code>true</code>, <code>false</code>, <code>nil</code> and the built-in procedures (section 6.8). They belong to a scope around every module, so a module or a procedure may declare the same name for something else, which hides them there.</p> |
| 62 | +<p><strong>Numbers.</strong> <code>123</code> is decimal. <code>0FFH</code> is hexadecimal: it starts with a digit, its digits are <code>0</code> to <code>9</code> and <code>A</code> to <code>F</code>, and it ends in <code>H</code>, all in capitals. <code>1.5</code> and <code>2.0E-3</code> are reals; a real always has a digit on both sides of its point, and its exponent, if it has one, is <code>E</code>, a sign if wanted, and digits. A decimal number above the largest <code>I64</code>, or a hexadecimal one above the largest <code>U64</code>, is an error.</p> |
| 63 | +<p>An integer is an untyped constant, which takes the type it is used as, up to the largest <code>I64</code>. A hexadecimal number above that, such as <code>0FFFFFFFFFFFFFFFFH</code>, has type <code>U64</code>: so <code>-1</code> never fits a <code>U64</code>, and the big number goes nowhere else without a conversion. Arithmetic on untyped constants doesn’t wrap: a result beyond the <code>I64</code>s, such as <code>9223372036854775807 + 1</code>, is an error.</p> |
| 64 | +<p>A real is an untyped constant too, which takes <code>F32</code> or <code>F64</code>, whichever it is used as, rounded to the nearest value of that type; where nothing says which, it is an <code>F64</code>. A real never becomes an integer, nor an integer a real, without a conversion: <code>x := 1;</code> is an error for <code>x: F64</code>, and <code>x := 1.0;</code> is right.</p> |
| 65 | +<p><strong>Characters.</strong> <code>'a'</code> is the code point of one character, an integer: <code>'a'</code> is 97 and <code>'é'</code> is 233.</p> |
| 66 | +<p><strong>Strings.</strong> <code>"héllo\n"</code> is a sequence of bytes, the UTF-8 of its text: <code>"é"</code> is the two bytes 0C3H, 0A9H. Both characters and strings take the escapes <code>\n</code> (line feed), <code>\t</code> (tab), <code>\r</code> (carriage return), <code>\0</code> (zero), <code>\\</code>, <code>\'</code>, <code>\"</code>, <code>\u{H...}</code> (the code point with hexadecimal value H…, in a string its UTF-8) and, in strings only, <code>\xHH</code> (the byte with hexadecimal value HH, which needn’t make valid UTF-8). In an escape, the hexadecimal digits may be capitals or not. A character or a string ends on the line it starts on.</p> |
| 67 | +<p>Messages give the line and the column, counted in characters (section 10).</p> |
| 68 | +<p><strong>Comments</strong> are <code>(*</code> … <code>*)</code> and nest. By convention, the comment just before an exported declaration is its documentation, and the one before <code>module</code> is the module’s.</p> |
| 141 | 69 | <p><strong>Operators and delimiters:</strong></p> |
| 142 | 70 | <pre><code>+ - * / & | ~ = # < <= > >= := : ; , . .. ... ( ) [ ] { } ^</code></pre> |
| 143 | | -<p><code>|</code> is also what starts an arm of <code>match</code> |
| 144 | | -(section 7.4); an arm comes where a statement could, never after an |
| 145 | | -operand, so the two don’t mix.</p> |
| 71 | +<p><code>|</code> is also what starts an arm of <code>match</code> (section 7.4); an arm comes where a statement could, never after an operand, so the two don’t mix.</p> |
| 146 | 72 | <h1 id="modules">3. Modules</h1> |
| 147 | 73 | <pre><code>Module = "module" ident [ "(" ModParams ")" ] ";" |
| 148 | 74 | { Import } { Link } [ Check ] { Declaration } |
| | @@ -152,141 +78,40 @@ |
| 152 | 78 | ImportItem = ident [ ":=" ident [ "(" Type { "," Type } ")" ] ] . |
| 153 | 79 | Link = "link" ( string | "static" ) ";" . |
| 154 | 80 | Check = "check" "off" ";" .</code></pre> |
| 155 | | -<p>A module lives in a file of its own name: module <code>Out</code> is |
| 156 | | -<code>Out.raz</code>. The name after <code>end</code> repeats the |
| 157 | | -module’s name.</p> |
| 158 | | -<p><strong>Exports.</strong> A name declared at the top of a module with |
| 159 | | -<code>*</code> after it is exported: other modules can use it as |
| 160 | | -<code>Module.name</code>. Record fields are exported the same way. |
| 161 | | -Everything else is private to the module. An exported variable can be |
| 162 | | -read by the modules that import it, but only its own module changes it. |
| 163 | | -Outside it, the variable is a value, not a variable: nothing that could |
| 164 | | -write it, or let something else write it, may name it or a field or |
| 165 | | -element of it. So it can’t be assigned, passed as a <code>var</code> |
| 166 | | -argument, changed by <code>INC</code>, <code>DEC</code> or |
| 167 | | -<code>NEW</code>, or given to <code>ADR</code>; an array in it can’t be |
| 168 | | -sliced, or used where a slice is expected (the silent conversion of an |
| 169 | | -array, section 5.10), though a slice in it may be sliced, since that |
| 170 | | -makes a new slice and can’t change the variable; in <code>match</code>, |
| 171 | | -its arms’ names stand for the fields of a copy. It can be read, copied |
| 172 | | -and compared, and a copy is the importer’s own. What a pointer or a |
| 173 | | -slice in it points at is another variable, and isn’t held to this: with |
| 174 | | -<code>s: []U8</code> exported, <code>M.s[0] := 0</code> writes what |
| 175 | | -<code>M.s</code> points at, not <code>M</code>. A module that means to |
| 176 | | -share an array exports a pointer to it, or a procedure that returns a |
| 177 | | -slice of it.</p> |
| 178 | | -<p><strong>Imports.</strong> <code>import Out;</code> makes |
| 179 | | -<code>Out</code>’s exports available as <code>Out.x</code>. |
| 180 | | -<code>import O := Out;</code> uses the name <code>O</code> instead. A |
| 181 | | -module can’t import itself, directly or through others.</p> |
| 81 | +<p>A module lives in a file of its own name: module <code>Out</code> is <code>Out.raz</code>. The name after <code>end</code> repeats the module’s name.</p> |
| 82 | +<p><strong>Exports.</strong> A name declared at the top of a module with <code>*</code> after it is exported: other modules can use it as <code>Module.name</code>. Record fields are exported the same way. Everything else is private to the module. An exported variable can be read by the modules that import it, but only its own module changes it. Outside it, the variable is a value, not a variable: nothing that could write it, or let something else write it, may name it or a field or element of it. So it can’t be assigned, passed as a <code>var</code> argument, changed by <code>INC</code>, <code>DEC</code> or <code>NEW</code>, or given to <code>ADR</code>; an array in it can’t be sliced, or used where a slice is expected (the silent conversion of an array, section 5.10), though a slice in it may be sliced, since that makes a new slice and can’t change the variable; in <code>match</code>, its arms’ names stand for the fields of a copy. It can be read, copied and compared, and a copy is the importer’s own. What a pointer or a slice in it points at is another variable, and isn’t held to this: with <code>s: []U8</code> exported, <code>M.s[0] := 0</code> writes what <code>M.s</code> points at, not <code>M</code>. A module that means to share an array exports a pointer to it, or a procedure that returns a slice of it.</p> |
| 83 | +<p><strong>Imports.</strong> <code>import Out;</code> makes <code>Out</code>’s exports available as <code>Out.x</code>. <code>import O := Out;</code> uses the name <code>O</code> instead. A module can’t import itself, directly or through others.</p> |
| 182 | 84 | <p><strong>Generic modules</strong> take types as parameters:</p> |
| 183 | 85 | <pre><code>module List(T); |
| 184 | 86 | type |
| 185 | 87 | Node* = record next*: ^Node; val*: T; end; |
| 186 | 88 | ... |
| 187 | 89 | end List.</code></pre> |
| 188 | | -<p>An import gives the arguments: |
| 189 | | -<code>import Ints := List(I64);</code>. Each distinct list of arguments |
| 190 | | -makes one <em>instance</em>: the module compiled with <code>T</code> |
| 191 | | -standing for <code>I64</code>. Two imports of <code>List(I64)</code>, in |
| 192 | | -any modules, share one instance, its variables and its |
| 193 | | -<code>ERROR</code>s with it: two lists of arguments are the same when |
| 194 | | -their types are the same (section 5.10), an alias as what it stands for, |
| 195 | | -and their procedures are the same procedures. Since every instance is |
| 196 | | -checked on its own, an operation the argument type doesn’t support |
| 197 | | -(adding two records, say) is reported as an error in that instance.</p> |
| 198 | | -<p>An instance is made where its module is first used, not at the |
| 199 | | -import: so the arguments may name types the importing module declares |
| 200 | | -after its imports, as long as they are declared before that first use. |
| 201 | | -The instance is made before the code of the procedure, or the module |
| 202 | | -body, that first uses it, and its arguments are read in the scope |
| 203 | | -there.</p> |
| 90 | +<p>An import gives the arguments: <code>import Ints := List(I64);</code>. Each distinct list of arguments makes one <em>instance</em>: the module compiled with <code>T</code> standing for <code>I64</code>. Two imports of <code>List(I64)</code>, in any modules, share one instance, its variables and its <code>ERROR</code>s with it: two lists of arguments are the same when their types are the same (section 5.10), an alias as what it stands for, and their procedures are the same procedures. Since every instance is checked on its own, an operation the argument type doesn’t support (adding two records, say) is reported as an error in that instance.</p> |
| 91 | +<p>An instance is made where its module is first used, not at the import: so the arguments may name types the importing module declares after its imports, as long as they are declared before that first use. The instance is made before the code of the procedure, or the module body, that first uses it, and its arguments are read in the scope there.</p> |
| 204 | 92 | <pre><code>import Table := Map([]U8, Colour, Str.hash, Str.eq); |
| 205 | 93 | |
| 206 | 94 | type Colour = (red, green, blue); |
| 207 | 95 | |
| 208 | 96 | var names: Table.Map; (* the instance is made here *)</code></pre> |
| 209 | | -<p>What a generic module needs to do with its types, beyond moving them |
| 210 | | -about, it is given as procedures, after the types:</p> |
| 97 | +<p>What a generic module needs to do with its types, beyond moving them about, it is given as procedures, after the types:</p> |
| 211 | 98 | <pre><code>module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);</code></pre> |
| 212 | 99 | <pre><code>import Names := Map([]U8, I64, Str.hash, Str.eq);</code></pre> |
| 213 | | -<p>A procedure argument names a procedure, not a variable or an |
| 214 | | -expression, and its signature, with the type arguments put in, must be |
| 215 | | -the parameter’s. Inside the module, <code>hash(k)</code> calls it |
| 216 | | -directly. Two imports make the same instance only if their types and |
| 217 | | -their procedures are the same.</p> |
| 218 | | -<p><strong>Finding modules.</strong> The compiler looks for module |
| 219 | | -<code>Name</code> in the main module’s directory, then in each directory |
| 220 | | -given with <code>-I</code>, then in the <code>lib</code> directory |
| 221 | | -beside the compiler. In each directory it takes the first of |
| 222 | | -<code>Name.OS-ARCH.raz</code>, <code>Name.OS.raz</code>, |
| 223 | | -<code>Name.ARCH.raz</code> and <code>Name.raz</code> that exists, where |
| 224 | | -<code>OS-ARCH</code> is the target, for now only |
| 225 | | -<code>linux-x86_64</code>. So a module whose code depends on the machine |
| 226 | | -or the system comes in one file per target, and the modules that import |
| 227 | | -it don’t change. Machine code (section 8) and system calls belong in |
| 228 | | -such files.</p> |
| 229 | | -<p><strong>One module to a name.</strong> A program has one module of |
| 230 | | -each name, so the compiler reports an error, naming the files, if a |
| 231 | | -module it looks for has files in more than one of these directories, |
| 232 | | -rather than take the first: a <code>Buf.raz</code> beside a program |
| 233 | | -would otherwise stand in for the library’s <code>Buf</code> in every |
| 234 | | -module that imports it, the library’s own among them. A link is the |
| 235 | | -exception: a file that is a symbolic link to one of the module’s files |
| 236 | | -in a later directory chooses that file, so that a directory holding a |
| 237 | | -link to <code>lib/Sys.raz</code> builds a program over the C library’s |
| 238 | | -<code>Sys</code> on Linux too.</p> |
| 239 | | -<p><strong>Names are flat.</strong> The names of a program’s modules are |
| 240 | | -one space, with no packages or paths. So the names of the library’s |
| 241 | | -modules (section 11) are kept for it: a program or another library |
| 242 | | -doesn’t name a module of its own <code>Arena</code>, <code>Buf</code>, |
| 243 | | -<code>C</code>, <code>Files</code>, <code>Fmt</code>, <code>Heap</code>, |
| 244 | | -<code>List</code>, <code>Lock</code>, <code>Map</code>, |
| 245 | | -<code>Math</code>, <code>Mem</code>, <code>Opt</code>, <code>Out</code>, |
| 246 | | -<code>Rt</code>, <code>Sort</code>, <code>Str</code>, <code>Sys</code> |
| 247 | | -or <code>Thread</code>, nor any name the library takes later. The |
| 248 | | -modules of any other library share a prefix of their own, as an X11 |
| 249 | | -library’s <code>Xauth</code> and <code>Xkeys</code> do, and a program |
| 250 | | -that wants a shorter name imports one: |
| 251 | | -<code>import Auth := Xauth;</code>.</p> |
| 252 | | -<p>The compiler holds every file of such a module to the same interface. |
| 253 | | -When it takes one of <code>Name.*.raz</code>, it also reads the |
| 254 | | -declarations of the others beside it, and of <code>Name.raz</code> if |
| 255 | | -there is one, and reports an error unless they all export the same |
| 256 | | -names, each the same kind of thing:</p> |
| 100 | +<p>A procedure argument names a procedure, not a variable or an expression, and its signature, with the type arguments put in, must be the parameter’s. Inside the module, <code>hash(k)</code> calls it directly. Two imports make the same instance only if their types and their procedures are the same.</p> |
| 101 | +<p><strong>Finding modules.</strong> The compiler looks for module <code>Name</code> in the main module’s directory, then in each directory given with <code>-I</code>, then in the <code>lib</code> directory beside the compiler. In each directory it takes the first of <code>Name.OS-ARCH.raz</code>, <code>Name.OS.raz</code>, <code>Name.ARCH.raz</code> and <code>Name.raz</code> that exists, where <code>OS-ARCH</code> is the target, for now only <code>linux-x86_64</code>. So a module whose code depends on the machine or the system comes in one file per target, and the modules that import it don’t change. Machine code (section 8) and system calls belong in such files.</p> |
| 102 | +<p><strong>One module to a name.</strong> A program has one module of each name, so the compiler reports an error, naming the files, if a module it looks for has files in more than one of these directories, rather than take the first: a <code>Buf.raz</code> beside a program would otherwise stand in for the library’s <code>Buf</code> in every module that imports it, the library’s own among them. A link is the exception: a file that is a symbolic link to one of the module’s files in a later directory chooses that file, so that a directory holding a link to <code>lib/Sys.raz</code> builds a program over the C library’s <code>Sys</code> on Linux too.</p> |
| 103 | +<p><strong>Names are flat.</strong> The names of a program’s modules are one space, with no packages or paths. So the names of the library’s modules (section 11) are kept for it: a program or another library doesn’t name a module of its own <code>Arena</code>, <code>Buf</code>, <code>C</code>, <code>Files</code>, <code>Fmt</code>, <code>Heap</code>, <code>List</code>, <code>Lock</code>, <code>Map</code>, <code>Math</code>, <code>Mem</code>, <code>Opt</code>, <code>Out</code>, <code>Rt</code>, <code>Sort</code>, <code>Str</code>, <code>Sys</code> or <code>Thread</code>, nor any name the library takes later. The modules of any other library share a prefix of their own, as an X11 library’s <code>Xauth</code> and <code>Xkeys</code> do, and a program that wants a shorter name imports one: <code>import Auth := Xauth;</code>.</p> |
| 104 | +<p>The compiler holds every file of such a module to the same interface. When it takes one of <code>Name.*.raz</code>, it also reads the declarations of the others beside it, and of <code>Name.raz</code> if there is one, and reports an error unless they all export the same names, each the same kind of thing:</p> |
| 257 | 105 | <ul> |
| 258 | | -<li>a constant of the same type (its value may differ: a system call |
| 259 | | -number, say);</li> |
| 260 | | -<li>a type of the same kind, and for a record or variant the same |
| 261 | | -exported fields and cases, with the same types (unexported fields, and |
| 262 | | -so the size and layout, may differ);</li> |
| 106 | +<li>a constant of the same type (its value may differ: a system call number, say);</li> |
| 107 | +<li>a type of the same kind, and for a record or variant the same exported fields and cases, with the same types (unexported fields, and so the size and layout, may differ);</li> |
| 263 | 108 | <li>a variable of the same type;</li> |
| 264 | 109 | <li>a procedure of the same signature.</li> |
| 265 | 110 | </ul> |
| 266 | | -<p>Two types here are the same if they are built alike, a name in one |
| 267 | | -file standing for the same name in another.</p> |
| 268 | | -<p><strong>Module bodies.</strong> A module may end with |
| 269 | | -<code>begin</code> and statements, its body. Every module’s body runs |
| 270 | | -once, before the main module’s <code>main</code>: a module’s body runs |
| 271 | | -after the bodies of the modules it imports, in the order of its |
| 272 | | -<code>import</code>s, and once however many modules import it. Each |
| 273 | | -instance of a generic module has its body run once. Module variables are |
| 274 | | -zero before any body runs. <code>return;</code> ends a body early.</p> |
| 275 | | -<p><strong><code>link "libX11.so.6";</code></strong> names a shared |
| 276 | | -library to load at run time, for the <code>extern</code> declarations of |
| 277 | | -section 9. <strong><code>link static;</code></strong> says the module |
| 278 | | -needs a program without C (section 10): it makes its own system calls, |
| 279 | | -or its own threads, which the C library wouldn’t know about.</p> |
| 280 | | -<p><strong><code>check off;</code></strong>, after the links, leaves out |
| 281 | | -of the module’s own code the checks of indexes and slice bounds of |
| 282 | | -section 6.5, and that a slice of a pointer doesn’t run backwards |
| 283 | | -(section 6.4), for speed where the program is sure of them. An index out |
| 284 | | -of range there reads or writes whatever memory it reaches, and what |
| 285 | | -follows isn’t defined. Every other check stays, and the module’s callers |
| 286 | | -keep theirs. An instance of a generic module follows the generic |
| 287 | | -module’s own <code>check</code> line, not that of the module that made |
| 288 | | -it. <code>check</code> and <code>off</code> aren’t reserved words, as |
| 289 | | -<code>static</code> isn’t.</p> |
| 111 | +<p>Two types here are the same if they are built alike, a name in one file standing for the same name in another.</p> |
| 112 | +<p><strong>Module bodies.</strong> A module may end with <code>begin</code> and statements, its body. Every module’s body runs once, before the main module’s <code>main</code>: a module’s body runs after the bodies of the modules it imports, in the order of its <code>import</code>s, and once however many modules import it. Each instance of a generic module has its body run once. Module variables are zero before any body runs. <code>return;</code> ends a body early.</p> |
| 113 | +<p><strong><code>link "libX11.so.6";</code></strong> names a shared library to load at run time, for the <code>extern</code> declarations of section 9. <strong><code>link static;</code></strong> says the module needs a program without C (section 10): it makes its own system calls, or its own threads, which the C library wouldn’t know about.</p> |
| 114 | +<p><strong><code>check off;</code></strong>, after the links, leaves out of the module’s own code the checks of indexes and slice bounds of section 6.5, and that a slice of a pointer doesn’t run backwards (section 6.4), for speed where the program is sure of them. An index out of range there reads or writes whatever memory it reaches, and what follows isn’t defined. Every other check stays, and the module’s callers keep theirs. An instance of a generic module follows the generic module’s own <code>check</code> line, not that of the module that made it. <code>check</code> and <code>off</code> aren’t reserved words, as <code>static</code> isn’t.</p> |
| 290 | 115 | <h1 id="declarations">4. Declarations</h1> |
| 291 | 116 | <pre><code>Declaration = "const" { IdentDef [ ":" Type ] "=" Expr ";" } |
| 292 | 117 | | "type" { IdentDef "=" Type ";" } |
| | @@ -295,55 +120,20 @@ |
| 295 | 120 | | ExternDeclaration ";" . |
| 296 | 121 | IdentDef = ident [ "*" ] . |
| 297 | 122 | IdentList = IdentDef { "," IdentDef } .</code></pre> |
| 298 | | -<p>A name must be declared before it is used, with two exceptions: the |
| 299 | | -base type of a pointer may name a type declared later in the same |
| 300 | | -<code>type</code> section (so that two records can point at each other), |
| 301 | | -and a procedure may be declared <code>forward</code> and defined later |
| 302 | | -(section 8).</p> |
| 303 | | -<p><strong>Constants</strong> are evaluated by the compiler. Their |
| 304 | | -expressions use only literals, other constants, operators, conversions, |
| 305 | | -<code>SIZE</code> and the <code>LEN</code> of arrays and strings. A |
| 306 | | -constant is an integer, a real, a <code>BOOL</code>, a name of an |
| 307 | | -enumeration, a set, a string, an array, a record or an |
| 308 | | -<code>ERROR</code>. A constant of an integer type wraps as the program |
| 309 | | -would: <code>U8(250) + 10</code> is 4. Dividing by zero in a constant is |
| 310 | | -an error, and so is dividing the smallest value of a signed type by -1, |
| 311 | | -with <code>div</code> or <code>mod</code>, which stops a program |
| 312 | | -(section 6.1). A string constant can be indexed and sliced as an array |
| 313 | | -of <code>U8</code> is, and its <code>LEN</code> is its length in bytes. |
| 314 | | -<code>+</code> joins two string constants into one:</p> |
| 123 | +<p>A name must be declared before it is used, with two exceptions: the base type of a pointer may name a type declared later in the same <code>type</code> section (so that two records can point at each other), and a procedure may be declared <code>forward</code> and defined later (section 8).</p> |
| 124 | +<p><strong>Constants</strong> are evaluated by the compiler. Their expressions use only literals, other constants, operators, conversions, <code>SIZE</code> and the <code>LEN</code> of arrays and strings. A constant is an integer, a real, a <code>BOOL</code>, a name of an enumeration, a set, a string, an array, a record or an <code>ERROR</code>. A constant of an integer type wraps as the program would: <code>U8(250) + 10</code> is 4. Dividing by zero in a constant is an error, and so is dividing the smallest value of a signed type by -1, with <code>div</code> or <code>mod</code>, which stops a program (section 6.1). A string constant can be indexed and sliced as an array of <code>U8</code> is, and its <code>LEN</code> is its length in bytes. <code>+</code> joins two string constants into one:</p> |
| 315 | 125 | <pre><code>const Text = "the cat"; |
| 316 | 126 | Line = Text + "\n"; |
| 317 | 127 | ... |
| 318 | 128 | w := Text[4..7]; (* "cat" *)</code></pre> |
| 319 | | -<p>A constant with a type is an <code>ERROR</code> (section 5.8), an |
| 320 | | -array or a record, and the value of an array or record constant is a |
| 321 | | -literal (section 6.5) of constants. Its elements and fields are |
| 322 | | -integers, reals, <code>BOOL</code>s, enumerations, sets, |
| 323 | | -<code>ERROR</code>s, procedures, <code>nil</code>, a string for a |
| 324 | | -<code>[]U8</code>, an array constant for a slice of its element type, |
| 325 | | -and arrays and records of these; no variant and no other pointer:</p> |
| 129 | +<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> |
| 326 | 130 | <pre><code>const Primes: [5]I64 = [2, 3, 5, 7, 11]; |
| 327 | 131 | type Rule = record class: []U8; tag: INT; float: BOOL; end; |
| 328 | 132 | const Rules: [2]Rule = [{class: "firefox", tag: 2}, {class: "mpv", float: true}]; |
| 329 | 133 | ... |
| 330 | 134 | p := Primes[i];</code></pre> |
| 331 | | -<p>It is used as a variable of that type is, but can’t be changed: an |
| 332 | | -array constant can be indexed and sliced and its <code>LEN</code> is |
| 333 | | -<code>n</code>, a record constant’s fields can be read, and either can |
| 334 | | -be assigned, compared as its type allows, and passed. Its parts are |
| 335 | | -constant as a string’s bytes are (section 5.10). A part of it isn’t a |
| 336 | | -constant, even at a constant index.</p> |
| 337 | | -<p><strong>Variables</strong> of a module live as long as the program; |
| 338 | | -variables of a procedure live as long as its call. Both start at zero: |
| 339 | | -every byte of them is zero. So a number starts as <code>0</code> |
| 340 | | -(<code>0.0</code> for <code>F32</code> and <code>F64</code>), a |
| 341 | | -<code>BOOL</code> as <code>false</code>, a pointer, procedure or |
| 342 | | -<code>ERROR</code> as <code>nil</code>, a slice as the empty slice |
| 343 | | -(<code>ptr</code> is <code>nil</code>, <code>len</code> is 0), a variant |
| 344 | | -as its first case with its fields zero, a set as the empty set, and an |
| 345 | | -enumeration as its first name. No variable is ever read before it has a |
| 346 | | -value.</p> |
| 135 | +<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> |
| 136 | +<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> |
| 347 | 137 | <h1 id="types">5. Types</h1> |
| 348 | 138 | <pre><code>Type = QualIdent |
| 349 | 139 | | "[" Expr "]" Type (* array *) |
| | @@ -359,116 +149,68 @@ |
| 359 | 149 | QualIdent = [ ident "." ] ident .</code></pre> |
| 360 | 150 | <h2 id="basic-types">5.1 Basic types</h2> |
| 361 | 151 | <table> |
| 362 | | -<colgroup> |
| 363 | | -<col style="width: 33%" /> |
| 364 | | -<col style="width: 33%" /> |
| 365 | | -<col style="width: 33%" /> |
| 366 | | -</colgroup> |
| 367 | 152 | <thead> |
| 368 | | -<tr class="header"> |
| 153 | +<tr> |
| 369 | 154 | <th>Type</th> |
| 370 | 155 | <th>Size</th> |
| 371 | 156 | <th>Values</th> |
| 372 | 157 | </tr> |
| 373 | 158 | </thead> |
| 374 | 159 | <tbody> |
| 375 | | -<tr class="odd"> |
| 376 | | -<td><code>I8</code> <code>I16</code> <code>I32</code> |
| 377 | | -<code>I64</code></td> |
| 160 | +<tr> |
| 161 | +<td><code>I8</code> <code>I16</code> <code>I32</code> <code>I64</code></td> |
| 378 | 162 | <td>1, 2, 4, 8</td> |
| 379 | 163 | <td>signed integers, two’s complement</td> |
| 380 | 164 | </tr> |
| 381 | | -<tr class="even"> |
| 165 | +<tr> |
| 382 | 166 | <td><code>INT</code></td> |
| 383 | 167 | <td>an address’s: 8 on x86-64</td> |
| 384 | 168 | <td>signed, as big as an address</td> |
| 385 | 169 | </tr> |
| 386 | | -<tr class="odd"> |
| 170 | +<tr> |
| 387 | 171 | <td><code>UINT</code></td> |
| 388 | 172 | <td>an address’s</td> |
| 389 | 173 | <td>unsigned, as big as an address</td> |
| 390 | 174 | </tr> |
| 391 | | -<tr class="even"> |
| 392 | | -<td><code>U8</code> <code>U16</code> <code>U32</code> |
| 393 | | -<code>U64</code></td> |
| 175 | +<tr> |
| 176 | +<td><code>U8</code> <code>U16</code> <code>U32</code> <code>U64</code></td> |
| 394 | 177 | <td>1, 2, 4, 8</td> |
| 395 | 178 | <td>unsigned integers</td> |
| 396 | 179 | </tr> |
| 397 | | -<tr class="odd"> |
| 180 | +<tr> |
| 398 | 181 | <td><code>BOOL</code></td> |
| 399 | 182 | <td>1</td> |
| 400 | 183 | <td><code>false</code> (0), <code>true</code> (1)</td> |
| 401 | 184 | </tr> |
| 402 | | -<tr class="even"> |
| 185 | +<tr> |
| 403 | 186 | <td><code>F32</code></td> |
| 404 | 187 | <td>4</td> |
| 405 | 188 | <td>IEEE 754 single precision</td> |
| 406 | 189 | </tr> |
| 407 | | -<tr class="odd"> |
| 190 | +<tr> |
| 408 | 191 | <td><code>F64</code></td> |
| 409 | 192 | <td>8</td> |
| 410 | 193 | <td>IEEE 754 double precision</td> |
| 411 | 194 | </tr> |
| 412 | 195 | </tbody> |
| 413 | 196 | </table> |
| 414 | | -<p><code>INT</code> is the everyday integer: lengths, indexes and sizes |
| 415 | | -are <code>INT</code>s. It is as big as an address, so it is the integer |
| 416 | | -a pointer converts to (section 6.7), and a word the atomic built-ins |
| 417 | | -work on (section 6.8). <code>INT</code> and <code>I64</code> are two |
| 418 | | -types, even where they have the same size: an <code>INT</code> goes |
| 419 | | -silently into an <code>I64</code> (section 5.10), but the two don’t mix |
| 420 | | -in an expression (section 6.1), and an <code>I64</code> becomes an |
| 421 | | -<code>INT</code> only by <code>INT(x)</code>, which may drop bits on a |
| 422 | | -smaller target. A type bigger than the largest <code>INT</code> in bytes |
| 423 | | -is an error.</p> |
| 424 | | -<p><code>F32</code> and <code>F64</code> exist only on a target that has |
| 425 | | -reals (appendix A). On one that hasn’t, a real literal, and a use of |
| 426 | | -<code>F32</code>, <code>F64</code> or of a name whose type is made of |
| 427 | | -them, is an error; a declaration that no used code names isn’t, so a |
| 428 | | -module like <code>Fmt</code> keeps one interface on every target.</p> |
| 429 | | -<p>Each is aligned as the target’s C aligns it: to its size on x86-64. |
| 430 | | -So are <code>ERROR</code> (section 5.8), pointers and procedures, each |
| 431 | | -as big as an address; a slice is aligned as an address is.</p> |
| 197 | +<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> |
| 198 | +<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> |
| 199 | +<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> |
| 432 | 200 | <h2 id="arrays">5.2 Arrays</h2> |
| 433 | | -<p><code>[n]T</code> is <code>n</code> elements of type <code>T</code>, |
| 434 | | -one after another. <code>n</code> is a constant greater than zero. |
| 435 | | -Arrays are values: assigning one copies it.</p> |
| 201 | +<p><code>[n]T</code> is <code>n</code> elements of type <code>T</code>, one after another. <code>n</code> is a constant greater than zero. Arrays are values: assigning one copies it.</p> |
| 436 | 202 | <h2 id="slices">5.3 Slices</h2> |
| 437 | | -<p><code>[]T</code> is a view of elements that live elsewhere: a pointer |
| 438 | | -to the first and a count, two addresses’ worth, 16 bytes on x86-64.</p> |
| 203 | +<p><code>[]T</code> is a view of elements that live elsewhere: a pointer to the first and a count, two addresses’ worth, 16 bytes on x86-64.</p> |
| 439 | 204 | <pre><code>Slice = record ptr: ^T; len: INT; end; (* its layout *)</code></pre> |
| 440 | | -<p>A slice is made from an array (<code>a[2..5]</code>, or an array used |
| 441 | | -where a slice is wanted), from another slice, or from a pointer |
| 442 | | -(<code>p[0..n]</code>). It doesn’t own what it points at.</p> |
| 205 | +<p>A slice is made from an array (<code>a[2..5]</code>, or an array used where a slice is wanted), from another slice, or from a pointer (<code>p[0..n]</code>). It doesn’t own what it points at.</p> |
| 443 | 206 | <h2 id="pointers">5.4 Pointers</h2> |
| 444 | | -<p><code>^T</code> is the address of a <code>T</code>, as big as an |
| 445 | | -<code>INT</code>. <code>nil</code> is the pointer to nothing.</p> |
| 446 | | -<p><code>^VOLATILE T</code> is the address of a <code>T</code> that |
| 447 | | -something besides the program may read or change: a device register, |
| 448 | | -memory shared with another process. Section 7.6 says what that |
| 449 | | -promises.</p> |
| 207 | +<p><code>^T</code> is the address of a <code>T</code>, as big as an <code>INT</code>. <code>nil</code> is the pointer to nothing.</p> |
| 208 | +<p><code>^VOLATILE T</code> is the address of a <code>T</code> that something besides the program may read or change: a device register, memory shared with another process. Section 7.6 says what that promises.</p> |
| 450 | 209 | <h2 id="records">5.5 Records</h2> |
| 451 | 210 | <pre><code>type Cell = record ch: U8; attr: U8; end;</code></pre> |
| 452 | | -<p>Fields are laid out in order as C lays them out: each at the next |
| 453 | | -multiple of its own alignment, and the whole record padded to a multiple |
| 454 | | -of its largest alignment. A record declared like a C struct has the same |
| 455 | | -layout as that struct. A record may have no fields; its size is then |
| 456 | | -0.</p> |
| 457 | | -<p>A <code>packed record</code> has no padding: each field starts at the |
| 458 | | -byte after the field before it, and the record’s alignment is 1. So a |
| 459 | | -field of a packed record may sit at any address. It is read and written |
| 460 | | -like any other, on every target, even one whose machine can’t reach a |
| 461 | | -misaligned word in one access: the compiled code reaches it in parts |
| 462 | | -there. But a field of a packed record whose type is aligned to more than |
| 463 | | -1 byte, and anything inside it, has no address: <code>ADR</code> of it, |
| 464 | | -a <code>var</code> parameter of it and a slice of it are errors. Nor can |
| 465 | | -it be reached through a <code>^VOLATILE</code> pointer, since a |
| 466 | | -misaligned access can’t be promised as one access (section 7.6). Its |
| 467 | | -value can be copied out and back in.</p> |
| 468 | | -<p>The bytes of a value of more than one byte are in the target’s order |
| 469 | | -(appendix A). A file or a message in an order of its own is read and |
| 470 | | -written a byte at a time, as <code>Str.le</code>, <code>Str.be</code>, |
| 471 | | -<code>Str.putLE</code> and <code>Str.putBE</code> do (section 11).</p> |
| 211 | +<p>Fields are laid out in order as C lays them out: each at the next multiple of its own alignment, and the whole record padded to a multiple of its largest alignment. A record declared like a C struct has the same layout as that struct. A record may have no fields; its size is then 0.</p> |
| 212 | +<p>A <code>packed record</code> has no padding: each field starts at the byte after the field before it, and the record’s alignment is 1. So a field of a packed record may sit at any address. It is read and written like any other, on every target, even one whose machine can’t reach a misaligned word in one access: the compiled code reaches it in parts there. But a field of a packed record whose type is aligned to more than 1 byte, and anything inside it, has no address: <code>ADR</code> of it, a <code>var</code> parameter of it and a slice of it are errors. Nor can it be reached through a <code>^VOLATILE</code> pointer, since a misaligned access can’t be promised as one access (section 7.6). Its value can be copied out and back in.</p> |
| 213 | +<p>The bytes of a value of more than one byte are in the target’s order (appendix A). A file or a message in an order of its own is read and written a byte at a time, as <code>Str.le</code>, <code>Str.be</code>, <code>Str.putLE</code> and <code>Str.putBE</code> do (section 11).</p> |
| 472 | 214 | <h2 id="variants">5.6 Variants</h2> |
| 473 | 215 | <p>A variant holds one of several cases, each with its own fields:</p> |
| 474 | 216 | <pre><code>type |
| | @@ -477,110 +219,40 @@ |
| 477 | 219 | rect(w, h: F64); |
| 478 | 220 | dot; |
| 479 | 221 | end;</code></pre> |
| 480 | | -<p>The case names are declared in the scope around the type, so |
| 481 | | -<code>circle</code> can be used directly, and as |
| 482 | | -<code>Shapes.circle</code> from another module if <code>Shape</code> is |
| 483 | | -exported. A case with fields is built by calling it like a procedure, |
| 484 | | -<code>circle(2.0)</code>; a case without fields is just |
| 485 | | -<code>dot</code>. The only way to read the fields is <code>match</code> |
| 486 | | -(section 7.4).</p> |
| 487 | | -<p>Layout: an <code>INT</code> tag (0 for the first case, 1 for the |
| 488 | | -second, and so on), then the space for the fields. Each case’s fields |
| 489 | | -are laid out in that space as a record’s would be, after the tag, and |
| 490 | | -the cases share it, so the variant is as big as its largest case needs, |
| 491 | | -padded to its alignment, the largest of an <code>INT</code>‘s and its |
| 492 | | -fields’. <code>variant a(x: U8); b(y: I64; z: U8); end</code> is 24 |
| 493 | | -bytes on x86-64. A variant that starts at zero therefore holds its first |
| 494 | | -case.</p> |
| 222 | +<p>The case names are declared in the scope around the type, so <code>circle</code> can be used directly, and as <code>Shapes.circle</code> from another module if <code>Shape</code> is exported. A case with fields is built by calling it like a procedure, <code>circle(2.0)</code>; a case without fields is just <code>dot</code>. The only way to read the fields is <code>match</code> (section 7.4).</p> |
| 223 | +<p>Layout: an <code>INT</code> tag (0 for the first case, 1 for the second, and so on), then the space for the fields. Each case’s fields are laid out in that space as a record’s would be, after the tag, and the cases share it, so the variant is as big as its largest case needs, padded to its alignment, the largest of an <code>INT</code>‘s and its fields’. <code>variant a(x: U8); b(y: I64; z: U8); end</code> is 24 bytes on x86-64. A variant that starts at zero therefore holds its first case.</p> |
| 495 | 224 | <h2 id="enumerations-and-sets">5.7 Enumerations and sets</h2> |
| 496 | 225 | <pre><code>type |
| 497 | 226 | Color* = (red, green, blue); |
| 498 | 227 | Colors* = set of Color;</code></pre> |
| 499 | | -<p>An enumeration is a list of names, in order. Like the cases of a |
| 500 | | -variant, the names are declared in the scope around the type. An |
| 501 | | -enumeration has at most 256 names and takes 1 byte; the first name is 0, |
| 502 | | -the next 1, and so on.</p> |
| 503 | | -<p><code>set of E</code>, for an enumeration <code>E</code> of at most |
| 504 | | -64 names, holds any choice of <code>E</code>’s names: bit <code>i</code> |
| 505 | | -stands for the name at position <code>i</code>. It takes the smallest of |
| 506 | | -1, 2, 4 and 8 bytes that has a bit for each name, so a set can stand for |
| 507 | | -a C flags word.</p> |
| 228 | +<p>An enumeration is a list of names, in order. Like the cases of a variant, the names are declared in the scope around the type. An enumeration has at most 256 names and takes 1 byte; the first name is 0, the next 1, and so on.</p> |
| 229 | +<p><code>set of E</code>, for an enumeration <code>E</code> of at most 64 names, holds any choice of <code>E</code>’s names: bit <code>i</code> stands for the name at position <code>i</code>. It takes the smallest of 1, 2, 4 and 8 bytes that has a bit for each name, so a set can stand for a C flags word.</p> |
| 508 | 230 | <h2 id="errors">5.8 Errors</h2> |
| 509 | 231 | <pre><code>const NotFound*: ERROR = "not found";</code></pre> |
| 510 | | -<p><code>ERROR</code> is a basic type as big as an address. A value of |
| 511 | | -it is made only by a constant declared as above, and each such |
| 512 | | -declaration makes an error different from every other, whatever its |
| 513 | | -message: two modules that both declare <code>"not found"</code> have two |
| 514 | | -different errors. A generic module makes its errors once per instance. |
| 515 | | -<code>nil</code> is no error.</p> |
| 516 | | -<p>Errors compare with <code>=</code> and <code>#</code> only, by which |
| 517 | | -declaration made them, never by their message. <code>MSG(e)</code> is |
| 518 | | -the message, a <code>[]U8</code>; <code>MSG(nil)</code> is |
| 519 | | -<code>""</code>.</p> |
| 232 | +<p><code>ERROR</code> is a basic type as big as an address. A value of it is made only by a constant declared as above, and each such declaration makes an error different from every other, whatever its message: two modules that both declare <code>"not found"</code> have two different errors. A generic module makes its errors once per instance. <code>nil</code> is no error.</p> |
| 233 | +<p>Errors compare with <code>=</code> and <code>#</code> only, by which declaration made them, never by their message. <code>MSG(e)</code> is the message, a <code>[]U8</code>; <code>MSG(nil)</code> is <code>""</code>.</p> |
| 520 | 234 | <pre><code>proc find*(key: I64): (^Node, ERROR); |
| 521 | 235 | ... |
| 522 | 236 | n, err := Table.find(7); |
| 523 | 237 | if err = Table.NotFound then ... end;</code></pre> |
| 524 | 238 | <h2 id="procedure-types">5.9 Procedure types</h2> |
| 525 | 239 | <pre><code>type Compare = proc(a, b: I64): BOOL;</code></pre> |
| 526 | | -<p>A value of a procedure type is the address of a procedure with that |
| 527 | | -signature, as big as an address, or <code>nil</code>. Two signatures are |
| 528 | | -alike when their parameters have the same types, each <code>var</code> |
| 529 | | -or not alike, in order, and their results are the same types; the |
| 530 | | -parameters’ names don’t matter.</p> |
| 240 | +<p>A value of a procedure type is the address of a procedure with that signature, as big as an address, or <code>nil</code>. Two signatures are alike when their parameters have the same types, each <code>var</code> or not alike, in order, and their results are the same types; the parameters’ names don’t matter.</p> |
| 531 | 241 | <h2 id="same-and-assignable">5.10 Same and assignable</h2> |
| 532 | | -<p>Two named types are the same only if they are the same declaration. |
| 533 | | -Two anonymous pointer, array, slice, set or procedure types are the same |
| 534 | | -if they are built alike: <code>^I64</code> is <code>^I64</code> wherever |
| 535 | | -it is written. A record, variant or enumeration, though, is a type of |
| 536 | | -its own wherever it is written, named or not: two |
| 537 | | -<code>record x: I64; end</code> are two types. A named and an anonymous |
| 538 | | -type are the same if the named one is built like the anonymous one, so |
| 539 | | -that <code>p := ADR(n)</code> works for <code>p: NodePtr</code> with |
| 540 | | -<code>type NodePtr = ^Node</code>. Sameness doesn’t pass through the |
| 541 | | -anonymous type: with <code>type P1 = ^Node; P2 = ^Node;</code>, each of |
| 542 | | -<code>P1</code> and <code>P2</code> is the same as <code>^Node</code>, |
| 543 | | -but they are two declarations, so not the same as each other.</p> |
| 544 | | -<p>A type declared with only a name on the right, |
| 545 | | -<code>type Count = I64;</code>, is another name for that type, not a new |
| 546 | | -one.</p> |
| 547 | | -<p>A value of type <code>A</code> can be assigned to a variable of type |
| 548 | | -<code>B</code> (and passed to a parameter of type <code>B</code>, and |
| 549 | | -returned as a <code>B</code>) if they are the same type, or if:</p> |
| 242 | +<p>Two named types are the same only if they are the same declaration. Two anonymous pointer, array, slice, set or procedure types are the same if they are built alike: <code>^I64</code> is <code>^I64</code> wherever it is written. A record, variant or enumeration, though, is a type of its own wherever it is written, named or not: two <code>record x: I64; end</code> are two types. A named and an anonymous type are the same if the named one is built like the anonymous one, so that <code>p := ADR(n)</code> works for <code>p: NodePtr</code> with <code>type NodePtr = ^Node</code>. Sameness doesn’t pass through the anonymous type: with <code>type P1 = ^Node; P2 = ^Node;</code>, each of <code>P1</code> and <code>P2</code> is the same as <code>^Node</code>, but they are two declarations, so not the same as each other.</p> |
| 243 | +<p>A type declared with only a name on the right, <code>type Count = I64;</code>, is another name for that type, not a new one.</p> |
| 244 | +<p>A value of type <code>A</code> can be assigned to a variable of type <code>B</code> (and passed to a parameter of type <code>B</code>, and returned as a <code>B</code>) if they are the same type, or if:</p> |
| 550 | 245 | <ul> |
| 551 | | -<li><code>A</code> is an integer constant whose value fits in the |
| 552 | | -integer type <code>B</code>;</li> |
| 553 | | -<li><code>A</code> is <code>nil</code> and <code>B</code> is a pointer, |
| 554 | | -procedure or <code>ERROR</code> type;</li> |
| 555 | | -<li><code>A</code> is <code>^T</code> and <code>B</code> is |
| 556 | | -<code>^VOLATILE T</code>;</li> |
| 557 | | -<li><code>A</code> is <code>[n]T</code> and <code>B</code> is |
| 558 | | -<code>[]T</code>;</li> |
| 559 | | -<li><code>A</code> is a string literal and <code>B</code> is |
| 560 | | -<code>[]U8</code>, <code>^U8</code> (a pointer to the bytes followed by |
| 561 | | -a zero byte, as C wants), or <code>[n]U8</code> with <code>n</code> at |
| 562 | | -least the string’s length (the rest is zero);</li> |
| 563 | | -<li><code>A</code> is a procedure and <code>B</code> a procedure type |
| 564 | | -with the same signature;</li> |
| 565 | | -<li><code>A</code> is <code>INT</code> and <code>B</code> is |
| 566 | | -<code>I64</code>, or <code>A</code> is <code>UINT</code> and |
| 567 | | -<code>B</code> is <code>U64</code>, which loses nothing on any |
| 568 | | -target.</li> |
| 246 | +<li><code>A</code> is an integer constant whose value fits in the integer type <code>B</code>;</li> |
| 247 | +<li><code>A</code> is <code>nil</code> and <code>B</code> is a pointer, procedure or <code>ERROR</code> type;</li> |
| 248 | +<li><code>A</code> is <code>^T</code> and <code>B</code> is <code>^VOLATILE T</code>;</li> |
| 249 | +<li><code>A</code> is <code>[n]T</code> and <code>B</code> is <code>[]T</code>;</li> |
| 250 | +<li><code>A</code> is a string literal and <code>B</code> is <code>[]U8</code>, <code>^U8</code> (a pointer to the bytes followed by a zero byte, as C wants), or <code>[n]U8</code> with <code>n</code> at least the string’s length (the rest is zero);</li> |
| 251 | +<li><code>A</code> is a procedure and <code>B</code> a procedure type with the same signature;</li> |
| 252 | +<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> |
| 569 | 253 | </ul> |
| 570 | | -<p>A string’s bytes are constant. A <code>[]U8</code> or |
| 571 | | -<code>^U8</code> made from a string literal, or a slice of a string |
| 572 | | -constant, points at them, and writing through it isn’t defined: on a |
| 573 | | -target with read-only memory, it may stop the program as a bad address |
| 574 | | -(section 10). A <code>[n]U8</code> made from a string is a copy, the |
| 575 | | -program’s to change.</p> |
| 576 | | -<p>Nothing else converts silently. In particular, there is no other |
| 577 | | -conversion between integer types, between integers and reals, or between |
| 578 | | -<code>F32</code> and <code>F64</code>, without a written conversion |
| 579 | | -(section 6.7). Results passed on whole need the same types exactly: in |
| 580 | | -<code>a, b := f(x)</code> and in <code>return f(x)</code>, an |
| 581 | | -<code>INT</code> result doesn’t pass up as an <code>I64</code>. |
| 582 | | -Elsewhere, <code>return n, n</code> included, an <code>INT</code> widens |
| 583 | | -as above.</p> |
| 254 | +<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> |
| 255 | +<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.</p> |
| 584 | 256 | <h1 id="expressions">6. Expressions</h1> |
| 585 | 257 | <pre><code>Expr = Conjunct { "or" Conjunct } . |
| 586 | 258 | Conjunct = Relation { "and" Relation } . |
| | @@ -599,344 +271,189 @@ |
| 599 | 271 | Designator = QualIdent { Selector } . |
| 600 | 272 | Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]" |
| 601 | 273 | | "(" [ Expr { "," Expr } ] ")" .</code></pre> |
| 602 | | -<p>The operators bind in the order of the grammar: <code>not</code> and |
| 603 | | -<code>~</code> most tightly, then the multiplying operators, then the |
| 604 | | -adding ones, then the relations, then <code>and</code>, then |
| 605 | | -<code>or</code>. So <code>a + b * c = d</code> is |
| 606 | | -<code>(a + (b * c)) = d</code>, <code>x & 0FH = 0</code> is |
| 607 | | -<code>(x & 0FH) = 0</code>, and |
| 608 | | -<code>i < n and a[i] # 0 or done</code> is |
| 609 | | -<code>((i < n) and (a[i] # 0)) or done</code>. A unary minus applies |
| 610 | | -to the first term: <code>-a * b</code> is <code>-(a * b)</code>.</p> |
| 611 | | -<p><code>and</code>, <code>or</code> and <code>not</code> are for |
| 612 | | -<code>BOOL</code>s only; <code>&</code>, <code>|</code>, |
| 613 | | -<code>xor</code> and <code>~</code> are for integers only.</p> |
| 614 | | -<p>A pointer to a record isn’t the record: its fields are |
| 615 | | -<code>p^.f</code>, never <code>p.f</code>, and the elements of a pointer |
| 616 | | -to an array are <code>p^[i]</code>. A statement starts with a name, so |
| 617 | | -<code>^T(x)^ := v</code> isn’t one.</p> |
| 274 | +<p>The operators bind in the order of the grammar: <code>not</code> and <code>~</code> 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> |
| 275 | +<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> |
| 276 | +<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, so <code>^T(x)^ := v</code> isn’t one.</p> |
| 618 | 277 | <h2 id="integers">6.1 Integers</h2> |
| 619 | | -<p>Both operands of <code>+ - * div mod & | xor</code>, and of a |
| 620 | | -relation, have the same integer type, or one is an untyped constant that |
| 621 | | -takes the other’s type, which it must fit. So do <code>v</code> and |
| 622 | | -<code>k</code> in <code>INC(v, k)</code> and <code>DEC(v, k)</code>. The |
| 623 | | -result has that type and wraps around at its width: in <code>U8</code>, |
| 624 | | -<code>200 + 100</code> is <code>44</code>. A unary minus wraps too: for |
| 625 | | -<code>x</code> of an unsigned type, <code>-x</code> is |
| 626 | | -<code>0 - x</code>. Two integer types are never mixed, whatever their |
| 627 | | -sizes: with <code>i: I64</code>, <code>i < LEN(s)</code> is an error, |
| 628 | | -and <code>i</code> should be an <code>INT</code>.</p> |
| 629 | | -<p>Where the report asks for an integer and names no type, as an index, |
| 630 | | -a slice’s bound, a shift’s count, a pointer’s offset <code>i</code> in |
| 631 | | -<code>p + i</code>, or a conversion’s operand, any integer type will do, |
| 632 | | -and its value is taken. On a target whose <code>INT</code> is narrower, |
| 633 | | -a pointer’s offset is cut to an <code>INT</code>.</p> |
| 634 | | -<p><code>SIZE(T)</code>, and <code>LEN</code> of an array or a string |
| 635 | | -constant, are untyped constants, like a literal.</p> |
| 636 | | -<p><code>div</code> rounds toward zero and <code>mod</code> takes the |
| 637 | | -sign of the dividend, as in C: <code>-7 div 2</code> is <code>-3</code>, |
| 638 | | -<code>-7 mod 2</code> is <code>-1</code>. Dividing by zero, and dividing |
| 639 | | -the smallest value of a signed type by <code>-1</code>, stop the program |
| 640 | | -(<code>SIGFPE</code>), whatever the type and the target.</p> |
| 641 | | -<p><code>&</code> (and), <code>|</code> (or), <code>xor</code> and |
| 642 | | -<code>~</code> (not) work on every bit. <code>x shl n</code> and |
| 643 | | -<code>x shr n</code> shift by <code>n</code> modulo the width of |
| 644 | | -<code>x</code>’s type, in bits: for <code>x: U8</code>, |
| 645 | | -<code>x shl 9</code> is <code>x shl 1</code>. <code>shr</code> brings in |
| 646 | | -copies of the sign bit for signed types and zeros for unsigned ones. |
| 647 | | -<code>n</code> may be of any integer type. The result has |
| 648 | | -<code>x</code>’s type, cut to its width: in <code>U8</code>, |
| 649 | | -<code>0C0H shl 1</code> is <code>80H</code>.</p> |
| 278 | +<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> |
| 279 | +<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> |
| 280 | +<p><code>SIZE(T)</code>, and <code>LEN</code> of an array or a string constant, are untyped constants, like a literal.</p> |
| 281 | +<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 (<code>SIGFPE</code>), whatever the type and the target.</p> |
| 282 | +<p><code>&</code> (and), <code>|</code> (or), <code>xor</code> and <code>~</code> (not) work on every bit. <code>x shl n</code> and <code>x shr n</code> shift by <code>n</code> modulo the width of <code>x</code>’s type, in bits: for <code>x: U8</code>, <code>x shl 9</code> is <code>x shl 1</code>. <code>shr</code> brings in copies of the sign bit for signed types and zeros for unsigned ones. <code>n</code> may be of any integer type. The result has <code>x</code>’s type, cut to its width: in <code>U8</code>, <code>0C0H shl 1</code> is <code>80H</code>.</p> |
| 650 | 283 | <p>Relations compare as signed or unsigned according to the type.</p> |
| 651 | 284 | <h2 id="booleans">6.2 Booleans</h2> |
| 652 | | -<p><code>and</code> and <code>or</code> evaluate their right operand |
| 653 | | -only when it decides the result. <code>not</code> negates, and |
| 654 | | -<code>=</code> and <code>#</code> compare; <code>#</code> is |
| 655 | | -“different”.</p> |
| 285 | +<p><code>and</code> and <code>or</code> evaluate their right operand only when it decides the result. <code>not</code> negates, and <code>=</code> and <code>#</code> compare; <code>#</code> is “different”.</p> |
| 656 | 286 | <h2 id="reals">6.3 Reals</h2> |
| 657 | | -<p><code>+ - * /</code> and the relations work on <code>F32</code> and |
| 658 | | -on <code>F64</code>. Both operands have the same type, or one is a real |
| 659 | | -constant, which takes the other’s type (section 2). The result has that |
| 660 | | -type: <code>F32</code> arithmetic is done in single precision, rounded |
| 661 | | -at each step as C rounds <code>float</code>. A constant expression of |
| 662 | | -reals is evaluated in double precision, then rounded once to the type it |
| 663 | | -takes.</p> |
| 664 | | -<p>Relations are IEEE 754’s: a NaN is equal to nothing, itself included, |
| 665 | | -so for a NaN <code>x</code>, <code>x # x</code> is true and every other |
| 666 | | -relation with it is false. Dividing by zero gives an infinity or a NaN; |
| 667 | | -arithmetic on reals never stops the program.</p> |
| 287 | +<p><code>+ - * /</code> and the relations work on <code>F32</code> and on <code>F64</code>. Both operands have the same type, or one is a real constant, which takes the other’s type (section 2). The result has that type: <code>F32</code> arithmetic is done in single precision, rounded at each step as C rounds <code>float</code>. A constant expression of reals is evaluated in double precision, then rounded once to the type it takes.</p> |
| 288 | +<p>Relations are IEEE 754’s: a NaN is equal to nothing, itself included, so for a NaN <code>x</code>, <code>x # x</code> is true and every other relation with it is false. Dividing by zero gives an infinity or a NaN; arithmetic on reals never stops the program.</p> |
| 668 | 289 | <h2 id="pointers-1">6.4 Pointers</h2> |
| 669 | 290 | <ul> |
| 670 | 291 | <li><code>p^</code> is the variable <code>p</code> points at.</li> |
| 671 | | -<li><code>p + i</code> and <code>p - i</code>, with <code>i</code> an |
| 672 | | -integer, move <code>p</code> by <code>i</code> elements: by |
| 673 | | -<code>i * SIZE(T)</code> bytes for <code>p: ^T</code>.</li> |
| 674 | | -<li><code>p - q</code>, with both of the same pointer type, is the |
| 675 | | -number of elements between them, an <code>INT</code>.</li> |
| 676 | | -<li><code>p[i]</code> is <code>(p + i)^</code>. <code>p[lo..hi]</code> |
| 677 | | -is the slice of the <code>hi - lo</code> elements from |
| 678 | | -<code>p + lo</code>. Neither is checked against a length, which a |
| 679 | | -pointer doesn’t have, but a slice of a pointer that runs backwards, |
| 680 | | -<code>hi < lo</code>, is a failed check (section 6.5). A pointer to |
| 681 | | -an array isn’t indexed this way, since a step would be a whole array: |
| 682 | | -its elements are <code>p^[i]</code>.</li> |
| 683 | | -<li>Pointers compare with <code>= # < <= > >=</code> as |
| 684 | | -unsigned addresses. Procedure values compare with <code>=</code> and |
| 685 | | -<code>#</code> only.</li> |
| 292 | +<li><code>p + i</code> and <code>p - i</code>, with <code>i</code> an integer, move <code>p</code> by <code>i</code> elements: by <code>i * SIZE(T)</code> bytes for <code>p: ^T</code>.</li> |
| 293 | +<li><code>p - q</code>, with both of the same pointer type, is the number of elements between them, an <code>INT</code>.</li> |
| 294 | +<li><code>p[i]</code> is <code>(p + i)^</code>. <code>p[lo..hi]</code> is the slice of the <code>hi - lo</code> elements from <code>p + lo</code>. Neither is checked against a length, which a pointer doesn’t have, but a slice of a pointer that runs backwards, <code>hi < lo</code>, is a failed check (section 6.5). A pointer to an array isn’t indexed this way, since a step would be a whole array: its elements are <code>p^[i]</code>.</li> |
| 295 | +<li>Pointers compare with <code>= # < <= > >=</code> as unsigned addresses. Procedure values compare with <code>=</code> and <code>#</code> only.</li> |
| 686 | 296 | </ul> |
| 687 | 297 | <h2 id="arrays-and-slices">6.5 Arrays and slices</h2> |
| 688 | | -<p><code>a[i]</code> is an element of an array or slice, and |
| 689 | | -<code>a[lo..hi]</code> is the slice of the elements from <code>lo</code> |
| 690 | | -up to but not including <code>hi</code>. Both are checked: |
| 691 | | -<code>i</code> must be below the length, and |
| 692 | | -<code>0 <= lo <= hi <= length</code>. A check that fails prints |
| 693 | | -to standard error where it is, as file, line and column, and what |
| 694 | | -failed, with the values involved |
| 695 | | -(<code>index 7 is out of range for a length of 5</code>), and stops the |
| 696 | | -program with status 2. An index or a bound may be of any integer type, |
| 697 | | -and is checked by its value, whatever its type: a negative |
| 698 | | -<code>I8</code>, or a <code>U64</code> past the length, fails like any |
| 699 | | -other. A module that says <code>check off;</code> (section 3) leaves |
| 700 | | -these checks out of its code.</p> |
| 701 | | -<p><strong>Literals.</strong> <code>[e1, e2, ...]</code> builds an |
| 702 | | -array, and <code>{f: e, g: e, ...}</code> a record, where the type it |
| 703 | | -must have is known: assigned to a variable, passed to a parameter, |
| 704 | | -returned, compared with a value of that type, or an element of a bigger |
| 705 | | -literal. The elements must be assignable to the element type, and an |
| 706 | | -array literal for <code>[n]T</code> has exactly <code>n</code> of them. |
| 707 | | -A record literal names its fields, in any order, each at most once; the |
| 708 | | -fields it leaves out are zero, so <code>{}</code> is the record with |
| 709 | | -every field zero. An array literal used as a slice is a hidden variable |
| 710 | | -of the procedure, and lives as long as its call.</p> |
| 298 | +<p><code>a[i]</code> is an element of an array or slice, and <code>a[lo..hi]</code> is the slice of the elements from <code>lo</code> up to but not including <code>hi</code>. Both are checked: <code>i</code> must be below the length, and <code>0 <= lo <= hi <= length</code>. A check that fails prints to standard error where it is, as file, line and column, and what failed, with the values involved (<code>index 7 is out of range for a length of 5</code>), and stops the program with status 2. An index or a bound may be of any integer type, and is checked by its value, whatever its type: a negative <code>I8</code>, or a <code>U64</code> past the length, fails like any other. A module that says <code>check off;</code> (section 3) leaves these checks out of its code.</p> |
| 299 | +<p><strong>Literals.</strong> <code>[e1, e2, ...]</code> builds an array, and <code>{f: e, g: e, ...}</code> a record, where the type it must have is known: assigned to a variable, passed to a parameter, returned, compared with a value of that type, or an element of a bigger literal. The elements must be assignable to the element type, and an array literal for <code>[n]T</code> has exactly <code>n</code> of them. A record literal names its fields, in any order, each at most once; the fields it leaves out are zero, so <code>{}</code> is the record with every field zero. An array literal used as a slice is a hidden variable of the procedure, and lives as long as its call.</p> |
| 711 | 300 | <pre><code>c := {ch: 'A', attr: 7}; |
| 712 | 301 | Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.int(m)]);</code></pre> |
| 713 | | -<p><strong>Arrays and strings compare.</strong> Two operands that are |
| 714 | | -each a <code>[]T</code> or a <code>[n]T</code>, of the same element type |
| 715 | | -<code>T</code>, are <code>=</code> when they have the same length and |
| 716 | | -the same elements, and <code>#</code> otherwise, where <code>T</code> is |
| 717 | | -an integer type, <code>BOOL</code>, an enumeration, a set or a pointer |
| 718 | | -type. A string literal compares as a <code>[]U8</code>. The comparison |
| 719 | | -is code of its own where it stands, a check of the lengths and then of |
| 720 | | -the bytes, not a call.</p> |
| 721 | | -<p>Arrays of reals don’t compare this way, since <code>-0.0 = 0.0</code> |
| 722 | | -and a NaN is equal to nothing, though their bytes say otherwise; nor do |
| 723 | | -arrays of records, variants or procedures, nor records and variants |
| 724 | | -themselves. Write a loop. Ordering strings is the library’s |
| 725 | | -<code>Str.cmp</code>.</p> |
| 302 | +<p><strong>Arrays and strings compare.</strong> Two operands that are each a <code>[]T</code> or a <code>[n]T</code>, of the same element type <code>T</code>, are <code>=</code> when they have the same length and the same elements, and <code>#</code> otherwise, where <code>T</code> is an integer type, <code>BOOL</code>, an enumeration, a set or a pointer type. A string literal compares as a <code>[]U8</code>. The comparison is code of its own where it stands, a check of the lengths and then of the bytes, not a call.</p> |
| 303 | +<p>Arrays of reals don’t compare this way, since <code>-0.0 = 0.0</code> and a NaN is equal to nothing, though their bytes say otherwise; nor do arrays of records, variants or procedures, nor records and variants themselves. Write a loop. Ordering strings is the library’s <code>Str.cmp</code>.</p> |
| 726 | 304 | <h2 id="enumerations-and-sets-1">6.6 Enumerations and sets</h2> |
| 727 | | -<p>Enumerations compare with all six relations, by position, and an |
| 728 | | -enumeration can be the variable of a <code>for</code> loop and the |
| 729 | | -expression of a <code>match</code> (section 7.4).</p> |
| 730 | | -<p><code>{red, blue}</code> is a set, of the set type of its names’ |
| 731 | | -enumeration; <code>{}</code> is the empty set, where the type it must |
| 732 | | -have is known. On two sets of the same type, <code>+</code> is union, |
| 733 | | -<code>*</code> intersection and <code>-</code> difference; |
| 734 | | -<code>=</code> and <code>#</code> compare, and <code><=</code> and |
| 735 | | -<code>>=</code> are “is a subset of” and “is a superset of”. |
| 736 | | -<code>x in s</code> is <code>true</code> if <code>x</code>, a value of |
| 737 | | -the enumeration, is in the set <code>s</code>.</p> |
| 305 | +<p>Enumerations compare with all six relations, by position, and an enumeration can be the variable of a <code>for</code> loop and the expression of a <code>match</code> (section 7.4).</p> |
| 306 | +<p><code>{red, blue}</code> is a set, of the set type of its names’ enumeration; <code>{}</code> is the empty set, where the type it must have is known. On two sets of the same type, <code>+</code> is union, <code>*</code> intersection and <code>-</code> difference; <code>=</code> and <code>#</code> compare, and <code><=</code> and <code>>=</code> are “is a subset of” and “is a superset of”. <code>x in s</code> is <code>true</code> if <code>x</code>, a value of the enumeration, is in the set <code>s</code>.</p> |
| 738 | 307 | <h2 id="conversions">6.7 Conversions</h2> |
| 739 | | -<p>A type name applied like a procedure converts: <code>I32(x)</code>, |
| 740 | | -<code>F64(n)</code>, <code>^Cell(addr)</code>. A pointer type written |
| 741 | | -out uses the <code>^</code> form of the grammar: |
| 742 | | -<code>^VOLATILE U32(0FEE00000H)</code>.</p> |
| 308 | +<p>A type name applied like a procedure converts: <code>I32(x)</code>, <code>F64(n)</code>, <code>^Cell(addr)</code>. A pointer type written out uses the <code>^</code> form of the grammar: <code>^VOLATILE U32(0FEE00000H)</code>.</p> |
| 743 | 309 | <table> |
| 744 | | -<colgroup> |
| 745 | | -<col style="width: 33%" /> |
| 746 | | -<col style="width: 33%" /> |
| 747 | | -<col style="width: 33%" /> |
| 748 | | -</colgroup> |
| 749 | 310 | <thead> |
| 750 | | -<tr class="header"> |
| 311 | +<tr> |
| 751 | 312 | <th>From</th> |
| 752 | 313 | <th>To</th> |
| 753 | 314 | <th>Result</th> |
| 754 | 315 | </tr> |
| 755 | 316 | </thead> |
| 756 | 317 | <tbody> |
| 757 | | -<tr class="odd"> |
| 318 | +<tr> |
| 758 | 319 | <td>integer</td> |
| 759 | 320 | <td>integer</td> |
| 760 | | -<td>the value cut to the new width, or extended by its sign (signed) or |
| 761 | | -zeros (unsigned)</td> |
| 321 | +<td>the value cut to the new width, or extended by its sign (signed) or zeros (unsigned)</td> |
| 762 | 322 | </tr> |
| 763 | | -<tr class="even"> |
| 323 | +<tr> |
| 764 | 324 | <td>integer</td> |
| 765 | 325 | <td><code>F32</code>, <code>F64</code></td> |
| 766 | 326 | <td>the nearest real</td> |
| 767 | 327 | </tr> |
| 768 | | -<tr class="odd"> |
| 328 | +<tr> |
| 769 | 329 | <td><code>F32</code>, <code>F64</code></td> |
| 770 | 330 | <td>integer</td> |
| 771 | | -<td>rounded toward zero; a NaN, or a real out of the range of |
| 772 | | -<code>I64</code>, gives the smallest <code>I64</code>, then cut</td> |
| 331 | +<td>rounded toward zero; a NaN, or a real out of the range of <code>I64</code>, gives the smallest <code>I64</code>, then cut</td> |
| 773 | 332 | </tr> |
| 774 | | -<tr class="even"> |
| 333 | +<tr> |
| 775 | 334 | <td><code>F32</code></td> |
| 776 | 335 | <td><code>F64</code></td> |
| 777 | 336 | <td>the same value</td> |
| 778 | 337 | </tr> |
| 779 | | -<tr class="odd"> |
| 338 | +<tr> |
| 780 | 339 | <td><code>F64</code></td> |
| 781 | 340 | <td><code>F32</code></td> |
| 782 | 341 | <td>the nearest <code>F32</code>; too big a value gives an infinity</td> |
| 783 | 342 | </tr> |
| 784 | | -<tr class="even"> |
| 343 | +<tr> |
| 785 | 344 | <td>enumeration</td> |
| 786 | 345 | <td>integer</td> |
| 787 | 346 | <td>its position</td> |
| 788 | 347 | </tr> |
| 789 | | -<tr class="odd"> |
| 348 | +<tr> |
| 790 | 349 | <td>integer</td> |
| 791 | 350 | <td>enumeration</td> |
| 792 | | -<td>the name at that position; a position out of range is a failed check |
| 793 | | -(6.5)</td> |
| 351 | +<td>the name at that position; a position out of range is a failed check (6.5)</td> |
| 794 | 352 | </tr> |
| 795 | | -<tr class="even"> |
| 353 | +<tr> |
| 796 | 354 | <td>set</td> |
| 797 | 355 | <td>integer</td> |
| 798 | 356 | <td>its bits</td> |
| 799 | 357 | </tr> |
| 800 | | -<tr class="odd"> |
| 358 | +<tr> |
| 801 | 359 | <td>integer</td> |
| 802 | 360 | <td>set</td> |
| 803 | 361 | <td>the low bits, as many as the set has</td> |
| 804 | 362 | </tr> |
| 805 | | -<tr class="even"> |
| 363 | +<tr> |
| 806 | 364 | <td><code>INT</code>, <code>UINT</code></td> |
| 807 | 365 | <td>pointer, procedure</td> |
| 808 | 366 | <td>the address</td> |
| 809 | 367 | </tr> |
| 810 | | -<tr class="odd"> |
| 368 | +<tr> |
| 811 | 369 | <td>pointer, procedure</td> |
| 812 | 370 | <td><code>INT</code>, <code>UINT</code></td> |
| 813 | 371 | <td>the address</td> |
| 814 | 372 | </tr> |
| 815 | | -<tr class="even"> |
| 373 | +<tr> |
| 816 | 374 | <td>pointer, procedure</td> |
| 817 | 375 | <td>pointer, procedure</td> |
| 818 | 376 | <td>the same address, seen as the new type</td> |
| 819 | 377 | </tr> |
| 820 | 378 | </tbody> |
| 821 | 379 | </table> |
| 822 | | -<p>There are no others: no conversion between <code>BOOL</code> and |
| 823 | | -integers, none between pointers and integers other than <code>INT</code> |
| 824 | | -and <code>UINT</code>, and none to or from records, arrays, slices, |
| 825 | | -variants or <code>ERROR</code>. A conversion of a constant is made by |
| 826 | | -the compiler, by the same rules: <code>U8(300)</code> is 44.</p> |
| 380 | +<p>There are no others: no conversion between <code>BOOL</code> and integers, none between pointers and integers other than <code>INT</code> and <code>UINT</code>, and none to or from records, arrays, slices, variants or <code>ERROR</code>. A conversion of a constant is made by the compiler, by the same rules: <code>U8(300)</code> is 44.</p> |
| 827 | 381 | <h2 id="built-in-procedures">6.8 Built-in procedures</h2> |
| 828 | 382 | <table> |
| 829 | | -<colgroup> |
| 830 | | -<col style="width: 50%" /> |
| 831 | | -<col style="width: 50%" /> |
| 832 | | -</colgroup> |
| 833 | 383 | <thead> |
| 834 | | -<tr class="header"> |
| 384 | +<tr> |
| 835 | 385 | <th>Call</th> |
| 836 | 386 | <th>Result</th> |
| 837 | 387 | </tr> |
| 838 | 388 | </thead> |
| 839 | 389 | <tbody> |
| 840 | | -<tr class="odd"> |
| 390 | +<tr> |
| 841 | 391 | <td><code>SIZE(T)</code></td> |
| 842 | | -<td>the size of type <code>T</code> in bytes, an untyped constant; |
| 843 | | -<code>T</code> is a type, not a variable</td> |
| 392 | +<td>the size of type <code>T</code> in bytes, an untyped constant; <code>T</code> is a type, not a variable</td> |
| 844 | 393 | </tr> |
| 845 | | -<tr class="even"> |
| 394 | +<tr> |
| 846 | 395 | <td><code>ADR(x)</code></td> |
| 847 | 396 | <td>the address of variable <code>x</code>, a <code>^T</code></td> |
| 848 | 397 | </tr> |
| 849 | | -<tr class="odd"> |
| 398 | +<tr> |
| 850 | 399 | <td><code>LEN(a)</code></td> |
| 851 | | -<td>the number of elements of an array or a string constant (an untyped |
| 852 | | -constant) or of a slice (an <code>INT</code>)</td> |
| 400 | +<td>the number of elements of an array or a string constant (an untyped constant) or of a slice (an <code>INT</code>)</td> |
| 853 | 401 | </tr> |
| 854 | | -<tr class="even"> |
| 402 | +<tr> |
| 855 | 403 | <td><code>NEW(p, a)</code></td> |
| 856 | | -<td>sets <code>p: ^T</code> to a fresh, zeroed <code>T</code> from the |
| 857 | | -allocator <code>a: ^Mem.Allocator</code>, or to <code>nil</code> if |
| 858 | | -<code>a</code> has no room</td> |
| 404 | +<td>sets <code>p: ^T</code> to a fresh, zeroed <code>T</code> from the allocator <code>a: ^Mem.Allocator</code>, or to <code>nil</code> if <code>a</code> has no room</td> |
| 859 | 405 | </tr> |
| 860 | | -<tr class="odd"> |
| 406 | +<tr> |
| 861 | 407 | <td><code>MSG(e)</code></td> |
| 862 | 408 | <td>the message of the error <code>e</code>, a <code>[]U8</code></td> |
| 863 | 409 | </tr> |
| 864 | | -<tr class="even"> |
| 410 | +<tr> |
| 865 | 411 | <td><code>COPY(d, s)</code></td> |
| 866 | | -<td>copies the elements of <code>s</code> to the start of |
| 867 | | -<code>d</code>, both arrays or slices of one type <code>T</code>; |
| 868 | | -<code>LEN(s)</code> must be at most <code>LEN(d)</code></td> |
| 412 | +<td>copies the elements of <code>s</code> to the start of <code>d</code>, both arrays or slices of one type <code>T</code>; <code>LEN(s)</code> must be at most <code>LEN(d)</code></td> |
| 869 | 413 | </tr> |
| 870 | | -<tr class="odd"> |
| 414 | +<tr> |
| 871 | 415 | <td><code>SYSCALL(n, x1, ..., x6)</code></td> |
| 872 | | -<td>Linux system call <code>n</code> with up to six arguments, each a |
| 873 | | -pointer or an integer no wider than <code>INT</code>; the raw result, an |
| 874 | | -<code>INT</code> (a negative number is <code>-errno</code>)</td> |
| 416 | +<td>Linux system call <code>n</code> with up to six arguments, each a pointer or an integer no wider than <code>INT</code>; the raw result, an <code>INT</code> (a negative number is <code>-errno</code>)</td> |
| 875 | 417 | </tr> |
| 876 | | -<tr class="even"> |
| 418 | +<tr> |
| 877 | 419 | <td><code>INC(v)</code>, <code>INC(v, k)</code></td> |
| 878 | | -<td>adds 1, or <code>k</code>, to the integer variable <code>v</code>; |
| 879 | | -it wraps, like <code>+</code></td> |
| 420 | +<td>adds 1, or <code>k</code>, to the integer variable <code>v</code>; it wraps, like <code>+</code></td> |
| 880 | 421 | </tr> |
| 881 | | -<tr class="odd"> |
| 422 | +<tr> |
| 882 | 423 | <td><code>DEC(v)</code>, <code>DEC(v, k)</code></td> |
| 883 | 424 | <td>subtracts 1, or <code>k</code>, from <code>v</code></td> |
| 884 | 425 | </tr> |
| 885 | | -<tr class="even"> |
| 426 | +<tr> |
| 886 | 427 | <td><code>FENCE</code></td> |
| 887 | 428 | <td>a full memory barrier</td> |
| 888 | 429 | </tr> |
| 889 | | -<tr class="odd"> |
| 430 | +<tr> |
| 890 | 431 | <td><code>CAS(p, old, new)</code></td> |
| 891 | | -<td>if <code>p^ = old</code>, sets <code>p^ := new</code> and returns |
| 892 | | -<code>true</code>; atomically</td> |
| 432 | +<td>if <code>p^ = old</code>, sets <code>p^ := new</code> and returns <code>true</code>; atomically</td> |
| 893 | 433 | </tr> |
| 894 | | -<tr class="even"> |
| 434 | +<tr> |
| 895 | 435 | <td><code>XCHG(p, x)</code></td> |
| 896 | | -<td>sets <code>p^ := x</code> and returns the value before, |
| 897 | | -atomically</td> |
| 436 | +<td>sets <code>p^ := x</code> and returns the value before, atomically</td> |
| 898 | 437 | </tr> |
| 899 | | -<tr class="odd"> |
| 438 | +<tr> |
| 900 | 439 | <td><code>XADD(p, x)</code></td> |
| 901 | | -<td>adds <code>x</code> to <code>p^</code> and returns the value before, |
| 902 | | -atomically</td> |
| 440 | +<td>adds <code>x</code> to <code>p^</code> and returns the value before, atomically</td> |
| 903 | 441 | </tr> |
| 904 | | -<tr class="even"> |
| 442 | +<tr> |
| 905 | 443 | <td><code>ARGC()</code></td> |
| 906 | 444 | <td>the number of command line arguments, an <code>INT</code></td> |
| 907 | 445 | </tr> |
| 908 | | -<tr class="odd"> |
| 446 | +<tr> |
| 909 | 447 | <td><code>ARGV()</code></td> |
| 910 | 448 | <td>the arguments, a <code>^^U8</code> (C strings)</td> |
| 911 | 449 | </tr> |
| 912 | 450 | </tbody> |
| 913 | 451 | </table> |
| 914 | | -<p><code>SYSCALL</code> exists only on Linux targets. Other systems have |
| 915 | | -no stable system call interface, and their modules call the C library |
| 916 | | -instead (section 9).</p> |
| 917 | | -<p><code>NEW(p, a)</code> calls <code>a^.alloc(a, SIZE(T), A)</code>, |
| 918 | | -where <code>A</code> is the alignment of <code>T</code>, and zeroes the |
| 919 | | -bytes it gets if they aren’t <code>nil</code>. |
| 920 | | -<code>Mem.Allocator</code> (section 11) is the one type of the library |
| 921 | | -the language names.</p> |
| 922 | | -<p><code>COPY</code> is a statement, as <code>INC</code> is. It copies |
| 923 | | -the bytes of <code>LEN(s)</code> elements as one block, as an assignment |
| 924 | | -of an array does, and is right when <code>d</code> and <code>s</code> |
| 925 | | -overlap, as in <code>COPY(a[1..n], a[0..n - 1])</code>. It is checked as |
| 926 | | -a slice is: past <code>LEN(d)</code>, it stops the program, unless the |
| 927 | | -module says <code>check off;</code>.</p> |
| 928 | | -<p><code>INC</code> and <code>DEC</code> evaluate <code>v</code> once, |
| 929 | | -so <code>INC(a[f(i)])</code> calls <code>f</code> once. They read |
| 930 | | -<code>v</code> and then write it: two accesses, even through a |
| 931 | | -<code>^VOLATILE</code> pointer, and not atomic; <code>XADD</code> is the |
| 932 | | -atomic one.</p> |
| 933 | | -<p>For <code>CAS</code>, <code>XCHG</code> and <code>XADD</code>, |
| 934 | | -<code>p</code> is a <code>^T</code> or <code>^VOLATILE T</code> where |
| 935 | | -<code>T</code> is <code>INT</code>, <code>UINT</code> or a pointer type |
| 936 | | -(<code>INT</code> or <code>UINT</code> for <code>XADD</code>), on every |
| 937 | | -target. They are sequentially consistent, on every target; how a machine |
| 938 | | -without atomic instructions makes them so is the implementation’s choice |
| 939 | | -(appendix A).</p> |
| 452 | +<p><code>SYSCALL</code> exists only on Linux targets. Other systems have no stable system call interface, and their modules call the C library instead (section 9).</p> |
| 453 | +<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> (section 11) is the one type of the library the language names.</p> |
| 454 | +<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> |
| 455 | +<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> |
| 456 | +<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> |
| 940 | 457 | <h1 id="statements">7. Statements</h1> |
| 941 | 458 | <pre><code>Statements = { Statement ";" } . |
| 942 | 459 | Statement = Designator [ ":=" Expr ] |
| | @@ -955,120 +472,42 @@ |
| 955 | 472 | | Label { "," Label } | "else" ) ":" Statements . |
| 956 | 473 | Label = Expr [ ".." Expr ] . |
| 957 | 474 | Target = Designator | "_" .</code></pre> |
| 958 | | -<p>Every statement ends in <code>;</code>, including the ones that end |
| 959 | | -in <code>end</code> or <code>until</code>.</p> |
| 475 | +<p>Every statement ends in <code>;</code>, including the ones that end in <code>end</code> or <code>until</code>.</p> |
| 960 | 476 | <h2 id="assignment-and-calls">7.1 Assignment and calls</h2> |
| 961 | | -<p><code>x := e</code> evaluates <code>e</code> and stores it in |
| 962 | | -<code>x</code>; arrays, records, slices and variants are copied. A |
| 963 | | -designator on its own is a call, and any result it returns is dropped: |
| 964 | | -<code>Out.ln;</code>, <code>write(fd, buf);</code>.</p> |
| 965 | | -<p><code>a, b := f(x);</code> assigns the results of a procedure that |
| 966 | | -returns several (section 8), in order.</p> |
| 967 | | -<p>The blank, <code>_</code>, drops what would be assigned to it: |
| 968 | | -<code>_ := e;</code> evaluates <code>e</code> and drops it, and |
| 969 | | -<code>n, _ := f(x);</code> keeps the first result and drops the second. |
| 970 | | -What is dropped is still evaluated as any expression is: its calls are |
| 971 | | -made, and a volatile variable in it is read (section 7.6). A result |
| 972 | | -dropped by a call standing on its own is warned of (section 10); one |
| 973 | | -dropped through <code>_</code> isn’t.</p> |
| 477 | +<p><code>x := e</code> evaluates <code>e</code> and stores it in <code>x</code>; arrays, records, slices and variants are copied. A designator on its own is a call, and any result it returns is dropped: <code>Out.ln;</code>, <code>write(fd, buf);</code>.</p> |
| 478 | +<p><code>a, b := f(x);</code> assigns the results of a procedure that returns several (section 8), in order.</p> |
| 479 | +<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 warned of (section 10); one dropped through <code>_</code> isn’t.</p> |
| 974 | 480 | <h2 id="conditions-and-loops">7.2 Conditions and loops</h2> |
| 975 | | -<p><code>if</code>, <code>while</code> and <code>repeat</code> take |
| 976 | | -<code>BOOL</code> expressions. <code>repeat</code> runs its body at |
| 977 | | -least once and stops when the condition is true.</p> |
| 978 | | -<p><code>for i := a to b by s do ... end;</code> sets <code>i</code> to |
| 979 | | -<code>a</code>, <code>a + s</code>, … while <code>i <= b</code> (or |
| 980 | | -<code>i >= b</code> if <code>s</code> is negative). <code>i</code> is |
| 981 | | -a variable of an integer or enumeration type, named by its name alone; |
| 982 | | -<code>a</code> and then <code>b</code> are evaluated once, before the |
| 983 | | -first round; <code>s</code> is a constant other than zero, and 1 if left |
| 984 | | -out. <code>a</code> and <code>b</code> have <code>i</code>’s type, or |
| 985 | | -are untyped constants that take it, as with <code>+</code> (section |
| 986 | | -6.1): so in <code>for i := 0 to LEN(s) - 1</code>, <code>i</code> must |
| 987 | | -be an <code>INT</code>.</p> |
| 988 | | -<p>The loop never steps past <code>b</code>, so <code>i</code> can’t |
| 989 | | -wrap: afterwards <code>i</code> holds the last value it took, or |
| 990 | | -<code>a</code> if the body never ran. The body may assign |
| 991 | | -<code>i</code>. Precisely, for a positive <code>s</code>: at the end of |
| 992 | | -a round, if <code>i <= b</code> and <code>b - i < s</code>, the |
| 993 | | -loop ends with <code>i</code> as it is; otherwise <code>i</code> becomes |
| 994 | | -<code>i + s</code>, wrapping, and the loop goes on while |
| 995 | | -<code>i <= b</code>. A negative <code>s</code> is the same, the other |
| 996 | | -way round.</p> |
| 997 | | -<p><code>break</code> ends the innermost <code>while</code>, |
| 998 | | -<code>repeat</code> or <code>for</code> it stands in, and the program |
| 999 | | -goes on after it. There is no <code>goto</code>.</p> |
| 481 | +<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> |
| 482 | +<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> |
| 483 | +<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> |
| 484 | +<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> |
| 1000 | 485 | <h2 id="return">7.3 Return</h2> |
| 1001 | | -<p><code>return e;</code> ends the procedure with the result |
| 1002 | | -<code>e</code>; <code>return a, b;</code> with several results; |
| 1003 | | -<code>return f(x);</code> with the several results of <code>f</code>, if |
| 1004 | | -they are the procedure’s own (section 8); <code>return;</code> from a |
| 1005 | | -procedure without results. A procedure with results that runs off its |
| 1006 | | -end returns zeros, each result as a variable of its type starts (section |
| 1007 | | -4).</p> |
| 486 | +<p><code>return e;</code> ends the procedure with the result <code>e</code>; <code>return a, b;</code> with several results; <code>return f(x);</code> with the several results of <code>f</code>, if they are the procedure’s own (section 8); <code>return;</code> from a procedure without results. A procedure with results that runs off its end returns zeros, each result as a variable of its type starts (section 4).</p> |
| 1008 | 487 | <h2 id="match">7.4 Match</h2> |
| 1009 | 488 | <pre><code>match s of |
| 1010 | 489 | | circle(r): area := 3.14159 * r * r; |
| 1011 | 490 | | rect(w, h): area := w * h; |
| 1012 | 491 | | dot: area := 0.0; |
| 1013 | 492 | end;</code></pre> |
| 1014 | | -<p><code>match</code> runs the arm of the case <code>s</code> holds. An |
| 1015 | | -arm may name the case’s fields, in order, all of them; the names stand |
| 1016 | | -for those fields of <code>s</code> itself, so assigning to one changes |
| 1017 | | -<code>s</code>; if <code>s</code> isn’t a variable, they stand for the |
| 1018 | | -fields of a copy. An arm may also leave the fields unnamed. Every case |
| 1019 | | -must have an arm, and only one, unless there is an <code>| else:</code> |
| 1020 | | -arm, which must come last.</p> |
| 1021 | | -<p><code>match</code> on an enumeration works the same way: each arm |
| 1022 | | -names one of the enumeration’s names.</p> |
| 1023 | | -<p><code>match</code> on an integer runs the arm whose labels hold its |
| 1024 | | -value:</p> |
| 493 | +<p><code>match</code> runs the arm of the case <code>s</code> holds. An arm may name the case’s fields, in order, all of them; the names stand for those fields of <code>s</code> itself, so assigning to one changes <code>s</code>; if <code>s</code> isn’t a variable, they stand for the fields of a copy. An arm may also leave the fields unnamed. Every case must have an arm, and only one, unless there is an <code>| else:</code> arm, which must come last.</p> |
| 494 | +<p><code>match</code> on an enumeration works the same way: each arm names one of the enumeration’s names.</p> |
| 495 | +<p><code>match</code> on an integer runs the arm whose labels hold its value:</p> |
| 1025 | 496 | <pre><code>match ch of |
| 1026 | 497 | | ' ', '\t': skip := true; |
| 1027 | 498 | | '0' .. '9': digit := true; |
| 1028 | 499 | | else: |
| 1029 | 500 | end;</code></pre> |
| 1030 | | -<p>A label is a constant, or <code>lo .. hi</code>, the constants from |
| 1031 | | -<code>lo</code> to <code>hi</code>, both included (unlike a slice, which |
| 1032 | | -leaves <code>hi</code> out). Each must fit the integer’s type, and no |
| 1033 | | -value may be in two labels. An integer <code>match</code> must have an |
| 1034 | | -<code>| else:</code> arm, which may be empty. Whether an arm names a |
| 1035 | | -case or gives labels is set by the type matched: <code>| Max:</code> is |
| 1036 | | -a case of a variant or an enumeration, and a constant for an |
| 1037 | | -integer.</p> |
| 501 | +<p>A label is a constant, or <code>lo .. hi</code>, the constants from <code>lo</code> to <code>hi</code>, both included (unlike a slice, which leaves <code>hi</code> out). Each must fit the integer’s type, and no value may be in two labels. An integer <code>match</code> must have an <code>| else:</code> arm, which may be empty. Whether an arm names a case or gives labels is set by the type matched: <code>| Max:</code> is a case of a variant or an enumeration, and a constant for an integer.</p> |
| 1038 | 502 | <h2 id="order-of-evaluation">7.5 Order of evaluation</h2> |
| 1039 | | -<p>Operands, arguments and the designators of an assignment are |
| 1040 | | -evaluated from left to right, and the designators of an assignment |
| 1041 | | -before its right side: <code>a[i()] := g()</code> calls <code>i</code> |
| 1042 | | -and then <code>g</code>.</p> |
| 503 | +<p>Operands, arguments and the designators of an assignment are evaluated from left to right, and the designators of an assignment before its right side: <code>a[i()] := g()</code> calls <code>i</code> and then <code>g</code>.</p> |
| 1043 | 504 | <h2 id="volatile">7.6 Volatile</h2> |
| 1044 | | -<p>A variable reached through a <code>^VOLATILE T</code> is volatile, |
| 1045 | | -and so is any field or element of it. Each read or write of a volatile |
| 1046 | | -variable of a basic, enumeration, set or pointer type is exactly one |
| 1047 | | -memory access, of exactly its size, and volatile accesses happen in the |
| 1048 | | -order the program writes them. Copying a whole record or array through a |
| 1049 | | -volatile pointer is not allowed, since it can’t be one access.</p> |
| 1050 | | -<p>A <code>^VOLATILE T</code> doesn’t convert to <code>^T</code> without |
| 1051 | | -a written conversion.</p> |
| 1052 | | -<p><code>FENCE</code> also orders the program’s ordinary accesses |
| 1053 | | -against those of other processors and devices.</p> |
| 1054 | | -<p><strong>Ordinary variables.</strong> Only volatile accesses, |
| 1055 | | -<code>CAS</code>, <code>XCHG</code>, <code>XADD</code> and |
| 1056 | | -<code>FENCE</code> are exact. Any other access is ordinary: an |
| 1057 | | -implementation may keep a variable in a register, leave out a write that |
| 1058 | | -nothing reads, and change the order of ordinary accesses, as long as |
| 1059 | | -each thread, on its own, does what this report says. A variable that one |
| 1060 | | -thread writes while another reads or writes it, unless both use volatile |
| 1061 | | -accesses or the atomic built-ins, or a <code>FENCE</code> orders them, |
| 1062 | | -holds no particular value then: a read of it gives a value it had or was |
| 1063 | | -given, or a mixture of their bytes. Nothing else of the program becomes |
| 1064 | | -undefined by it.</p> |
| 505 | +<p>A variable reached through a <code>^VOLATILE T</code> is volatile, and so is any field or element of it. Each read or write of a volatile variable of a basic, enumeration, set or pointer type is exactly one memory access, of exactly its size, and volatile accesses happen in the order the program writes them. Copying a whole record or array through a volatile pointer is not allowed, since it can’t be one access.</p> |
| 506 | +<p>A <code>^VOLATILE T</code> doesn’t convert to <code>^T</code> without a written conversion.</p> |
| 507 | +<p><code>FENCE</code> also orders the program’s ordinary accesses against those of other processors and devices.</p> |
| 508 | +<p><strong>Ordinary variables.</strong> Only volatile accesses, <code>CAS</code>, <code>XCHG</code>, <code>XADD</code> and <code>FENCE</code> are exact. Any other access is ordinary: an implementation may keep a variable in a register, leave out a write that nothing reads, and change the order of ordinary accesses, as long as each thread, on its own, does what this report says. A variable that one thread writes while another reads or writes it, unless both use volatile accesses or the atomic built-ins, or a <code>FENCE</code> orders them, holds no particular value then: a read of it gives a value it had or was given, or a mixture of their bytes. Nothing else of the program becomes undefined by it.</p> |
| 1065 | 509 | <h2 id="defer">7.7 Defer</h2> |
| 1066 | | -<p><code>defer s;</code> runs the statement <code>s</code> when the |
| 1067 | | -program leaves the statement list the <code>defer</code> stands in: at |
| 1068 | | -its end, or by a <code>return</code>, or by a <code>break</code> out of |
| 1069 | | -it. Statements deferred in the same list run in the opposite order to |
| 1070 | | -their <code>defer</code>s, and only those whose <code>defer</code> has |
| 1071 | | -been reached run.</p> |
| 510 | +<p><code>defer s;</code> runs the statement <code>s</code> when the program leaves the statement list the <code>defer</code> stands in: at its end, or by a <code>return</code>, or by a <code>break</code> out of it. Statements deferred in the same list run in the opposite order to their <code>defer</code>s, and only those whose <code>defer</code> has been reached run.</p> |
| 1072 | 511 | <pre><code>module Head; |
| 1073 | 512 | import Sys, Out; |
| 1074 | 513 | |
| | @@ -1095,11 +534,7 @@ |
| 1095 | 534 | end; |
| 1096 | 535 | |
| 1097 | 536 | end Head.</code></pre> |
| 1098 | | -<p><code>return e;</code> evaluates <code>e</code> before the deferred |
| 1099 | | -statements run. A deferred statement can’t contain <code>return</code>, |
| 1100 | | -<code>break</code> or <code>defer</code> that would leave it. A failed |
| 1101 | | -check, <code>Rt.exit</code> and <code>Sys.exit</code> end the program |
| 1102 | | -without running the deferred statements still pending.</p> |
| 537 | +<p><code>return e;</code> evaluates <code>e</code> before the deferred statements run. A deferred statement can’t contain <code>return</code>, <code>break</code> or <code>defer</code> that would leave it. A failed check, <code>Rt.exit</code> and <code>Sys.exit</code> end the program without running the deferred statements still pending.</p> |
| 1103 | 538 | <h1 id="procedures">8. Procedures</h1> |
| 1104 | 539 | <pre><code>ProcDeclaration = "proc" IdentDef Signature ";" |
| 1105 | 540 | ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ] |
| | @@ -1111,20 +546,9 @@ |
| 1111 | 546 | | [ ":" Results ] . |
| 1112 | 547 | Params = [ "var" ] ident { "," ident } ":" Type . |
| 1113 | 548 | Results = Type | "(" Type { "," Type } ")" .</code></pre> |
| 1114 | | -<p>A procedure without parameters may leave out the parentheses, both |
| 1115 | | -where it is declared (<code>proc main*;</code>) and where it is called |
| 1116 | | -(<code>Out.ln</code>). Used without parentheses in an expression, a |
| 1117 | | -procedure is a procedure value, not a call.</p> |
| 1118 | | -<p><strong>Parameters.</strong> A value parameter is a copy: the |
| 1119 | | -procedure may change it, and the caller’s variable stays as it was. A |
| 1120 | | -<code>var</code> parameter is the caller’s variable itself; its argument |
| 1121 | | -must be a variable of exactly the parameter’s type.</p> |
| 1122 | | -<p><strong>Results.</strong> A procedure returns nothing, one value, or |
| 1123 | | -several. A call that returns several values can only stand on the right |
| 1124 | | -of a multiple assignment, alone as a statement, or alone after |
| 1125 | | -<code>return</code> in a procedure whose results are of the same types, |
| 1126 | | -in the same order, which then returns them as they are: |
| 1127 | | -<code>return divmod(n, 2);</code>.</p> |
| 549 | +<p>A procedure without parameters may leave out the parentheses, both where it is declared (<code>proc main*;</code>) and where it is called (<code>Out.ln</code>). Used without parentheses in an expression, a procedure is a procedure value, not a call.</p> |
| 550 | +<p><strong>Parameters.</strong> A value parameter is a copy: the procedure may change it, and the caller’s variable stays as it was. A <code>var</code> parameter is the caller’s variable itself; its argument must be a variable of exactly the parameter’s type.</p> |
| 551 | +<p><strong>Results.</strong> A procedure returns nothing, one value, or several. A call that returns several values can only stand on the right of a multiple assignment, alone as a statement, or alone after <code>return</code> in a procedure whose results are of the same types, in the same order, which then returns them as they are: <code>return divmod(n, 2);</code>.</p> |
| 1128 | 552 | <pre><code>module Divide; |
| 1129 | 553 | import Out; |
| 1130 | 554 | |
| | @@ -1141,17 +565,10 @@ |
| 1141 | 565 | end; |
| 1142 | 566 | |
| 1143 | 567 | end Divide.</code></pre> |
| 1144 | | -<p><strong>forward.</strong> <code>proc f(x: I64): I64; forward;</code> |
| 1145 | | -declares <code>f</code> so that it can be called before its body |
| 1146 | | -appears; the body comes later with the same signature, though its |
| 1147 | | -parameters may have other names.</p> |
| 1148 | | -<p>A name after a procedure’s <code>end</code>, if written, is the |
| 1149 | | -procedure’s own.</p> |
| 568 | +<p><strong>forward.</strong> <code>proc f(x: I64): I64; forward;</code> declares <code>f</code> so that it can be called before its body appears; the body comes later with the same signature, though its parameters may have other names.</p> |
| 569 | +<p>A name after a procedure’s <code>end</code>, if written, is the procedure’s own.</p> |
| 1150 | 570 | <p>Procedures don’t nest.</p> |
| 1151 | | -<p><strong>Code procedures.</strong> A procedure whose body is |
| 1152 | | -<code>code</code> is machine code, written by hand in the target’s |
| 1153 | | -assembly language (appendix A): one instruction to each <code>;</code>, |
| 1154 | | -and labels for jumps, up to <code>end</code>:</p> |
| 571 | +<p><strong>Code procedures.</strong> A procedure whose body is <code>code</code> is machine code, written by hand in the target’s assembly language (appendix A): one instruction to each <code>;</code>, and labels for jumps, up to <code>end</code>:</p> |
| 1155 | 572 | <pre><code>proc rdtsc*: U64; |
| 1156 | 573 | code |
| 1157 | 574 | rdtsc; |
| | @@ -1159,24 +576,8 @@ |
| 1159 | 576 | or rax, rdx; |
| 1160 | 577 | ret; |
| 1161 | 578 | end rdtsc;</code></pre> |
| 1162 | | -<p>A label, <code>name:</code>, names the place of the instruction after |
| 1163 | | -it, within the procedure. A constant of the module may stand wherever a |
| 1164 | | -number does. Any bytes may be written with <code>db</code>, as in |
| 1165 | | -<code>db 0FH, 31H;</code>, for an instruction the assembler doesn’t |
| 1166 | | -know.</p> |
| 1167 | | -<p>It is called like any procedure, with the System V convention: |
| 1168 | | -integer and pointer arguments arrive in <code>rdi</code>, |
| 1169 | | -<code>rsi</code>, <code>rdx</code>, <code>rcx</code>, <code>r8</code> |
| 1170 | | -and <code>r9</code>, <code>F32</code> and <code>F64</code> ones in |
| 1171 | | -<code>xmm0</code> to <code>xmm7</code>, and the result goes in |
| 1172 | | -<code>rax</code> (and <code>rdx</code> for a second) or |
| 1173 | | -<code>xmm0</code>. Its parameters and results are therefore of the types |
| 1174 | | -C can take (section 9), at most six integer and eight real parameters, |
| 1175 | | -and at most two results. The instructions are the whole procedure, |
| 1176 | | -<code>ret</code> included; they must keep <code>rbx</code>, |
| 1177 | | -<code>rbp</code>, <code>rsp</code> and <code>r12</code> to |
| 1178 | | -<code>r15</code> as they found them. The compiler doesn’t look past what |
| 1179 | | -they say.</p> |
| 579 | +<p>A label, <code>name:</code>, names the place of the instruction after it, within the procedure. A constant of the module may stand wherever a number does. Any bytes may be written with <code>db</code>, as in <code>db 0FH, 31H;</code>, for an instruction the assembler doesn’t know.</p> |
| 580 | +<p>It is called like any procedure, with the System V convention: integer and pointer arguments arrive in <code>rdi</code>, <code>rsi</code>, <code>rdx</code>, <code>rcx</code>, <code>r8</code> and <code>r9</code>, <code>F32</code> and <code>F64</code> ones in <code>xmm0</code> to <code>xmm7</code>, and the result goes in <code>rax</code> (and <code>rdx</code> for a second) or <code>xmm0</code>. Its parameters and results are therefore of the types C can take (section 9), or slices, at most six integer and eight real parameters, and at most two results. A slice is two integers, as System V passes a struct of them: as a parameter its address arrives in one register and its length in the next, the two counting among the six; as the result its address goes in <code>rax</code> and its length in <code>rdx</code>, and it is the only result. The instructions are the whole procedure, <code>ret</code> included; they must keep <code>rbx</code>, <code>rbp</code>, <code>rsp</code> and <code>r12</code> to <code>r15</code> as they found them. The compiler doesn’t look past what they say.</p> |
| 1180 | 581 | <h1 id="calling-c">9. Calling C</h1> |
| 1181 | 582 | <pre><code>ExternDeclaration = "extern" ( "proc" IdentDef Signature |
| 1182 | 583 | | "var" IdentDef ":" Type ) .</code></pre> |
| | @@ -1184,27 +585,10 @@ |
| 1184 | 585 | extern proc XOpenDisplay(name: ^U8): ^U8; |
| 1185 | 586 | extern proc printf(format: ^U8; ...): I32; |
| 1186 | 587 | extern var environ: ^^U8;</code></pre> |
| 1187 | | -<p>An <code>extern</code> procedure or variable is found by its name in |
| 1188 | | -the C libraries the program links: the C library always, and each |
| 1189 | | -library named by <code>link</code>.</p> |
| 1190 | | -<p>An <code>extern</code> procedure’s parameters and result are |
| 1191 | | -integers, <code>BOOL</code>, <code>F32</code>, <code>F64</code>, |
| 1192 | | -pointers or procedures; a record goes to C by pointer, never by value. |
| 1193 | | -<code>...</code> accepts any number of further arguments of those types, |
| 1194 | | -and is allowed only in an <code>extern</code>. C takes no |
| 1195 | | -<code>float</code> there, so an <code>F32</code> given to |
| 1196 | | -<code>...</code> is an error: the program writes <code>F64(x)</code>. |
| 1197 | | -Past the parameters, a real constant goes as an <code>F64</code>, an |
| 1198 | | -integer constant as an <code>INT</code>, and a string literal as a |
| 1199 | | -pointer to its bytes followed by a zero byte.</p> |
| 1200 | | -<p>An <code>extern</code> variable is C’s variable itself: reading it |
| 1201 | | -reads C’s, assigning it writes C’s. It can be of any type, declared as C |
| 1202 | | -declares it, a record laid out as C lays it out (section 5.5); the |
| 1203 | | -compiler can’t check that it is. A C variable that is local to each |
| 1204 | | -thread, such as <code>errno</code>, can’t be reached this way; the C |
| 1205 | | -library has a procedure for it (<code>__errno_location</code>).</p> |
| 1206 | | -<p>A C union has no raz type. A program reads one as an array of words, |
| 1207 | | -and the member it wants through a pointer conversion:</p> |
| 588 | +<p>An <code>extern</code> procedure or variable is found by its name in the C libraries the program links: the C library always, and each library named by <code>link</code>.</p> |
| 589 | +<p>An <code>extern</code> procedure’s parameters and result are integers, <code>BOOL</code>, <code>F32</code>, <code>F64</code>, pointers or procedures; a record goes to C by pointer, never by value. <code>...</code> accepts any number of further arguments of those types, and is allowed only in an <code>extern</code>. C takes no <code>float</code> there, so an <code>F32</code> given to <code>...</code> is an error: the program writes <code>F64(x)</code>. Past the parameters, a real constant goes as an <code>F64</code>, an integer constant as an <code>INT</code>, and a string literal as a pointer to its bytes followed by a zero byte.</p> |
| 590 | +<p>An <code>extern</code> variable is C’s variable itself: reading it reads C’s, assigning it writes C’s. It can be of any type, declared as C declares it, a record laid out as C lays it out (section 5.5); the compiler can’t check that it is. A C variable that is local to each thread, such as <code>errno</code>, can’t be reached this way; the C library has a procedure for it (<code>__errno_location</code>).</p> |
| 591 | +<p>A C union has no raz type. A program reads one as an array of words, and the member it wants through a pointer conversion:</p> |
| 1208 | 592 | <pre><code>var ev: [24]I64; (* XEvent: 192 bytes *) |
| 1209 | 593 | ... |
| 1210 | 594 | XNextEvent(dpy, ADR(ev)); |
| | @@ -1212,229 +596,116 @@ |
| 1212 | 596 | key := ^XKeyEvent(ADR(ev)); |
| 1213 | 597 | ... |
| 1214 | 598 | end;</code></pre> |
| 1215 | | -<p>Every raz procedure uses the System V calling convention, so a raz |
| 1216 | | -procedure whose parameters and result are of those types can be given to |
| 1217 | | -C as a callback.</p> |
| 599 | +<p>Every raz procedure uses the System V calling convention, so a raz procedure whose parameters and result are of those types can be given to C as a callback.</p> |
| 1218 | 600 | <h1 id="programs">10. Programs</h1> |
| 1219 | | -<p>Any module may export a procedure <code>main</code>, declared |
| 1220 | | -<code>proc main*;</code> or <code>proc main*: INT;</code>. The program |
| 1221 | | -runs the bodies of all its modules (section 3), then the main module’s |
| 1222 | | -<code>main</code>, if it has one, and ends with status 0, or with the |
| 1223 | | -status <code>main</code> returns, by calling <code>Rt.exit</code>. The |
| 1224 | | -<code>main</code> of an imported module is an ordinary procedure, which |
| 1225 | | -runs only if something calls it; so a library module can carry a |
| 1226 | | -<code>main</code> that tests it. A program uses C if any of its modules |
| 1227 | | -has an <code>extern</code> declaration or a <code>link</code> of a |
| 1228 | | -library; a module’s other files, read only to check its interface |
| 1229 | | -(section 3), don’t count. A program that doesn’t is a static executable |
| 1230 | | -with no run time besides the code it uses. One that does is linked |
| 1231 | | -dynamically and starts through the C library, so that C’s own set-up and |
| 1232 | | -clean-up run. A program with a module that says |
| 1233 | | -<code>link static;</code> must not use C, and the compiler reports an |
| 1234 | | -error if it does.</p> |
| 1235 | | -<p><strong>Ending.</strong> <code>Rt.exit(status)</code> ends the |
| 1236 | | -program. First it runs the procedures registered with |
| 1237 | | -<code>Rt.atExit</code>, the last registered first, each at most once; |
| 1238 | | -then it ends the program with <code>status</code>, through C’s |
| 1239 | | -<code>exit</code> in a program that uses C, so that C flushes its own |
| 1240 | | -buffers. A failed check (section 6.5) runs them too, after its message, |
| 1241 | | -so that <code>Out</code>’s buffer is written out. <code>Sys.exit</code> |
| 1242 | | -ends the program at once, and a program stopped by a signal runs none of |
| 1243 | | -them.</p> |
| 601 | +<p>Any module may export a procedure <code>main</code>, declared <code>proc main*;</code> or <code>proc main*: INT;</code>. The program runs the bodies of all its modules (section 3), then the main module’s <code>main</code>, if it has one, and ends with status 0, or with the status <code>main</code> returns, by calling <code>Rt.exit</code>. The <code>main</code> of an imported module is an ordinary procedure, which runs only if something calls it; so a library module can carry a <code>main</code> that tests it. A program uses C if any of its modules has an <code>extern</code> declaration or a <code>link</code> of a library; a module’s other files, read only to check its interface (section 3), don’t count. A program that doesn’t is a static executable with no run time besides the code it uses. One that does is linked dynamically and starts through the C library, so that C’s own set-up and clean-up run. A program with a module that says <code>link static;</code> must not use C, and the compiler reports an error if it does.</p> |
| 602 | +<p><strong>Ending.</strong> <code>Rt.exit(status)</code> ends the program. First it runs the procedures registered with <code>Rt.atExit</code>, the last registered first, each at most once; then it ends the program with <code>status</code>, through C’s <code>exit</code> in a program that uses C, so that C flushes its own buffers. A failed check (section 6.5) runs them too, after its message, so that <code>Out</code>’s buffer is written out. <code>Sys.exit</code> ends the program at once, and a program stopped by a signal runs none of them.</p> |
| 1244 | 603 | <pre><code>proc atExit*(p: proc): ERROR; (* Rt.Full if 32 are registered *) |
| 1245 | 604 | proc exit*(status: INT); |
| 1246 | 605 | proc trace*; (* the calls under way, on standard error *)</code></pre> |
| 1247 | | -<p><code>Rt.trace</code> says on standard error the calls under way, |
| 1248 | | -from the one that calls it out, each with its variables, as a program |
| 1249 | | -that stops does.</p> |
| 1250 | | -<p>Only what can be reached from <code>main</code> and the module bodies |
| 1251 | | -goes into the executable: unused procedures, variables and strings are |
| 1252 | | -left out. The executable names its procedures and carries line numbers, |
| 1253 | | -so that gdb can stop at a line of the source and show where it is.</p> |
| 1254 | | -<p><strong>Defined behaviour.</strong> Every variable starts at zero, |
| 1255 | | -integers wrap, and indexing arrays and slices is checked, so a program’s |
| 1256 | | -behaviour is defined by this report, with one exception: memory reached |
| 1257 | | -other than through a checked variable. Dereferencing a stale or made-up |
| 1258 | | -pointer, indexing through a pointer, an index out of range in a module |
| 1259 | | -that says <code>check off;</code>, writing a string’s bytes, a code |
| 1260 | | -procedure that breaks its rules, and C code can read or write any |
| 1261 | | -memory, and what follows isn’t defined. Dereferencing <code>nil</code>, |
| 1262 | | -dividing by zero, calling a <code>nil</code> procedure and running out |
| 1263 | | -of stack stop the program, on every target. Before it stops, the program |
| 1264 | | -prints to standard error where it was, as file, line and column, and |
| 1265 | | -why, as far as it can tell: a <code>nil</code> pointer, a |
| 1266 | | -<code>nil</code> procedure, a stack overflow, a bad address or a |
| 1267 | | -division fault; then the calls under way, each with its variables of a |
| 1268 | | -basic, enumeration, set or pointer type. Whether the machine catches |
| 1269 | | -these or the compiled code checks for them is the implementation’s |
| 1270 | | -choice (appendix A); that they stop the program isn’t.</p> |
| 1271 | | -<p><strong>Separate compilation.</strong> An importer depends only on a |
| 1272 | | -module’s interface: what it exports, as an importer sees it, never its |
| 1273 | | -source or its code. So a module can be compiled on its own, and a |
| 1274 | | -program made from modules compiled at different times. Whether an |
| 1275 | | -implementation does so, and into what, is its own choice (appendix A); |
| 1276 | | -one that does makes the same program as if every module were compiled |
| 1277 | | -together, and keeps one copy of each instance.</p> |
| 1278 | | -<p><strong>Messages.</strong> The compiler reports an error with its |
| 1279 | | -file, line and column, the line of source with the offending part |
| 1280 | | -underlined, and, where they help, notes pointing at other places, such |
| 1281 | | -as where a name is declared, and a suggestion, such as a name in reach |
| 1282 | | -spelled almost alike. It goes on after an error to find the others, up |
| 1283 | | -to 20, and writes no executable. After an error it skips the statement |
| 1284 | | -or declaration the error is in and carries on with the next; a name that |
| 1285 | | -isn’t declared is reported once, and nothing that follows from it is |
| 1286 | | -reported. An error in the text itself, such as a string left open, stops |
| 1287 | | -the compile, and so does a module with errors, after it.</p> |
| 1288 | | -<p>The compiler also warns of what is declared and never used: an |
| 1289 | | -import; a constant, type, variable or procedure that isn’t exported; a |
| 1290 | | -parameter of a procedure written in raz. It warns of a variable that is |
| 1291 | | -assigned but never read, too, and of a call standing on its own that |
| 1292 | | -drops a result, as <code>Sys.write(fd, s);</code> does: |
| 1293 | | -<code>_ := Sys.write(fd, s);</code> says it is meant. A name that starts |
| 1294 | | -with <code>_</code> is never warned about. Warnings don’t stop the |
| 1295 | | -compile; <code>raz -Werror</code> makes them errors.</p> |
| 606 | +<p><code>Rt.trace</code> says on standard error the calls under way, from the one that calls it out, each with its variables, as a program that stops does.</p> |
| 607 | +<p>Only what can be reached from <code>main</code> and the module bodies goes into the executable: unused procedures, variables and strings are left out. The executable names its procedures and carries line numbers, so that gdb can stop at a line of the source and show where it is.</p> |
| 608 | +<p><strong>Defined behaviour.</strong> Every variable starts at zero, integers wrap, and indexing arrays and slices is checked, so a program’s behaviour is defined by this report, with one exception: memory reached other than through a checked variable. Dereferencing a stale or made-up pointer, indexing through a pointer, an index out of range in a module that says <code>check off;</code>, writing a string’s bytes, a code procedure that breaks its rules, and C code can read or write any memory, and what follows isn’t defined. Dereferencing <code>nil</code>, dividing by zero, calling a <code>nil</code> procedure and running out of stack stop the program, on every target. Before it stops, the program prints to standard error where it was, as file, line and column, and why, as far as it can tell: a <code>nil</code> pointer, a <code>nil</code> procedure, a stack overflow, a bad address or a division fault; then the calls under way, each with its variables of a basic, enumeration, set or pointer type. Whether the machine catches these or the compiled code checks for them is the implementation’s choice (appendix A); that they stop the program isn’t.</p> |
| 609 | +<p><strong>Separate compilation.</strong> An importer depends only on a module’s interface: what it exports, as an importer sees it, never its source or its code. So a module can be compiled on its own, and a program made from modules compiled at different times. Whether an implementation does so, and into what, is its own choice (appendix A); one that does makes the same program as if every module were compiled together, and keeps one copy of each instance.</p> |
| 610 | +<p><strong>Messages.</strong> The compiler reports an error with its file, line and column, the line of source with the offending part underlined, and, where they help, notes pointing at other places, such as where a name is declared, and a suggestion, such as a name in reach spelled almost alike. It goes on after an error to find the others, up to 20, and writes no executable. After an error it skips the statement or declaration the error is in and carries on with the next; a name that isn’t declared is reported once, and nothing that follows from it is reported. An error in the text itself, such as a string left open, stops the compile, and so does a module with errors, after it.</p> |
| 611 | +<p>The compiler also warns of what is declared and never used: an import; a constant, type, variable or procedure that isn’t exported; a parameter of a procedure written in raz. It warns of a variable that is assigned but never read, too, and of a call standing on its own that drops a result, as <code>Sys.write(fd, s);</code> does: <code>_ := Sys.write(fd, s);</code> says it is meant. A name that starts with <code>_</code> is never warned about. Warnings don’t stop the compile; <code>raz -Werror</code> makes them errors.</p> |
| 1296 | 612 | <h1 id="the-library">11. The library</h1> |
| 1297 | | -<p>Modules written in raz, in the compiler’s <code>lib</code> |
| 1298 | | -directory:</p> |
| 613 | +<p>Modules written in raz, in the compiler’s <code>lib</code> directory:</p> |
| 1299 | 614 | <table> |
| 1300 | | -<colgroup> |
| 1301 | | -<col style="width: 50%" /> |
| 1302 | | -<col style="width: 50%" /> |
| 1303 | | -</colgroup> |
| 1304 | 615 | <thead> |
| 1305 | | -<tr class="header"> |
| 616 | +<tr> |
| 1306 | 617 | <th>Module</th> |
| 1307 | 618 | <th>What</th> |
| 1308 | 619 | </tr> |
| 1309 | 620 | </thead> |
| 1310 | 621 | <tbody> |
| 1311 | | -<tr class="odd"> |
| 622 | +<tr> |
| 1312 | 623 | <td><code>Rt</code></td> |
| 1313 | | -<td>ending the program, <code>exit</code> and <code>atExit</code>; |
| 1314 | | -<code>stop</code>, which ends it with a message; <code>trace</code>, the |
| 1315 | | -calls under way; what the compiler’s checks call when they fail</td> |
| 624 | +<td>ending the program, <code>exit</code> and <code>atExit</code>; <code>stop</code>, which ends it with a message; <code>trace</code>, the calls under way; what the compiler’s checks call when they fail</td> |
| 1316 | 625 | </tr> |
| 1317 | | -<tr class="even"> |
| 626 | +<tr> |
| 1318 | 627 | <td><code>Sys</code></td> |
| 1319 | | -<td>system calls: <code>open</code>, <code>openRead</code>, |
| 1320 | | -<code>create</code>, <code>read</code>, <code>write</code>, |
| 1321 | | -<code>seek</code>, <code>close</code>, <code>mmap</code>, |
| 1322 | | -<code>munmap</code>, <code>isTerminal</code>, <code>yield</code>, |
| 1323 | | -<code>exit</code>; files and directories, <code>rename</code>, |
| 1324 | | -<code>remove</code>, <code>removeDir</code>, <code>makeDir</code>, |
| 1325 | | -<code>chmod</code>, <code>symlink</code>, <code>readLink</code>, |
| 1326 | | -<code>info</code>, <code>openDir</code>, <code>nextEntry</code>, |
| 1327 | | -<code>closeDir</code>; the terminal, <code>rawMode</code>, |
| 1328 | | -<code>restoreMode</code>, <code>termSize</code>, <code>ready</code>; the |
| 1329 | | -environment, <code>env</code>, <code>workDir</code>, |
| 1330 | | -<code>setWorkDir</code>; programs, pipes and signals; time, |
| 1331 | | -<code>clock</code>, <code>now</code>, <code>sleep</code>; |
| 1332 | | -<code>random</code>; and sockets. One file per target, and |
| 1333 | | -<code>Sys.raz</code> over the C library for the others</td> |
| 628 | +<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>; and sockets. One file per target, and <code>Sys.raz</code> over the C library for the others</td> |
| 1334 | 629 | </tr> |
| 1335 | | -<tr class="odd"> |
| 630 | +<tr> |
| 1336 | 631 | <td><code>Out</code></td> |
| 1337 | | -<td>writing text and numbers to standard output, buffered; safe to call |
| 1338 | | -from several threads</td> |
| 632 | +<td>writing text and numbers to standard output, buffered; safe to call from several threads</td> |
| 1339 | 633 | </tr> |
| 1340 | | -<tr class="even"> |
| 634 | +<tr> |
| 1341 | 635 | <td><code>C</code></td> |
| 1342 | | -<td>the parts of the C library the others use, and <code>printf</code>, |
| 1343 | | -<code>fflush</code>, <code>stdout</code>, <code>pthread_create</code>; a |
| 1344 | | -program that imports it uses C</td> |
| 636 | +<td>the parts of the C library the others use, and <code>printf</code>, <code>fflush</code>, <code>stdout</code>, <code>pthread_create</code>; a program that imports it uses C</td> |
| 1345 | 637 | </tr> |
| 1346 | | -<tr class="odd"> |
| 638 | +<tr> |
| 1347 | 639 | <td><code>Lock</code></td> |
| 1348 | | -<td>a lock for short sections, for C’s threads and <code>Thread</code>’s |
| 1349 | | -alike</td> |
| 640 | +<td>a lock for short sections, for C’s threads and <code>Thread</code>’s alike</td> |
| 1350 | 641 | </tr> |
| 1351 | | -<tr class="even"> |
| 642 | +<tr> |
| 1352 | 643 | <td><code>Thread</code></td> |
| 1353 | | -<td>threads for a program without C: <code>spawn</code>, |
| 1354 | | -<code>join</code>, and <code>Mutex</code>; it says |
| 1355 | | -<code>link static</code></td> |
| 644 | +<td>threads for a program without C: <code>spawn</code>, <code>join</code>, and <code>Mutex</code>; it says <code>link static</code></td> |
| 1356 | 645 | </tr> |
| 1357 | | -<tr class="odd"> |
| 646 | +<tr> |
| 1358 | 647 | <td><code>Mem</code></td> |
| 1359 | | -<td><code>Allocator</code>, the interface of every allocator, and pages |
| 1360 | | -from the system</td> |
| 648 | +<td><code>Allocator</code>, the interface of every allocator, and pages from the system</td> |
| 1361 | 649 | </tr> |
| 1362 | | -<tr class="even"> |
| 650 | +<tr> |
| 1363 | 651 | <td><code>Heap</code></td> |
| 1364 | | -<td>a general allocator, <code>Heap.heap</code>, safe to call from |
| 1365 | | -several threads; freeing a block twice, or one it never gave out, stops |
| 1366 | | -the program</td> |
| 652 | +<td>a general allocator, <code>Heap.heap</code>, safe to call from several threads; freeing a block twice, or one it never gave out, stops the program</td> |
| 1367 | 653 | </tr> |
| 1368 | | -<tr class="odd"> |
| 654 | +<tr> |
| 1369 | 655 | <td><code>Arena</code></td> |
| 1370 | | -<td>an allocator that frees everything at once; |
| 1371 | | -<code>ADR(arena.base)</code> is its allocator</td> |
| 656 | +<td>an allocator that frees everything at once; <code>ADR(arena.base)</code> is its allocator</td> |
| 1372 | 657 | </tr> |
| 1373 | | -<tr class="even"> |
| 658 | +<tr> |
| 1374 | 659 | <td><code>Str</code></td> |
| 1375 | | -<td>operations on <code>[]U8</code> strings: comparing, hashing, |
| 1376 | | -finding, UTF-8, reading a number</td> |
| 660 | +<td>operations on <code>[]U8</code> strings: comparing, hashing, finding, UTF-8, reading a number</td> |
| 661 | +</tr> |
| 662 | +<tr> |
| 663 | +<td><code>Bytes</code></td> |
| 664 | +<td>searches of bytes, many at a time: <code>index</code>, <code>last</code>, <code>count</code> and <code>find</code> in a <code>[]U8</code>; one file per target where the target has instructions for it</td> |
| 1377 | 665 | </tr> |
| 1378 | | -<tr class="odd"> |
| 666 | +<tr> |
| 1379 | 667 | <td><code>Buf</code></td> |
| 1380 | 668 | <td>a growable byte buffer</td> |
| 1381 | 669 | </tr> |
| 1382 | | -<tr class="even"> |
| 670 | +<tr> |
| 1383 | 671 | <td><code>Fmt</code></td> |
| 1384 | | -<td>formatted output: |
| 1385 | | -<code>Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])</code>, or |
| 1386 | | -into a <code>Buf</code> with <code>Fmt.write</code></td> |
| 672 | +<td>formatted output: <code>Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)])</code>, or into a <code>Buf</code> with <code>Fmt.write</code></td> |
| 1387 | 673 | </tr> |
| 1388 | | -<tr class="odd"> |
| 674 | +<tr> |
| 1389 | 675 | <td><code>List</code></td> |
| 1390 | 676 | <td>a generic singly linked list, <code>List(T)</code></td> |
| 1391 | 677 | </tr> |
| 1392 | | -<tr class="even"> |
| 678 | +<tr> |
| 1393 | 679 | <td><code>Map</code></td> |
| 1394 | 680 | <td>a generic hash map, <code>Map(K, V; hash, eq)</code></td> |
| 1395 | 681 | </tr> |
| 1396 | | -<tr class="odd"> |
| 682 | +<tr> |
| 1397 | 683 | <td><code>Sort</code></td> |
| 1398 | 684 | <td>sorting a slice in place, <code>Sort(T; less)</code></td> |
| 1399 | 685 | </tr> |
| 1400 | | -<tr class="even"> |
| 686 | +<tr> |
| 1401 | 687 | <td><code>Math</code></td> |
| 1402 | | -<td>roots, powers, logarithms, rounding and trigonometry, and |
| 1403 | | -<code>pi</code>, for <code>F64</code></td> |
| 688 | +<td>roots, powers, logarithms, rounding and trigonometry, and <code>pi</code>, for <code>F64</code></td> |
| 1404 | 689 | </tr> |
| 1405 | | -<tr class="odd"> |
| 690 | +<tr> |
| 1406 | 691 | <td><code>Files</code></td> |
| 1407 | 692 | <td>an argument as a string, and a whole file read or written</td> |
| 1408 | 693 | </tr> |
| 1409 | | -<tr class="even"> |
| 694 | +<tr> |
| 1410 | 695 | <td><code>Opt</code></td> |
| 1411 | | -<td>a command line read through one table, by <code>getopt</code>’s |
| 1412 | | -rules with long options; its help and its mistakes</td> |
| 696 | +<td>a command line read through one table, by <code>getopt</code>’s rules with long options; its help and its mistakes</td> |
| 1413 | 697 | </tr> |
| 1414 | 698 | </tbody> |
| 1415 | 699 | </table> |
| 1416 | | -<p>Lengths, counts, file descriptors, process ids and statuses are |
| 1417 | | -<code>INT</code>s, and a character is an <code>I32</code>. What may pass |
| 1418 | | -an <code>INT</code> on a small target is an <code>I64</code>: a file’s |
| 1419 | | -size and position, a time in nanoseconds, and a number read from |
| 1420 | | -text.</p> |
| 1421 | | -<p>What follows is each module’s interface, as |
| 1422 | | -<code>tools/Doc.raz</code> prints it from the module’s file; |
| 1423 | | -<code>...</code> stands for fields that are the module’s own. A module’s |
| 1424 | | -<code>main</code>, where it has one, is its test, and is left out |
| 1425 | | -here.</p> |
| 700 | +<p>Lengths, counts, file descriptors, process ids and statuses are <code>INT</code>s, and a character is an <code>I32</code>. What may pass an <code>INT</code> on a small target is an <code>I64</code>: a file’s size and position, a time in nanoseconds, and a number read from text.</p> |
| 701 | +<p>What follows is each module’s interface, as <code>tools/Doc.raz</code> prints it from the module’s file; <code>...</code> stands for fields that are the module’s own. A module’s <code>main</code>, where it has one, is its test, and is left out here.</p> |
| 1426 | 702 | <pre><code>module Rt; |
| 1427 | 703 | const Full*: ERROR = "too many procedures to run at exit"; |
| 1428 | 704 | proc atExit*(p: proc): ERROR; (* p run when the program ends by exit *) |
| 1429 | 705 | proc exit*(status: INT); (* the atExit procedures, the last first, then the end *) |
| 1430 | 706 | proc stop*(msg: []U8); (* msg on standard error, then exit with status 2 *) |
| 1431 | 707 | proc trace*; (* the calls under way, and their variables, on standard error *)</code></pre> |
| 1432 | | -<p><code>Rt</code> also holds what the compiler’s checks call when they |
| 1433 | | -fail, and the handlers of the signals that stop a program (section 10). |
| 1434 | | -<code>exit</code> takes each procedure off the list before it runs it, |
| 1435 | | -so it runs once even if it fails or calls <code>exit</code> itself; in a |
| 1436 | | -program that uses C, it ends through C’s <code>exit</code>, so that C |
| 1437 | | -flushes its buffers too.</p> |
| 708 | +<p><code>Rt</code> also holds what the compiler’s checks call when they fail, and the handlers of the signals that stop a program (section 10). <code>exit</code> takes each procedure off the list before it runs it, so it runs once even if it fails or calls <code>exit</code> itself; in a program that uses C, it ends through C’s <code>exit</code>, so that C flushes its buffers too.</p> |
| 1438 | 709 | <pre><code>module Mem; |
| 1439 | 710 | type |
| 1440 | 711 | Allocator* = record |
| | @@ -1458,11 +729,7 @@ |
| 1458 | 729 | proc init*(ar: ^Arena; size: INT): BOOL; (* an arena of size bytes; false if the system has none *) |
| 1459 | 730 | proc reset*(ar: ^Arena); (* every allocation forgotten; the memory used again *) |
| 1460 | 731 | proc done*(ar: ^Arena); (* its memory given back *)</code></pre> |
| 1461 | | -<p><code>NEW(p, a)</code> calls <code>a^.alloc</code>. <code>Heap</code> |
| 1462 | | -keeps blocks of nine sizes, from 16 to 4096 bytes with their header, |
| 1463 | | -each with a list of the free ones; a bigger block gets pages of its own. |
| 1464 | | -An arena hands out memory from one block: its <code>free</code> does |
| 1465 | | -nothing, and <code>reset</code> forgets everything.</p> |
| 732 | +<p><code>NEW(p, a)</code> calls <code>a^.alloc</code>. <code>Heap</code> keeps blocks of nine sizes, from 16 to 4096 bytes with their header, each with a list of the free ones; a bigger block gets pages of its own. An arena hands out memory from one block: its <code>free</code> does nothing, and <code>reset</code> forgets everything.</p> |
| 1466 | 733 | <pre><code>module Lock; |
| 1467 | 734 | type Lock* = record ... end; |
| 1468 | 735 | proc take*(l: ^Lock); |
| | @@ -1477,13 +744,7 @@ |
| 1477 | 744 | proc join*(t: ^Thread); (* waits for t to end, then frees its stack *) |
| 1478 | 745 | proc lock*(m: ^Mutex); |
| 1479 | 746 | proc unlock*(m: ^Mutex);</code></pre> |
| 1480 | | -<p>A <code>Lock</code> is for short sections: a thread that finds it |
| 1481 | | -taken tries again, giving up the processor now and then. A |
| 1482 | | -<code>Mutex</code> sleeps. Each of <code>Thread</code>’s threads runs on |
| 1483 | | -a stack of its own, with a page below it that can’t be touched, so that |
| 1484 | | -running off the stack stops the program. A program that uses C makes its |
| 1485 | | -threads with C’s <code>pthread_create</code>, and so can’t use |
| 1486 | | -<code>Thread</code>.</p> |
| 747 | +<p>A <code>Lock</code> is for short sections: a thread that finds it taken tries again, giving up the processor now and then. A <code>Mutex</code> sleeps. Each of <code>Thread</code>’s threads runs on a stack of its own, with a page below it that can’t be touched, so that running off the stack stops the program. A program that uses C makes its threads with C’s <code>pthread_create</code>, and so can’t use <code>Thread</code>.</p> |
| 1487 | 748 | <pre><code>module Out; |
| 1488 | 749 | proc str*(s: []U8); |
| 1489 | 750 | proc char*(c: U8); |
| | @@ -1491,14 +752,7 @@ |
| 1491 | 752 | proc int*(n: I64); (* n in decimal *) |
| 1492 | 753 | proc hex*(n: U64; digits: INT); (* n in hexadecimal, digits digits at least *) |
| 1493 | 754 | proc flush*;</code></pre> |
| 1494 | | -<p><strong>Out and C’s stdio.</strong> <code>Out</code> keeps a buffer |
| 1495 | | -of <code>Sys.outSize</code> bytes, 4 KB on Linux. It writes it out when |
| 1496 | | -it is full, at each line feed when standard output is a terminal, at |
| 1497 | | -<code>Out.flush</code>, and when the program ends by |
| 1498 | | -<code>Rt.exit</code>. A lock keeps what each call writes together when |
| 1499 | | -threads write at once. C’s <code>stdio</code> keeps a buffer of its own |
| 1500 | | -for the same standard output. A program that writes with both flushes |
| 1501 | | -one before writing with the other, or the text comes out of order:</p> |
| 755 | +<p><strong>Out and C’s stdio.</strong> <code>Out</code> keeps a buffer of <code>Sys.outSize</code> bytes, 4 KB on Linux. It writes it out when it is full, at each line feed when standard output is a terminal, at <code>Out.flush</code>, and when the program ends by <code>Rt.exit</code>. A lock keeps what each call writes together when threads write at once. C’s <code>stdio</code> keeps a buffer of its own for the same standard output. A program that writes with both flushes one before writing with the other, or the text comes out of order:</p> |
| 1502 | 756 | <pre><code>module Both; |
| 1503 | 757 | import Out, C; (* C: printf, fflush and C's FILE *stdout *) |
| 1504 | 758 | |
| | @@ -1512,10 +766,7 @@ |
| 1512 | 766 | end; |
| 1513 | 767 | |
| 1514 | 768 | end Both.</code></pre> |
| 1515 | | -<p><strong>Sys</strong> gives every target the same procedures, so that |
| 1516 | | -a program that needs only these runs wherever raz does: through system |
| 1517 | | -calls on Linux, through the C library elsewhere. A result that can fail |
| 1518 | | -is a number, minus an errno, as on Linux; 0 is success.</p> |
| 769 | +<p><strong>Sys</strong> gives every target the same procedures, so that a program that needs only these runs wherever raz does: through system calls on Linux, through the C library elsewhere. A result that can fail is a number, minus an errno, as on Linux; 0 is success.</p> |
| 1519 | 770 | <pre><code>module Sys; |
| 1520 | 771 | const |
| 1521 | 772 | stdin* = 0; stdout* = 1; stderr* = 2; |
| | @@ -1616,127 +867,18 @@ |
| 1616 | 867 | proc listenTcp*(addr: []U8; port: INT): INT; |
| 1617 | 868 | proc accept*(fd: INT): INT; (* the next connection, waiting for one *) |
| 1618 | 869 | proc send*(fd: INT; s: []U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *)</code></pre> |
| 1619 | | -<p><code>info</code> follows a symbolic link: <code>size</code>, |
| 1620 | | -<code>dir</code> and <code>file</code> are those of what it names, and |
| 1621 | | -<code>symlink</code> says the path was a link, so that a program walking |
| 1622 | | -a tree can choose not to follow one back up it. A link to nothing is |
| 1623 | | -<code>-ENOENT</code>, as a missing file is. <code>file</code> lets a |
| 1624 | | -walk open only what holds data: opening a pipe would wait for a program |
| 1625 | | -to write to it.</p> |
| 1626 | | -<p><code>nextEntry</code> gives every name in the directory except |
| 1627 | | -<code>.</code> and <code>..</code>, in no particular order. |
| 1628 | | -<code>rawMode</code> turns off what the terminal does with keys before |
| 1629 | | -the program reads them: echo, waiting for a whole line, Ctrl-C and |
| 1630 | | -Ctrl-Z as signals, Ctrl-S and Ctrl-Q as flow control, and CR to NL; and |
| 1631 | | -with output, NL to CR NL. A program that turns it on restores it before |
| 1632 | | -it ends: <code>Rt.atExit</code> is the place.</p> |
| 1633 | | -<p><strong>Files.</strong> <code>open</code> gives a file for what |
| 1634 | | -<code>how</code> asks: <code>reading</code>, <code>writing</code> or |
| 1635 | | -both; <code>creating</code> it if it is missing, readable by all and |
| 1636 | | -writable by its owner; with <code>exclusive</code>, failing with -17 |
| 1637 | | -(EEXIST) if it is there, as a program does that makes a lock or a file |
| 1638 | | -of its own; <code>truncating</code> it to nothing; |
| 1639 | | -<code>appending</code>, each write going to its end, however other |
| 1640 | | -programs write it too. <code>seek</code> moves to <code>pos</code> from |
| 1641 | | -the start of the file, or from its end if <code>fromEnd</code>, where a |
| 1642 | | -negative <code>pos</code> reads back from the end. |
| 1643 | | -<code>Info.mtime</code> is what a program checks to see whether a file |
| 1644 | | -changed under it; <code>Info.mode</code> holds the nine bits of |
| 1645 | | -<code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for |
| 1646 | | -a program, 1A4H (644) for data.</p> |
| 1647 | | -<p><strong>Programs.</strong> <code>openPty</code> makes a |
| 1648 | | -pseudo-terminal, a terminal that a program, a terminal emulator, plays |
| 1649 | | -the screen and the keyboard of: what it writes to the fd is typed, and |
| 1650 | | -what the programs on the terminal write it reads.</p> |
| 1651 | | -<p><code>spawn</code> starts the program at <code>p.path</code>, with |
| 1652 | | -<code>p.args</code> as its arguments, the first being its name. Its |
| 1653 | | -environment is this program’s with each <code>NAME=value</code> of |
| 1654 | | -<code>p.env</code> added or put in place of the old value, or, if |
| 1655 | | -<code>p.onlyEnv</code>, <code>p.env</code> alone. It starts in |
| 1656 | | -<code>p.dir</code>. Its standard input, output and error are |
| 1657 | | -<code>p.fds</code>; a 0 gives it this program’s own, so |
| 1658 | | -<code>{path: "/bin/ls", args: ["ls"]}</code> runs <code>ls</code> as a |
| 1659 | | -shell runs it, with every other field zero. It gets no other open file, |
| 1660 | | -every signal does what it does at first, and none is blocked. If |
| 1661 | | -<code>p.tty</code> is a pseudo-terminal’s fd, the program starts a |
| 1662 | | -session on that terminal: it is its controlling terminal, and its |
| 1663 | | -standard input, output and error, and <code>p.fds</code> and |
| 1664 | | -<code>p.group</code> are not used.</p> |
| 1665 | | -<p><code>p.group</code> puts the program in a process group, the |
| 1666 | | -programs that a shell’s job is made of: -1 makes it the first of a new |
| 1667 | | -group, whose number is its process id, and the other programs of the job |
| 1668 | | -join that number. The terminal gives its keys, and the signals of Ctrl-C |
| 1669 | | -and Ctrl-Z, to one group, the one in its foreground: |
| 1670 | | -<code>p.foreground</code> puts the program’s group there before it |
| 1671 | | -starts, and <code>foreground</code> puts any group there, as a shell |
| 1672 | | -does to take the terminal back. <code>setGroup</code> moves a program to |
| 1673 | | -a group, as the shell does for each program it starts, for whichever of |
| 1674 | | -the two runs first; a shell runs in a group of its own, |
| 1675 | | -<code>setGroup(0, 0)</code>.</p> |
| 1676 | | -<p><code>wait</code> waits for the program <code>pid</code> to change, |
| 1677 | | -or for any of this program’s if <code>pid</code> is -1, and says which |
| 1678 | | -one, and how: <code>exited</code>, with its exit status; |
| 1679 | | -<code>killed</code>, with the number of the signal that ended it; or |
| 1680 | | -<code>stopped</code>, by Ctrl-Z or a signal, with the signal’s number. |
| 1681 | | -Without <code>block</code> it doesn’t wait, and is 0 and |
| 1682 | | -<code>running</code> if no program changed. A program that has nothing |
| 1683 | | -to wait for is -10 (ECHILD). <code>kill</code> sends a program a signal, |
| 1684 | | -<code>sigTerm</code> to end it; to a group, if <code>pid</code> is minus |
| 1685 | | -its number.</p> |
| 1686 | | -<p><strong>Signals.</strong> <code>catch</code> takes the signals |
| 1687 | | -<code>sigs</code> away from what they would do, ending the program most |
| 1688 | | -of them, and gives an fd that is ready to read when one comes, so that a |
| 1689 | | -program waits for signals with <code>ready</code> and |
| 1690 | | -<code>readyOf</code> as it waits for keys or a server. |
| 1691 | | -<code>caught</code> gives each signal that came, in turn. A later |
| 1692 | | -<code>catch</code> adds to the signals caught and gives the same fd. |
| 1693 | | -Catching <code>sigKill</code> or <code>sigStop</code>, or the signals |
| 1694 | | -<code>Rt</code> reports, SIGSEGV and SIGFPE, is -22 (EINVAL). |
| 1695 | | -<code>ignore</code> makes a signal do nothing, as a shell ignores |
| 1696 | | -<code>sigTtou</code> so that it can take its terminal back, and |
| 1697 | | -<code>ignore(sig, false)</code> gives it back what it does at first. A |
| 1698 | | -program that <code>spawn</code> starts begins with neither.</p> |
| 1699 | | -<p><strong>Time.</strong> <code>clock</code> is for measuring how long |
| 1700 | | -something takes: it never goes back, and says nothing of the date. |
| 1701 | | -<code>now</code> is the date and time, which a person or the network may |
| 1702 | | -set back. <code>sleep</code> waits <code>ms</code> milliseconds.</p> |
| 1703 | | -<p><strong>Sockets.</strong> <code>connect</code> is for talking to a |
| 1704 | | -server on the same machine, an X server at |
| 1705 | | -<code>/tmp/.X11-unix/X0</code> say; <code>connectTcp</code> for one |
| 1706 | | -across the network, given its address as numbers, |
| 1707 | | -<code>"127.0.0.1"</code> or <code>"::1"</code>: turning a name into an |
| 1708 | | -address is a library’s work over these. <code>connectUdp</code> gives a |
| 1709 | | -socket of datagrams, each <code>send</code> one message to that address |
| 1710 | | -and each <code>read</code> one message from it, as a program asks a name |
| 1711 | | -server. What a socket receives comes by <code>read</code>, what it sends |
| 1712 | | -goes by <code>send</code>, and <code>close</code> ends it. |
| 1713 | | -<code>send</code> is <code>write</code> for a socket, except when the |
| 1714 | | -other end has closed it: <code>write</code> would stop the program with |
| 1715 | | -the signal SIGPIPE, where <code>send</code> returns -32 (EPIPE), so that |
| 1716 | | -a program learns its server has gone and can end as it wants. |
| 1717 | | -<code>listen</code> and <code>listenTcp</code> are the other side: a |
| 1718 | | -server’s socket, on <code>path</code>, which must not be there yet, or |
| 1719 | | -on <code>port</code> at <code>addr</code>, <code>"0.0.0.0"</code> or |
| 1720 | | -<code>"::"</code> for every address the machine has; <code>accept</code> |
| 1721 | | -gives each connection that comes, as a socket of its own.</p> |
| 1722 | | -<p><code>readyOf</code> waits on several file descriptors at once, as a |
| 1723 | | -program does that listens to a server and a terminal both; a descriptor |
| 1724 | | -at its end, or failed, counts as ready, since a read of it won’t wait. |
| 1725 | | -It looks at the first 64 of <code>fds</code>, and <code>connect</code> |
| 1726 | | -and <code>listen</code> take a path of at most 107 bytes, as |
| 1727 | | -<code>struct sockaddr_un</code> holds; a longer one is -36 |
| 1728 | | -(ENAMETOOLONG).</p> |
| 1729 | | -<p>A program that both reads from a descriptor and writes much to it, as |
| 1730 | | -a terminal writes what is pasted to its program while it reads what the |
| 1731 | | -program writes back, must not wait on either: the program may be waiting |
| 1732 | | -for its own writes to be read. <code>setBlocking(fd, false)</code> has a |
| 1733 | | -read or a write on <code>fd</code> that would wait return -11 (EAGAIN) |
| 1734 | | -at once, a write having written what there was room for; |
| 1735 | | -<code>readyFor</code> waits, as <code>readyOf</code> does, for something |
| 1736 | | -to read on any of <code>reads</code> or for room to write on any of |
| 1737 | | -<code>writes</code>. A descriptor in both is given once, when it is |
| 1738 | | -ready for either; the 64 it looks at are <code>reads</code>, then |
| 1739 | | -<code>writes</code>.</p> |
| 870 | +<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> |
| 871 | +<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, waiting for a whole line, Ctrl-C and Ctrl-Z as signals, Ctrl-S and Ctrl-Q as flow control, and CR to NL; and with output, NL to CR NL. A program that turns it on restores it before it ends: <code>Rt.atExit</code> is the place.</p> |
| 872 | +<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> |
| 873 | +<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> |
| 874 | +<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> |
| 875 | +<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> |
| 876 | +<p><code>wait</code> waits for the program <code>pid</code> to change, or for any of this program’s if <code>pid</code> is -1, and says which one, and how: <code>exited</code>, with its exit status; <code>killed</code>, with the number of the signal that ended it; or <code>stopped</code>, by Ctrl-Z or a signal, with the signal’s number. Without <code>block</code> it doesn’t wait, and is 0 and <code>running</code> if no program changed. A program that has nothing to wait for is -10 (ECHILD). <code>kill</code> sends a program a signal, <code>sigTerm</code> to end it; to a group, if <code>pid</code> is minus its number.</p> |
| 877 | +<p><strong>Signals.</strong> <code>catch</code> takes the signals <code>sigs</code> away from what they would do, ending the program most of them, and gives an fd that is ready to read when one comes, so that a program waits for signals with <code>ready</code> and <code>readyOf</code> as it waits for keys or a server. <code>caught</code> gives each signal that came, in turn. A later <code>catch</code> adds to the signals caught and gives the same fd. Catching <code>sigKill</code> or <code>sigStop</code>, or the signals <code>Rt</code> reports, SIGSEGV and SIGFPE, is -22 (EINVAL). <code>ignore</code> makes a signal do nothing, as a shell ignores <code>sigTtou</code> so that it can take its terminal back, and <code>ignore(sig, false)</code> gives it back what it does at first. A program that <code>spawn</code> starts begins with neither.</p> |
| 878 | +<p><strong>Time.</strong> <code>clock</code> is for measuring how long something takes: it never goes back, and says nothing of the date. <code>now</code> is the date and time, which a person or the network may set back. <code>sleep</code> waits <code>ms</code> milliseconds.</p> |
| 879 | +<p><strong>Sockets.</strong> <code>connect</code> is for talking to a server on the same machine, an X server at <code>/tmp/.X11-unix/X0</code> say; <code>connectTcp</code> for one across the network, given its address as numbers, <code>"127.0.0.1"</code> or <code>"::1"</code>: turning a name into an address is a library’s work over these. <code>connectUdp</code> gives a socket of datagrams, each <code>send</code> one message to that address and each <code>read</code> one message from it, as a program asks a name server. What a socket receives comes by <code>read</code>, what it sends goes by <code>send</code>, and <code>close</code> ends it. <code>send</code> is <code>write</code> for a socket, except when the other end has closed it: <code>write</code> would stop the program with the signal SIGPIPE, where <code>send</code> returns -32 (EPIPE), so that a program learns its server has gone and can end as it wants. <code>listen</code> and <code>listenTcp</code> are the other side: a server’s socket, on <code>path</code>, which must not be there yet, or on <code>port</code> at <code>addr</code>, <code>"0.0.0.0"</code> or <code>"::"</code> for every address the machine has; <code>accept</code> gives each connection that comes, as a socket of its own.</p> |
| 880 | +<p><code>readyOf</code> waits on several file descriptors at once, as a program does that listens to a server and a terminal both; a descriptor at its end, or failed, counts as ready, since a read of it won’t wait. It looks at the first 64 of <code>fds</code>, and <code>connect</code> and <code>listen</code> take a path of at most 107 bytes, as <code>struct sockaddr_un</code> holds; a longer one is -36 (ENAMETOOLONG).</p> |
| 881 | +<p>A program that both reads from a descriptor and writes much to it, as a terminal writes what is pasted to its program while it reads what the program writes back, must not wait on either: the program may be waiting for its own writes to be read. <code>setBlocking(fd, false)</code> has a read or a write on <code>fd</code> that would wait return -11 (EAGAIN) at once, a write having written what there was room for; <code>readyFor</code> waits, as <code>readyOf</code> does, for something to read on any of <code>reads</code> or for room to write on any of <code>writes</code>. A descriptor in both is given once, when it is ready for either; the 64 it looks at are <code>reads</code>, then <code>writes</code>.</p> |
| 1740 | 882 | <p><strong>Strings, buffers and collections.</strong></p> |
| 1741 | 883 | <pre><code>module Str; |
| 1742 | 884 | proc cmp*(a, b: []U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *) |
| | @@ -1815,49 +957,19 @@ |
| 1815 | 957 | proc acos*(x: F64): F64; (* from 0 to pi *) |
| 1816 | 958 | proc atan*(x: F64): F64; (* from -pi/2 to pi/2 *) |
| 1817 | 959 | proc atan2*(y, x: F64): F64; (* the angle of (x, y) from the x axis, from -pi to pi *)</code></pre> |
| 1818 | | -<p>A <code>Buf</code>, a <code>List</code> and a <code>Map</code> take |
| 1819 | | -their memory from the allocator <code>init</code> gives them. |
| 1820 | | -<code>Fmt</code>’s format takes the next argument at each |
| 1821 | | -<code>{}</code>; <code>{{</code> and <code>}}</code> stand for |
| 1822 | | -<code>{</code> and <code>}</code>. A <code>Map</code> hashes and |
| 1823 | | -compares its keys with the procedures it is given: |
| 1824 | | -<code>import Count := Map([]U8, INT, Str.hash, Str.eq);</code>.</p> |
| 1825 | | -<p><code>Str</code> reads and writes UTF-8. <code>encode</code> writes a |
| 1826 | | -character, of at most 10FFFFH, in one to four bytes; a number that isn’t |
| 1827 | | -one becomes FFFDH, the character that stands for a wrong one. |
| 1828 | | -<code>decode</code> reads the first character of <code>s</code>, and |
| 1829 | | -gives FFFDH and 1 for a byte that doesn’t start one rightly, and 0 and 0 |
| 1830 | | -for an empty <code>s</code>. <code>toInt</code> reads a decimal number, |
| 1831 | | -with a <code>-</code> or <code>+</code> before it if any, and nothing |
| 1832 | | -else.</p> |
| 1833 | | -<p><code>le</code> and <code>be</code> read an unsigned number of up to |
| 1834 | | -8 bytes in a fixed order, little-endian or big-endian, whatever the |
| 1835 | | -machine’s own, and <code>putLE</code> and <code>putBE</code> write one; |
| 1836 | | -more than 8 bytes stops the program. They are how a file format or a |
| 1837 | | -network protocol is read and written:</p> |
| 960 | +<p>A <code>Buf</code>, a <code>List</code> and a <code>Map</code> take their memory from the allocator <code>init</code> gives them. <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> |
| 961 | +<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>. <code>toInt</code> reads a decimal number, with a <code>-</code> or <code>+</code> before it if any, and nothing else.</p> |
| 962 | +<p><code>le</code> and <code>be</code> read an unsigned number of up to 8 bytes in a fixed order, little-endian or big-endian, whatever the machine’s own, and <code>putLE</code> and <code>putBE</code> write one; more than 8 bytes stops the program. They are how a file format or a network protocol is read and written:</p> |
| 1838 | 963 | <pre><code>len := U32(Str.le(header[4..8])); |
| 1839 | 964 | Str.putBE(packet[0..2], U64(port));</code></pre> |
| 1840 | | -<p><code>Map.next</code> walks a map: from 0, it gives the next entry |
| 1841 | | -and the number to pass for the one after it, and -1 when there are no |
| 1842 | | -more. A walk sees each entry once if the map doesn’t change during |
| 1843 | | -it:</p> |
| 965 | +<p><code>Map.next</code> walks a map: from 0, it gives the next entry and the number to pass for the one after it, and -1 when there are no more. A walk sees each entry once if the map doesn’t change during it:</p> |
| 1844 | 966 | <pre><code>i, k, v := Names.next(ADR(names), 0); |
| 1845 | 967 | while i >= 0 do |
| 1846 | 968 | Fmt.print("{} is {}\n", [Fmt.str(k), Fmt.int(v)]); |
| 1847 | 969 | i, k, v := Names.next(ADR(names), i); |
| 1848 | 970 | end;</code></pre> |
| 1849 | | -<p><code>Sort.sort</code> puts <code>s</code> in the order |
| 1850 | | -<code>less</code> gives, in place, without memory of its own, in time |
| 1851 | | -<code>n log n</code> at most; two elements equal by <code>less</code> |
| 1852 | | -may change places. <code>Math</code>’s procedures are as C’s |
| 1853 | | -<code>math.h</code> has them, to within the last bit or so. |
| 1854 | | -<code>Fmt.real(x, places)</code> writes <code>x</code> with |
| 1855 | | -<code>places</code> digits after the point, at most 9, rounded to the |
| 1856 | | -nearest; from 1e15 on, with an exponent, as <code>1.50e17</code>; and |
| 1857 | | -<code>nan</code>, <code>inf</code> and <code>-inf</code> as |
| 1858 | | -themselves.</p> |
| 1859 | | -<p><strong>Files and Opt</strong> are what most programs need of their |
| 1860 | | -command line.</p> |
| 971 | +<p><code>Sort.sort</code> puts <code>s</code> in the order <code>less</code> gives, in place, without memory of its own, in time <code>n log n</code> at most; two elements equal by <code>less</code> may change places. <code>Math</code>’s procedures are as C’s <code>math.h</code> has them, to within the last bit or so. <code>Fmt.real(x, places)</code> writes <code>x</code> with <code>places</code> digits after the point, at most 9, rounded to the nearest; from 1e15 on, with an exponent, as <code>1.50e17</code>; and <code>nan</code>, <code>inf</code> and <code>-inf</code> as themselves.</p> |
| 972 | +<p><strong>Files and Opt</strong> are what most programs need of their command line.</p> |
| 1861 | 973 | <pre><code>module Files; |
| 1862 | 974 | proc arg*(i: INT): []U8; (* argument i; 0 is the program's name *) |
| 1863 | 975 | proc read*(path: []U8; b: ^Buf.Buf): BOOL; (* the whole file added to b *) |
| | @@ -1882,24 +994,8 @@ |
| 1882 | 994 | proc scope*(p: ^Parser; name: []U8); (* subcommand name's options, from the word after it *) |
| 1883 | 995 | proc number*(p: ^Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *) |
| 1884 | 996 | proc fail*(p: ^Parser; text: []U8); (* the program's own mistake *)</code></pre> |
| 1885 | | -<p><code>Opt</code> reads a command line by the rules of the C library’s |
| 1886 | | -<code>getopt</code>, with GNU’s long options, so that a program’s |
| 1887 | | -options behave as every other Unix program’s. The program describes |
| 1888 | | -itself once, in a table of <code>Row</code>s: its synopsis lines, its |
| 1889 | | -subcommands and its options. The help and the messages for mistakes are |
| 1890 | | -made from the table, so they can’t drift from it; the program’s test of |
| 1891 | | -<code>p.letter</code> is the only other place an option appears. The |
| 1892 | | -parser is a record the program owns, and the table a slice it keeps as |
| 1893 | | -long as the parser: a literal written in the call to <code>init</code> |
| 1894 | | -lives as long as the procedure that makes it.</p> |
| 1895 | | -<p>A row’s <code>scope</code> is <code>""</code> for the program itself, |
| 1896 | | -or a subcommand’s name: <code>"start"</code>, or |
| 1897 | | -<code>"remote.add"</code> for a subcommand of <code>remote</code>. An |
| 1898 | | -option has a <code>letter</code>, a <code>long</code> name, or both. One |
| 1899 | | -with a long name only has a key of its own below <code>'!'</code> for a |
| 1900 | | -letter, so that the program can tell it from the others. With a |
| 1901 | | -<code>value</code>, the name the help and the messages give it, the |
| 1902 | | -option takes a value; with <code>""</code> it is a flag.</p> |
| 997 | +<p><code>Opt</code> reads a command line by the rules of the C library’s <code>getopt</code>, with GNU’s long options, so that a program’s options behave as every other Unix program’s. The program describes itself once, in a table of <code>Row</code>s: its synopsis lines, its subcommands and its options. The help and the messages for mistakes are made from the table, so they can’t drift from it; the program’s test of <code>p.letter</code> is the only other place an option appears. The parser is a record the program owns, and the table a slice it keeps as long as the parser: a literal written in the call to <code>init</code> lives as long as the procedure that makes it.</p> |
| 998 | +<p>A row’s <code>scope</code> is <code>""</code> for the program itself, or a subcommand’s name: <code>"start"</code>, or <code>"remote.add"</code> for a subcommand of <code>remote</code>. An option has a <code>letter</code>, a <code>long</code> name, or both. One with a long name only has a key of its own below <code>'!'</code> for a letter, so that the program can tell it from the others. With a <code>value</code>, the name the help and the messages give it, the option takes a value; with <code>""</code> it is a flag.</p> |
| 1903 | 999 | <pre><code>const wait = 1; (* the key of --wait, which has no letter *) |
| 1904 | 1000 | ... |
| 1905 | 1001 | Opt.init(ADR(p), "keep", [ |
| | @@ -1923,250 +1019,65 @@ |
| 1923 | 1019 | job, ok := Opt.operand(ADR(p));</code></pre> |
| 1924 | 1020 | <p>The rules are <code>getopt</code>’s:</p> |
| 1925 | 1021 | <ul> |
| 1926 | | -<li>Flags may share a dash: <code>-lw</code> is <code>-l -w</code>. A |
| 1927 | | -letter’s value is the rest of its word, <code>-bdev</code>, or the whole |
| 1928 | | -next word, <code>-b dev</code>, even one that starts with a dash or is |
| 1929 | | -empty; in a group, the first letter that takes a value takes the rest, |
| 1930 | | -<code>-qn12</code>.</li> |
| 1931 | | -<li>A long option is written in full, with its value after a |
| 1932 | | -<code>=</code> or in the next word: <code>--bar=dev</code>, |
| 1933 | | -<code>--bar dev</code>. An abbreviation is unknown, not guessed, so that |
| 1934 | | -adding an option never breaks a script. A value given to a flag, |
| 1935 | | -<code>--follow=x</code>, is a mistake.</li> |
| 1936 | | -<li><code>--</code> ends the options, and isn’t an operand; |
| 1937 | | -<code>-</code> alone is one. The options end at the first operand, so in |
| 1938 | | -<code>keep start site python -m http.server</code> the <code>-m</code> |
| 1939 | | -is python’s.</li> |
| 1940 | | -<li>Each scope has its own options. An option given twice is read |
| 1941 | | -twice.</li> |
| 1022 | +<li>Flags may share a dash: <code>-lw</code> is <code>-l -w</code>. A letter’s value is the rest of its word, <code>-bdev</code>, or the whole next word, <code>-b dev</code>, even one that starts with a dash or is empty; in a group, the first letter that takes a value takes the rest, <code>-qn12</code>.</li> |
| 1023 | +<li>A long option is written in full, with its value after a <code>=</code> or in the next word: <code>--bar=dev</code>, <code>--bar dev</code>. An abbreviation is unknown, not guessed, so that adding an option never breaks a script. A value given to a flag, <code>--follow=x</code>, is a mistake.</li> |
| 1024 | +<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> |
| 1025 | +<li>Each scope has its own options. An option given twice is read twice.</li> |
| 1942 | 1026 | </ul> |
| 1943 | | -<p><code>number</code> reads the value as <code>Str.toInt</code> does. |
| 1944 | | -<code>operand</code> reads what options are left first, and the program |
| 1945 | | -doesn’t see them.</p> |
| 1946 | | -<p><code>-h</code> or <code>--help</code> among a scope’s options prints |
| 1947 | | -its help on standard output and ends the program with status 0. |
| 1948 | | -<code>init</code> and <code>scope</code> answer it, looking through the |
| 1949 | | -scope’s options before <code>next</code> gives one, so that the program |
| 1950 | | -has done nothing yet: <code>keep -v -h -x</code> prints the help, and |
| 1951 | | -the program never sees <code>-v</code>, nor the mistake. The help is the |
| 1952 | | -scope’s synopsis lines, a blank line, and its subcommands, its options |
| 1953 | | -and the help itself, each with its line of help:</p> |
| 1027 | +<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> |
| 1028 | +<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> |
| 1954 | 1029 | <pre><code>usage: keep start [-b label] name command [argument ...] |
| 1955 | 1030 | |
| 1956 | 1031 | -b, --bar label show the job in the bar as label |
| 1957 | 1032 | -h, --help print this and exit</code></pre> |
| 1958 | | -<p>A scope with several synopsis lines has the others indented under the |
| 1959 | | -first; one with none, <code>usage:</code> and its words, as |
| 1960 | | -<code>usage: keep start</code>.</p> |
| 1961 | | -<p>A mistake on the command line is a sentence on standard error, the |
| 1962 | | -program’s name first, then the scope’s synopsis lines, as the help |
| 1963 | | -begins, and status 2:</p> |
| 1033 | +<p>A scope with several synopsis lines has the others indented under the first; one with none, <code>usage:</code> and its words, as <code>usage: keep start</code>.</p> |
| 1034 | +<p>A mistake on the command line is a sentence on standard error, the program’s name first, then the scope’s synopsis lines, as the help begins, and status 2:</p> |
| 1964 | 1035 | <pre><code>keep: unknown option -x |
| 1965 | 1036 | usage: keep start [-b label] name command [argument ...]</code></pre> |
| 1966 | | -<p>The sentences are <code>unknown option -x</code> (or |
| 1967 | | -<code>--name</code>, up to its <code>=</code>), |
| 1968 | | -<code>-b needs a label</code>, <code>--follow takes no value</code> and |
| 1969 | | -<code>--wait wants a number from 1 to 600, not '0'</code>. |
| 1970 | | -<code>fail</code> writes the program’s own mistakes the same way, but |
| 1971 | | -alone, so that the sentence can say where the help is: |
| 1972 | | -<code>keep: no command; see keep -h</code>.</p> |
| 1973 | | -<p>A mistake in the table is the program’s, not its user’s. |
| 1974 | | -<code>init</code> checks the table before it reads a word, and ends the |
| 1975 | | -program with status 1: |
| 1976 | | -<code>keep: option table, row 3: the option is in the scope already</code>. |
| 1977 | | -The others are an option with neither a letter nor a long name, a long |
| 1978 | | -name with <code>=</code> in it, a letter <code>-</code>, and |
| 1979 | | -<code>-h</code> or <code>--help</code> in the table, since they are |
| 1980 | | -<code>Opt</code>’s.</p> |
| 1037 | +<p>The sentences are <code>unknown option -x</code> (or <code>--name</code>, up to its <code>=</code>), <code>-b needs a label</code>, <code>--follow takes no value</code> and <code>--wait wants a number from 1 to 600, not '0'</code>. <code>fail</code> writes the program’s own mistakes the same way, but alone, so that the sentence can say where the help is: <code>keep: no command; see keep -h</code>.</p> |
| 1038 | +<p>A mistake in the table is the program’s, not its user’s. <code>init</code> checks the table before it reads a word, and ends the program with status 1: <code>keep: option table, row 3: the option is in the scope already</code>. The others are an option with neither a letter nor a long name, a long name with <code>=</code> in it, a letter <code>-</code>, and <code>-h</code> or <code>--help</code> in the table, since they are <code>Opt</code>’s.</p> |
| 1981 | 1039 | <h1 id="versions">12. Versions</h1> |
| 1982 | | -<p>A version of this report is named by the month it was made in and a |
| 1983 | | -count: <code>2026-10.0</code>, then <code>2026-10.1</code> for the next |
| 1984 | | -change that month. <code>raz --version</code> says which version the |
| 1985 | | -compiler follows.</p> |
| 1040 | +<p>A version of this report is named by the month it was made in and a count: <code>2026-10.0</code>, then <code>2026-10.1</code> for the next change that month. <code>raz --version</code> says which version the compiler follows.</p> |
| 1986 | 1041 | <ul> |
| 1987 | | -<li><strong>2026-10.0.</strong> The first numbered version: the language |
| 1988 | | -of compiler 0.3.0.</li> |
| 1989 | | -<li><strong>2026-10.1.</strong> Messages: every error in a run, warnings |
| 1990 | | -of what is never used and <code>-Werror</code> (section 10); failed |
| 1991 | | -checks say what failed, and signals say where (sections 6.5 and |
| 1992 | | -10).</li> |
| 1993 | | -<li><strong>2026-10.2.</strong> <code>Sys</code> gains files and |
| 1994 | | -directories, the terminal, and the environment (section 11).</li> |
| 1995 | | -<li><strong>2026-10.3.</strong> <code>return f(x);</code> passes on the |
| 1996 | | -several results of <code>f</code> (sections 7.3 and 8). |
| 1997 | | -<code>and</code>, <code>or</code> and <code>not</code> are for |
| 1998 | | -<code>BOOL</code>s, and bind below the relations; <code>&</code>, |
| 1999 | | -<code>|</code> and <code>~</code> are the operators on bits, and |
| 2000 | | -<code>xor</code> is for integers only (sections 2 and 6). Arrays and |
| 2001 | | -slices of integers, <code>BOOL</code>s, enumerations, sets and pointers |
| 2002 | | -compare with <code>=</code>, by code where the comparison stands, not by |
| 2003 | | -a call into <code>Rt</code> (section 6.5). A generic module’s instance |
| 2004 | | -is made where it is first used, so its arguments may name types declared |
| 2005 | | -after the imports (section 3).</li> |
| 2006 | | -<li><strong>2026-10.4.</strong> <code>Sys</code> gains a connection to a |
| 2007 | | -Unix socket, waiting on several files at once, pseudo-terminals, and |
| 2008 | | -starting, waiting for and stopping programs (section 11).</li> |
| 2009 | | -<li><strong>2026-10.5.</strong> A module with files in more than one of |
| 2010 | | -the directories searched is an error, unless the first is a link to one |
| 2011 | | -of the others (section 3).</li> |
| 2012 | | -<li><strong>2026-10.6.</strong> <code>Sys.Info</code> says whether the |
| 2013 | | -path is a regular file, and whether it is a symbolic link (section |
| 2014 | | -11).</li> |
| 2015 | | -<li><strong>2026-10.7.</strong> <code>Sys.send</code> writes to a socket |
| 2016 | | -without SIGPIPE (section 11).</li> |
| 2017 | | -<li><strong>2026-10.8.</strong> <code>Sys.workDir</code> returns the |
| 2018 | | -directory, with no <code>var</code> parameter (section 11).</li> |
| 2019 | | -<li><strong>2026-10.9.</strong> The language is frozen (the start of the |
| 2020 | | -report). <code>Sys</code> gains opening a file for any use, seeking, |
| 2021 | | -links, permissions and times; pipes, process groups and the terminal’s |
| 2022 | | -foreground; signals caught on a file descriptor; clocks, sleeping and |
| 2023 | | -random bytes; and network sockets. <code>Sys.spawn</code> takes a |
| 2024 | | -<code>Program</code>, and <code>Sys.wait</code> says which program |
| 2025 | | -changed and how. <code>Str</code> gains UTF-8 and reading a number, |
| 2026 | | -<code>Map</code> removing and walking, <code>Fmt</code> reals; |
| 2027 | | -<code>Sort</code>, <code>Math</code>, <code>Files</code> and |
| 2028 | | -<code>Opt</code> are new (section 11).</li> |
| 2029 | | -<li><strong>2026-10.10.</strong> Said at last: what the compiler did and |
| 2030 | | -the report didn’t say, all through; what an implementation chooses, and |
| 2031 | | -the core every target has (appendix A); the whole grammar (appendix B); |
| 2032 | | -the library’s module names are kept for it (section 3); a record, |
| 2033 | | -variant or enumeration is a type of its own wherever it is written |
| 2034 | | -(section 5.10); a <code>nil</code> pointer, a division by zero and a |
| 2035 | | -stack overflow stop the program on every target (section 10); byte order |
| 2036 | | -is the target’s, and <code>Str</code> reads and writes a fixed one |
| 2037 | | -(sections 5.5 and 11); <code>Out</code>’s buffer is |
| 2038 | | -<code>Sys.outSize</code> bytes (section 11). Changed: a field of a |
| 2039 | | -packed record that may be misaligned has no address and no volatile |
| 2040 | | -access (section 5.5). Mended: arithmetic on untyped constants that |
| 2041 | | -overflows is an error (section 2); a slice of a pointer can’t run |
| 2042 | | -backwards (section 6.4). Ordinary accesses are no longer promised to be |
| 2043 | | -made exactly as written, only volatile and atomic ones (section |
| 2044 | | -7.6).</li> |
| 2045 | | -<li><strong>2026-10.11.</strong> <code>INT</code> and <code>UINT</code>, |
| 2046 | | -as big as an address, are the integers of lengths, slices, pointer |
| 2047 | | -differences, pointer conversions, <code>SYSCALL</code> and |
| 2048 | | -<code>ARGC</code>; <code>SIZE</code> and a constant <code>LEN</code> are |
| 2049 | | -untyped; <code>INT</code> widens silently to <code>I64</code> and |
| 2050 | | -<code>UINT</code> to <code>U64</code>, but neither mixes with them in an |
| 2051 | | -expression or a <code>for</code> (sections 5, 6.1 and 7.2); |
| 2052 | | -<code>Mem.Allocator</code> takes <code>INT</code> sizes. Pointers, |
| 2053 | | -procedures, <code>ERROR</code>s, slices and variants’ tags are sized by |
| 2054 | | -the address, and basic types aligned as the target’s C aligns them |
| 2055 | | -(section 5). Reals are a target’s to have or not (section 5.1). An |
| 2056 | | -exported variable can’t be changed, passed by <code>var</code> or |
| 2057 | | -addressed outside its module, nor an array in it sliced (section 3). |
| 2058 | | -<code>check off;</code> leaves a module’s index and slice checks out |
| 2059 | | -(sections 3, 6.4 and 6.5). An index of any integer type is checked by |
| 2060 | | -its value (6.5). A shift counts modulo its operand’s width, and the |
| 2061 | | -smallest value of every signed type divided by -1 stops the program |
| 2062 | | -(6.1). Writing a string’s bytes isn’t defined (5.10). The atomic |
| 2063 | | -built-ins work on <code>INT</code>, <code>UINT</code> and pointers |
| 2064 | | -(6.8). An integer constant past an extern’s parameters goes as an |
| 2065 | | -<code>INT</code> (9). An importer depends only on a module’s interface, |
| 2066 | | -so a module can be compiled on its own; whether it is, and into what, is |
| 2067 | | -the implementation’s (sections 3 and 10; appendix A). The library takes |
| 2068 | | -and gives <code>INT</code> for lengths, counts, descriptors, process ids |
| 2069 | | -and statuses, <code>main</code>’s and <code>Rt.exit</code>’s among them, |
| 2070 | | -and <code>I32</code> for a character; <code>I64</code> stays for what |
| 2071 | | -may pass an <code>INT</code> on a small target: a file’s size and |
| 2072 | | -position, times, and numbers read from text (section 11). An array |
| 2073 | | -constant has a type and is used as a variable that can’t be changed; |
| 2074 | | -<code>+</code> joins string constants (section 4). <code>match</code> |
| 2075 | | -takes an integer, with constants and ranges as labels (7.4).</li> |
| 2076 | | -<li><strong>2026-10.12.</strong> A code procedure is written in the |
| 2077 | | -target’s assembly language, not as bytes (section 8; appendix A). A |
| 2078 | | -program that stops says the calls under way and their variables, and |
| 2079 | | -<code>Rt.trace</code> says them on demand (sections 10 and 11).</li> |
| 2080 | | -<li><strong>2026-10.13.</strong> <code>Sys</code> gains writing without |
| 2081 | | -waiting, and waiting for room to write (section 11).</li> |
| 2082 | | -<li><strong>2026-10.14.</strong> The blank, <code>_</code>, drops a |
| 2083 | | -value or one of several results (section 7.1); a call that drops a |
| 2084 | | -result is warned of (section 10).</li> |
| 2085 | | -<li><strong>2026-10.15.</strong> <code>Math</code> gains |
| 2086 | | -<code>pow</code>, <code>ceil</code>, <code>round</code>, |
| 2087 | | -<code>log2</code>, <code>log10</code>, <code>tan</code>, |
| 2088 | | -<code>asin</code>, <code>acos</code> and <code>atan</code>; |
| 2089 | | -<code>Sys</code> gains <code>space</code> (section 11).</li> |
| 2090 | | -<li><strong>2026-10.16.</strong> Record constants, and array and record |
| 2091 | | -constants of records, strings, procedures and more (section 4); |
| 2092 | | -<code>Str</code> gains <code>copy</code> (section 11).</li> |
| 2093 | | -<li><strong>2026-10.17.</strong> <code>COPY</code> copies the elements |
| 2094 | | -of one array or slice to another as a block (section 6.8).</li> |
| 1042 | +<li><strong>2026-10.0.</strong> The first numbered version: the language of compiler 0.3.0.</li> |
| 1043 | +<li><strong>2026-10.1.</strong> Messages: every error in a run, warnings of what is never used and <code>-Werror</code> (section 10); failed checks say what failed, and signals say where (sections 6.5 and 10).</li> |
| 1044 | +<li><strong>2026-10.2.</strong> <code>Sys</code> gains files and directories, the terminal, and the environment (section 11).</li> |
| 1045 | +<li><strong>2026-10.3.</strong> <code>return f(x);</code> passes on the several results of <code>f</code> (sections 7.3 and 8). <code>and</code>, <code>or</code> and <code>not</code> are for <code>BOOL</code>s, and bind below the relations; <code>&</code>, <code>|</code> and <code>~</code> are the operators on bits, and <code>xor</code> is for integers only (sections 2 and 6). Arrays and slices of integers, <code>BOOL</code>s, enumerations, sets and pointers compare with <code>=</code>, by code where the comparison stands, not by a call into <code>Rt</code> (section 6.5). A generic module’s instance is made where it is first used, so its arguments may name types declared after the imports (section 3).</li> |
| 1046 | +<li><strong>2026-10.4.</strong> <code>Sys</code> gains a connection to a Unix socket, waiting on several files at once, pseudo-terminals, and starting, waiting for and stopping programs (section 11).</li> |
| 1047 | +<li><strong>2026-10.5.</strong> A module with files in more than one of the directories searched is an error, unless the first is a link to one of the others (section 3).</li> |
| 1048 | +<li><strong>2026-10.6.</strong> <code>Sys.Info</code> says whether the path is a regular file, and whether it is a symbolic link (section 11).</li> |
| 1049 | +<li><strong>2026-10.7.</strong> <code>Sys.send</code> writes to a socket without SIGPIPE (section 11).</li> |
| 1050 | +<li><strong>2026-10.8.</strong> <code>Sys.workDir</code> returns the directory, with no <code>var</code> parameter (section 11).</li> |
| 1051 | +<li><strong>2026-10.9.</strong> The language is frozen (the start of the report). <code>Sys</code> gains opening a file for any use, seeking, links, permissions and times; pipes, process groups and the terminal’s foreground; signals caught on a file descriptor; clocks, sleeping and random bytes; and network sockets. <code>Sys.spawn</code> takes a <code>Program</code>, and <code>Sys.wait</code> says which program changed and how. <code>Str</code> gains UTF-8 and reading a number, <code>Map</code> removing and walking, <code>Fmt</code> reals; <code>Sort</code>, <code>Math</code>, <code>Files</code> and <code>Opt</code> are new (section 11).</li> |
| 1052 | +<li><strong>2026-10.10.</strong> Said at last: what the compiler did and the report didn’t say, all through; what an implementation chooses, and the core every target has (appendix A); the whole grammar (appendix B); the library’s module names are kept for it (section 3); a record, variant or enumeration is a type of its own wherever it is written (section 5.10); a <code>nil</code> pointer, a division by zero and a stack overflow stop the program on every target (section 10); byte order is the target’s, and <code>Str</code> reads and writes a fixed one (sections 5.5 and 11); <code>Out</code>’s buffer is <code>Sys.outSize</code> bytes (section 11). Changed: a field of a packed record that may be misaligned has no address and no volatile access (section 5.5). Mended: arithmetic on untyped constants that overflows is an error (section 2); a slice of a pointer can’t run backwards (section 6.4). Ordinary accesses are no longer promised to be made exactly as written, only volatile and atomic ones (section 7.6).</li> |
| 1053 | +<li><strong>2026-10.11.</strong> <code>INT</code> and <code>UINT</code>, as big as an address, are the integers of lengths, slices, pointer differences, pointer conversions, <code>SYSCALL</code> and <code>ARGC</code>; <code>SIZE</code> and a constant <code>LEN</code> are untyped; <code>INT</code> widens silently to <code>I64</code> and <code>UINT</code> to <code>U64</code>, but neither mixes with them in an expression or a <code>for</code> (sections 5, 6.1 and 7.2); <code>Mem.Allocator</code> takes <code>INT</code> sizes. Pointers, procedures, <code>ERROR</code>s, slices and variants’ tags are sized by the address, and basic types aligned as the target’s C aligns them (section 5). Reals are a target’s to have or not (section 5.1). An exported variable can’t be changed, passed by <code>var</code> or addressed outside its module, nor an array in it sliced (section 3). <code>check off;</code> leaves a module’s index and slice checks out (sections 3, 6.4 and 6.5). An index of any integer type is checked by its value (6.5). A shift counts modulo its operand’s width, and the smallest value of every signed type divided by -1 stops the program (6.1). Writing a string’s bytes isn’t defined (5.10). The atomic built-ins work on <code>INT</code>, <code>UINT</code> and pointers (6.8). An integer constant past an extern’s parameters goes as an <code>INT</code> (9). An importer depends only on a module’s interface, so a module can be compiled on its own; whether it is, and into what, is the implementation’s (sections 3 and 10; appendix A). The library takes and gives <code>INT</code> for lengths, counts, descriptors, process ids and statuses, <code>main</code>’s and <code>Rt.exit</code>’s among them, and <code>I32</code> for a character; <code>I64</code> stays for what may pass an <code>INT</code> on a small target: a file’s size and position, times, and numbers read from text (section 11). An array constant has a type and is used as a variable that can’t be changed; <code>+</code> joins string constants (section 4). <code>match</code> takes an integer, with constants and ranges as labels (7.4).</li> |
| 1054 | +<li><strong>2026-10.12.</strong> A code procedure is written in the target’s assembly language, not as bytes (section 8; appendix A). A program that stops says the calls under way and their variables, and <code>Rt.trace</code> says them on demand (sections 10 and 11).</li> |
| 1055 | +<li><strong>2026-10.13.</strong> <code>Sys</code> gains writing without waiting, and waiting for room to write (section 11).</li> |
| 1056 | +<li><strong>2026-10.14.</strong> The blank, <code>_</code>, drops a value or one of several results (section 7.1); a call that drops a result is warned of (section 10).</li> |
| 1057 | +<li><strong>2026-10.15.</strong> <code>Math</code> gains <code>pow</code>, <code>ceil</code>, <code>round</code>, <code>log2</code>, <code>log10</code>, <code>tan</code>, <code>asin</code>, <code>acos</code> and <code>atan</code>; <code>Sys</code> gains <code>space</code> (section 11).</li> |
| 1058 | +<li><strong>2026-10.16.</strong> Record constants, and array and record constants of records, strings, procedures and more (section 4); <code>Str</code> gains <code>copy</code> (section 11).</li> |
| 1059 | +<li><strong>2026-10.17.</strong> <code>COPY</code> copies the elements of one array or slice to another as a block (section 6.8).</li> |
| 1060 | +<li><strong>2026-10.18.</strong> A code procedure takes and returns slices (section 8).</li> |
| 1061 | +<li><strong>2026-10.19.</strong> The library gains <code>Bytes</code> (section 11).</li> |
| 2095 | 1062 | </ul> |
| 2096 | | -<h1 id="appendix-a.-what-an-implementation-chooses">Appendix A. What an |
| 2097 | | -implementation chooses</h1> |
| 2098 | | -<p>Everything in this report binds every implementation of raz, except |
| 2099 | | -what follows, which each chooses for itself and documents. This |
| 2100 | | -compiler’s choices are in <code>doc/compiler.md</code>, under “This |
| 2101 | | -compiler’s choices”. The programs in <code>conform/</code> are how an |
| 2102 | | -implementation shows that it follows this report: it passes them all, |
| 2103 | | -its reals aside where its target has none; <code>conform/check.sh</code> |
| 2104 | | -runs them.</p> |
| 1063 | +<h1 id="appendix-a.-what-an-implementation-chooses">Appendix A. What an implementation chooses</h1> |
| 1064 | +<p>Everything in this report binds every implementation of raz, except what follows, which each chooses for itself and documents. This compiler’s choices are in <code>doc/compiler.md</code>, under “This compiler’s choices”. The programs in <code>conform/</code> are how an implementation shows that it follows this report: it passes them all, its reals aside where its target has none; <code>conform/check.sh</code> runs them.</p> |
| 2105 | 1065 | <ul> |
| 2106 | | -<li><strong>The target.</strong> Which machines and systems it compiles |
| 2107 | | -for, and the names of their files (<code>linux-x86_64</code>, section |
| 2108 | | -3); for each, the size of an address, and so of <code>INT</code>, and |
| 2109 | | -whether it has reals.</li> |
| 2110 | | -<li><strong>Separate compilation.</strong> Whether modules are compiled |
| 2111 | | -on their own, and if so into what: the object file, its format, the form |
| 2112 | | -of the code in it, when one is out of date, and how an instance is named |
| 2113 | | -so that the program keeps one (section 10). This compiler compiles every |
| 2114 | | -module of a program together, and writes no object files.</li> |
| 2115 | | -<li><strong>Atomics.</strong> How <code>CAS</code>, <code>XCHG</code> |
| 2116 | | -and <code>XADD</code> are made sequentially consistent on a machine |
| 2117 | | -without atomic instructions (section 6.8): on one with a single |
| 2118 | | -processor, by holding off interrupts while they run, for one.</li> |
| 2119 | | -<li><strong>The calling convention.</strong> On x86-64 Linux it is |
| 2120 | | -System V (sections 8 and 9); elsewhere it is the C library’s own, so |
| 2121 | | -that <code>extern</code> procedures and callbacks work. How a procedure |
| 2122 | | -returns several results, and how its frame is laid out, are the |
| 2123 | | -implementation’s.</li> |
| 2124 | | -<li><strong>What is the target’s.</strong> Code procedures (section 8) |
| 2125 | | -hold the target’s machine code, written in its assembly language, and |
| 2126 | | -exist only where the implementation says so; <code>SYSCALL</code> exists |
| 2127 | | -only on Linux targets (section 6.8). A record’s layout is the target’s C |
| 2128 | | -layout (section 5.5), and the order of a value’s bytes is the target’s |
| 2129 | | -(little-endian on x86-64).</li> |
| 2130 | | -<li><strong>The assembly language.</strong> How a code procedure’s |
| 2131 | | -instructions are written (section 8). On x86-64 it is Intel’s syntax, as |
| 2132 | | -NASM writes it: the destination first; registers by name |
| 2133 | | -(<code>rax</code>, <code>eax</code>, <code>al</code>, |
| 2134 | | -<code>xmm0</code>); memory as |
| 2135 | | -<code>[base + index * scale + disp]</code>, with <code>byte</code>, |
| 2136 | | -<code>word</code>, <code>dword</code> or <code>qword</code> before it |
| 2137 | | -where no register says the size; <code>lock</code> before an |
| 2138 | | -instruction. Which instructions it knows is the implementation’s; |
| 2139 | | -<code>db</code> gives any others.</li> |
| 2140 | | -<li><strong>Sizes in the library.</strong> How many bytes |
| 2141 | | -<code>Out</code> keeps before it writes them, <code>Sys.outSize</code> |
| 2142 | | -(section 11): 4 KB on Linux, fewer on a small machine.</li> |
| 2143 | | -<li><strong>The executable.</strong> Its format, and the names and line |
| 2144 | | -numbers it carries for a debugger (section 10).</li> |
| 2145 | | -<li><strong>How a failure is caught.</strong> A <code>nil</code> |
| 2146 | | -pointer, a division by zero or a stack overflow may be caught by the |
| 2147 | | -machine or by checks in the compiled code; either way the program stops |
| 2148 | | -with its message (section 10).</li> |
| 2149 | | -<li><strong>Messages and warnings.</strong> The text of the compiler’s |
| 2150 | | -messages, how many errors it reports before it stops, and which warnings |
| 2151 | | -it gives (section 10). A program that is an error in this report is one |
| 2152 | | -in every implementation; the words that say so are free.</li> |
| 2153 | | -<li><strong>The command line.</strong> How the compiler is run, its |
| 2154 | | -options, and where its library lives (sections 1 and 3).</li> |
| 1066 | +<li><strong>The target.</strong> Which machines and systems it compiles for, and the names of their files (<code>linux-x86_64</code>, section 3); for each, the size of an address, and so of <code>INT</code>, and whether it has reals.</li> |
| 1067 | +<li><strong>Separate compilation.</strong> Whether modules are compiled on their own, and if so into what: the object file, its format, the form of the code in it, when one is out of date, and how an instance is named so that the program keeps one (section 10). This compiler compiles every module of a program together, and writes no object files.</li> |
| 1068 | +<li><strong>Atomics.</strong> How <code>CAS</code>, <code>XCHG</code> and <code>XADD</code> are made sequentially consistent on a machine without atomic instructions (section 6.8): on one with a single processor, by holding off interrupts while they run, for one.</li> |
| 1069 | +<li><strong>The calling convention.</strong> On x86-64 Linux it is System V (sections 8 and 9); elsewhere it is the C library’s own, so that <code>extern</code> procedures and callbacks work. How a procedure returns several results, and how its frame is laid out, are the implementation’s.</li> |
| 1070 | +<li><strong>What is the target’s.</strong> Code procedures (section 8) hold the target’s machine code, written in its assembly language, and exist only where the implementation says so; <code>SYSCALL</code> exists only on Linux targets (section 6.8). A record’s layout is the target’s C layout (section 5.5), and the order of a value’s bytes is the target’s (little-endian on x86-64).</li> |
| 1071 | +<li><strong>The assembly language.</strong> How a code procedure’s instructions are written (section 8). On x86-64 it is Intel’s syntax, as NASM writes it: the destination first; registers by name (<code>rax</code>, <code>eax</code>, <code>al</code>, <code>xmm0</code>); memory as <code>[base + index * scale + disp]</code>, with <code>byte</code>, <code>word</code>, <code>dword</code> or <code>qword</code> before it where no register says the size; <code>lock</code> before an instruction. Which instructions it knows is the implementation’s; <code>db</code> gives any others.</li> |
| 1072 | +<li><strong>Sizes in the library.</strong> How many bytes <code>Out</code> keeps before it writes them, <code>Sys.outSize</code> (section 11): 4 KB on Linux, fewer on a small machine.</li> |
| 1073 | +<li><strong>The executable.</strong> Its format, and the names and line numbers it carries for a debugger (section 10).</li> |
| 1074 | +<li><strong>How a failure is caught.</strong> A <code>nil</code> pointer, a division by zero or a stack overflow may be caught by the machine or by checks in the compiled code; either way the program stops with its message (section 10).</li> |
| 1075 | +<li><strong>Messages and warnings.</strong> The text of the compiler’s messages, how many errors it reports before it stops, and which warnings it gives (section 10). A program that is an error in this report is one in every implementation; the words that say so are free.</li> |
| 1076 | +<li><strong>The command line.</strong> How the compiler is run, its options, and where its library lives (sections 1 and 3).</li> |
| 2155 | 1077 | </ul> |
| 2156 | | -<p><strong>The core.</strong> A target may leave out what the system |
| 2157 | | -under it can’t give: <code>Sys</code> past writing, ending the program |
| 2158 | | -and getting memory, <code>Thread</code>, <code>link</code> and |
| 2159 | | -<code>extern</code>. What it must have, besides the whole language, |
| 2160 | | -reals aside, is the core of the library: <code>Rt</code>, |
| 2161 | | -<code>Out</code>, <code>Mem</code>, <code>Arena</code>, |
| 2162 | | -<code>Lock</code>, <code>Str</code> and <code>Buf</code>, and of |
| 2163 | | -<code>Sys</code> what they need. A program that keeps to the core runs |
| 2164 | | -on every target.</p> |
| 1078 | +<p><strong>The core.</strong> A target may leave out what the system under it can’t give: <code>Sys</code> past writing, ending the program and getting memory, <code>Thread</code>, <code>link</code> and <code>extern</code>. What it must have, besides the whole language, reals aside, is the core of the library: <code>Rt</code>, <code>Out</code>, <code>Mem</code>, <code>Arena</code>, <code>Lock</code>, <code>Str</code> and <code>Buf</code>, and of <code>Sys</code> what they need. A program that keeps to the core runs on every target.</p> |
| 2165 | 1079 | <h1 id="appendix-b.-the-grammar">Appendix B. The grammar</h1> |
| 2166 | | -<p>The whole syntax of raz, gathered from the sections above, with the |
| 2167 | | -symbols of section 2. <code>{ x }</code> is any number of |
| 2168 | | -<code>x</code>, <code>[ x ]</code> is <code>x</code> or nothing, and |
| 2169 | | -<code>( x | y )</code> is one of them.</p> |
| 1080 | +<p>The whole syntax of raz, gathered from the sections above, with the symbols of section 2. <code>{ x }</code> is any number of <code>x</code>, <code>[ x ]</code> is <code>x</code> or nothing, and <code>( x | y )</code> is one of them.</p> |
| 2170 | 1081 | <pre><code>ident = letter { letter | digit } . |
| 2171 | 1082 | letter = "A" .. "Z" | "a" .. "z" | "_" . |
| 2172 | 1083 | digit = "0" .. "9" . |
| | @@ -2258,16 +1169,9 @@ |
| 2258 | 1169 | Results = Type | "(" Type { "," Type } ")" . |
| 2259 | 1170 | ExternDeclaration = "extern" ( "proc" IdentDef Signature |
| 2260 | 1171 | | "var" IdentDef ":" Type ) .</code></pre> |
| 2261 | | -<p><code>character</code> is any character but a line break, and in a |
| 2262 | | -string any but <code>"</code> and <code>\</code>, in a character any but |
| 2263 | | -<code>'</code> and <code>\</code>. A procedure argument of a generic |
| 2264 | | -module (section 3) is written as a <code>QualIdent</code>, which is a |
| 2265 | | -<code>Type</code> here. <code>...</code> comes last in a |
| 2266 | | -<code>Signature</code>, and only in an <code>extern</code>. An |
| 2267 | | -<code>Instruction</code> is written in the target’s assembly language |
| 2268 | | -(appendix A).</p> |
| 1172 | +<p><code>character</code> is any character but a line break, and in a string any but <code>"</code> and <code>\</code>, in a character any but <code>'</code> and <code>\</code>. A procedure argument of a generic module (section 3) is written as a <code>QualIdent</code>, which is a <code>Type</code> here. <code>...</code> comes last in a <code>Signature</code>, and only in an <code>extern</code>. An <code>Instruction</code> is written in the target’s assembly language (appendix A).</p> |
| 2269 | 1173 | </div> |
| 2270 | 1174 | </main> |
| 2271 | | -<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer> |
| 1175 | +<footer>raz is under the ISC license.</footer> |
| 2272 | 1176 | </body> |
| 2273 | 1177 | </html> |