raz / commit 668e440b

commit
668e440b017ffc66ca9a10c3b67d557c16e6450d
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
fbed5975
site: v0.13.0

patch · log

20 files changed, +412 -181

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

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

site/public/advanced/bytes.html (history) modified +19 -11

@@ -201,37 +201,45 @@
201201 </code></pre>
202202 <p>The output is the same on a little-endian and a big-endian machine, because the bytes are written one at a time in the order the format says.</p>
203203 <h2>Numbers and text</h2>
204-<p><code>Str.toInt</code> reads a decimal number with an optional sign and nothing else, and says whether it was one. <code>Fmt</code> goes the other way. <code>Str.encode</code> and <code>Str.decode</code> move between characters and UTF-8, as the first tour showed.</p>
204+<p><code>Str.toInt</code> reads a decimal number with an optional sign and nothing else, and says whether it was one; <code>Str.toHex</code> reads hexadecimal digits, and <code>Math.toReal</code> a real, as a constant is written but with <code>e</code> for <code>E</code> too, to the nearest <code>F64</code> exactly. <code>Str.fromInt</code> goes the other way into an array of 20 bytes, with no allocator, and <code>Fmt</code> does the rest. <code>Str.encode</code> and <code>Str.decode</code> move between characters and UTF-8, as the first tour showed.</p>
205205 <div class="file"><span>Numbers.raz</span></div><pre class="code"><code><span class="kw">module</span> Numbers;
206-<span class="kw">import</span> Out, Str;
206+<span class="kw">import</span> Out, Str, Math, Fmt;
207207
208208 <span class="comment">(* the sum of the numbers in a comma separated list, or -1 if any isn&#x27;t *)</span>
209209 <span class="kw">proc</span> total(s: []<span class="ty">U8</span>): <span class="ty">I64</span>;
210-<span class="kw">var</span> start, i: <span class="ty">INT</span>; sum, n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>;
210+<span class="kw">var</span> part: []<span class="ty">U8</span>; sum, n: <span class="ty">I64</span>; ok, more: <span class="ty">BOOL</span>;
211211 <span class="kw">begin</span>
212- start := <span class="number">0</span>;
213- <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(s) <span class="kw">do</span>
214- <span class="kw">if</span> (i = <span class="ty">LEN</span>(s)) <span class="kw">or</span> (s[i] = <span class="string">&#x27;,&#x27;</span>) <span class="kw">then</span>
215- n, ok := Str.toInt(s[start..i]);
216- <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">then</span> <span class="kw">return</span> -<span class="number">1</span>; <span class="kw">end</span>;
217- sum := sum + n;
218- start := i + <span class="number">1</span>;
219- <span class="kw">end</span>;
212+ more := <span class="ty">true</span>;
213+ <span class="kw">while</span> more <span class="kw">do</span>
214+ part, s, more := Str.cut(s, <span class="string">&quot;,&quot;</span>);
215+ n, ok := Str.toInt(part);
216+ <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">then</span> <span class="kw">return</span> -<span class="number">1</span>; <span class="kw">end</span>;
217+ sum := sum + n;
220218 <span class="kw">end</span>;
221219 <span class="kw">return</span> sum;
222220 <span class="kw">end</span>;
223221
224222 <span class="kw">proc</span> main*;
223+<span class="kw">var</span> u: <span class="ty">U64</span>; x: <span class="ty">F64</span>; ok: <span class="ty">BOOL</span>; digits: [<span class="number">20</span>]<span class="ty">U8</span>; k: <span class="ty">INT</span>;
225224 <span class="kw">begin</span>
226225 Out.int(total(<span class="string">&quot;10,20,-5,+7&quot;</span>)); Out.ln;
227226 Out.int(total(<span class="string">&quot;10,x,20&quot;</span>)); Out.ln;
228227 Out.int(total(<span class="string">&quot;10,,20&quot;</span>)); Out.ln;
228+ u, ok := Str.toHex(<span class="string">&quot;ff00&quot;</span>);
229+ <span class="kw">if</span> ok <span class="kw">then</span> Out.hex(u, <span class="number">1</span>); Out.ln; <span class="kw">end</span>;
230+ x, ok := Math.toReal(<span class="string">&quot;-1.5e3&quot;</span>);
231+ <span class="kw">if</span> ok <span class="kw">then</span> Fmt.print(<span class="string">&quot;{}\n&quot;</span>, [Fmt.real(x, <span class="number">1</span>)]); <span class="kw">end</span>;
232+ digits, k := Str.fromInt(-<span class="number">9223372036854775807</span> - <span class="number">1</span>);
233+ Out.str(digits[<span class="number">0</span>..k]); Out.ln;
229234 <span class="kw">end</span>;
230235
231236 <span class="kw">end</span> Numbers.</code></pre>
232237 <pre class="output"><code>32
233238 -1
234239 -1
240+FF00
241+-1500.0
242+-9223372036854775808
235243 </code></pre><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="fmt.html">&larr; Output and formatting</a><a href="index.html">Contents</a><a class="next" href="collections.html">Lists, maps and sorting &rarr;</a></div></article></div>
236244 </main>
237245 <footer>raz is under the ISC license.</footer>

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

@@ -125,18 +125,15 @@
125125
126126 <span class="kw">proc</span> main*;
127127 <span class="kw">var</span>
128- m: Count.Map; entries: [<span class="number">32</span>]Entry; i, start, k, n, found: <span class="ty">INT</span>;
129- w: []<span class="ty">U8</span>; key: []<span class="ty">U8</span>; v: <span class="ty">INT</span>;
128+ m: Count.Map; entries: [<span class="number">32</span>]Entry; i, k, n, found: <span class="ty">INT</span>;
129+ w, rest: []<span class="ty">U8</span>; key: []<span class="ty">U8</span>; v: <span class="ty">INT</span>;
130130 <span class="kw">begin</span>
131131 Count.init(<span class="ty">ADR</span>(m), Heap.heap);
132- start := <span class="number">0</span>;
133- <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(Text) <span class="kw">do</span>
134- <span class="kw">if</span> (i = <span class="ty">LEN</span>(Text)) <span class="kw">or</span> (Text[i] = <span class="string">&#x27; &#x27;</span>) <span class="kw">then</span>
135- w := Text[start..i];
136- k, _ := Count.get(<span class="ty">ADR</span>(m), w);
137- _ := Count.put(<span class="ty">ADR</span>(m), w, k + <span class="number">1</span>);
138- start := i + <span class="number">1</span>;
139- <span class="kw">end</span>;
132+ w, rest := Str.field(Text);
133+ <span class="kw">while</span> <span class="ty">LEN</span>(w) &gt; <span class="number">0</span> <span class="kw">do</span>
134+ k, _ := Count.get(<span class="ty">ADR</span>(m), w);
135+ _ := Count.put(<span class="ty">ADR</span>(m), w, k + <span class="number">1</span>);
136+ w, rest := Str.field(rest);
140137 <span class="kw">end</span>;
141138 n := <span class="number">0</span>;
142139 found, key, v := Count.next(<span class="ty">ADR</span>(m), <span class="number">0</span>);

site/public/advanced/linkstatic.html (history) modified +13 -14

@@ -49,28 +49,28 @@
4949 <h2>What <code>Thread</code> does</h2>
5050 <p><code>Thread</code> is a module of raz, in the library, that says <code>link static;</code> and makes the <code>clone</code>, <code>mmap</code> and <code>futex</code> system calls itself. Here is its beginning, printed from the library:</p>
5151 <pre class="output term"><code><span class="cmd">$ sed -n &#x27;1,22p&#x27; $RAZLIB/Thread.linux-x86_64.raz</span>
52-(* Threads for programs without C, made with the clone system call.
52+(* Threads, made with the clone system call in a program without C.
5353 Each runs on a stack of its own, mmaped, with a page below it that
5454 can&#x27;t be touched, so that running off the stack stops the program
5555 rather than spoiling what lies below; above it, a little stack for
5656 Rt&#x27;s signal handlers, so that they can say so. join waits on the
5757 thread&#x27;s id, which the kernel clears, and wakes it for, when the
58- thread ends; a Mutex sleeps on a futex. A program with C threads
59- uses C&#x27;s, through pthread_create, and so can&#x27;t use these. *)
58+ thread ends; a Mutex sleeps on a futex.
59+
60+ A program that uses C makes them with C&#x27;s pthread_create instead,
61+ since C keeps things of each thread&#x27;s own, errno and its buffers,
62+ that a thread clone makes wouldn&#x27;t have. The compiler&#x27;s entry point
63+ finds cCreate and cJoin by name and stores C&#x27;s calls in them, as it
64+ stores C&#x27;s exit in Rt.cExit; so this module uses no C itself, and
65+ serves both kinds of program. C&#x27;s thread has a stack and a guard
66+ page of C&#x27;s; the mapping here holds only the handlers&#x27; stack and
67+ the Thread. *)
6068 module Thread;
6169 import Sys, Rt;
62-link static;
6370
6471 const
6572 Failed*: ERROR = &quot;can&#x27;t start a thread&quot;;
66- Stack = 1 shl 20;
67- Alt = 16384; (* the signal handlers&#x27; stack *)
68- Page = 4096;
69- (* clone: share memory, files, signal handlers, the thread group;
70- write the id to tid, and clear it at the end *)
71- Flags = 350F00H;
72- FutexWait = 0;
73- FutexWake = 1;</code></pre>
73+ Stack = 1 shl 20;</code></pre>
7474 <p>A program that imports it is therefore a static executable, and every thread it spawns is a Linux thread with a stack of its own, mapped with a page below it that can't be touched, so that running off the stack ends the program with a message and not with corruption.</p>
7575 <div class="file"><span>Hello.raz</span></div><pre class="code"><code><span class="kw">module</span> Hello;
7676 <span class="kw">import</span> Out, Thread;
@@ -112,8 +112,7 @@
112112 <span class="kw">end</span>;
113113
114114 <span class="kw">end</span> Mixed.</code></pre>
115-<pre class="output term"><code><span class="cmd">$ raz Mixed.raz</span>
116-raz: Thread says link static, but C uses C</code></pre>
115+<pre class="output term"><code><span class="cmd">$ raz Mixed.raz</span></code></pre>
117116 <p>Which one you want depends on what the program is for. A program that calls a C library (a window system, a database client) uses C's threads, <code>C.pthread_create</code>, because the library may assume them. A program that talks only to the kernel uses <code>Thread</code>, and is a static executable.</p>
118117 <h2>The same promise for your own modules</h2>
119118 <p>The rule isn't special to <code>Thread</code>. A module of yours that makes <code>SYSCALL</code>s directly, or relies on being the only owner of signal handlers, or sets up its own stacks, says <code>link static;</code> too, and then the compiler keeps C away from it for you. The module is documenting a fact about itself, and the compiler checks it where the program is assembled.</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="static.html">&larr; A program with no run time</a><a href="index.html">Contents</a><a class="next" href="lifecycle.html">Starting, ending, and signals &rarr;</a></div></article></div>

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

@@ -117,55 +117,61 @@
117117 0 push rbp
118118 1 mov rbp, rsp
119119 4 sub rsp, 64
120- 11 mov [rbp - 48], rbx
120+ 11 mov [rbp - 56], rbx
121121 15 xor ebx, ebx
122- 17 mov [rbp - 40], r12
122+ 17 mov [rbp - 48], r12
123123 21 xor r12d, r12d
124- 24 mov [rbp - 32], rdx
125- 28 mov [rbp - 24], rcx
124+ 24 mov [rbp - 40], r13
125+ 28 xor r13d, r13d
126+ 31 mov [rbp - 32], rdx
127+ 35 mov [rbp - 24], rcx
126128 7 for i := 0 to LEN(a) - 1 do
127- 32 xor ebx, ebx
128- 34 mov r8, rsi
129- 37 lea r8, [r8 - 1]
130- 41 mov [rbp - 56], r8
131- 45 cmp rbx, [rbp - 56]
132- 49 jg 146
133- 55 jmp 68
134- 60 nop
135- 64 add rbx, 1
129+ 39 xor ebx, ebx
130+ 41 mov r8, rsi
131+ 44 lea r8, [r8 - 1]
132+ 48 mov r13, r8
133+ 51 cmp rbx, r13
134+ 54 jg 175
135+ 60 jmp 100
136+ 65 nop
137+ 73 nop
138+ 81 nop
139+ 89 nop
140+ 96 add rbx, 1
136141 8 sum := sum + a[i] * b[i];
137- 68 cmp rbx, rsi
138- 71 jae 159
139- 77 mov r10, rbx
140- 80 cmp r10, [rbp - 24]
141- 84 jae 170
142- 90 shl r10, 3
143- 94 add r10, [rbp - 32]
144- 98 mov r10, [r10]
145- 101 mov r9, [rdi + rbx * 8]
146- 105 imul r9, r10
147- 109 add r12, r9
148- 112 cmp rbx, [rbp - 56]
149- 116 jl 64
150- 122 cmp rbx, [rbp - 56]
151- 126 je 146
152- 132 add rbx, 1
153- 136 cmp rbx, [rbp - 56]
154- 140 jle 68
142+ 100 cmp rbx, rsi
143+ 103 jae 192
144+ 109 mov r10, rbx
145+ 112 cmp r10, [rbp - 24]
146+ 116 jae 203
147+ 122 shl r10, 3
148+ 126 add r10, [rbp - 32]
149+ 130 mov r10, [r10]
150+ 133 mov r9, [rdi + rbx * 8]
151+ 137 imul r9, r10
152+ 141 add r12, r9
153+ 144 cmp rbx, r13
154+ 147 jl 96
155+ 153 cmp rbx, r13
156+ 156 je 175
157+ 162 add rbx, 1
158+ 166 cmp rbx, r13
159+ 169 jle 100
155160 10 return sum;
156- 146 mov rax, r12
161+ 175 mov rax, r12
157162 11 end;
158- 149 mov rbx, [rbp - 48]
159- 153 mov r12, [rbp - 40]
160- 157 leave
161- 158 ret
163+ 178 mov rbx, [rbp - 56]
164+ 182 mov r12, [rbp - 48]
165+ 186 mov r13, [rbp - 40]
166+ 190 leave
167+ 191 ret
162168 8 sum := sum + a[i] * b[i];
163- 159 mov rax, rbx
164- 162 mov rcx, rsi
165- 165 call Rt.check0
166- 170 mov rax, r10
167- 173 mov rcx, [rbp - 24]
168- 177 call Rt.check0</code></pre>
169+ 192 mov rax, rbx
170+ 195 mov rcx, rsi
171+ 198 call Rt.check0
172+ 203 mov rax, r10
173+ 206 mov rcx, [rbp - 24]
174+ 210 call Rt.check0</code></pre>
169175 <p>Read it from the top. <code>a</code> arrives in <code>rdi</code> and <code>rsi</code>, pointer and length; <code>b</code> in <code>rdx</code> and <code>rcx</code>, which the procedure saves in its frame. <code>i</code> and <code>sum</code> live in <code>rbx</code> and <code>r12</code>, which the procedure saves and restores because the convention says they belong to the caller. The loop's test is at the bottom, and its limit, <code>LEN(a) - 1</code>, is taken once.</p>
170176 <p>In the body, each index has a check: <code>cmp rbx, rsi; jae</code> compares <code>i</code> with <code>a</code>'s length in a single instruction and jumps to a stub at the end of the procedure, which calls <code>Rt.check0</code> with the values it needs to say what failed. When the index is in range, which is nearly always, the cost is one compare and a branch that isn't taken.</p>
171177 <h2>Taking the checks out</h2>
@@ -197,48 +203,54 @@
197203 0 push rbp
198204 1 mov rbp, rsp
199205 4 sub rsp, 64
200- 11 mov [rbp - 48], rbx
206+ 11 mov [rbp - 56], rbx
201207 15 xor ebx, ebx
202- 17 mov [rbp - 40], r12
208+ 17 mov [rbp - 48], r12
203209 21 xor r12d, r12d
204- 24 mov [rbp - 32], rdx
205- 28 mov [rbp - 24], rcx
210+ 24 mov [rbp - 40], r13
211+ 28 xor r13d, r13d
212+ 31 mov [rbp - 32], rdx
213+ 35 mov [rbp - 24], rcx
206214 7 for i := 0 to LEN(a) - 1 do
207- 32 xor ebx, ebx
208- 34 mov r8, rsi
209- 37 lea r8, [r8 - 1]
210- 41 mov [rbp - 56], r8
211- 45 cmp rbx, [rbp - 56]
212- 49 jg 127
213- 55 jmp 68
214- 60 nop
215- 64 add rbx, 1
215+ 39 xor ebx, ebx
216+ 41 mov r8, rsi
217+ 44 lea r8, [r8 - 1]
218+ 48 mov r13, r8
219+ 51 cmp rbx, r13
220+ 54 jg 156
221+ 60 jmp 100
222+ 65 nop
223+ 73 nop
224+ 81 nop
225+ 89 nop
226+ 96 add rbx, 1
216227 8 sum := sum + a[i] * b[i];
217- 68 mov r10, rbx
218- 71 shl r10, 3
219- 75 add r10, [rbp - 32]
220- 79 mov r10, [r10]
221- 82 mov r9, [rdi + rbx * 8]
222- 86 imul r9, r10
223- 90 add r12, r9
224- 93 cmp rbx, [rbp - 56]
225- 97 jl 64
226- 103 cmp rbx, [rbp - 56]
227- 107 je 127
228- 113 add rbx, 1
229- 117 cmp rbx, [rbp - 56]
230- 121 jle 68
228+ 100 mov r10, rbx
229+ 103 shl r10, 3
230+ 107 add r10, [rbp - 32]
231+ 111 mov r10, [r10]
232+ 114 mov r9, [rdi + rbx * 8]
233+ 118 imul r9, r10
234+ 122 add r12, r9
235+ 125 cmp rbx, r13
236+ 128 jl 96
237+ 134 cmp rbx, r13
238+ 137 je 156
239+ 143 add rbx, 1
240+ 147 cmp rbx, r13
241+ 150 jle 100
231242 10 return sum;
232- 127 mov rax, r12
243+ 156 mov rax, r12
233244 11 end;
234- 130 mov rbx, [rbp - 48]
235- 134 mov r12, [rbp - 40]
236- 138 leave
237- 139 ret
245+ 159 mov rbx, [rbp - 56]
246+ 163 mov r12, [rbp - 48]
247+ 167 mov r13, [rbp - 40]
248+ 171 leave
249+ 172 ret
238250 <span class="cmd">$ raz -S Dot.raz | sed -n &#x27;/^Dot.dot:/,/^$/p&#x27; | wc -l</span>
239-55
251+61
240252 <span class="cmd">$ raz -S Dot2.raz | sed -n &#x27;/^Dot2.dot:/,/^$/p&#x27; | wc -l</span>
241-44</code></pre>
253+50</code></pre>
242254 <p>The compares, the branches and the stubs are gone. It is faster, and it is no longer safe: an index out of range reads or writes whatever memory it reaches, and what follows isn't defined. A program turns checks off where it has measured and is sure, and says so at the top of the module, where a reader can see it.</p>
243255 <h2>Comparing with C</h2>
244256 <p><code>make kernels</code> in the compiler's sources measures <code>test/bench</code>'s kernels against the same kernels written in Pascal and compiled by Free Pascal at <code>-O2</code>. The compiler's own guide, <code>doc/compiler.md</code>, gives the numbers and says what the compiler does and doesn't do. It is not the fastest code: it is code whose every instruction you can account for.</p>
@@ -246,18 +258,18 @@
246258 <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>
247259 <pre class="output term"><code><span class="cmd">$ raz -o Dot Dot.raz</span>
248260 <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>
249-0000000000403aa0 &lt;Dot.dot&gt;:
250- 403aa0: push rbp
251- 403aa1: mov rbp,rsp
252- 403aa4: sub rsp,0x40
253- 403aab: mov QWORD PTR [rbp-0x30],rbx
254- 403aaf: xor ebx,ebx
255- 403ab1: mov QWORD PTR [rbp-0x28],r12
256- 403ab5: xor r12d,r12d
257- 403ab8: mov QWORD PTR [rbp-0x20],rdx
258- 403abc: mov QWORD PTR [rbp-0x18],rcx
259- 403ac0: xor ebx,ebx
260- 403ac2: mov r8,rsi</code></pre>
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>
261273 <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>
262274 </main>
263275 <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-26096 bytes</code></pre>
68+26032 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; /usr/lib/libc.so.6
101- /lib64/ld-linux-x86-64.so.2 =&gt; /usr/lib64/ld-linux-x86-64.so.2</code></pre>
100+ libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6
101+ /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 28304
138-Lean 28296</code></pre>
137+Heavy 28208
138+Lean 28200</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 28296
145-Lean-stripped 6720
144+Lean 28200
145+Lean-stripped 6688
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:78
55+Sys.linux-x86_64.raz:89
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.12.0-linux-x86_64.tar.gz (history) added

Binary file; the patch has it.

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

@@ -0,0 +1 @@
1+6b7b935d159a56710d0ca942516be40d00d5858bc7e532d533d47e993ef3ee5d raz-0.12.0-linux-x86_64.tar.gz

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

Binary file; the patch has it.

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

@@ -0,0 +1 @@
1+5b7caa843dea6cc97f9164e3dacb08e513f78604abcee2652085f7202eb3616f raz-0.13.0-linux-x86_64.tar.gz

site/public/index.html (history) modified +11 -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>26</b> pages</span>
45+ <span><b>1</b> report, <b>27</b> pages</span>
4646 <span><b>static</b> executables, no run time to install</span>
4747 </div>
4848
@@ -85,6 +85,7 @@
8585 <li>Integers of 8 to 64 bits, signed and unsigned, which wrap; <code>BOOL</code>, <code>F32</code>, <code>F64</code>. Nothing converts unless it is written.</li>
8686 <li>Arrays, checked slices, pointers, records laid out as C lays them out, packed records, enumerations, sets, and variants with <code>match</code>.</li>
8787 <li>Every index and slice is checked; a failure says where it was, with the values, and the calls under way.</li>
88+ <li>Strings of UTF-8 bytes, raw strings over lines that keep the code's indent, and <code>match</code> on a string.</li>
8889 <li>Every variable starts at zero. Behaviour is defined everywhere but through pointers.</li>
8990 <li>No garbage collector: memory comes from an allocator the program chooses and passes.</li>
9091 <li>Several results, <code>ERROR</code> values and <code>defer</code>, in place of exceptions.</li>
@@ -108,28 +109,28 @@
108109 <p>The language is defined by <a href="spec.html">the report</a>, which is
109110 short on purpose: whatever it doesn&rsquo;t allow, raz doesn&rsquo;t have.
110111 Its second volume describes the library. Both are on one page, and each is
111-a PDF: <a href="raz.pdf">the language</a> (26 pages) and
112+a PDF: <a href="raz.pdf">the language</a> (27 pages) and
112113 <a href="raz-lib.pdf">the library</a>.</p>
113114
114115 <h2 id="download">Download</h2>
115-<p>raz 0.11.1 is a tarball for x86-64 Linux with the compiler, <code>raz</code>,
116+<p>raz 0.13.0 is a tarball for x86-64 Linux with the compiler, <code>raz</code>,
116117 its library, <code>lib/</code>, which it finds beside itself, the formatter
117118 and the documentation tool as sources in <code>tools/</code>, and the manual
118119 pages. It needs nothing else installed.</p>
119120 <div class="cta">
120- <a class="btn primary" href="download/raz-0.11.1-linux-x86_64.tar.gz">Download raz 0.11.1</a>
121- <a class="btn" href="download/raz-0.11.1-linux-x86_64.tar.gz.sha256">SHA-256</a>
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>
122123 </div>
123-<pre class="code"><code>$ tar xzf raz-0.11.1-linux-x86_64.tar.gz
124-$ ln -s $PWD/raz-0.11.1-linux-x86_64/raz ~/.local/bin/raz
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
125126 $ raz run Hello.raz</code></pre>
126127 <p>The link works since raz finds <code>lib/</code> beside its real path. The
127128 manual pages are in <code>man/</code>: <code>man ./man/raz-tutorial.7</code>.
128129 The address <code>download/latest</code> always leads to the newest release,
129-and <code>download/raz-0.11.1-linux-x86_64.tar.gz</code> to this one, for
130-good. Earlier releases: <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.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
131132 <code>.sha256</code> added to its name.</p>
132-<p class="sha">SHA-256 <code>65e8d5ffd5b1b22b917e260516bbf708bb6af9e25264bec5881aee02c821b22b</code></p>
133+<p class="sha">SHA-256 <code>5b7caa843dea6cc97f9164e3dacb08e513f78604abcee2652085f7202eb3616f</code></p>
133134
134135 </main>
135136 <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 +82 -14

@@ -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.23 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>.</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>
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;
@@ -65,7 +65,18 @@
6565 <p>An integer is an untyped constant, which takes the type it is used as, up to the largest <code>I64</code>. A hexadecimal number above that, such as <code>0FFFFFFFFFFFFFFFFH</code>, has type <code>U64</code>: so <code>-1</code> never fits a <code>U64</code>, and the big number goes nowhere else without a conversion. Arithmetic on untyped constants doesn’t wrap: a result beyond the <code>I64</code>s, such as <code>9223372036854775807 + 1</code>, is an error.</p>
6666 <p>A real is an untyped constant too. It is read as the nearest <code>F64</code> (a tie goes to the even one), and takes <code>F32</code> or <code>F64</code>, whichever it is used as: an <code>F32</code> is the nearest to that <code>F64</code>. Where nothing says which, it is an <code>F64</code>. A real never becomes an integer, nor an integer a real, without a conversion: <code>x := 1;</code> is an error for <code>x: F64</code>, and <code>x := 1.0;</code> is right.</p>
6767 <p><strong>Characters.</strong> <code>'a'</code> is the code point of one character, an integer: <code>'a'</code> is 97 and <code>'é'</code> is 233.</p>
68-<p><strong>Strings.</strong> <code>"héllo\n"</code> is a sequence of bytes, the UTF-8 of its text: <code>"é"</code> is the two bytes 0C3H, 0A9H. Both characters and strings take the escapes <code>\n</code> (line feed), <code>\t</code> (tab), <code>\r</code> (carriage return), <code>\0</code> (zero), <code>\\</code>, <code>\'</code>, <code>\"</code>, <code>\u{H...}</code> (the code point with hexadecimal value H…, up to 10FFFFH, in a string its UTF-8) and, in strings only, <code>\xHH</code> (the byte with hexadecimal value HH, which needn’t make valid UTF-8). In an escape, the hexadecimal digits may be capitals or not. A character or a string ends on the line it starts on.</p>
68+<p><strong>Strings.</strong> <code>"héllo\n"</code> is a sequence of bytes, the UTF-8 of its text: <code>"é"</code> is the two bytes 0C3H, 0A9H. Both characters and strings take the escapes <code>\n</code> (line feed), <code>\t</code> (tab), <code>\r</code> (carriage return), <code>\0</code> (zero), <code>\\</code>, <code>\'</code>, <code>\"</code>, <code>\u{H...}</code> (the code point with hexadecimal value H…, up to 10FFFFH, in a string its UTF-8) and, in strings only, <code>\xHH</code> (the byte with hexadecimal value HH, which needn’t make valid UTF-8). In an escape, the hexadecimal digits may be capitals or not. A character, or a string in double quotes, ends on the line it starts on.</p>
69+<p><strong>Raw strings.</strong> <code>`C:\tmp\"a b"`</code> is a string written between backquotes: its bytes are the text between them as it stands, with no escapes, so <code>\</code> and <code>"</code> are themselves. A raw string may run over lines, and each line break in it is a line feed, part of the string; a carriage return in it is left out.</p>
70+<p>A raw string whose opening backquote ends its line, and whose closing backquote has only blanks (spaces and tabs) before it on its line, is indented: the text is its lines between those two, each without the closing line’s blanks at its start, and each ending in a line feed. So a long text may stand at the indentation of the code around it:</p>
71+<pre><code>proc usage;
72+begin
73+ Out.str(`
74+ usage: tool [-v] file ...
75+ -v say what is done
76+ `);
77+end;</code></pre>
78+<p>writes <code>"usage: tool [-v] file ...\n -v say what is done\n"</code>. Each line of an indented raw string must start with the closing line’s blanks, the same spaces and tabs in the same order, or it is an error; a line holding only blanks is an empty line of the text. A raw string that isn’t indented is the text between its backquotes as it stands.</p>
79+<p>A raw string can’t hold a backquote; a text that needs one joins it with <code>+</code>, as <code>"`"</code>. Everywhere else a raw string is a string, as one in double quotes is.</p>
6980 <p>Messages give the line and the column, counted in characters (section 10).</p>
7081 <p><strong>Comments</strong> are <code>(*</code> … <code>*)</code> and nest. By convention, the comment just before an exported declaration is its documentation, and the one before <code>module</code> is the module’s.</p>
7182 <p><strong>Operators and delimiters:</strong></p>
@@ -495,7 +506,7 @@
495506 | rect(w, h): area := w * h;
496507 | dot: area := 0.0;
497508 end;</code></pre>
498-<p><code>match</code> takes a variant, an enumeration or an integer type (a <code>U8</code> included); other types are an error. It runs the arm of the case <code>s</code> holds. An arm may name the case’s fields, in order, all of them; the names stand for those fields of <code>s</code> itself, so assigning to one changes <code>s</code>; if <code>s</code> isn’t a variable, they stand for the fields of a copy. An arm may also leave the fields unnamed. Every case must have an arm, and only one, unless there is an <code>| else:</code> arm, which must come last.</p>
509+<p><code>match</code> takes a variant, an enumeration, an integer type (a <code>U8</code> included) or a string, a <code>[]U8</code>; other types are an error. It runs the arm of the case <code>s</code> holds. An arm may name the case’s fields, in order, all of them; the names stand for those fields of <code>s</code> itself, so assigning to one changes <code>s</code>; if <code>s</code> isn’t a variable, they stand for the fields of a copy. An arm may also leave the fields unnamed. Every case must have an arm, and only one, unless there is an <code>| else:</code> arm, which must come last.</p>
499510 <p><code>match</code> on an enumeration works the same way: each arm names one of the enumeration’s names, or several, separated by commas, for the arm to run when the value is any of them:</p>
500511 <pre><code>match op of
501512 | sPlus, sMinus: kind := additive;
@@ -509,7 +520,14 @@
509520 | &#39;0&#39; .. &#39;9&#39;: digit := true;
510521 | else:
511522 end;</code></pre>
512-<p>A label is a constant, or <code>lo .. hi</code>, the constants from <code>lo</code> to <code>hi</code>, both included (unlike a slice, which leaves <code>hi</code> out; <code>lo</code> may not be above <code>hi</code>). Each must fit the integer’s type, and no value may be in two labels. An integer <code>match</code> must have an <code>| else:</code> arm, which may be empty. Whether an arm names a case or gives labels is set by the type matched: <code>| Max:</code> is a case of a variant or an enumeration, and a constant for an integer.</p>
523+<p>A label is a constant, or <code>lo .. hi</code>, the constants from <code>lo</code> to <code>hi</code>, both included (unlike a slice, which leaves <code>hi</code> out; <code>lo</code> may not be above <code>hi</code>). Each must fit the integer’s type, and no value may be in two labels. An integer <code>match</code> must have an <code>| else:</code> arm, which may be empty.</p>
524+<p><code>match</code> on a string, a <code>[]U8</code> or what converts to one (section 5.10), runs the arm one of whose labels it equals, as <code>=</code> compares strings (section 6.5):</p>
525+<pre><code>match word of
526+| &quot;add&quot;, &quot;plus&quot;: op := add;
527+| &quot;sub&quot;: op := sub;
528+| else: op := none;
529+end;</code></pre>
530+<p>A label is a string constant, and no two labels may be equal; there are no ranges. A <code>match</code> on a string must have an <code>| else:</code> arm, which may be empty. Whether an arm names a case or gives labels is set by the type matched: <code>| Max:</code> is a case of a variant or an enumeration, and a constant for an integer or a string.</p>
513531 <h2 id="order-of-evaluation">7.5 Order of evaluation</h2>
514532 <p>Operands, arguments and the designators of an assignment are evaluated from left to right, and the designators of an assignment before its right side: <code>a[i()] := g()</code> calls <code>i</code> and then <code>g</code>.</p>
515533 <h2 id="volatile">7.6 Volatile</h2>
@@ -680,7 +698,8 @@
680698 | digit { hexDigit } &quot;H&quot;
681699 | digit { digit } &quot;.&quot; digit { digit } [ &quot;E&quot; [ &quot;+&quot; | &quot;-&quot; ] digit { digit } ] .
682700 char = &quot;&#39;&quot; ( character | escape ) &quot;&#39;&quot; .
683-string = &#39;&quot;&#39; { character | escape | &quot;\x&quot; anyHex anyHex } &#39;&quot;&#39; .
701+string = &#39;&quot;&#39; { character | escape | &quot;\x&quot; anyHex anyHex } &#39;&quot;&#39;
702+ | &quot;`&quot; { rawChar } &quot;`&quot; .
684703 escape = &quot;\n&quot; | &quot;\t&quot; | &quot;\r&quot; | &quot;\0&quot; | &quot;\\&quot; | &quot;\&#39;&quot; | &#39;\&quot;&#39;
685704 | &quot;\u{&quot; anyHex { anyHex } &quot;}&quot; .
686705 anyHex = hexDigit | &quot;a&quot; .. &quot;f&quot; .
@@ -765,7 +784,7 @@
765784 Results = Type | &quot;(&quot; Type { &quot;,&quot; Type } &quot;)&quot; .
766785 ExternDeclaration = &quot;extern&quot; ( &quot;proc&quot; IdentDef Signature
767786 | &quot;var&quot; IdentDef &quot;:&quot; Type ) .</code></pre>
768-<p><code>character</code> is any character but a line break, and in a string any but <code>"</code> and <code>\</code>, in a character any but <code>'</code> and <code>\</code>. A procedure argument of a generic module (section 3) is written as a <code>QualIdent</code>, which is a <code>Type</code> here. <code>...</code> comes last in a <code>Signature</code>, and only in an <code>extern</code>. An <code>Instruction</code> is written in the target’s assembly language (appendix A).</p>
787+<p><code>character</code> is any character but a line break, and in a string any but <code>"</code> and <code>\</code>, in a character any but <code>'</code> and <code>\</code>. <code>rawChar</code> is any character but <code>`</code>, line breaks included. A procedure argument of a generic module (section 3) is written as a <code>QualIdent</code>, which is a <code>Type</code> here. <code>...</code> comes last in a <code>Signature</code>, and only in an <code>extern</code>. An <code>Instruction</code> is written in the target’s assembly language (appendix A).</p>
769788 <h1 id="the-library-volume-2">The library, volume 2</h1>
770789 <p>The second volume of the report on raz. The first, <code>doc/raz.md</code>, defines the language; this one defines the modules that come with it, and refers to the first’s sections by number, as “section 6.8”. Only the core binds every implementation (appendix A of the first volume); the rest is the standard library.</p>
771790 <p>Modules written in raz, in the compiler’s <code>lib</code> directory:</p>
@@ -783,7 +802,7 @@
783802 </tr>
784803 <tr>
785804 <td><code>Sys</code></td>
786-<td>system calls: <code>open</code>, <code>openRead</code>, <code>create</code>, <code>read</code>, <code>write</code>, <code>seek</code>, <code>close</code>, <code>mmap</code>, <code>munmap</code>, <code>isTerminal</code>, <code>yield</code>, <code>exit</code>; files and directories, <code>rename</code>, <code>remove</code>, <code>removeDir</code>, <code>makeDir</code>, <code>chmod</code>, <code>symlink</code>, <code>readLink</code>, <code>info</code>, <code>openDir</code>, <code>nextEntry</code>, <code>closeDir</code>; the terminal, <code>rawMode</code>, <code>restoreMode</code>, <code>termSize</code>, <code>ready</code>; the environment, <code>env</code>, <code>workDir</code>, <code>setWorkDir</code>; programs, pipes and signals; time, <code>clock</code>, <code>now</code>, <code>sleep</code>; <code>random</code>; and sockets. One file per target, and <code>Sys.raz</code> over the C library for the others</td>
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>
787806 </tr>
788807 <tr>
789808 <td><code>Out</code></td>
@@ -799,7 +818,7 @@
799818 </tr>
800819 <tr>
801820 <td><code>Thread</code></td>
802-<td>threads for a program without C: <code>spawn</code>, <code>join</code>, and <code>Mutex</code>; it says <code>link static</code></td>
821+<td>threads, <code>spawn</code> and <code>join</code>, and <code>Mutex</code>, in any program; in one that uses C, made by C’s <code>pthread_create</code></td>
803822 </tr>
804823 <tr>
805824 <td><code>Mem</code></td>
@@ -815,7 +834,7 @@
815834 </tr>
816835 <tr>
817836 <td><code>Str</code></td>
818-<td>operations on <code>[]U8</code> strings: comparing, hashing, finding, UTF-8, reading a number</td>
837+<td>operations on <code>[]U8</code> strings: comparing, hashing, finding, trimming and cutting, case, joining and replacing, UTF-8, numbers to and from text</td>
819838 </tr>
820839 <tr>
821840 <td><code>Bytes</code></td>
@@ -902,7 +921,7 @@
902921 proc join*(t: ^Thread); (* waits for t to end, then frees its stack *)
903922 proc lock*(m: ^Mutex);
904923 proc unlock*(m: ^Mutex);</code></pre>
905-<p>A <code>Lock</code> is for short sections: a thread that finds it taken tries again, giving up the processor now and then. A <code>Mutex</code> sleeps. Each of <code>Thread</code>’s threads runs on a stack of its own, with a page below it that can’t be touched, so that running off the stack stops the program. A program that uses C makes its threads with C’s <code>pthread_create</code>, and so can’t use <code>Thread</code>.</p>
924+<p>A <code>Lock</code> is for short sections: a thread that finds it taken tries again, giving up the processor now and then. A <code>Mutex</code> sleeps. Each of <code>Thread</code>’s threads runs on a stack of its own, with a page below it that can’t be touched, so that running off the stack stops the program with a message, as in the main thread. In a program that uses C, <code>Thread</code> makes its threads with C’s <code>pthread_create</code>, so that C’s library knows of them; in one that doesn’t, with the system’s own calls. The program is the same either way.</p>
906925 <pre><code>module Out;
907926 proc str*(s: []U8);
908927 proc char*(c: U8);
@@ -958,6 +977,7 @@
958977 tty*: INT; (* a pseudo-terminal&#39;s fd, to start a session on; 0 for none *)
959978 end;
960979 Status* = (running, exited, killed, stopped);
980+ Peer* = record ... end; (* whom a datagram came from *)
961981
962982 proc write*(fd: INT; s: []U8): INT; (* the bytes written *)
963983 proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *)
@@ -1024,7 +1044,17 @@
10241044 proc listen*(path: []U8): INT; (* a Unix socket made at path, taking connections *)
10251045 proc listenTcp*(addr: []U8; port: INT): INT;
10261046 proc accept*(fd: INT): INT; (* the next connection, waiting for one *)
1027-proc send*(fd: INT; s: []U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *)</code></pre>
1047+proc send*(fd: INT; s: []U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *)
1048+proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *)
1049+proc listenUdp*(addr: []U8; port: INT): INT; (* a socket of datagrams on port at addr *)
1050+proc receive*(fd: INT; buf: []U8): (INT, Peer); (* the next datagram, in buf, and who sent it *)
1051+proc reply*(fd: INT; peer: Peer; s: []U8): INT; (* s, as one datagram, to peer *)
1052+proc localPort*(fd: INT): INT; (* the port the socket fd is bound to *)
1053+proc sendFds*(fd: INT; s: []U8; fds: []INT): INT; (* send, and with it fds, to a Unix socket *)
1054+proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT); (* read, and the fds that came with it *)
1055+
1056+proc memFile*(size: INT): INT; (* a file of size zero bytes, in memory, with no name: its fd *)
1057+proc mapFile*(fd, size: INT): ^U8; (* the first size bytes of fd, shared; munmap gives them back; or nil *)</code></pre>
10281058 <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>
10291059 <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>
10301060 <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>
@@ -1034,20 +1064,40 @@
10341064 <p><code>wait</code> waits for the program <code>pid</code> to change, or for any of this program’s if <code>pid</code> is -1, and says which one, and how: <code>exited</code>, with its exit status; <code>killed</code>, with the number of the signal that ended it; or <code>stopped</code>, by Ctrl-Z or a signal, with the signal’s number. Without <code>block</code> it doesn’t wait, and is 0 and <code>running</code> if no program changed. A program that has nothing to wait for is -10 (ECHILD). <code>kill</code> sends a program a signal, <code>sigTerm</code> to end it; to a group, if <code>pid</code> is minus its number.</p>
10351065 <p><strong>Signals.</strong> <code>catch</code> takes the signals <code>sigs</code> away from what they would do, ending the program most of them, and gives an fd that is ready to read when one comes, so that a program waits for signals with <code>ready</code> and <code>readyOf</code> as it waits for keys or a server. <code>caught</code> gives each signal that came, in turn. A later <code>catch</code> adds to the signals caught and gives the same fd. Catching <code>sigKill</code> or <code>sigStop</code>, or the signals <code>Rt</code> reports, SIGSEGV and SIGFPE, is -22 (EINVAL). <code>ignore</code> makes a signal do nothing, as a shell ignores <code>sigTtou</code> so that it can take its terminal back, and <code>ignore(sig, false)</code> gives it back what it does at first. A program that <code>spawn</code> starts begins with neither.</p>
10361066 <p><strong>Time.</strong> <code>clock</code> is for measuring how long something takes: it never goes back, and says nothing of the date. <code>now</code> is the date and time, which a person or the network may set back. <code>sleep</code> waits <code>ms</code> milliseconds.</p>
1037-<p><strong>Sockets.</strong> <code>connect</code> is for talking to a server on the same machine, an X server at <code>/tmp/.X11-unix/X0</code> say; <code>connectTcp</code> for one across the network, given its address as numbers, <code>"127.0.0.1"</code> or <code>"::1"</code>: turning a name into an address is a library’s work over these. <code>connectUdp</code> gives a socket of datagrams, each <code>send</code> one message to that address and each <code>read</code> one message from it, as a program asks a name server. What a socket receives comes by <code>read</code>, what it sends goes by <code>send</code>, and <code>close</code> ends it. <code>send</code> is <code>write</code> for a socket, except when the other end has closed it: <code>write</code> would stop the program with the signal SIGPIPE, where <code>send</code> returns -32 (EPIPE), so that a program learns its server has gone and can end as it wants. <code>listen</code> and <code>listenTcp</code> are the other side: a server’s socket, on <code>path</code>, which must not be there yet, or on <code>port</code> at <code>addr</code>, <code>"0.0.0.0"</code> or <code>"::"</code> for every address the machine has; <code>accept</code> gives each connection that comes, as a socket of its own.</p>
1067+<p><strong>Sockets.</strong> <code>connect</code> is for talking to a server on the same machine, an X server at <code>/tmp/.X11-unix/X0</code> say; <code>connectTcp</code> for one across the network, given its address as numbers, <code>"127.0.0.1"</code> or <code>"::1"</code>: turning a name into an address is a library’s work over these. <code>connectUdp</code> gives a socket of datagrams, each <code>send</code> one message to that address and each <code>read</code> one message from it, as a program asks a name server. What a socket receives comes by <code>read</code>, what it sends goes by <code>send</code>, and <code>close</code> ends it. <code>send</code> is <code>write</code> for a socket, except when the other end has closed it: <code>write</code> would stop the program with the signal SIGPIPE, where <code>send</code> returns -32 (EPIPE), so that a program learns its server has gone and can end as it wants. <code>listen</code> and <code>listenTcp</code> are the other side: a server’s socket, on <code>path</code>, which must not be there yet, or on <code>port</code> at <code>addr</code>, <code>"0.0.0.0"</code> or <code>"::"</code> for every address the machine has; <code>accept</code> gives each connection that comes, as a socket of its own. A server started on port 0 gets a port the system chooses, which <code>localPort</code> says, as a test does that must not take a port in use. <code>connectTcp</code> waits as long as the system does, minutes for a host that never answers; <code>connectTcpWithin</code> gives up after <code>ms</code> milliseconds, with -110 (ETIMEDOUT).</p>
1068+<p><code>listenUdp</code> is the server’s side of <code>connectUdp</code>: a socket of datagrams from anyone, as a name server has. <code>receive</code> gives the next datagram, at most <code>LEN(buf)</code> bytes of it, the rest lost, and a <code>Peer</code> that <code>reply</code> sends an answer to.</p>
1069+<p><strong>Passing files and sharing memory.</strong> A program can give another an open file over a Unix socket: <code>sendFds</code> sends <code>s</code>, at least one byte, and with it <code>fds</code>, at most 16, each a file the other program then has open too; <code>receiveFds</code> reads into <code>buf</code> as <code>read</code> does, puts the fds that came with it in <code>fds</code>, as many as there is room for, closing the rest, and says how many bytes and how many fds. Each is closed in a program exec starts. <code>memFile</code> makes a file that lives in memory, of <code>size</code> zeroed bytes, and <code>mapFile</code> maps it, or any file, so that what one program writes there another sees at once: so a program hands an X server or a Wayland compositor its pixels, by <code>sendFds</code> of a <code>memFile</code> both map, rather than by copying them down a socket.</p>
10381070 <p><code>readyOf</code> waits on several file descriptors at once, as a program does that listens to a server and a terminal both; a descriptor at its end, or failed, counts as ready, since a read of it won’t wait. It looks at the first 64 of <code>fds</code>, and <code>connect</code> and <code>listen</code> take a path of at most 107 bytes, as <code>struct sockaddr_un</code> holds; a longer one is -36 (ENAMETOOLONG).</p>
10391071 <p>A program that both reads from a descriptor and writes much to it, as a terminal writes what is pasted to its program while it reads what the program writes back, must not wait on either: the program may be waiting for its own writes to be read. <code>setBlocking(fd, false)</code> has a read or a write on <code>fd</code> that would wait return -11 (EAGAIN) at once, a write having written what there was room for; <code>readyFor</code> waits, as <code>readyOf</code> does, for something to read on any of <code>reads</code> or for room to write on any of <code>writes</code>. A descriptor in both is given once, when it is ready for either; the 64 it looks at are <code>reads</code>, then <code>writes</code>.</p>
10401072 <p><strong>Strings, buffers and collections.</strong></p>
10411073 <pre><code>module Str;
10421074 proc cmp*(a, b: []U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *)
10431075 proc eq*(a, b: []U8): BOOL;
1076+proc eqFold*(a, b: []U8): BOOL; (* eq, an ASCII letter&#39;s capital and small the same *)
10441077 proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); (* s&#39;s bytes in memory from a; false if a has none *)
10451078 proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *)
10461079 proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *)
1080+proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *)
10471081 proc hasPrefix*(s, p: []U8): BOOL;
1082+proc hasSuffix*(s, x: []U8): BOOL;
1083+proc trim*(s: []U8): []U8; (* s without the spaces at either end *)
1084+proc trimLeft*(s: []U8): []U8; (* s without the spaces at its start *)
1085+proc trimRight*(s: []U8): []U8; (* s without the spaces at its end *)
1086+proc cut*(s, sep: []U8): ([]U8, []U8, BOOL); (* what comes before the first sep, and after it *)
1087+proc field*(s: []U8): ([]U8, []U8); (* the first run of bytes in s that aren&#39;t spaces, and what follows it *)
1088+proc lower*(s: []U8); (* s&#39;s ASCII capitals made small, in place *)
1089+proc upper*(s: []U8); (* s&#39;s ASCII small letters made capitals, in place *)
1090+proc join*(b: ^Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *)
1091+proc replace*(b: ^Buf.Buf; s, old, new: []U8); (* s added to b, with new for each old in it *)
10481092 proc encode*(c: I32): ([4]U8, INT); (* c in UTF-8: its bytes, and how many *)
10491093 proc decode*(s: []U8): (I32, INT); (* the character s starts with, and its length in bytes *)
1094+proc decodeLast*(s: []U8): (I32, INT); (* the character s ends with, and its length in bytes *)
1095+proc next*(s: []U8; i: INT): (I32, INT); (* the character at i, and where the next starts; -1 at the end *)
1096+proc valid*(s: []U8): BOOL; (* s is UTF-8, every character rightly spelled *)
1097+proc chars*(s: []U8): INT; (* the characters in s, as decode reads them *)
10501098 proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn&#39;t a number an I64 holds *)
1099+proc toHex*(s: []U8): (U64, BOOL); (* s, in hexadecimal; false if it isn&#39;t a number a U64 holds *)
1100+proc fromInt*(n: I64): ([20]U8, INT); (* n in decimal: its bytes, and how many *)
10511101 proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *)
10521102 proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *)
10531103 proc putLE*(s: []U8; v: U64); (* v into s, the lowest byte first *)
@@ -1117,12 +1167,30 @@
11171167 proc atan2*(y, x: F64): F64; (* the angle of (x, y) from the x axis, from -pi to pi *)
11181168 proc bits*(x: F64): U64; (* the bits of x, as IEEE 754 lays them out *)
11191169 proc fromBits*(u: U64): F64; (* the F64 whose bits are u *)
1120-proc mul*(a, b: U64): (U64, U64); (* a times b: the low 64 bits of the product, and the high *)</code></pre>
1170+proc mul*(a, b: U64): (U64, U64); (* a times b: the low 64 bits of the product, and the high *)
1171+proc toReal*(s: []U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn&#39;t one *)</code></pre>
11211172 <p>A <code>Buf</code>, a <code>List</code> and a <code>Map</code> take their memory from the allocator <code>init</code> gives them. <code>Fmt</code>’s format takes the next argument at each <code>{}</code>; <code>{{</code> and <code>}}</code> stand for <code>{</code> and <code>}</code>. A <code>Map</code> hashes and compares its keys with the procedures it is given: <code>import Count := Map([]U8, INT, Str.hash, Str.eq);</code>.</p>
1122-<p><code>Str</code> reads and writes UTF-8. <code>encode</code> writes a character, of at most 10FFFFH, in one to four bytes; a number that isn’t one becomes FFFDH, the character that stands for a wrong one. <code>decode</code> reads the first character of <code>s</code>, and gives FFFDH and 1 for a byte that doesn’t start one rightly, and 0 and 0 for an empty <code>s</code>. <code>toInt</code> reads a decimal number, with a <code>-</code> or <code>+</code> before it if any, and nothing else.</p>
1173+<p><code>Str</code> reads and writes UTF-8. <code>encode</code> writes a character, of at most 10FFFFH, in one to four bytes; a number that isn’t one becomes FFFDH, the character that stands for a wrong one. <code>decode</code> reads the first character of <code>s</code>, and gives FFFDH and 1 for a byte that doesn’t start one rightly, and 0 and 0 for an empty <code>s</code>: a continuation, a byte followed by too few continuations, a surrogate, a character beyond 10FFFFH or one spelled in more bytes than it needs. <code>decodeLast</code> reads the last character in the same way, to walk a string backwards. <code>valid</code> says that <code>decode</code> finds no such byte in <code>s</code>, and <code>chars</code> counts the characters, each such byte one. <code>next</code> walks a string a character at a time, as <code>decode</code> reads them: from 0, it gives the character at <code>i</code> and where the one after it starts, and 0 and -1 when <code>i</code> is at the end, or past it:</p>
1174+<pre><code>c, i := Str.next(s, 0);
1175+while i &gt;= 0 do
1176+ ... (* c is a character of s *)
1177+ c, i := Str.next(s, i);
1178+end;</code></pre>
1179+<p>A string is bytes, and <code>Str</code> takes no more of Unicode than UTF-8: <code>lower</code>, <code>upper</code> and <code>eqFold</code> change and match ASCII letters alone, and nothing says how wide a character is on a terminal, nor which spellings of a text are the same.</p>
1180+<p>A space, to <code>trim</code>, <code>trimLeft</code>, <code>trimRight</code> and <code>field</code>, is a blank, a tab, a line feed, a carriage return, a vertical tab or a form feed. <code>cut</code> gives what comes before the first <code>sep</code> in <code>s</code>, what comes after it, and <code>true</code>; or <code>s</code>, an empty string and <code>false</code> if <code>s</code> has no <code>sep</code>. <code>field</code> passes the spaces at the start of <code>s</code> and gives the bytes up to the next space or the end, and the rest of <code>s</code> from there; two empty strings if <code>s</code> holds only spaces. They take a string apart without memory of their own, each part a slice of <code>s</code>:</p>
1181+<pre><code>line, rest, more := Str.cut(text, &quot;\n&quot;); (* the first line, and the others *)
1182+key, val, ok := Str.cut(line, &quot;=&quot;);
1183+w, rest := Str.field(line); (* the words of a line *)
1184+while LEN(w) &gt; 0 do
1185+ Out.str(w); Out.ln;
1186+ w, rest := Str.field(rest);
1187+end;</code></pre>
1188+<p><code>lower</code> and <code>upper</code> write <code>s</code>’s bytes, so they take a variable’s, not a string literal’s (section 5.10). <code>join</code> and <code>replace</code> add to a <code>Buf</code> as <code>Buf.str</code> does; an empty <code>old</code> adds <code>s</code> as it is.</p>
1189+<p><code>toInt</code> reads a decimal number, with a <code>-</code> or <code>+</code> before it if any, and nothing else. <code>toHex</code> reads hexadecimal digits, capitals or not, and nothing else: no sign, <code>0x</code> or <code>H</code>. <code>fromInt</code> writes a number as <code>Out.int</code> does, with a <code>-</code> if it is negative, its bytes the first of the 20 it gives.</p>
11231190 <p><code>le</code> and <code>be</code> read an unsigned number of up to 8 bytes in a fixed order, little-endian or big-endian, whatever the machine’s own, and <code>putLE</code> and <code>putBE</code> write one; more than 8 bytes stops the program. They are how a file format or a network protocol is read and written:</p>
11241191 <pre><code>len := U32(Str.le(header[4..8]));
11251192 Str.putBE(packet[0..2], U64(port));</code></pre>
1193+<p><code>Math.toReal</code> reads a real as a constant is written (section 2), with a <code>-</code> or <code>+</code> before it if any, its point and the digits after it, or its exponent, left out if it has none, and <code>e</code> taken as <code>E</code>: <code>2</code>, <code>-0.5</code> and <code>1.5e-3</code> are reals to it. It gives the <code>F64</code> nearest the number, and <code>false</code> for anything else, or a number too big for an <code>F64</code>.</p>
11261194 <p><code>Map.next</code> walks a map: from 0, it gives the next entry and the number to pass for the one after it, and -1 when there are no more. A walk sees each entry once if the map doesn’t change during it:</p>
11271195 <pre><code>i, k, v := Names.next(ADR(names), 0);
11281196 while i &gt;= 0 do

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

@@ -70,10 +70,10 @@
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-28304 bytes</code></pre>
73+28208 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>
76-<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>.</p>
76+<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>
7777 <div class="file"><span>Numbers.raz</span></div><pre class="code"><code><span class="kw">module</span> Numbers;
7878 <span class="kw">import</span> Out;
7979

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

@@ -35,7 +35,7 @@
3535 <li><a href="procedures.html">Procedures and several results</a> <span>Value and var parameters, several results, forward, and procedures as values.</span></li>
3636 </ol>
3737 <h2>Set</h2><ol start='6'>
38-<li><a href="slices.html">Arrays, slices and strings</a> <span>Five in a row, a window onto them, bytes in UTF-8, and checks on every index.</span></li>
38+<li><a href="slices.html">Arrays, slices and strings</a> <span>Five in a row, a window onto them, bytes in UTF-8, raw strings over lines, and checks on every index.</span></li>
3939 <li><a href="records.html">Records and pointers</a> <span>Laid out as C lays out a struct, packed records, pointers and nil.</span></li>
4040 <li><a href="variants.html">Enumerations, sets, variants and match</a> <span>Names in order, sets as bits, and cases the compiler makes you handle.</span></li>
4141 <li><a href="memory.html">Memory and allocators</a> <span>No garbage collector; you choose where memory comes from, and what happens when it runs out.</span></li>

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

@@ -159,7 +159,7 @@
159159 <h2>Buffers that grow</h2>
160160 <p><code>Buf</code> is a growable byte buffer; like everything else it takes the allocator it grows from. It is what the program uses to build a string of unknown length.</p>
161161 <div class="file"><span>Build.raz</span></div><pre class="code"><code><span class="kw">module</span> Build;
162-<span class="kw">import</span> Out, Buf, Heap;
162+<span class="kw">import</span> Out, Buf, Heap, Str;
163163
164164 <span class="kw">proc</span> main*;
165165 <span class="kw">var</span> b: Buf.Buf; i: <span class="ty">INT</span>;
@@ -173,12 +173,21 @@
173173 <span class="kw">end</span>;
174174 Out.str(Buf.bytes(<span class="ty">ADR</span>(b))); Out.ln;
175175 Out.int(b.len); Out.str(<span class="string">&quot; bytes\n&quot;</span>);
176+ Buf.reset(<span class="ty">ADR</span>(b));
177+ Str.join(<span class="ty">ADR</span>(b), [<span class="string">&quot;red&quot;</span>, <span class="string">&quot;green&quot;</span>, <span class="string">&quot;blue&quot;</span>], <span class="string">&quot;, &quot;</span>);
178+ Out.str(Buf.bytes(<span class="ty">ADR</span>(b))); Out.ln;
179+ Buf.reset(<span class="ty">ADR</span>(b));
180+ Str.replace(<span class="ty">ADR</span>(b), <span class="string">&quot;a-b-c&quot;</span>, <span class="string">&quot;-&quot;</span>, <span class="string">&quot; and &quot;</span>);
181+ Out.str(Buf.bytes(<span class="ty">ADR</span>(b))); Out.ln;
176182 <span class="kw">end</span>;
177183
178184 <span class="kw">end</span> Build.</code></pre>
179185 <pre class="output"><code>item 1,item 2,item 3,item 4,item 5,
180186 35 bytes
187+red, green, blue
188+a and b and c
181189 </code></pre>
190+<p><code>Str.join</code> and <code>Str.replace</code> build into a <code>Buf</code> too: the parts with a separator between them, or a string with one text replaced by another wherever it is.</p>
182191 <h2>Pages</h2>
183192 <p>Under the allocators are pages from the system: <code>Mem.pages(size)</code> gives zeroed memory in whole pages, or <code>nil</code>, and <code>Mem.unmap</code> gives it back. Most programs never need to call it directly.</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="variants.html">&larr; Enumerations, sets, variants and match</a><a href="index.html">Contents</a><a class="next" href="errors.html">Errors &rarr;</a></div></article></div>
184193 </main>

site/public/tour/slices.html (history) modified +106 -20

@@ -51,7 +51,7 @@
5151 <span class="kw">end</span>;
5252
5353 <span class="kw">proc</span> main*;
54-<span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>; s: []<span class="ty">U8</span>; name: [<span class="number">8</span>]<span class="ty">U8</span>;
54+<span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>; s, val: []<span class="ty">U8</span>; name: [<span class="number">8</span>]<span class="ty">U8</span>; ok: <span class="ty">BOOL</span>;
5555 <span class="kw">begin</span>
5656 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4</span> <span class="kw">do</span> a[i] := i * i; <span class="kw">end</span>;
5757 Out.int(sum(a)); Out.ln; <span class="comment">(* the array, as a slice *)</span>
@@ -60,9 +60,15 @@
6060 Out.str(s[<span class="number">7</span>..<span class="ty">LEN</span>(s)]); Out.ln;
6161 <span class="kw">if</span> s[<span class="number">0</span>..<span class="number">5</span>] = <span class="string">&quot;hello&quot;</span> <span class="kw">then</span> Out.str(<span class="string">&quot;it says hello\n&quot;</span>); <span class="kw">end</span>;
6262 Out.int(Str.find(s, <span class="string">&quot;world&quot;</span>)); Out.ln;
63+ _, val, ok := Str.cut(<span class="string">&quot; size = 42 &quot;</span>, <span class="string">&quot;=&quot;</span>);
64+ <span class="kw">if</span> ok <span class="kw">then</span> Out.str(Str.trim(val)); Out.ln; <span class="kw">end</span>;
6365 name := <span class="string">&quot;raz&quot;</span>; <span class="comment">(* a copy, the program&#x27;s own *)</span>
6466 name[<span class="number">0</span>] := <span class="string">&#x27;R&#x27;</span>;
6567 Out.str(name[<span class="number">0</span>..<span class="number">3</span>]); Out.ln;
68+ Out.str(<span class="string">`
69+ C:\tmp\ &quot;as it stands&quot;
70+ over lines
71+ `</span>);
6672 <span class="kw">end</span>;
6773
6874 <span class="kw">end</span> Slices.</code></pre>
@@ -71,7 +77,10 @@
7177 world
7278 it says hello
7379 7
80+42
7481 Raz
82+C:\tmp\ &quot;as it stands&quot;
83+ over lines
7584 </code></pre>
7685 <h2>Arrays</h2>
7786 <p><code>[5]INT</code> is an array: five <code>INT</code>s in a row, copied when it is assigned or passed. Its length is part of its type, so <code>[5]INT</code> and <code>[6]INT</code> are different types, and <code>LEN(a)</code> of an array is a constant. Array literals are written in brackets, and need a type from the place they are used.</p>
@@ -152,7 +161,7 @@
152161 <pre class="output"><code>1 1 2 3 4 5
153162 </code></pre>
154163 <h2>Strings</h2>
155-<p>A string is a slice of bytes, <code>[]U8</code>, in UTF-8, and a string constant's bytes are constant. A <code>[8]U8</code> made from one is a copy you can change. Arrays and slices of integers compare with <code>=</code> and <code>#</code>, so strings do too. <code>Str</code> has the rest: ordering, finding, hashing, prefixes, UTF-8 and reading a number.</p>
164+<p>A string is a slice of bytes, <code>[]U8</code>, in UTF-8, and a string constant's bytes are constant. A <code>[8]U8</code> made from one is a copy you can change. Arrays and slices of integers compare with <code>=</code> and <code>#</code>, so strings do too. <code>Str</code> has the rest: ordering, finding, hashing, prefixes and suffixes, trimming, taking apart, ASCII case, UTF-8, and numbers to and from text.</p>
156165 <div class="file"><span>Words.raz</span></div><pre class="code"><code><span class="kw">module</span> Words;
157166 <span class="kw">import</span> Out, Str;
158167
@@ -164,8 +173,12 @@
164173 <span class="kw">if</span> s[<span class="number">0</span>..<span class="number">5</span>] = <span class="string">&quot;hello&quot;</span> <span class="kw">then</span> Out.str(<span class="string">&quot;it says hello\n&quot;</span>); <span class="kw">end</span>;
165174 Out.int(Str.find(s, <span class="string">&quot;world&quot;</span>)); Out.ln; <span class="comment">(* 7 *)</span>
166175 Out.int(Str.find(s, <span class="string">&quot;xyz&quot;</span>)); Out.ln; <span class="comment">(* -1 *)</span>
176+ Out.int(Str.findLast(<span class="string">&quot;abcabc&quot;</span>, <span class="string">&quot;bc&quot;</span>)); Out.ln; <span class="comment">(* 4 *)</span>
167177 <span class="kw">if</span> Str.hasPrefix(s, <span class="string">&quot;hell&quot;</span>) <span class="kw">then</span> Out.str(<span class="string">&quot;prefix\n&quot;</span>); <span class="kw">end</span>;
178+ <span class="kw">if</span> Str.hasSuffix(s, <span class="string">&quot;world&quot;</span>) <span class="kw">then</span> Out.str(<span class="string">&quot;suffix\n&quot;</span>); <span class="kw">end</span>;
168179 Out.int(Str.cmp(<span class="string">&quot;apple&quot;</span>, <span class="string">&quot;banana&quot;</span>)); Out.ln; <span class="comment">(* -1 *)</span>
180+ <span class="kw">if</span> Str.eqFold(<span class="string">&quot;RAZ&quot;</span>, <span class="string">&quot;raz&quot;</span>) <span class="kw">then</span> Out.str(<span class="string">&quot;the same, but for case\n&quot;</span>); <span class="kw">end</span>;
181+ Out.str(<span class="string">&quot;[&quot;</span>); Out.str(Str.trim(<span class="string">&quot; padded \t\n&quot;</span>)); Out.str(<span class="string">&quot;]\n&quot;</span>);
169182
170183 n, ok := Str.toInt(<span class="string">&quot;-1234&quot;</span>);
171184 <span class="kw">if</span> ok <span class="kw">then</span> Out.int(n + <span class="number">1</span>); Out.ln; <span class="kw">end</span>;
@@ -175,6 +188,8 @@
175188 name := <span class="string">&quot;raz&quot;</span>; <span class="comment">(* a copy, the program&#x27;s own *)</span>
176189 name[<span class="number">0</span>] := <span class="string">&#x27;R&#x27;</span>;
177190 Out.str(name[<span class="number">0</span>..<span class="number">3</span>]); Out.ln;
191+ Str.upper(name[<span class="number">0</span>..<span class="number">3</span>]); <span class="comment">(* in place: name is a variable *)</span>
192+ Out.str(name[<span class="number">0</span>..<span class="number">3</span>]); Out.ln;
178193 <span class="kw">end</span>;
179194
180195 <span class="kw">end</span> Words.</code></pre>
@@ -182,48 +197,119 @@
182197 it says hello
183198 7
184199 -1
200+4
185201 prefix
202+suffix
186203 -1
204+the same, but for case
205+[padded]
187206 -1233
188207 not a number
189208 Raz
209+RAZ
190210 </code></pre>
191-<h2>UTF-8</h2>
192-<p>A character is an <code>I32</code>, its code point. <code>Str.decode</code> reads the first character of a string and says how many bytes it took; <code>Str.encode</code> goes the other way. Byte length and character count are different things, and the program is the one that says which it wants.</p>
193-<div class="file"><span>Runes.raz</span></div><pre class="code"><code><span class="kw">module</span> Runes;
211+<h2>Taking a string apart</h2>
212+<p><code>Str.cut</code> splits a string at the first place a separator is: what comes before it, what comes after, and whether it was there at all. <code>Str.field</code> gives the first word, past any spaces, and the rest. Neither copies anything: every part is a slice of the string it came from, so taking a line or a file apart costs no memory.</p>
213+<div class="file"><span>Parts.raz</span></div><pre class="code"><code><span class="kw">module</span> Parts;
194214 <span class="kw">import</span> Out, Str;
215+
216+<span class="kw">const</span> Text = <span class="string">&quot;name = raz\nkind = language\nyear = 2026\n&quot;</span>;
195217
196-<span class="comment">(* the characters of s, not its bytes *)</span>
197-<span class="kw">proc</span> chars(s: []<span class="ty">U8</span>): <span class="ty">INT</span>;
198-<span class="kw">var</span> i, n, size: <span class="ty">INT</span>;
218+<span class="kw">proc</span> main*;
219+<span class="kw">var</span> line, rest, key, val, w: []<span class="ty">U8</span>; more, ok: <span class="ty">BOOL</span>;
199220 <span class="kw">begin</span>
200- i := <span class="number">0</span>;
201- n := <span class="number">0</span>;
202- <span class="kw">while</span> i &lt; <span class="ty">LEN</span>(s) <span class="kw">do</span>
203- _, size := Str.decode(s[i..<span class="ty">LEN</span>(s)]);
204- <span class="ty">INC</span>(i, size);
205- <span class="ty">INC</span>(n);
221+ <span class="comment">(* line by line, then each line at its = *)</span>
222+ line, rest, more := Str.cut(Text, <span class="string">&quot;\n&quot;</span>);
223+ <span class="kw">while</span> more <span class="kw">do</span>
224+ key, val, ok := Str.cut(line, <span class="string">&quot;=&quot;</span>);
225+ <span class="kw">if</span> ok <span class="kw">then</span>
226+ Out.str(Str.trim(key)); Out.str(<span class="string">&quot; is &quot;</span>); Out.str(Str.trim(val)); Out.ln;
227+ <span class="kw">end</span>;
228+ line, rest, more := Str.cut(rest, <span class="string">&quot;\n&quot;</span>);
229+ <span class="kw">end</span>;
230+
231+ <span class="comment">(* word by word *)</span>
232+ w, rest := Str.field(<span class="string">&quot; the quick brown fox &quot;</span>);
233+ <span class="kw">while</span> <span class="ty">LEN</span>(w) &gt; <span class="number">0</span> <span class="kw">do</span>
234+ Out.str(<span class="string">&quot;&lt;&quot;</span>); Out.str(w); Out.str(<span class="string">&quot;&gt;&quot;</span>);
235+ w, rest := Str.field(rest);
206236 <span class="kw">end</span>;
207- <span class="kw">return</span> n;
237+ Out.ln;
208238 <span class="kw">end</span>;
209239
240+<span class="kw">end</span> Parts.</code></pre>
241+<pre class="output"><code>name is raz
242+kind is language
243+year is 2026
244+&lt;the&gt;&lt;quick&gt;&lt;brown&gt;&lt;fox&gt;
245+</code></pre>
246+<p>When <code>Str.cut</code> finds no separator it gives the whole string, an empty one and <code>false</code>, so the loop above ends after the last line that has a line feed. Building a string goes the other way, in a <code>Buf</code>: <code>Str.join</code> adds parts with a separator between them, and <code>Str.replace</code> adds a string with every match of one text replaced by another (lesson 9 shows <code>Buf</code>).</p>
247+<h2>Raw strings</h2>
248+<p>A string between backquotes is raw: its bytes are the text as it stands, with no escapes, so a backslash or a double quote is itself, and it may run over lines. When the opening backquote ends its line and the closing one has only spaces and tabs before it, those blanks come off the start of every line between, and the text keeps the indent of the code it sits in. Each line ends in a line feed, the last one too.</p>
249+<div class="file"><span>Raw.raz</span></div><pre class="code"><code><span class="kw">module</span> Raw;
250+<span class="kw">import</span> Out;
251+
252+<span class="kw">const</span> Path = <span class="string">`C:\Users\raz\&quot;notes&quot;.txt`</span>;
253+
254+<span class="kw">proc</span> usage;
255+<span class="kw">begin</span>
256+ Out.str(<span class="string">`
257+ usage: tool [-v] file ...
258+ -v say what is done
259+ file read this, not standard input
260+ `</span>);
261+<span class="kw">end</span>;
262+
210263 <span class="kw">proc</span> main*;
211-<span class="kw">var</span> s: []<span class="ty">U8</span>; c: <span class="ty">I32</span>; size: <span class="ty">INT</span>; bytes: [<span class="number">4</span>]<span class="ty">U8</span>; k: <span class="ty">INT</span>;
264+<span class="kw">begin</span>
265+ Out.str(Path); Out.ln;
266+ usage;
267+ Out.str(<span class="string">`a raw string on one line, \n and all`</span>); Out.ln;
268+<span class="kw">end</span>;
269+
270+<span class="kw">end</span> Raw.</code></pre>
271+<pre class="output"><code>C:\Users\raz\&quot;notes&quot;.txt
272+usage: tool [-v] file ...
273+ -v say what is done
274+ file read this, not standard input
275+a raw string on one line, \n and all
276+</code></pre>
277+<p>A line of an indented raw string that doesn't start with the closing line's blanks is an error, so a slip in the indent is caught where it is made, not printed. A raw string can't hold a backquote; join one in with <code>+</code> and an ordinary string that holds it.</p>
278+<h2>UTF-8</h2>
279+<p>A character is an <code>I32</code>, its code point. A string stays bytes; UTF-8 is how <code>Str</code> reads them. <code>Str.next</code> walks a string a character at a time, giving each one and where the next starts, -1 at the end. <code>Str.decode</code> reads the first character of a string and says how many bytes it took, <code>Str.decodeLast</code> the last, and <code>Str.encode</code> goes the other way. <code>Str.chars</code> counts characters and <code>Str.valid</code> says whether a string is UTF-8 at all; a byte that doesn't start a character rightly reads as FFFDH, the character that stands for a wrong one. Byte length and character count are different things, and the program is the one that says which it wants.</p>
280+<div class="file"><span>Runes.raz</span></div><pre class="code"><code><span class="kw">module</span> Runes;
281+<span class="kw">import</span> Out, Str;
282+
283+<span class="kw">proc</span> main*;
284+<span class="kw">var</span> s: []<span class="ty">U8</span>; c: <span class="ty">I32</span>; i, size: <span class="ty">INT</span>; bytes: [<span class="number">4</span>]<span class="ty">U8</span>; k: <span class="ty">INT</span>;
212285 <span class="kw">begin</span>
213286 s := <span class="string">&quot;caf\u{E9} \u{3BB}&quot;</span>;
214287 Out.int(<span class="ty">LEN</span>(s)); Out.str(<span class="string">&quot; bytes, &quot;</span>);
215- Out.int(chars(s)); Out.str(<span class="string">&quot; characters\n&quot;</span>);
216- c, size := Str.decode(s[<span class="number">3</span>..<span class="ty">LEN</span>(s)]);
217- Out.int(<span class="ty">I64</span>(c)); Out.str(<span class="string">&quot; took &quot;</span>); Out.int(size); Out.str(<span class="string">&quot; bytes\n&quot;</span>);
288+ Out.int(Str.chars(s)); Out.str(<span class="string">&quot; characters\n&quot;</span>);
289+
290+ <span class="comment">(* each character, as its code point *)</span>
291+ c, i := Str.next(s, <span class="number">0</span>);
292+ <span class="kw">while</span> i &gt;= <span class="number">0</span> <span class="kw">do</span>
293+ Out.int(<span class="ty">I64</span>(c)); Out.str(<span class="string">&quot; &quot;</span>);
294+ c, i := Str.next(s, i);
295+ <span class="kw">end</span>;
296+ Out.ln;
297+
298+ c, size := Str.decodeLast(s);
299+ Out.int(<span class="ty">I64</span>(c)); Out.str(<span class="string">&quot; is last, in &quot;</span>); Out.int(size); Out.str(<span class="string">&quot; bytes\n&quot;</span>);
218300 bytes, k := Str.encode(<span class="number">955</span>);
219301 Out.str(bytes[<span class="number">0</span>..k]); Out.ln;
302+ <span class="kw">if</span> <span class="kw">not</span> Str.valid(s[<span class="number">0</span>..<span class="number">4</span>]) <span class="kw">then</span> Out.str(<span class="string">&quot;cut in the middle of a character\n&quot;</span>); <span class="kw">end</span>;
220303 <span class="kw">end</span>;
221304
222305 <span class="kw">end</span> Runes.</code></pre>
223306 <pre class="output"><code>8 bytes, 6 characters
224-233 took 2 bytes
307+99 97 102 233 32 955
308+955 is last, in 2 bytes
225309 λ
310+cut in the middle of a character
226311 </code></pre>
312+<p><code>Str</code> knows UTF-8 and no more of Unicode: its case is ASCII's, and it doesn't say how wide a character is on a terminal or which spellings of a text are the same. Those need tables bigger than the rest of the library.</p>
227313 <h2>Loops over lengths</h2>
228314 <p>A <code>for</code> loop takes its bounds once, and never steps past the last: with an empty slice, <code>0 to LEN(s) - 1</code> doesn't run at all, and the variable can't wrap. <code>for</code> takes a constant step with <code>by</code>, and <code>while</code> and <code>repeat ... until</code> do the rest.</p>
229315 <div class="file"><span>Loops.raz</span></div><pre class="code"><code><span class="kw">module</span> Loops;

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

@@ -70,6 +70,15 @@
7070 <span class="kw">end</span>;
7171 <span class="kw">end</span>;
7272
73+<span class="kw">proc</span> colour(name: []<span class="ty">U8</span>): Colour;
74+<span class="kw">begin</span>
75+ <span class="kw">match</span> name <span class="kw">of</span>
76+ | <span class="string">&quot;red&quot;</span>, <span class="string">&quot;rose&quot;</span>: <span class="kw">return</span> red;
77+ | <span class="string">&quot;green&quot;</span>: <span class="kw">return</span> green;
78+ | <span class="kw">else</span>: <span class="kw">return</span> blue;
79+ <span class="kw">end</span>;
80+<span class="kw">end</span>;
81+
7382 <span class="kw">proc</span> main*;
7483 <span class="kw">var</span> shapes: [<span class="number">3</span>]Shape; i: <span class="ty">INT</span>; warm: Colours; c: Colour;
7584 <span class="kw">begin</span>
@@ -83,6 +92,7 @@
8392 <span class="kw">for</span> c := red <span class="kw">to</span> blue <span class="kw">do</span>
8493 <span class="kw">if</span> c <span class="kw">in</span> warm <span class="kw">then</span> Out.int(<span class="ty">I64</span>(c)); Out.ln; <span class="kw">end</span>;
8594 <span class="kw">end</span>;
95+ Out.int(<span class="ty">I64</span>(colour(<span class="string">&quot;rose&quot;</span>))); Out.int(<span class="ty">I64</span>(colour(<span class="string">&quot;green&quot;</span>))); Out.ln;
8696 <span class="kw">end</span>;
8797
8898 <span class="kw">end</span> Shapes.</code></pre>
@@ -92,6 +102,7 @@
92102 digit letter other
93103 0
94104 1
105+01
95106 </code></pre>
96107 <h2>Enumerations</h2>
97108 <p>An enumeration is a list of names, in order. They convert to their position and back, and a <code>for</code> loop can run over them. An integer that has no name at its position is a failed check, never a silent wrong value.</p>
@@ -261,7 +272,45 @@
261272 is space
262273 _ is letter
263274 y is letter
264-</code></pre><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="records.html">&larr; Records and pointers</a><a href="index.html">Contents</a><a class="next" href="memory.html">Memory and allocators &rarr;</a></div></article></div>
275+</code></pre>
276+<h2>Matching strings</h2>
277+<p>On a string, <code>match</code> takes string constants, several to an arm if need be, and compares them as <code>=</code> does. It too must have an <code>| else:</code>. It is how a program reads a command, an option or a keyword without a chain of <code>elsif</code>s.</p>
278+<div class="file"><span>Commands.raz</span></div><pre class="code"><code><span class="kw">module</span> Commands;
279+<span class="kw">import</span> Out;
280+
281+<span class="kw">type</span> Op = (none, add, sub, show);
282+
283+<span class="kw">proc</span> op(word: []<span class="ty">U8</span>): Op;
284+<span class="kw">begin</span>
285+ <span class="kw">match</span> word <span class="kw">of</span>
286+ | <span class="string">&quot;add&quot;</span>, <span class="string">&quot;plus&quot;</span>, <span class="string">&quot;+&quot;</span>: <span class="kw">return</span> add;
287+ | <span class="string">&quot;sub&quot;</span>, <span class="string">&quot;minus&quot;</span>, <span class="string">&quot;-&quot;</span>: <span class="kw">return</span> sub;
288+ | <span class="string">&quot;show&quot;</span>: <span class="kw">return</span> show;
289+ | <span class="kw">else</span>: <span class="kw">return</span> none;
290+ <span class="kw">end</span>;
291+<span class="kw">end</span>;
292+
293+<span class="kw">proc</span> main*;
294+<span class="kw">var</span> total: <span class="ty">INT</span>; words: [<span class="number">6</span>][]<span class="ty">U8</span>; i: <span class="ty">INT</span>;
295+<span class="kw">begin</span>
296+ words := [<span class="string">&quot;add&quot;</span>, <span class="string">&quot;plus&quot;</span>, <span class="string">&quot;sub&quot;</span>, <span class="string">&quot;show&quot;</span>, <span class="string">&quot;times&quot;</span>, <span class="string">&quot;-&quot;</span>];
297+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">5</span> <span class="kw">do</span>
298+ <span class="kw">match</span> op(words[i]) <span class="kw">of</span>
299+ | add: <span class="ty">INC</span>(total);
300+ | sub: <span class="ty">DEC</span>(total);
301+ | show: Out.int(total); Out.ln;
302+ | none: Out.str(words[i]); Out.str(<span class="string">&quot;?\n&quot;</span>);
303+ <span class="kw">end</span>;
304+ <span class="kw">end</span>;
305+ Out.int(total); Out.ln;
306+<span class="kw">end</span>;
307+
308+<span class="kw">end</span> Commands.</code></pre>
309+<pre class="output"><code>1
310+times?
311+0
312+</code></pre>
313+<p>Two labels that are the same string are an error, as two arms for one case are.</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="records.html">&larr; Records and pointers</a><a href="index.html">Contents</a><a class="next" href="memory.html">Memory and allocators &rarr;</a></div></article></div>
265314 </main>
266315 <footer>raz is under the ISC license.</footer>
267316 </body>