| | @@ -38,7 +38,7 @@ |
| 38 | 38 | <p>raz is a small systems language, first made for x86-64 Linux. A raz program can lay a record over a device’s registers, read and write a device’s memory exactly as written, take part in lock-free algorithms, make system calls and call C libraries. It has no garbage collector and no hidden run time: what the program does is what its text says.</p> |
| 39 | 39 | <p>This is the first volume of the report: the language, what an implementation chooses, and the grammar. The second, <code>doc/raz-lib.md</code>, is the library, which sections 3 and 10 and appendix A name.</p> |
| 40 | 40 | <p>This report defines the language. It is short on purpose. Whatever it doesn’t allow, raz doesn’t have.</p> |
| 41 | | -<p>This is version 2026-10.29 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program. 2026-10.27 mends a defect: returning a slice of the procedure’s own frame is an error (section 7.3), which breaks only a program that read memory already gone. 2026-10.28 mends another: an array a call returns lives to the end of its statement, and a slice of it kept past that is undefined (sections 8 and 10). 2026-10.29 is a sixth thaw: an array constant typed <code>[]T</code> takes its length from its literal (section 4); it breaks no program.</p> |
| 41 | +<p>This is version 2026-10.31 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program. 2026-10.27 mends a defect: returning a slice of the procedure’s own frame is an error (section 7.3), which breaks only a program that read memory already gone. 2026-10.28 mends another: an array a call returns lives to the end of its statement, and a slice of it kept past that is undefined (sections 8 and 10). 2026-10.29 is a sixth thaw: an array constant typed <code>[]T</code> takes its length from its literal (section 4); it breaks no program. 2026-10.30 mends a defect: <code>[]</code> used as a slice is the empty slice, which may be returned (sections 6.5 and 7.3); it breaks no program. 2026-10.31 is a seventh thaw: <code>INT</code> is at least 16 bits, and <code>U8</code>, <code>I8</code> and <code>I16</code> widen to it (sections 5.1, 5.10 and 6.1); an enumeration’s name may be written with its type, and must be where another declaration shares it (section 5.7); and <code>continue</code> (section 7.2), which breaks a program that has a name <code>continue</code>.</p> |
| 42 | 42 | <p>The language is frozen, softly, from this version on: it changes only to mend a defect, and its library grows only by what breaks no program written for it. What anyone wishes it had waits on a list, for a later thaw.</p> |
| 43 | 43 | <h1 id="a-first-look">1. A first look</h1> |
| 44 | 44 | <pre><code>module Hello; |
| | @@ -55,10 +55,10 @@ |
| 55 | 55 | <p>A program is written in UTF-8. The compiler rejects a file that isn’t valid UTF-8: a malformed or overlong sequence, a surrogate, or a code point beyond 10FFFFH, and a file with a NUL byte. Everything outside strings, characters and comments is ASCII. Spaces, tabs and line breaks separate symbols and are otherwise ignored.</p> |
| 56 | 56 | <p><strong>Identifiers</strong> are an ASCII letter or <code>_</code> followed by letters, digits and <code>_</code>, up to 63 characters. Case matters: <code>count</code> and <code>Count</code> are different names. <code>_</code> alone isn’t a name: it is the blank, which stands only to the left of <code>:=</code> (section 7.1).</p> |
| 57 | 57 | <p><strong>Reserved words</strong> are lowercase, except <code>VOLATILE</code>:</p> |
| 58 | | -<pre><code>and begin break by code const defer div do else elsif end extern for |
| 59 | | -forward if import in link match mod module not of or packed proc record |
| 60 | | -repeat return set shl shr then to try type until var variant VOLATILE |
| 61 | | -while xor</code></pre> |
| 58 | +<pre><code>and begin break by code const continue defer div do else elsif end |
| 59 | +extern for forward if import in link match mod module not of or packed |
| 60 | +proc record repeat return set shl shr then to try type until var |
| 61 | +variant VOLATILE while xor</code></pre> |
| 62 | 62 | <p><code>static</code>, in <code>link static;</code> (section 3), and <code>check</code> and <code>off</code>, in <code>check off;</code> (section 3), aren’t reserved.</p> |
| 63 | 63 | <p><strong>Predeclared names</strong> are the basic types (section 5.1), <code>ERROR</code>, <code>true</code>, <code>false</code>, <code>nil</code> and the built-in procedures (section 6.8). They belong to a scope around every module, so a module or a procedure may declare the same name for something else, which hides them there.</p> |
| 64 | 64 | <p><strong>Numbers.</strong> <code>123</code> is decimal. <code>0FFH</code> is hexadecimal: it starts with a digit, its digits are <code>0</code> to <code>9</code> and <code>A</code> to <code>F</code>, and it ends in <code>H</code>, all in capitals. <code>1.5</code> and <code>2.0E-3</code> are reals; a real always has a digit on both sides of its point, and its exponent, if it has one, is <code>E</code>, a sign if wanted, and digits. A decimal number above the largest <code>I64</code>, or a hexadecimal one above the largest <code>U64</code>, is an error; so is a real above the largest <code>F64</code>. A real below the least <code>F64</code> is 0.</p> |
| | @@ -210,7 +210,7 @@ |
| 210 | 210 | </tr> |
| 211 | 211 | </tbody> |
| 212 | 212 | </table> |
| 213 | | -<p><code>INT</code> is the everyday integer: lengths, indexes and sizes are <code>INT</code>s. It is as big as an address, so it is the integer a pointer converts to (section 6.7), and a word the atomic built-ins work on (section 6.8). <code>INT</code> and <code>I64</code> are two types, even where they have the same size: an <code>INT</code> goes silently into an <code>I64</code> wherever it meets one, in an expression too (sections 5.10 and 6.1), and an <code>I64</code> becomes an <code>INT</code> only by <code>INT(x)</code>, which may drop bits on a smaller target. A type bigger than the largest <code>INT</code> in bytes is an error.</p> |
| 213 | +<p><code>INT</code> is the everyday integer: lengths, indexes and sizes are <code>INT</code>s. It is as big as an address, and at least 16 bits, as <code>UINT</code> is, on every target; so it is the integer a pointer converts to (section 6.7), and a word the atomic built-ins work on (section 6.8). <code>INT</code> and <code>I64</code> are two types, even where they have the same size: an <code>INT</code> goes silently into an <code>I64</code> wherever it meets one, in an expression too (sections 5.10 and 6.1), and an <code>I64</code> becomes an <code>INT</code> only by <code>INT(x)</code>, which may drop bits on a smaller target. A type bigger than the largest <code>INT</code> in bytes is an error.</p> |
| 214 | 214 | <p><code>F32</code> and <code>F64</code> exist only on a target that has reals (appendix A). On one that hasn’t, a real literal, and a use of <code>F32</code>, <code>F64</code> or of a name whose type is made of them, is an error; a declaration that no used code names isn’t, so a module like <code>Fmt</code> keeps one interface on every target.</p> |
| 215 | 215 | <p>Each is aligned as the target’s C aligns it: to its size on x86-64. So are <code>ERROR</code> (section 5.8), pointers and procedures, each as big as an address; a slice is aligned as an address is.</p> |
| 216 | 216 | <h2 id="arrays">5.2 Arrays</h2> |
| | @@ -242,6 +242,17 @@ |
| 242 | 242 | Color* = (red, green, blue); |
| 243 | 243 | Colors* = set of Color;</code></pre> |
| 244 | 244 | <p>An enumeration is a list of names, in order. Like the cases of a variant, the names are declared in the scope around the type. An enumeration has at most 256 names and takes 1 byte; the first name is 0, the next 1, and so on.</p> |
| 245 | +<p>A name of an enumeration may also be written with its type, <code>Color.red</code>, and one imported with the type’s module, <code>M.Color.red</code>. Two enumerations of a scope may share a name, and an enumeration may have a name that something else of its scope has, a constant, a variable or a procedure: such a name is written with its type, <code>Elem.bold</code>. Written alone, it names the one declaration of it that isn’t an enumeration’s, if there is one, and is an error if there isn’t:</p> |
| 246 | +<pre><code>type |
| 247 | + Style = (bold, italic); |
| 248 | + Elem = (para, bold, rule); |
| 249 | +proc rule; (* a procedure, beside Elem's rule *) |
| 250 | +... |
| 251 | +s := Style.bold; e := Elem.bold; e := Elem.rule; |
| 252 | +e := para; (* para is Elem's alone *) |
| 253 | +rule; (* the procedure *) |
| 254 | +s := bold; (* an error: write Style.bold or Elem.bold *)</code></pre> |
| 255 | +<p>The type is never taken from where the name stands: <code>s := bold</code> is an error though <code>s</code> is a <code>Style</code>.</p> |
| 245 | 256 | <p><code>set of E</code>, for an enumeration <code>E</code> of at most 64 names, holds any choice of <code>E</code>’s names: bit <code>i</code> stands for the name at position <code>i</code>. It takes the smallest of 1, 2, 4 and 8 bytes that has a bit for each name, so a set can stand for a C flags word.</p> |
| 246 | 257 | <h2 id="errors">5.8 Errors</h2> |
| 247 | 258 | <pre><code>const NotFound*: ERROR = "not found";</code></pre> |
| | @@ -266,10 +277,17 @@ |
| 266 | 277 | <li><code>A</code> is <code>[n]T</code> and <code>B</code> is <code>[]T</code>;</li> |
| 267 | 278 | <li><code>A</code> is a string literal and <code>B</code> is <code>[]U8</code>, <code>^U8</code> (a pointer to the bytes followed by a zero byte, as C wants), or <code>[n]U8</code> with <code>n</code> at least the string’s length (the rest is zero);</li> |
| 268 | 279 | <li><code>A</code> is a procedure and <code>B</code> a procedure type with the same signature;</li> |
| 269 | | -<li><code>A</code> is <code>INT</code> and <code>B</code> is <code>I64</code>, or <code>A</code> is <code>UINT</code> and <code>B</code> is <code>U64</code>, which loses nothing on any target.</li> |
| 280 | +<li><code>A</code> is <code>INT</code> and <code>B</code> is <code>I64</code>, or <code>A</code> is <code>UINT</code> and <code>B</code> is <code>U64</code>, which loses nothing on any target;</li> |
| 281 | +<li><code>A</code> is <code>U8</code>, <code>I8</code> or <code>I16</code> and <code>B</code> is <code>INT</code>, or <code>A</code> is <code>U8</code> or <code>U16</code> and <code>B</code> is <code>UINT</code>, which an <code>INT</code> or a <code>UINT</code> of 16 bits holds whole, so on every target; unless the value is what an operator gives (below).</li> |
| 270 | 282 | </ul> |
| 271 | 283 | <p>A string’s bytes are constant. A <code>[]U8</code> or <code>^U8</code> made from a string literal, or a slice of a string constant, points at them, and writing through it isn’t defined: on a target with read-only memory, it may stop the program as a bad address (section 10). A <code>[n]U8</code> made from a string is a copy, the program’s to change.</p> |
| 272 | 284 | <p>Nothing else converts silently. In particular, there is no other conversion between integer types, between integers and reals, or between <code>F32</code> and <code>F64</code>, without a written conversion (section 6.7). An <code>INT</code> widens to <code>I64</code>, and a <code>UINT</code> to <code>U64</code>, wherever its value goes where an <code>I64</code> is wanted: in <code>a, b := f(x)</code> and <code>return f(x)</code> as in any other assignment, to the bounds of a <code>for</code> and the step of <code>INC</code> and <code>DEC</code> whose variable is an <code>I64</code>, and in an expression, where it meets an <code>I64</code> (section 6.1). The types themselves stay two: a <code>^INT</code> isn’t a <code>^I64</code>, an <code>INT</code> variable isn’t passed to a <code>var</code> parameter of type <code>I64</code>, and a procedure returning an <code>INT</code> isn’t of a procedure type returning an <code>I64</code>.</p> |
| 285 | +<p>A <code>U8</code>, <code>I8</code> or <code>I16</code> widens to <code>INT</code>, and a <code>U8</code> or <code>U16</code> to <code>UINT</code>, in the same places, as an <code>INT</code> to <code>I64</code>: assigned, passed, returned, in <code>a, b := f(x)</code>, to a <code>for</code>’s bounds and <code>INC</code> and <code>DEC</code>’s step, and in an expression, where it meets an <code>INT</code> or a <code>UINT</code> (section 6.1); not in <code>return f(x)</code>, which passes <code>f</code>’s results on as they are. They widen only that far: a <code>U8</code> meets an <code>I64</code> only as <code>INT(b)</code> or <code>I64(b)</code>. And they widen only as values that are moved: a variable, an element, a field, a call’s result or a conversion’s. What an operator gives on them doesn’t: it was computed in their type, and has wrapped there. With <code>a, b: U8</code> holding 200 and 100, <code>a + b</code> is 44, so <code>n := a + b</code>, for an <code>INT</code> <code>n</code>, is an error; <code>n := INT(a) + b</code> is 300, and says where the sum is made.</p> |
| 286 | +<pre><code>var s: []U8; n, d: INT; |
| 287 | +n := s[i]; (* an element: widens *) |
| 288 | +n := n * 10 + s[i] - '0'; (* n * 10 + s[i] is an INT, and so the rest *) |
| 289 | +d := s[i] - '0'; (* an error: a U8, which may have wrapped *) |
| 290 | +d := INT(s[i]) - '0'; (* below '0' too, it is negative *)</code></pre> |
| 273 | 291 | <h1 id="expressions">6. Expressions</h1> |
| 274 | 292 | <pre><code>Expr = Conjunct { "or" Conjunct } . |
| 275 | 293 | Conjunct = Relation { "and" Relation } . |
| | @@ -293,7 +311,7 @@ |
| 293 | 311 | <p><code>and</code>, <code>or</code> and <code>not</code> are for <code>BOOL</code>s only; <code>&</code>, <code>|</code>, <code>xor</code> and <code>~</code> are for integers only.</p> |
| 294 | 312 | <p>A pointer to a record isn’t the record: its fields are <code>p^.f</code>, never <code>p.f</code>, and the elements of a pointer to an array are <code>p^[i]</code>. A statement starts with a name or <code>try</code>, so <code>^T(x)^ := v</code> isn’t one; a view, <code>mem[a].(I64) := v</code> (section 6.9), says it without a pointer.</p> |
| 295 | 313 | <h2 id="integers">6.1 Integers</h2> |
| 296 | | -<p>Both operands of <code>+ - * div mod & | xor</code>, and of a relation, have the same integer type, or one is an untyped constant that takes the other’s type, which it must fit, or one is an <code>INT</code> and the other an <code>I64</code> (a <code>UINT</code> and a <code>U64</code>), and the <code>INT</code> widens to <code>I64</code> (section 5.10). So do <code>v</code> and <code>k</code> in <code>INC(v, k)</code> and <code>DEC(v, k)</code>, where only <code>k</code> widens: an <code>I64</code> <code>k</code> isn’t added to an <code>INT</code> <code>v</code>. The result has that type and wraps around at its width: in <code>U8</code>, <code>200 + 100</code> is <code>44</code>. A unary minus wraps too: for <code>x</code> of an unsigned type, <code>-x</code> is <code>0 - x</code>. Other integer types are never mixed, whatever their sizes: with <code>i: I32</code>, <code>i < LEN(s)</code> is an error, and <code>i</code> should be an <code>INT</code>. With <code>pos: I64</code>, <code>pos + LEN(s)</code> is an <code>I64</code>.</p> |
| 314 | +<p>Both operands of <code>+ - * div mod & | xor</code>, and of a relation, have the same integer type, or one is an untyped constant that takes the other’s type, which it must fit, or one is an <code>INT</code> and the other an <code>I64</code> (a <code>UINT</code> and a <code>U64</code>), and the <code>INT</code> widens to <code>I64</code>, or one is an <code>INT</code> and the other a <code>U8</code>, <code>I8</code> or <code>I16</code> (a <code>UINT</code> and a <code>U8</code> or <code>U16</code>) that no operator gave, and it widens to <code>INT</code> (section 5.10). So do <code>v</code> and <code>k</code> in <code>INC(v, k)</code> and <code>DEC(v, k)</code>, where only <code>k</code> widens: an <code>I64</code> <code>k</code> isn’t added to an <code>INT</code> <code>v</code>. The result has that type and wraps around at its width: in <code>U8</code>, <code>200 + 100</code> is <code>44</code>. A unary minus wraps too: for <code>x</code> of an unsigned type, <code>-x</code> is <code>0 - x</code>. Other integer types are never mixed, whatever their sizes: with <code>i: I32</code>, <code>i < LEN(s)</code> is an error, and <code>i</code> should be an <code>INT</code>; with <code>c: U8</code>, <code>c < LEN(s)</code> is an <code>INT</code> comparison. With <code>pos: I64</code>, <code>pos + LEN(s)</code> is an <code>I64</code>.</p> |
| 297 | 315 | <p>Where the report asks for an integer and names no type, as an index, a slice’s bound, a shift’s count, a pointer’s offset <code>i</code> in <code>p + i</code>, or a conversion’s operand, any integer type will do, and its value is taken. On a target whose <code>INT</code> is narrower, a pointer’s offset is cut to an <code>INT</code>.</p> |
| 298 | 316 | <p><code>SIZE(T)</code>, and <code>LEN</code> of an array or a string constant, are untyped constants, like a literal.</p> |
| 299 | 317 | <p><code>div</code> rounds toward zero and <code>mod</code> takes the sign of the dividend, as in C: <code>-7 div 2</code> is <code>-3</code>, <code>-7 mod 2</code> is <code>-1</code>. Dividing by zero, and dividing the smallest value of a signed type by <code>-1</code>, stop the program (section 10), whatever the type and the target.</p> |
| | @@ -314,7 +332,7 @@ |
| 314 | 332 | </ul> |
| 315 | 333 | <h2 id="arrays-and-slices">6.5 Arrays and slices</h2> |
| 316 | 334 | <p><code>a[i]</code> is an element of an array or slice, and <code>a[lo..hi]</code> is the slice of the elements from <code>lo</code> up to but not including <code>hi</code>. Both are checked: <code>i</code> must be below the length, and <code>0 <= lo <= hi <= length</code>. A check that fails prints to standard error where it is, as file, line and column, and what failed, with the values involved (<code>index 7 is out of range for a length of 5</code>), and stops the program with status 2. An index or a bound may be of any integer type, and is checked by its value, whatever its type: a negative <code>I8</code>, or a <code>U64</code> past the length, fails like any other. A module that says <code>check off;</code> (section 3) leaves these checks out of its code.</p> |
| 317 | | -<p><strong>Literals.</strong> <code>[e1, e2, ...]</code> builds an array, and <code>{f: e, g: e, ...}</code> a record, where the type it must have is known: assigned to a variable, passed to a parameter, returned, compared with a value of that type, or an element of a bigger literal. The elements must be assignable to the element type, and an array literal for <code>[n]T</code> has exactly <code>n</code> of them. A record literal names its fields, in any order, each at most once; the fields it leaves out are zero, so <code>{}</code> is the record with every field zero. An array literal used as a slice is a hidden variable of the procedure, and lives as long as its call.</p> |
| 335 | +<p><strong>Literals.</strong> <code>[e1, e2, ...]</code> builds an array, and <code>{f: e, g: e, ...}</code> a record, where the type it must have is known: assigned to a variable, passed to a parameter, returned, compared with a value of that type, or an element of a bigger literal. The elements must be assignable to the element type, and an array literal for <code>[n]T</code> has exactly <code>n</code> of them. A record literal names its fields, in any order, each at most once; the fields it leaves out are zero, so <code>{}</code> is the record with every field zero. An array literal used as a slice is a hidden variable of the procedure, and lives as long as its call; the empty one, <code>[]</code>, is the empty slice, as a slice variable starts (section 4), and no variable at all.</p> |
| 318 | 336 | <pre><code>c := {ch: 'A', attr: 7}; |
| 319 | 337 | Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.int(m)]);</code></pre> |
| 320 | 338 | <p><strong>Arrays and strings compare.</strong> Two operands that are each a <code>[]T</code> or a <code>[n]T</code>, of the same element type <code>T</code>, are <code>=</code> when they have the same length and the same elements, and <code>#</code> otherwise, where <code>T</code> is an integer type, <code>BOOL</code>, an enumeration, a set or a pointer type. A string literal compares as a <code>[]U8</code>. The comparison is code of its own where it stands, a check of the lengths and then of the bytes, not a call.</p> |
| | @@ -513,6 +531,7 @@ |
| 513 | 531 | | "match" Expr "of" { "|" Arm } "end" |
| 514 | 532 | | "return" [ Expr { "," Expr } ] |
| 515 | 533 | | "break" |
| 534 | + | "continue" |
| 516 | 535 | | "defer" Statement |
| 517 | 536 | | "try" Designator . |
| 518 | 537 | Arm = ( ident [ "(" ident { "," ident } ")" ] |
| | @@ -528,10 +547,17 @@ |
| 528 | 547 | <p><code>if</code>, <code>while</code> and <code>repeat</code> take <code>BOOL</code> expressions. <code>repeat</code> runs its body at least once and stops when the condition is true.</p> |
| 529 | 548 | <p><code>for i := a to b by s do ... end;</code> sets <code>i</code> to <code>a</code>, <code>a + s</code>, … while <code>i <= b</code> (or <code>i >= b</code> if <code>s</code> is negative). <code>i</code> is a variable of an integer or enumeration type, named by its name alone; <code>a</code> and then <code>b</code> are evaluated once, before the first round; <code>s</code> is a constant other than zero, and 1 if left out. <code>a</code> and <code>b</code> have <code>i</code>’s type, or are untyped constants that take it, or are <code>INT</code>s that widen to an <code>I64</code> <code>i</code>, as with <code>+</code> (section 6.1): so in <code>for i := 0 to LEN(s) - 1</code>, <code>i</code> is an <code>INT</code> or an <code>I64</code>.</p> |
| 530 | 549 | <p>The loop never steps past <code>b</code>, so <code>i</code> can’t wrap: afterwards <code>i</code> holds the last value it took, or <code>a</code> if the body never ran. The body may assign <code>i</code>. Precisely, for a positive <code>s</code>: at the end of a round, if <code>i <= b</code> and <code>b - i < s</code>, the loop ends with <code>i</code> as it is; otherwise <code>i</code> becomes <code>i + s</code>, wrapping, and the loop goes on while <code>i <= b</code>. A negative <code>s</code> is the same, the other way round.</p> |
| 531 | | -<p><code>break</code> ends the innermost <code>while</code>, <code>repeat</code> or <code>for</code> it stands in, and the program goes on after it. There is no <code>goto</code>.</p> |
| 550 | +<p><code>break</code> ends the innermost <code>while</code>, <code>repeat</code> or <code>for</code> it stands in, and the program goes on after it. <code>continue</code> ends the round of the innermost one, as reaching the end of its body would: a <code>while</code> tests its condition again, a <code>repeat</code> tests its <code>until</code>, and a <code>for</code> steps its variable, or ends. There is no <code>goto</code>.</p> |
| 551 | +<p>A <code>continue</code> passes over the rest of the body, the step of a <code>while</code> or a <code>repeat</code> included, if it stands there: so the step comes first.</p> |
| 552 | +<pre><code>while i < LEN(t) do |
| 553 | + ch, n := Str.decode(t[i..LEN(t)]); |
| 554 | + i := i + n; (* before any continue *) |
| 555 | + if ch = 0ADH then continue; end; (* a soft hyphen, passed over *) |
| 556 | + ... |
| 557 | +end;</code></pre> |
| 532 | 558 | <h2 id="return">7.3 Return</h2> |
| 533 | 559 | <p><code>return e;</code> ends the procedure with the result <code>e</code>; <code>return a, b;</code> with several results; <code>return f(x);</code> with the several results of <code>f</code>, if they are the procedure’s own (section 8); <code>return;</code> from a procedure without results. A procedure with results that runs off its end returns zeros, each result as a variable of its type starts (section 4).</p> |
| 534 | | -<p>A slice returned must not point into the procedure’s own frame, which is gone once it returns. It is an error to return, as a slice, a local array, a value parameter that is an array, an array literal, or an array a call returns, or a slice of one, a field or an element of one included:</p> |
| 560 | +<p>A slice returned must not point into the procedure’s own frame, which is gone once it returns. It is an error to return, as a slice, a local array, a value parameter that is an array, an array literal other than <code>[]</code>, or an array a call returns, or a slice of one, a field or an element of one included:</p> |
| 535 | 561 | <pre><code>proc short(h: Hex): []U8; (* Hex = [64]U8 *) |
| 536 | 562 | begin |
| 537 | 563 | return h[0..8]; (* an error: h is the procedure's copy *) |
| | @@ -573,7 +599,7 @@ |
| 573 | 599 | <p><code>FENCE</code> also orders the program’s ordinary accesses against those of other processors and devices.</p> |
| 574 | 600 | <p><strong>Ordinary variables.</strong> Only volatile accesses, <code>CAS</code>, <code>XCHG</code>, <code>XADD</code> and <code>FENCE</code> are exact. Any other access is ordinary: an implementation may keep a variable in a register, leave out a write that nothing reads, and change the order of ordinary accesses, as long as each thread, on its own, does what this report says. A variable that one thread writes while another reads or writes it, unless both use volatile accesses or the atomic built-ins, or a <code>FENCE</code> orders them, holds no particular value then: a read of it gives a value it had or was given, or a mixture of their bytes. Nothing else of the program becomes undefined by it.</p> |
| 575 | 601 | <h2 id="defer">7.7 Defer</h2> |
| 576 | | -<p><code>defer s;</code> runs the statement <code>s</code> when the program leaves the statement list the <code>defer</code> stands in: at its end, or by a <code>return</code>, or by a <code>break</code> out of it. Statements deferred in the same list run in the opposite order to their <code>defer</code>s, and only those whose <code>defer</code> has been reached run.</p> |
| 602 | +<p><code>defer s;</code> runs the statement <code>s</code> when the program leaves the statement list the <code>defer</code> stands in: at its end, or by a <code>return</code>, or by a <code>break</code> or <code>continue</code> out of it. Statements deferred in the same list run in the opposite order to their <code>defer</code>s, and only those whose <code>defer</code> has been reached run.</p> |
| 577 | 603 | <pre><code>module Head; |
| 578 | 604 | import Sys, Out; |
| 579 | 605 | |
| | @@ -600,7 +626,7 @@ |
| 600 | 626 | end; |
| 601 | 627 | |
| 602 | 628 | end Head.</code></pre> |
| 603 | | -<p><code>return e;</code> evaluates <code>e</code> before the deferred statements run. A deferred statement can’t contain <code>defer</code>, nor a <code>return</code>, <code>break</code> or <code>try</code> that would leave it. A failed check, <code>Rt.exit</code> and <code>Sys.exit</code> end the program without running the deferred statements still pending.</p> |
| 629 | +<p><code>return e;</code> evaluates <code>e</code> before the deferred statements run. A deferred statement can’t contain <code>defer</code>, nor a <code>return</code>, <code>break</code>, <code>continue</code> or <code>try</code> that would leave it. A failed check, <code>Rt.exit</code> and <code>Sys.exit</code> end the program without running the deferred statements still pending.</p> |
| 604 | 630 | <h2 id="try">7.8 Try</h2> |
| 605 | 631 | <p>A procedure that can fail returns an <code>ERROR</code> as its last result (section 5.8), <code>nil</code> when it didn’t. A caller that can’t deal with the failure passes it on to its own caller, which <code>try</code> does in a word:</p> |
| 606 | 632 | <pre><code>proc total(words: [][]U8): (INT, ERROR); |
| | @@ -727,7 +753,7 @@ |
| 727 | 753 | <li><strong>Messages and warnings.</strong> The text of the compiler’s messages, how many errors it reports before it stops, and which warnings it gives, <code>-Werror</code> included (section 10). That a program that is an error in this report is rejected isn’t free; the words that say so are. So is what a program that fails says on standard error: conformance is judged by a program’s standard output and exit status, never by its standard error.</li> |
| 728 | 754 | <li><strong>The command line.</strong> How the compiler is run, its options, and where its library lives (sections 1 and 3).</li> |
| 729 | 755 | </ul> |
| 730 | | -<p><strong>The core.</strong> A target may leave out what the system under it can’t give: <code>Sys</code> past writing, ending the program and getting memory, <code>Thread</code>, <code>link</code> and <code>extern</code>. What it must have, besides the whole language, reals aside, is the core of the library: <code>Rt</code>, <code>Out</code>, <code>Mem</code>, <code>Arena</code>, <code>Lock</code>, <code>Str</code> and <code>Buf</code>, and of <code>Sys</code> what they need. A program that keeps to the core runs on every target. The rest of the library (<code>Heap</code>, <code>Fmt</code>, <code>List</code>, <code>Map</code>, <code>Vec</code>, <code>Sort</code>, <code>Math</code>, <code>Files</code>, <code>Opt</code> and the rest of <code>Sys</code>) is the standard library: a target may leave a module out, and one that has it gives what <code>doc/raz-lib.md</code> says. <code>conform/</code> tests the core only.</p> |
| 756 | +<p><strong>The core.</strong> A target may leave out what the system under it can’t give: <code>Sys</code> past writing, ending the program and getting memory, <code>Thread</code>, <code>link</code> and <code>extern</code>. What it must have, besides the whole language, reals aside, is the core of the library: <code>Rt</code>, <code>Out</code>, <code>Mem</code>, <code>Arena</code>, <code>Lock</code>, <code>Str</code> and <code>Buf</code>, and of <code>Sys</code> what they need. A program that keeps to the core runs on every target. The rest of the library (<code>Heap</code>, <code>Fmt</code>, <code>List</code>, <code>Map</code>, <code>Vec</code>, <code>Sort</code>, <code>Math</code>, <code>Files</code>, <code>Opt</code>, <code>Width</code> and the rest of <code>Sys</code>) is the standard library: a target may leave a module out, and one that has it gives what <code>doc/raz-lib.md</code> says. <code>conform/</code> tests the core only.</p> |
| 731 | 757 | <h1 id="appendix-b.-the-grammar">Appendix B. The grammar</h1> |
| 732 | 758 | <p>The whole syntax of raz, gathered from the sections above, with the symbols of section 2. <code>{ x }</code> is any number of <code>x</code>, <code>[ x ]</code> is <code>x</code> or nothing, and <code>( x | y )</code> is one of them.</p> |
| 733 | 759 | <pre><code>ident = letter { letter | digit } . |
| | @@ -805,6 +831,7 @@ |
| 805 | 831 | | "match" Expr "of" { "|" Arm } "end" |
| 806 | 832 | | "return" [ Expr { "," Expr } ] |
| 807 | 833 | | "break" |
| 834 | + | "continue" |
| 808 | 835 | | "defer" Statement |
| 809 | 836 | | "try" Designator . |
| 810 | 837 | Arm = ( ident [ "(" ident { "," ident } ")" ] |
| | @@ -916,6 +943,10 @@ |
| 916 | 943 | <td><code>Opt</code></td> |
| 917 | 944 | <td>a command line read through one table, by <code>getopt</code>’s rules with long options; its help and its mistakes</td> |
| 918 | 945 | </tr> |
| 946 | +<tr> |
| 947 | +<td><code>Width</code></td> |
| 948 | +<td>how many columns of a terminal a character, or a string, takes: 0, 1 or 2, as <code>wcwidth</code> says</td> |
| 949 | +</tr> |
| 919 | 950 | </tbody> |
| 920 | 951 | </table> |
| 921 | 952 | <p>Lengths, counts, file descriptors, process ids and statuses are <code>INT</code>s, and a character is an <code>I32</code>. What may pass an <code>INT</code> on a small target is an <code>I64</code>: a file’s size and position, a time in nanoseconds, and a number read from text.</p> |
| | @@ -1368,6 +1399,11 @@ |
| 1368 | 1399 | usage: keep start [-b label] name command [argument ...]</code></pre> |
| 1369 | 1400 | <p>The sentences are <code>unknown option -x</code> (or <code>--name</code>, up to its <code>=</code>), <code>-b needs a label</code>, <code>--follow takes no value</code> and <code>--wait wants a number from 1 to 600, not '0'</code>. <code>fail</code> writes the program’s own mistakes the same way, but alone, so that the sentence can say where the help is: <code>keep: no command; see keep -h</code>.</p> |
| 1370 | 1401 | <p>A mistake in the table is the program’s, not its user’s. <code>init</code> checks the table before it reads a word, and ends the program with status 1: <code>keep: option table, row 3: the option is in the scope already</code>. The others are an option with neither a letter nor a long name, a long name with <code>=</code> in it, a letter <code>-</code>, and <code>-h</code> or <code>--help</code> in the table, since they are <code>Opt</code>’s.</p> |
| 1402 | +<p><strong>Width</strong> says how many columns of a terminal text takes, for a program with a screen that wraps its lines and cuts them at the window’s edge.</p> |
| 1403 | +<pre><code>module Width; |
| 1404 | +proc width*(c: I32): INT; (* the columns the character c takes: 0, 1 or 2 *) |
| 1405 | +proc columns*(s: []U8): INT; (* the columns the UTF-8 text s takes *)</code></pre> |
| 1406 | +<p>A combining mark, a format character, a control and Hangul’s medial and final jamo take 0 columns; an ideograph, a kana, most emoji and the rest that Unicode calls East Asian Wide or Fullwidth take 2; every other character takes 1, the ambiguous ones among them, as most terminals draw them. These are the C library’s <code>wcwidth</code>’s answers, but for a control, which is -1 there. <code>Width</code> knows characters, not clusters: an emoji joined to another by a zero width joiner, or a flag, counts as its parts. Its tables, about 4 KB, come from Unicode’s data, made again by <code>tools/Widths.raz</code> for a new version of Unicode; a program that imports <code>Width</code> and calls neither procedure carries none of them.</p> |
| 1371 | 1407 | </div> |
| 1372 | 1408 | </main> |
| 1373 | 1409 | <footer>raz is under the ISC license.</footer> |