raz / commit 16bb4f80

commit
16bb4f80958c85fa9ca7177986355250e66ac078
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
5d999d9d
site: v0.14.0

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

patch · log

14 files changed, +144 -50

site/public/_redirects (history) modified +1 -1

@@ -1 +1 @@
1-/download/latest /download/raz-0.13.0-linux-x86_64.tar.gz 302
1+/download/latest /download/raz-0.14.0-linux-x86_64.tar.gz 302

site/public/advanced/listing.html (history) modified +12 -12

@@ -258,18 +258,18 @@
258258 <ul><li><code>gdb</code> finds procedures by name and lines by number, since the executable carries them: <code>break Dot.dot</code> and <code>list</code> work, and so does stepping a line at a time.</li><li><code>RAZ_PROFILE=1</code> samples the program every millisecond and says which procedures and lines took the time.</li><li><code>objdump -d</code> disassembles what is really in the file, if you want to see the bytes behind a listing.</li></ul>
259259 <pre class="output term"><code><span class="cmd">$ raz -o Dot Dot.raz</span>
260260 <span class="cmd">$ objdump -d -M intel --no-show-raw-insn Dot | sed -n &#x27;/&lt;Dot.dot&gt;:/,/ret/p&#x27; | head -12</span>
261-0000000000403a60 &lt;Dot.dot&gt;:
262- 403a60: push rbp
263- 403a61: mov rbp,rsp
264- 403a64: sub rsp,0x40
265- 403a6b: mov QWORD PTR [rbp-0x38],rbx
266- 403a6f: xor ebx,ebx
267- 403a71: mov QWORD PTR [rbp-0x30],r12
268- 403a75: xor r12d,r12d
269- 403a78: mov QWORD PTR [rbp-0x28],r13
270- 403a7c: xor r13d,r13d
271- 403a7f: mov QWORD PTR [rbp-0x20],rdx
272- 403a83: mov QWORD PTR [rbp-0x18],rcx</code></pre>
261+0000000000403ae0 &lt;Dot.dot&gt;:
262+ 403ae0: push rbp
263+ 403ae1: mov rbp,rsp
264+ 403ae4: sub rsp,0x40
265+ 403aeb: mov QWORD PTR [rbp-0x38],rbx
266+ 403aef: xor ebx,ebx
267+ 403af1: mov QWORD PTR [rbp-0x30],r12
268+ 403af5: xor r12d,r12d
269+ 403af8: mov QWORD PTR [rbp-0x28],r13
270+ 403afc: xor r13d,r13d
271+ 403aff: mov QWORD PTR [rbp-0x20],rdx
272+ 403b03: mov QWORD PTR [rbp-0x18],rcx</code></pre>
273273 <p>The listing is what the encoder meant; <code>objdump</code> is what the processor will read. They tell the same story.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="syscalls.html">&larr; System calls</a><a href="index.html">Contents</a><a class="next" href="volatile.html">Volatile &rarr;</a></div></article></div>
274274 </main>
275275 <footer>raz is under the ISC license.</footer>

site/public/advanced/static.html (history) modified +7 -7

@@ -65,7 +65,7 @@
6565 <span class="cmd">$ ldd Bare</span>
6666 not a dynamic executable
6767 <span class="cmd">$ ls -l Bare | awk &#x27;{print $5, &quot;bytes&quot;}&#x27;</span>
68-26032 bytes</code></pre>
68+26224 bytes</code></pre>
6969 <p>It is statically linked: no interpreter, no shared libraries, no <code>libc</code>. <code>ldd</code> says so.</p>
7070 <h2>What it does when it runs</h2>
7171 <p><code>strace</code> lists every system call a program makes. This is the whole of it:</p>
@@ -97,8 +97,8 @@
9797 Cprint: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, with debug_info, not stripped
9898 <span class="cmd">$ ldd Cprint | sed &#x27;s/ (0x[0-9a-f]*)//&#x27;</span>
9999 linux-vdso.so.1
100- libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6
101- /lib64/ld-linux-x86-64.so.2</code></pre>
100+ libc.so.6 =&gt; /usr/lib/libc.so.6
101+ /lib64/ld-linux-x86-64.so.2 =&gt; /usr/lib64/ld-linux-x86-64.so.2</code></pre>
102102 <p>Both are fine. The point is that you choose, and the language says which you chose: a program uses C exactly when a module of it has an <code>extern</code> or a <code>link</code> of a library.</p>
103103 <h2>Only what is reached</h2>
104104 <p>The compiler keeps only what can be reached from <code>main</code> and the module bodies. Unused procedures, variables and strings are left out. Here are two programs that differ by one large procedure that nothing calls.</p>
@@ -134,15 +134,15 @@
134134 <pre class="output term"><code><span class="cmd">$ raz Lean.raz</span>
135135 <span class="cmd">$ raz Heavy.raz</span>
136136 <span class="cmd">$ ls -l Lean Heavy | awk &#x27;{print $9, $5}&#x27;</span>
137-Heavy 28208
138-Lean 28200</code></pre>
137+Heavy 28408
138+Lean 28392</code></pre>
139139 <p>The sizes differ by a few bytes, not by the hundreds of kilobytes that the table and the procedure would take: they are not in the executable at all. You can link in a library module without worrying what else it offers.</p>
140140 <h2>Stripping</h2>
141141 <p><code>-s</code> leaves out what is there for finding faults: the symbols and line numbers, the places a failure is told by, and the profiler. It is for the final build of a size-conscious program; the default keeps them so that gdb and failure messages work.</p>
142142 <pre class="output term"><code><span class="cmd">$ raz -s -o Lean-stripped Lean.raz</span>
143143 <span class="cmd">$ ls -l Lean Lean-stripped | awk &#x27;{print $9, $5}&#x27;</span>
144-Lean 28200
145-Lean-stripped 6688
144+Lean 28392
145+Lean-stripped 6856
146146 <span class="cmd">$ ./Lean-stripped</span>
147147 same program</code></pre>
148148 <p><code>raz -S Lean.raz</code> is the other direction: it prints the machine code the compiler made, one source line at a time. The next two parts of the tour use it.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="allocators.html">&larr; Writing an allocator</a><a href="index.html">Contents</a><a class="next" href="linkstatic.html">Threads without C, and keeping the two apart &rarr;</a></div></article></div>

site/public/advanced/targets.html (history) modified +1 -1

@@ -52,7 +52,7 @@
5252 Sys.raz
5353 Thread.linux-x86_64.raz
5454 <span class="cmd">$ cd $RAZLIB &amp;&amp; grep -c SYSCALL Sys.linux-x86_64.raz Sys.raz</span>
55-Sys.linux-x86_64.raz:89
55+Sys.linux-x86_64.raz:93
5656 Sys.raz:0</code></pre>
5757 <h2>A module of your own</h2>
5858 <p>Here is a module with two files. <code>Clock.linux-x86_64.raz</code> is for this target; <code>Clock.raz</code> is what any other target would use. Both say what they are in <code>Name</code>, and both make <code>ticks</code>.</p>

site/public/download/raz-0.14.0-linux-x86_64.tar.gz (history) added

Binary file; the patch has it.

site/public/download/raz-0.14.0-linux-x86_64.tar.gz.sha256 (history) added +1 -0

@@ -0,0 +1 @@
1+a410faef783d33ccd84f85b0788f7f06e2e9f682bd6c8945e8729fce52d8abba raz-0.14.0-linux-x86_64.tar.gz

site/public/index.html (history) modified +10 -10

@@ -42,7 +42,7 @@
4242 <div class="facts">
4343 <span><b>~10,700</b> lines of compiler</span>
4444 <span><b>~500K</b> lines compiled a second</span>
45- <span><b>1</b> report, <b>27</b> pages</span>
45+ <span><b>1</b> report, <b>28</b> pages</span>
4646 <span><b>static</b> executables, no run time to install</span>
4747 </div>
4848
@@ -109,28 +109,28 @@
109109 <p>The language is defined by <a href="spec.html">the report</a>, which is
110110 short on purpose: whatever it doesn&rsquo;t allow, raz doesn&rsquo;t have.
111111 Its second volume describes the library. Both are on one page, and each is
112-a PDF: <a href="raz.pdf">the language</a> (27 pages) and
112+a PDF: <a href="raz.pdf">the language</a> (28 pages) and
113113 <a href="raz-lib.pdf">the library</a>.</p>
114114
115115 <h2 id="download">Download</h2>
116-<p>raz 0.13.0 is a tarball for x86-64 Linux with the compiler, <code>raz</code>,
116+<p>raz 0.14.0 is a tarball for x86-64 Linux with the compiler, <code>raz</code>,
117117 its library, <code>lib/</code>, which it finds beside itself, the formatter
118118 and the documentation tool as sources in <code>tools/</code>, and the manual
119119 pages. It needs nothing else installed.</p>
120120 <div class="cta">
121- <a class="btn primary" href="download/raz-0.13.0-linux-x86_64.tar.gz">Download raz 0.13.0</a>
122- <a class="btn" href="download/raz-0.13.0-linux-x86_64.tar.gz.sha256">SHA-256</a>
121+ <a class="btn primary" href="download/raz-0.14.0-linux-x86_64.tar.gz">Download raz 0.14.0</a>
122+ <a class="btn" href="download/raz-0.14.0-linux-x86_64.tar.gz.sha256">SHA-256</a>
123123 </div>
124-<pre class="code"><code>$ tar xzf raz-0.13.0-linux-x86_64.tar.gz
125-$ ln -s $PWD/raz-0.13.0-linux-x86_64/raz ~/.local/bin/raz
124+<pre class="code"><code>$ tar xzf raz-0.14.0-linux-x86_64.tar.gz
125+$ ln -s $PWD/raz-0.14.0-linux-x86_64/raz ~/.local/bin/raz
126126 $ raz run Hello.raz</code></pre>
127127 <p>The link works since raz finds <code>lib/</code> beside its real path. The
128128 manual pages are in <code>man/</code>: <code>man ./man/raz-tutorial.7</code>.
129129 The address <code>download/latest</code> always leads to the newest release,
130-and <code>download/raz-0.13.0-linux-x86_64.tar.gz</code> to this one, for
131-good. Earlier releases: <a href="download/raz-0.12.0-linux-x86_64.tar.gz">0.12.0</a>, <a href="download/raz-0.11.1-linux-x86_64.tar.gz">0.11.1</a>, <a href="download/raz-0.11.0-linux-x86_64.tar.gz">0.11.0</a>, <a href="download/raz-0.10.1-linux-x86_64.tar.gz">0.10.1</a>, <a href="download/raz-0.10.0-linux-x86_64.tar.gz">0.10.0</a>, <a href="download/raz-0.9.1-linux-x86_64.tar.gz">0.9.1</a>, <a href="download/raz-0.9.0-linux-x86_64.tar.gz">0.9.0</a>, <a href="download/raz-0.8.0-linux-x86_64.tar.gz">0.8.0</a>, <a href="download/raz-0.7.1-linux-x86_64.tar.gz">0.7.1</a>; each has its SHA-256 beside it, with
130+and <code>download/raz-0.14.0-linux-x86_64.tar.gz</code> to this one, for
131+good. Earlier releases: <a href="download/raz-0.13.0-linux-x86_64.tar.gz">0.13.0</a>, <a href="download/raz-0.12.0-linux-x86_64.tar.gz">0.12.0</a>, <a href="download/raz-0.11.1-linux-x86_64.tar.gz">0.11.1</a>, <a href="download/raz-0.11.0-linux-x86_64.tar.gz">0.11.0</a>, <a href="download/raz-0.10.1-linux-x86_64.tar.gz">0.10.1</a>, <a href="download/raz-0.10.0-linux-x86_64.tar.gz">0.10.0</a>, <a href="download/raz-0.9.1-linux-x86_64.tar.gz">0.9.1</a>, <a href="download/raz-0.9.0-linux-x86_64.tar.gz">0.9.0</a>, <a href="download/raz-0.8.0-linux-x86_64.tar.gz">0.8.0</a>, <a href="download/raz-0.7.1-linux-x86_64.tar.gz">0.7.1</a>; each has its SHA-256 beside it, with
132132 <code>.sha256</code> added to its name.</p>
133-<p class="sha">SHA-256 <code>5b7caa843dea6cc97f9164e3dacb08e513f78604abcee2652085f7202eb3616f</code></p>
133+<p class="sha">SHA-256 <code>a410faef783d33ccd84f85b0788f7f06e2e9f682bd6c8945e8729fce52d8abba</code></p>
134134
135135 </main>
136136 <footer>raz is under the ISC license.</footer>

site/public/raz-lib.pdf (history) modified

Binary file; the patch has it.

site/public/raz.pdf (history) modified

Binary file; the patch has it.

site/public/spec.html (history) modified +47 -12

@@ -38,7 +38,7 @@
3838 <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>
3939 <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>
4040 <p>This report defines the language. It is short on purpose. Whatever it doesn’t allow, raz doesn’t have.</p>
41-<p>This is version 2026-10.25 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program.</p>
41+<p>This is version 2026-10.26 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program.</p>
4242 <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>
4343 <h1 id="a-first-look">1. A first look</h1>
4444 <pre><code>module Hello;
@@ -207,7 +207,7 @@
207207 </tr>
208208 </tbody>
209209 </table>
210-<p><code>INT</code> is the everyday integer: lengths, indexes and sizes are <code>INT</code>s. It is as big as an address, so it is the integer a pointer converts to (section 6.7), and a word the atomic built-ins work on (section 6.8). <code>INT</code> and <code>I64</code> are two types, even where they have the same size: an <code>INT</code> goes silently into an <code>I64</code> (section 5.10), but the two don’t mix in an expression (section 6.1), and an <code>I64</code> becomes an <code>INT</code> only by <code>INT(x)</code>, which may drop bits on a smaller target. A type bigger than the largest <code>INT</code> in bytes is an error.</p>
210+<p><code>INT</code> is the everyday integer: lengths, indexes and sizes are <code>INT</code>s. It is as big as an address, so it is the integer a pointer converts to (section 6.7), and a word the atomic built-ins work on (section 6.8). <code>INT</code> and <code>I64</code> are two types, even where they have the same size: an <code>INT</code> goes silently into an <code>I64</code> wherever it meets one, in an expression too (sections 5.10 and 6.1), and an <code>I64</code> becomes an <code>INT</code> only by <code>INT(x)</code>, which may drop bits on a smaller target. A type bigger than the largest <code>INT</code> in bytes is an error.</p>
211211 <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>
212212 <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>
213213 <h2 id="arrays">5.2 Arrays</h2>
@@ -266,7 +266,7 @@
266266 <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>
267267 </ul>
268268 <p>A string’s bytes are constant. A <code>[]U8</code> or <code>^U8</code> made from a string literal, or a slice of a string constant, points at them, and writing through it isn’t defined: on a target with read-only memory, it may stop the program as a bad address (section 10). A <code>[n]U8</code> made from a string is a copy, the program’s to change.</p>
269-<p>Nothing else converts silently. In particular, there is no other conversion between integer types, between integers and reals, or between <code>F32</code> and <code>F64</code>, without a written conversion (section 6.7). Results passed on whole need the same types exactly: in <code>a, b := f(x)</code> and in <code>return f(x)</code>, an <code>INT</code> result doesn’t pass up as an <code>I64</code>. Elsewhere, <code>return n, n</code> included, an <code>INT</code> widens as above; the bounds of a <code>for</code> and the step of <code>INC</code> and <code>DEC</code> take the type of the variable they work on, and an <code>INT</code> is not widened to it.</p>
269+<p>Nothing else converts silently. In particular, there is no other conversion between integer types, between integers and reals, or between <code>F32</code> and <code>F64</code>, without a written conversion (section 6.7). An <code>INT</code> widens to <code>I64</code>, and a <code>UINT</code> to <code>U64</code>, wherever its value goes where an <code>I64</code> is wanted: in <code>a, b := f(x)</code> and <code>return f(x)</code> as in any other assignment, to the bounds of a <code>for</code> and the step of <code>INC</code> and <code>DEC</code> whose variable is an <code>I64</code>, and in an expression, where it meets an <code>I64</code> (section 6.1). The types themselves stay two: a <code>^INT</code> isn’t a <code>^I64</code>, an <code>INT</code> variable isn’t passed to a <code>var</code> parameter of type <code>I64</code>, and a procedure returning an <code>INT</code> isn’t of a procedure type returning an <code>I64</code>.</p>
270270 <h1 id="expressions">6. Expressions</h1>
271271 <pre><code>Expr = Conjunct { &quot;or&quot; Conjunct } .
272272 Conjunct = Relation { &quot;and&quot; Relation } .
@@ -285,12 +285,12 @@
285285 Element = Expr | ident &quot;:&quot; Expr .
286286 Designator = QualIdent { Selector } .
287287 Selector = &quot;.&quot; ident | &quot;^&quot; | &quot;[&quot; Expr [ &quot;..&quot; Expr ] &quot;]&quot;
288- | &quot;(&quot; [ Expr { &quot;,&quot; Expr } ] &quot;)&quot; .</code></pre>
288+ | &quot;(&quot; [ Expr { &quot;,&quot; Expr } ] &quot;)&quot; | &quot;.&quot; &quot;(&quot; Type &quot;)&quot; .</code></pre>
289289 <p>The operators bind in the order of the grammar: <code>not</code>, <code>~</code> and <code>try</code> (section 7.8) most tightly, then the multiplying operators, then the adding ones, then the relations, then <code>and</code>, then <code>or</code>. So <code>a + b * c = d</code> is <code>(a + (b * c)) = d</code>, <code>x &amp; 0FH = 0</code> is <code>(x &amp; 0FH) = 0</code>, and <code>i &lt; n and a[i] # 0 or done</code> is <code>((i &lt; n) and (a[i] # 0)) or done</code>. A unary minus applies to the first term: <code>-a * b</code> is <code>-(a * b)</code>.</p>
290290 <p><code>and</code>, <code>or</code> and <code>not</code> are for <code>BOOL</code>s only; <code>&amp;</code>, <code>|</code>, <code>xor</code> and <code>~</code> are for integers only.</p>
291-<p>A pointer to a record isn’t the record: its fields are <code>p^.f</code>, never <code>p.f</code>, and the elements of a pointer to an array are <code>p^[i]</code>. A statement starts with a name or <code>try</code>, so <code>^T(x)^ := v</code> isn’t one.</p>
291+<p>A pointer to a record isn’t the record: its fields are <code>p^.f</code>, never <code>p.f</code>, and the elements of a pointer to an array are <code>p^[i]</code>. A statement starts with a name or <code>try</code>, so <code>^T(x)^ := v</code> isn’t one; a view, <code>mem[a].(I64) := v</code> (section 6.9), says it without a pointer.</p>
292292 <h2 id="integers">6.1 Integers</h2>
293-<p>Both operands of <code>+ - * div mod &amp; | xor</code>, and of a relation, have the same integer type, or one is an untyped constant that takes the other’s type, which it must fit. So do <code>v</code> and <code>k</code> in <code>INC(v, k)</code> and <code>DEC(v, k)</code>. The result has that type and wraps around at its width: in <code>U8</code>, <code>200 + 100</code> is <code>44</code>. A unary minus wraps too: for <code>x</code> of an unsigned type, <code>-x</code> is <code>0 - x</code>. Two integer types are never mixed, whatever their sizes: with <code>i: I64</code>, <code>i &lt; LEN(s)</code> is an error, and <code>i</code> should be an <code>INT</code>.</p>
293+<p>Both operands of <code>+ - * div mod &amp; | 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 &lt; 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>
294294 <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>
295295 <p><code>SIZE(T)</code>, and <code>LEN</code> of an array or a string constant, are untyped constants, like a literal.</p>
296296 <p><code>div</code> rounds toward zero and <code>mod</code> takes the sign of the dividend, as in C: <code>-7 div 2</code> is <code>-3</code>, <code>-7 mod 2</code> is <code>-1</code>. Dividing by zero, and dividing the smallest value of a signed type by <code>-1</code>, stop the program (section 10), whatever the type and the target.</p>
@@ -468,7 +468,35 @@
468468 <p><code>NEW(p, a)</code> calls <code>a^.alloc(a, SIZE(T), A)</code>, where <code>A</code> is the alignment of <code>T</code>, and zeroes the bytes it gets if they aren’t <code>nil</code>. <code>Mem.Allocator</code> (the library, <code>doc/raz-lib.md</code>) is the one type of the library the language names.</p>
469469 <p><code>COPY</code> is a statement, as <code>INC</code> is. It copies the bytes of <code>LEN(s)</code> elements as one block, as an assignment of an array does, and is right when <code>d</code> and <code>s</code> overlap, as in <code>COPY(a[1..n], a[0..n - 1])</code>. It is checked as a slice is: past <code>LEN(d)</code>, it stops the program, unless the module says <code>check off;</code>.</p>
470470 <p><code>INC</code> and <code>DEC</code> evaluate <code>v</code> once, so <code>INC(a[f(i)])</code> calls <code>f</code> once. They read <code>v</code> and then write it: two accesses, even through a <code>^VOLATILE</code> pointer, and not atomic; <code>XADD</code> is the atomic one.</p>
471-<p>For <code>CAS</code>, <code>XCHG</code> and <code>XADD</code>, <code>p</code> is a <code>^T</code> or <code>^VOLATILE T</code> where <code>T</code> is <code>INT</code>, <code>UINT</code> or a pointer type (<code>INT</code> or <code>UINT</code> for <code>XADD</code>), on every target. They are sequentially consistent, on every target; how a machine without atomic instructions makes them so is the implementation’s choice (appendix A).</p>
471+<p>For <code>CAS</code>, <code>XCHG</code> and <code>XADD</code>, <code>p</code> is a <code>^T</code> or <code>^VOLATILE T</code> where <code>T</code> is <code>INT</code>, <code>UINT</code> or a pointer type (<code>INT</code> or <code>UINT</code> for <code>XADD</code>), on every target. They are sequentially consistent, on every target; how a machine without atomic instructions makes them so is the implementation’s choice (appendix A). ### 6.9 Views</p>
472+<p>A view reads and writes the bytes of a variable as a value of another type. <code>v.(T)</code>, with <code>v</code> a variable, is the <code>T</code> whose bytes start where <code>v</code> starts:</p>
473+<pre><code>var mem: [4096]U8; n: I64; x: F64; bits: U64;
474+
475+mem[16].(I64) := -1; (* the 8 bytes mem[16] to mem[23] now hold -1 *)
476+n := mem[16].(I64); (* and are read back as an I64: n is -1 *)
477+bits := x.(U64); (* the 8 bytes of x, as a U64 *)</code></pre>
478+<p>A view is a variable, not a value: <code>mem[16].(I64)</code> is made of the 8 bytes <code>mem[16]</code> to <code>mem[23]</code>, as <code>p^</code> is the variable <code>p</code> points at. Reading it reads those bytes; assigning it writes them, and so changes <code>mem</code>. Nothing is copied to make it. It stands wherever a variable may: on the left of <code>:=</code>, as the variable of <code>INC</code>, and with selectors after it:</p>
479+<pre><code>type Header = record magic: U32; count: U32; end;
480+var buf: [512]U8; k: U32;
481+
482+k := buf[0].(Header).count; (* bytes 4 to 7 of buf, as a U32 *)
483+buf[0].(Header).count := 0;
484+INC(mem[a].(I64), 8);</code></pre>
485+<p><strong>A view isn’t a conversion.</strong> A conversion works on a value, a view on bytes. With <code>x: F64</code> holding 1.5, <code>I64(x)</code> is 1, the number rounded toward zero, while <code>x.(I64)</code> is 3FF8000000000000H, the bits that hold 1.5. With <code>n: I64</code> holding 258, <code>U8(n)</code> is 2, the number cut to its low byte, while <code>n.(U8)</code> is the first byte of <code>n</code> in memory, which is the low one only on a target that puts its low byte first (appendix A). When the program means a number, it wants a conversion; when it means the bytes as they lie, a view.</p>
486+<p><strong>The types.</strong> <code>T</code> is an integer type, a real type, a pointer type, or a record or array made of these alone, at any depth: the types for which any bytes at all are a value. <code>BOOL</code>, an enumeration, a set, a slice, a variant, a procedure type and <code>ERROR</code> are left out, since bytes that are none of their values would make the program mean nothing. To read one of them from bytes, view the integer and convert it: <code>Colour(mem[a])</code>. <code>v</code> may be of any type, and is a variable: a name of one, a field or element of one, or what a pointer points at; not a constant, nor the result of a call or of an operator.</p>
487+<p><strong>Where its bytes lie.</strong> The <code>T</code> lies within the variable <code>v</code> is part of:</p>
488+<ul>
489+<li>for an element of an array or a slice, <code>a[i].(T)</code>, from that element to the end of <code>a</code>: <code>mem[4088].(I64)</code> is the last 8 bytes of <code>mem</code>, and <code>mem[4090].(I64)</code> would run 2 bytes past its end;</li>
490+<li>for anything else, within <code>v</code> itself: <code>x.(U64)</code> is allowed only if <code>x</code> has at least 8 bytes.</li>
491+</ul>
492+<p>This is checked, as an index is: by the compiler when it can tell, otherwise while the program runs, where a view that runs past the end is a failed check (section 6.5), unless the module says <code>check off;</code>. An element reached through a pointer, <code>p[i].(T)</code>, is checked no more than <code>p[i]</code> is, since a pointer has no length.</p>
493+<p><strong>Any address.</strong> The <code>T</code> may start at any byte: <code>mem[3].(I64)</code> is an <code>I64</code> whose first byte is <code>mem[3]</code>. As a field of a packed record is (section 5.5), it is read and written on every target, in parts where the machine needs that; and, as such a field, a view has no address: <code>ADR</code> of it, a <code>var</code> argument of it and a slice of it are errors. Copy it out to a variable, and back in. A view of a volatile variable is an error too, since its access can’t be promised as one (section 7.6): a device’s registers are reached through a <code>^VOLATILE</code> pointer to a record of them.</p>
494+<p><strong>Byte order.</strong> The bytes are in the target’s order (appendix A). A file or a message whose format fixes an order of its own is read with <code>Str.le</code>, <code>Str.be</code> and the like (section 5.5), if the program is to read it right on every target.</p>
495+<p><strong>Before views.</strong> A program used to make a pointer for each access, and may still: <code>mem[a].(I64) := n</code> does what</p>
496+<pre><code>p := ^I64(ADR(mem[a]));
497+p^ := n;</code></pre>
498+<p>does, but with <code>a</code> checked against the length of <code>mem</code>, no pointer variable, and no address of a word that may be misaligned.</p>
499+<p><code>v</code> is evaluated once, and its check made, before the bytes are read or written.</p>
472500 <h1 id="statements">7. Statements</h1>
473501 <pre><code>Statements = { Statement &quot;;&quot; } .
474502 Statement = Designator [ &quot;:=&quot; Expr ]
@@ -495,7 +523,7 @@
495523 <p>The blank, <code>_</code>, drops what would be assigned to it: <code>_ := e;</code> evaluates <code>e</code> and drops it, and <code>n, _ := f(x);</code> keeps the first result and drops the second. What is dropped is still evaluated as any expression is: its calls are made, and a volatile variable in it is read (section 7.6). A result dropped by a call standing on its own is a lint an implementation may warn of (section 10); one dropped through <code>_</code> says it is meant.</p>
496524 <h2 id="conditions-and-loops">7.2 Conditions and loops</h2>
497525 <p><code>if</code>, <code>while</code> and <code>repeat</code> take <code>BOOL</code> expressions. <code>repeat</code> runs its body at least once and stops when the condition is true.</p>
498-<p><code>for i := a to b by s do ... end;</code> sets <code>i</code> to <code>a</code>, <code>a + s</code>, … while <code>i &lt;= b</code> (or <code>i &gt;= b</code> if <code>s</code> is negative). <code>i</code> is a variable of an integer or enumeration type, named by its name alone; <code>a</code> and then <code>b</code> are evaluated once, before the first round; <code>s</code> is a constant other than zero, and 1 if left out. <code>a</code> and <code>b</code> have <code>i</code>’s type, or are untyped constants that take it, as with <code>+</code> (section 6.1): so in <code>for i := 0 to LEN(s) - 1</code>, <code>i</code> must be an <code>INT</code>.</p>
526+<p><code>for i := a to b by s do ... end;</code> sets <code>i</code> to <code>a</code>, <code>a + s</code>, … while <code>i &lt;= b</code> (or <code>i &gt;= 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>
499527 <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 &lt;= b</code> and <code>b - i &lt; 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 &lt;= b</code>. A negative <code>s</code> is the same, the other way round.</p>
500528 <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>
501529 <h2 id="return">7.3 Return</h2>
@@ -751,7 +779,7 @@
751779 Element = Expr | ident &quot;:&quot; Expr .
752780 Designator = QualIdent { Selector } .
753781 Selector = &quot;.&quot; ident | &quot;^&quot; | &quot;[&quot; Expr [ &quot;..&quot; Expr ] &quot;]&quot;
754- | &quot;(&quot; [ Expr { &quot;,&quot; Expr } ] &quot;)&quot; .
782+ | &quot;(&quot; [ Expr { &quot;,&quot; Expr } ] &quot;)&quot; | &quot;.&quot; &quot;(&quot; Type &quot;)&quot; .
755783
756784 Statements = { Statement &quot;;&quot; } .
757785 Statement = Designator [ &quot;:=&quot; Expr ]
@@ -802,7 +830,7 @@
802830 </tr>
803831 <tr>
804832 <td><code>Sys</code></td>
805-<td>system calls: <code>open</code>, <code>openRead</code>, <code>create</code>, <code>read</code>, <code>write</code>, <code>seek</code>, <code>close</code>, <code>mmap</code>, <code>munmap</code>, <code>isTerminal</code>, <code>yield</code>, <code>exit</code>; files and directories, <code>rename</code>, <code>remove</code>, <code>removeDir</code>, <code>makeDir</code>, <code>chmod</code>, <code>symlink</code>, <code>readLink</code>, <code>info</code>, <code>openDir</code>, <code>nextEntry</code>, <code>closeDir</code>; the terminal, <code>rawMode</code>, <code>restoreMode</code>, <code>termSize</code>, <code>ready</code>; the environment, <code>env</code>, <code>workDir</code>, <code>setWorkDir</code>; programs, pipes and signals; time, <code>clock</code>, <code>now</code>, <code>sleep</code>; <code>random</code>; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and <code>Sys.raz</code> over the C library for the others</td>
833+<td>system calls: <code>open</code>, <code>openRead</code>, <code>create</code>, <code>read</code>, <code>write</code>, <code>seek</code>, <code>position</code>, <code>truncate</code>, <code>sync</code>, <code>close</code>, <code>mmap</code>, <code>munmap</code>, <code>isTerminal</code>, <code>yield</code>, <code>exit</code>; files and directories, <code>rename</code>, <code>remove</code>, <code>removeDir</code>, <code>makeDir</code>, <code>chmod</code>, <code>symlink</code>, <code>readLink</code>, <code>info</code>, <code>infoOf</code>, <code>openDir</code>, <code>nextEntry</code>, <code>closeDir</code>; the terminal, <code>rawMode</code>, <code>restoreMode</code>, <code>termSize</code>, <code>ready</code>; the environment, <code>env</code>, <code>workDir</code>, <code>setWorkDir</code>; programs, pipes and signals; time, <code>clock</code>, <code>now</code>, <code>sleep</code>; <code>random</code>; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and <code>Sys.raz</code> over the C library for the others</td>
806834 </tr>
807835 <tr>
808836 <td><code>Out</code></td>
@@ -985,6 +1013,9 @@
9851013 proc openRead*(path: []U8): INT; (* open(path, {reading}) *)
9861014 proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *)
9871015 proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; (* the new position *)
1016+proc position*(fd: INT): I64; (* where the next read or write is *)
1017+proc truncate*(fd: INT; size: I64): INT; (* the file made size bytes long *)
1018+proc sync*(fd: INT): INT; (* what was written to it, on the disk *)
9881019 proc close*(fd: INT);
9891020 proc isTerminal*(fd: INT): BOOL;
9901021 proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *)
@@ -1000,6 +1031,7 @@
10001031 proc symlink*(target, path: []U8): INT; (* path made a link to target *)
10011032 proc readLink*(path: []U8): ([]U8, INT); (* what the link names, good until the next call *)
10021033 proc info*(path: []U8): (Info, INT); (* following a symbolic link *)
1034+proc infoOf*(fd: INT): (Info, INT); (* the open file&#39;s *)
10031035 proc space*(path: []U8): (I64, I64, INT); (* the bytes free and in all on path&#39;s file system *)
10041036
10051037 proc openDir*(d: ^Dir; path: []U8): INT;
@@ -1057,7 +1089,7 @@
10571089 proc mapFile*(fd, size: INT): ^U8; (* the first size bytes of fd, shared; munmap gives them back; or nil *)</code></pre>
10581090 <p><code>info</code> follows a symbolic link: <code>size</code>, <code>dir</code> and <code>file</code> are those of what it names, and <code>symlink</code> says the path was a link, so that a program walking a tree can choose not to follow one back up it. A link to nothing is <code>-ENOENT</code>, as a missing file is. <code>file</code> lets a walk open only what holds data: opening a pipe would wait for a program to write to it.</p>
10591091 <p><code>nextEntry</code> gives every name in the directory except <code>.</code> and <code>..</code>, in no particular order. <code>rawMode</code> turns off what the terminal does with keys before the program reads them (echo, line editing, signals, flow control), and NL to CR NL on output. A program that turns it on restores it before it ends: <code>Rt.atExit</code> is the place.</p>
1060-<p><strong>Files.</strong> <code>open</code> gives a file for what <code>how</code> asks: <code>reading</code>, <code>writing</code> or both; <code>creating</code> it if it is missing, readable by all and writable by its owner; with <code>exclusive</code>, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; <code>truncating</code> it to nothing; <code>appending</code>, each write going to its end, however other programs write it too. <code>seek</code> moves to <code>pos</code> from the start of the file, or from its end if <code>fromEnd</code>, where a negative <code>pos</code> reads back from the end. <code>Info.mtime</code> is what a program checks to see whether a file changed under it; <code>Info.mode</code> holds the nine bits of <code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for a program, 1A4H (644) for data.</p>
1092+<p><strong>Files.</strong> <code>open</code> gives a file for what <code>how</code> asks: <code>reading</code>, <code>writing</code> or both; <code>creating</code> it if it is missing, readable by all and writable by its owner; with <code>exclusive</code>, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; <code>truncating</code> it to nothing; <code>appending</code>, each write going to its end, however other programs write it too. <code>seek</code> moves to <code>pos</code> from the start of the file, or from its end if <code>fromEnd</code>, where a negative <code>pos</code> reads back from the end; <code>position</code> says where the file is, without moving. <code>truncate</code> makes the file <code>size</code> bytes long, cutting it or adding zeros, and leaves the position where it was. <code>sync</code> returns once what was written to the file is on the disk, as a program needs before it says a file is saved. <code>infoOf</code> is <code>info</code> of an open file: its size is what it holds now, however it was written. <code>Info.mtime</code> is what a program checks to see whether a file changed under it; <code>Info.mode</code> holds the nine bits of <code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for a program, 1A4H (644) for data.</p>
10611093 <p><strong>Programs.</strong> <code>openPty</code> makes a pseudo-terminal, a terminal that a program, a terminal emulator, plays the screen and the keyboard of: what it writes to the fd is typed, and what the programs on the terminal write it reads.</p>
10621094 <p><code>spawn</code> starts the program at <code>p.path</code>, with <code>p.args</code> as its arguments, the first being its name. Its environment is this program’s with each <code>NAME=value</code> of <code>p.env</code> added or put in place of the old value, or, if <code>p.onlyEnv</code>, <code>p.env</code> alone. It starts in <code>p.dir</code>. Its standard input, output and error are <code>p.fds</code>; a 0 gives it this program’s own, so <code>{path: "/bin/ls", args: ["ls"]}</code> runs <code>ls</code> as a shell runs it, with every other field zero. It gets no other open file, every signal does what it does at first, and none is blocked. If <code>p.tty</code> is a pseudo-terminal’s fd, the program starts a session on that terminal: it is its controlling terminal, and its standard input, output and error, and <code>p.fds</code> and <code>p.group</code> are not used.</p>
10631095 <p><code>p.group</code> puts the program in a process group, the programs that a shell’s job is made of: -1 makes it the first of a new group, whose number is its process id, and the other programs of the job join that number. The terminal gives its keys, and the signals of Ctrl-C and Ctrl-Z, to one group, the one in its foreground: <code>p.foreground</code> puts the program’s group there before it starts, and <code>foreground</code> puts any group there, as a shell does to take the terminal back. <code>setGroup</code> moves a program to a group, as the shell does for each program it starts, for whichever of the two runs first; a shell runs in a group of its own, <code>setGroup(0, 0)</code>.</p>
@@ -1220,6 +1252,7 @@
12201252 proc init*(p: ^Parser; name: []U8; table: []Row); (* the arguments from 1 on, by table *)
12211253 proc next*(p: ^Parser): BOOL; (* the next option; false when they end *)
12221254 proc operand*(p: ^Parser): ([]U8, BOOL); (* the next operand; false when there are no more *)
1255+proc at*(p: ^Parser): INT; (* the argument operand would give next *)
12231256 proc scope*(p: ^Parser; name: []U8); (* subcommand name&#39;s options, from the word after it *)
12241257 proc number*(p: ^Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *)
12251258 proc fail*(p: ^Parser; text: []U8); (* the program&#39;s own mistake *)</code></pre>
@@ -1253,7 +1286,9 @@
12531286 <li><code>--</code> ends the options, and isn’t an operand; <code>-</code> alone is one. The options end at the first operand, so in <code>keep start site python -m http.server</code> the <code>-m</code> is python’s.</li>
12541287 <li>Each scope has its own options. An option given twice is read twice.</li>
12551288 </ul>
1256-<p><code>number</code> reads the value as <code>Str.toInt</code> does. <code>operand</code> reads what options are left first, and the program doesn’t see them.</p>
1289+<p><code>number</code> reads the value as <code>Str.toInt</code> does. <code>operand</code> reads what options are left first, and the program doesn’t see them. So does <code>at</code>, which says where the operands are: the arguments from <code>operand</code>’s next on are <code>Files.arg(i)</code> for <code>i</code> from <code>at(p)</code> to <code>ARGC() - 1</code>, so that an interpreter hands a script its arguments as they are:</p>
1290+<pre><code>script, ok := Opt.operand(ADR(p)); (* tery file arg ... *)
1291+first := Opt.at(ADR(p)); (* file&#39;s own arguments, from here on *)</code></pre>
12571292 <p><code>-h</code> or <code>--help</code> among a scope’s options prints its help on standard output and ends the program with status 0. <code>init</code> and <code>scope</code> answer it, looking through the scope’s options before <code>next</code> gives one, so that the program has done nothing yet: <code>keep -v -h -x</code> prints the help, and the program never sees <code>-v</code>, nor the mistake. The help is the scope’s synopsis lines, a blank line, and its subcommands, its options and the help itself, each with its line of help:</p>
12581293 <pre><code>usage: keep start [-b label] name command [argument ...]
12591294

site/public/tour/hello.html (history) modified +1 -1

@@ -70,7 +70,7 @@
7070 <span class="cmd">$ raz run Quiet.raz</span>
7171 hello, again
7272 <span class="cmd">$ ls -l Quiet | awk &#x27;{print $5, &quot;bytes&quot;}&#x27;</span>
73-28208 bytes</code></pre>
73+28400 bytes</code></pre>
7474 <p>The program is a static executable with no run time besides the code it uses, so it is a few kilobytes, and runs without any library installed.</p>
7575 <h2>What <code>Out</code> can write</h2>
7676 <p><code>Out</code> is the library's buffered output. It writes strings, single characters, numbers in decimal or hexadecimal, and a line feed. A string is written as it is, with <code>\n</code> for a line feed, <code>\t</code> for a tab, <code>\"</code> and <code>\\</code>; or between backquotes, as a raw string with no escapes at all, which may run over lines (lesson 6).</p>

site/public/tour/integers.html (history) modified +1 -1

@@ -70,7 +70,7 @@
7070 </code></pre>
7171 <h2>INT is the everyday one</h2>
7272 <p>Lengths, indexes, counts and sizes are <code>INT</code>s. <code>LEN(s)</code> of a slice is an <code>INT</code>, and so is the loop variable that walks it. It is as big as an address, so it is the integer a pointer converts to, and the word the atomics work on. Reach for <code>INT</code> unless you have a reason: a byte is a <code>U8</code>, a character an <code>I32</code>, a file's size an <code>I64</code>, a hash a <code>U64</code>.</p>
73-<p><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> (and a <code>UINT</code> into a <code>U64</code>), which is why <code>Out.int(n)</code> and <code>Fmt.int(n)</code> take an <code>I64</code> and accept any <code>INT</code>. Nothing else converts unasked: any other integer is converted where it is written, as <code>Out.int(I64(b))</code>.</p>
73+<p><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> (and a <code>UINT</code> into a <code>U64</code>) wherever it meets one, in <code>pos + LEN(s)</code> as in a call, which is why <code>Out.int(n)</code> and <code>Fmt.int(n)</code> take an <code>I64</code> and accept any <code>INT</code>. The other way is written, <code>INT(pos)</code>, since an <code>INT</code> may be smaller on another machine. Nothing else converts unasked: any other integer is converted where it is written, as <code>Out.int(I64(b))</code>.</p>
7474 <h2>A number written in the program</h2>
7575 <p>A number in the program text is untyped until it meets a type, and then it must fit. <code>0FFH</code> is hexadecimal (it starts with a digit and ends in <code>H</code>), a character in quotes is its code point, and <code>'\u{E9}'</code> is a code point written by number. A hexadecimal literal too big for an <code>I64</code> is a <code>U64</code>.</p>
7676 <div class="file"><span>Literals.raz</span></div><pre class="code"><code><span class="kw">module</span> Literals;

site/public/tour/records.html (history) modified +58 -0

@@ -160,6 +160,64 @@
160160 [5]I16: 10
161161 </code></pre>
162162 <p>A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with <code>ADR</code>.</p>
163+<h2>Bytes as numbers and records: views</h2>
164+<p>A file, a socket or a device hands a program bytes. A view reads and writes them as another type: <code>msg[3].(U32)</code> is the <code>U32</code> whose four bytes start at <code>msg[3]</code>, and <code>msg[0].(Head)</code> the <code>Head</code> whose bytes start at <code>msg[0]</code>.</p>
165+<div class="file"><span>Wire.raz</span></div><pre class="code"><code><span class="kw">module</span> Wire;
166+<span class="kw">import</span> Out;
167+
168+<span class="kw">type</span> Head = <span class="kw">packed</span> <span class="kw">record</span> magic: <span class="ty">U16</span>; kind: <span class="ty">U8</span>; len: <span class="ty">U32</span>; <span class="kw">end</span>; <span class="comment">(* 7 bytes, as sent *)</span>
169+
170+<span class="kw">var</span> msg: [<span class="number">32</span>]<span class="ty">U8</span>;
171+
172+<span class="kw">proc</span> main*;
173+<span class="kw">var</span> h: Head; i: <span class="ty">INT</span>; sum: <span class="ty">I64</span>;
174+<span class="kw">begin</span>
175+ <span class="comment">(* what a socket might have handed us *)</span>
176+ msg[<span class="number">0</span>] := <span class="number">52H</span>; msg[<span class="number">1</span>] := <span class="number">5AH</span>;
177+ msg[<span class="number">2</span>] := <span class="number">1</span>;
178+ msg[<span class="number">3</span>].(<span class="ty">U32</span>) := <span class="number">8</span>;
179+ msg[<span class="number">7</span>].(<span class="ty">I32</span>) := <span class="number">100</span>;
180+ msg[<span class="number">11</span>].(<span class="ty">I32</span>) := <span class="number">200</span>;
181+
182+ h := msg[<span class="number">0</span>].(Head);
183+ Out.hex(<span class="ty">U64</span>(h.magic), <span class="number">4</span>); Out.char(<span class="string">&#x27; &#x27;</span>);
184+ Out.int(<span class="ty">I64</span>(h.kind)); Out.char(<span class="string">&#x27; &#x27;</span>);
185+ Out.int(<span class="ty">I64</span>(h.len)); Out.ln;
186+ sum := <span class="number">0</span>;
187+ i := <span class="number">7</span>;
188+ <span class="kw">while</span> i &lt; <span class="number">7</span> + <span class="ty">INT</span>(h.len) <span class="kw">do</span>
189+ sum := sum + <span class="ty">I64</span>(msg[i].(<span class="ty">I32</span>));
190+ i := i + <span class="number">4</span>;
191+ <span class="kw">end</span>;
192+ Out.int(sum); Out.ln;
193+<span class="kw">end</span>;
194+
195+<span class="kw">end</span> Wire.</code></pre>
196+<pre class="output"><code>5A52 1 8
197+300
198+</code></pre>
199+<p>A view isn't a copy: it *is* those bytes, so <code>msg[3].(U32) := 8</code> writes <code>msg[3]</code> to <code>msg[6]</code>, and a view goes wherever a variable does, with a field after it, in <code>INC</code>, on the left of <code>:=</code>. Nor is it a conversion: <code>I64(x)</code> is a real's number, <code>x.(U64)</code> its bits. The magic number reads <code>5A52</code>, its bytes backwards, because x86-64 keeps a number's low byte first; a format with an order of its own is read with <code>Str.le</code> or <code>Str.be</code>.</p>
200+<p>A view of an element must end inside the array, and the program checks it as it checks an index:</p>
201+<div class="file"><span>Short.raz</span></div><pre class="code"><code><span class="kw">module</span> Short;
202+<span class="kw">import</span> Out;
203+
204+<span class="kw">var</span> msg: [<span class="number">10</span>]<span class="ty">U8</span>;
205+
206+<span class="kw">proc</span> main*;
207+<span class="kw">var</span> i: <span class="ty">INT</span>;
208+<span class="kw">begin</span>
209+ i := <span class="number">0</span>;
210+ <span class="kw">while</span> i &lt; <span class="ty">LEN</span>(msg) <span class="kw">do</span>
211+ Out.int(<span class="ty">I64</span>(msg[i].(<span class="ty">U32</span>))); Out.char(<span class="string">&#x27; &#x27;</span>);
212+ i := i + <span class="number">4</span>;
213+ <span class="kw">end</span>;
214+<span class="kw">end</span>;
215+
216+<span class="kw">end</span> Short.</code></pre>
217+<pre class="output"><code>0 0 <span class="err">Short.raz:11:25: a view at index 8 runs past a length of 10
218+ in Short.main at Short.raz:11:25: i = 8
219+</span></code></pre>
220+<p>The type of a view is an integer, a real or a pointer, or a record or array made of them: types any bytes are a value of, which a <code>BOOL</code> or an enumeration isn't. A view may start at any byte, so, as a packed record's field, it has no address of its own.</p>
163221 <h2>Pointers</h2>
164222 <p><code>ADR(x)</code> takes the address of a variable, <code>^T</code> is a pointer to a <code>T</code>, and <code>p^</code> is what it points at. A pointer that points nowhere is <code>nil</code>, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory.</p>
165223 <div class="file"><span>Pointers.raz</span></div><pre class="code"><code><span class="kw">module</span> Pointers;

site/public/tour/values.html (history) modified +5 -5

@@ -64,13 +64,13 @@
6464 <pre class="output"><code>300 4 300 42.857 233 true
6565 </code></pre>
6666 <h2>Conversions are written</h2>
67-<p>Two types never mix: with <code>i: I64</code> and a slice <code>s</code>, <code>i &lt; LEN(s)</code> is an error. A type's name converts: <code>F64(n)</code>, <code>I64(b)</code>, <code>U8(x)</code>. The one silent conversion between numbers is an <code>INT</code> into an <code>I64</code>, which loses nothing. The <a href="integers.html">lesson on integers</a> says what each width conversion does.</p>
67+<p>Two types never mix but for one: an <code>INT</code> goes into an <code>I64</code> wherever it meets one, which loses nothing, so with <code>pos: I64</code>, <code>pos + LEN(s)</code> is an <code>I64</code>. With <code>i: I32</code> and a slice <code>s</code>, <code>i &lt; LEN(s)</code> is an error. A type's name converts: <code>F64(n)</code>, <code>I64(b)</code>, <code>U8(x)</code>; an <code>I64</code> becomes an <code>INT</code> only so, <code>INT(pos)</code>, since an <code>INT</code> may be smaller on another machine. The <a href="integers.html">lesson on integers</a> says what each width conversion does.</p>
6868 <p>A number written in the program is untyped until it meets a type, and <code>1.5</code> is a real, which an integer never becomes without a conversion.</p>
6969 <div class="file"><span>Mix.raz</span></div><pre class="code"><code><span class="kw">module</span> Mix;
7070 <span class="kw">import</span> Out;
7171
7272 <span class="kw">proc</span> main*;
73-<span class="kw">var</span> i: <span class="ty">I64</span>; s: []<span class="ty">U8</span>;
73+<span class="kw">var</span> i: <span class="ty">I32</span>; s: []<span class="ty">U8</span>;
7474 <span class="kw">begin</span>
7575 s := <span class="string">&quot;four&quot;</span>;
7676 i := <span class="number">3</span>;
@@ -79,12 +79,12 @@
7979
8080 <span class="kw">end</span> Mix.</code></pre>
8181 <pre class="output term"><code><span class="cmd">$ raz Mix.raz</span>
82-Mix.raz:9:8: error: I64 and INT are different types
82+Mix.raz:9:8: error: I32 and INT are different types
8383 9 | if i &lt; LEN(s) then Out.str(&quot;smaller\n&quot;); end;
8484 | ^^^^^^^^^^
85-help: convert one to the other&#x27;s type, as in I64(LEN(s))
85+help: convert one to the other&#x27;s type, as in INT(i)
8686 raz: 1 error</code></pre>
87-<p>The fix is the conversion, written where it happens: <code>if i &lt; I64(LEN(s))</code>. It costs nothing at run time and says what is meant.</p>
87+<p>The fix is the conversion, written where it happens: <code>if INT(i) &lt; LEN(s)</code>. It costs nothing at run time and says what is meant.</p>
8888 <h2>What converts to what</h2>
8989 <p>Everything else that converts does so with the type's name, and the rules are short:</p>
9090 <table><thead><tr><th>From</th><th>To</th><th>Result</th></tr></thead><tbody><tr><td data-label="From">integer</td><td data-label="To">integer</td><td data-label="Result">cut to the new width, or extended by the sign (signed) or zeros (unsigned)</td></tr><tr><td data-label="From">integer</td><td data-label="To"><code>F32</code>, <code>F64</code></td><td data-label="Result">the nearest real</td></tr><tr><td data-label="From"><code>F32</code>, <code>F64</code></td><td data-label="To">integer</td><td data-label="Result">rounded toward zero</td></tr><tr><td data-label="From"><code>F32</code></td><td data-label="To"><code>F64</code></td><td data-label="Result">the same value; the other way, the nearest <code>F32</code></td></tr><tr><td data-label="From">enumeration</td><td data-label="To">integer</td><td data-label="Result">its position; and back, if there is a name at that position</td></tr><tr><td data-label="From">set</td><td data-label="To">integer</td><td data-label="Result">its bits; and back</td></tr><tr><td data-label="From"><code>INT</code>, <code>UINT</code></td><td data-label="To">pointer</td><td data-label="Result">the address; and back</td></tr></tbody></table>