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