site: v0.13.0
| @@ -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 |
| @@ -201,37 +201,45 @@ | ||
| 201 | 201 | </code></pre> |
| 202 | 202 | <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> |
| 203 | 203 | <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> | |
| 205 | 205 | <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; | |
| 207 | 207 | |
| 208 | 208 | <span class="comment">(* the sum of the numbers in a comma separated list, or -1 if any isn't *)</span> |
| 209 | 209 | <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>; | |
| 211 | 211 | <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">','</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">","</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; | |
| 220 | 218 | <span class="kw">end</span>; |
| 221 | 219 | <span class="kw">return</span> sum; |
| 222 | 220 | <span class="kw">end</span>; |
| 223 | 221 | |
| 224 | 222 | <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>; | |
| 225 | 224 | <span class="kw">begin</span> |
| 226 | 225 | Out.int(total(<span class="string">"10,20,-5,+7"</span>)); Out.ln; |
| 227 | 226 | Out.int(total(<span class="string">"10,x,20"</span>)); Out.ln; |
| 228 | 227 | Out.int(total(<span class="string">"10,,20"</span>)); Out.ln; |
| 228 | + u, ok := Str.toHex(<span class="string">"ff00"</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">"-1.5e3"</span>); | |
| 231 | + <span class="kw">if</span> ok <span class="kw">then</span> Fmt.print(<span class="string">"{}\n"</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; | |
| 229 | 234 | <span class="kw">end</span>; |
| 230 | 235 | |
| 231 | 236 | <span class="kw">end</span> Numbers.</code></pre> |
| 232 | 237 | <pre class="output"><code>32 |
| 233 | 238 | -1 |
| 234 | 239 | -1 |
| 240 | +FF00 | |
| 241 | +-1500.0 | |
| 242 | +-9223372036854775808 | |
| 235 | 243 | </code></pre><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="fmt.html">← Output and formatting</a><a href="index.html">Contents</a><a class="next" href="collections.html">Lists, maps and sorting →</a></div></article></div> |
| 236 | 244 | </main> |
| 237 | 245 | <footer>raz is under the ISC license.</footer> |
| @@ -125,18 +125,15 @@ | ||
| 125 | 125 | |
| 126 | 126 | <span class="kw">proc</span> main*; |
| 127 | 127 | <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>; | |
| 130 | 130 | <span class="kw">begin</span> |
| 131 | 131 | 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">' '</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) > <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); | |
| 140 | 137 | <span class="kw">end</span>; |
| 141 | 138 | n := <span class="number">0</span>; |
| 142 | 139 | found, key, v := Count.next(<span class="ty">ADR</span>(m), <span class="number">0</span>); |
| @@ -49,28 +49,28 @@ | ||
| 49 | 49 | <h2>What <code>Thread</code> does</h2> |
| 50 | 50 | <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> |
| 51 | 51 | <pre class="output term"><code><span class="cmd">$ sed -n '1,22p' $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. | |
| 53 | 53 | Each runs on a stack of its own, mmaped, with a page below it that |
| 54 | 54 | can't be touched, so that running off the stack stops the program |
| 55 | 55 | rather than spoiling what lies below; above it, a little stack for |
| 56 | 56 | Rt's signal handlers, so that they can say so. join waits on the |
| 57 | 57 | thread'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's, through pthread_create, and so can't use these. *) | |
| 58 | + thread ends; a Mutex sleeps on a futex. | |
| 59 | + | |
| 60 | + A program that uses C makes them with C's pthread_create instead, | |
| 61 | + since C keeps things of each thread's own, errno and its buffers, | |
| 62 | + that a thread clone makes wouldn't have. The compiler's entry point | |
| 63 | + finds cCreate and cJoin by name and stores C's calls in them, as it | |
| 64 | + stores C's exit in Rt.cExit; so this module uses no C itself, and | |
| 65 | + serves both kinds of program. C's thread has a stack and a guard | |
| 66 | + page of C's; the mapping here holds only the handlers' stack and | |
| 67 | + the Thread. *) | |
| 60 | 68 | module Thread; |
| 61 | 69 | import Sys, Rt; |
| 62 | -link static; | |
| 63 | 70 | |
| 64 | 71 | const |
| 65 | 72 | Failed*: ERROR = "can't start a thread"; |
| 66 | - Stack = 1 shl 20; | |
| 67 | - Alt = 16384; (* the signal handlers' 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> | |
| 74 | 74 | <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> |
| 75 | 75 | <div class="file"><span>Hello.raz</span></div><pre class="code"><code><span class="kw">module</span> Hello; |
| 76 | 76 | <span class="kw">import</span> Out, Thread; |
| @@ -112,8 +112,7 @@ | ||
| 112 | 112 | <span class="kw">end</span>; |
| 113 | 113 | |
| 114 | 114 | <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> | |
| 117 | 116 | <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> |
| 118 | 117 | <h2>The same promise for your own modules</h2> |
| 119 | 118 | <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’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="static.html">← A program with no run time</a><a href="index.html">Contents</a><a class="next" href="lifecycle.html">Starting, ending, and signals →</a></div></article></div> |
| @@ -117,55 +117,61 @@ | ||
| 117 | 117 | 0 push rbp |
| 118 | 118 | 1 mov rbp, rsp |
| 119 | 119 | 4 sub rsp, 64 |
| 120 | - 11 mov [rbp - 48], rbx | |
| 120 | + 11 mov [rbp - 56], rbx | |
| 121 | 121 | 15 xor ebx, ebx |
| 122 | - 17 mov [rbp - 40], r12 | |
| 122 | + 17 mov [rbp - 48], r12 | |
| 123 | 123 | 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 | |
| 126 | 128 | 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 | |
| 136 | 141 | 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 | |
| 155 | 160 | 10 return sum; |
| 156 | - 146 mov rax, r12 | |
| 161 | + 175 mov rax, r12 | |
| 157 | 162 | 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 | |
| 162 | 168 | 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> | |
| 169 | 175 | <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> |
| 170 | 176 | <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> |
| 171 | 177 | <h2>Taking the checks out</h2> |
| @@ -197,48 +203,54 @@ | ||
| 197 | 203 | 0 push rbp |
| 198 | 204 | 1 mov rbp, rsp |
| 199 | 205 | 4 sub rsp, 64 |
| 200 | - 11 mov [rbp - 48], rbx | |
| 206 | + 11 mov [rbp - 56], rbx | |
| 201 | 207 | 15 xor ebx, ebx |
| 202 | - 17 mov [rbp - 40], r12 | |
| 208 | + 17 mov [rbp - 48], r12 | |
| 203 | 209 | 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 | |
| 206 | 214 | 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 | |
| 216 | 227 | 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 | |
| 231 | 242 | 10 return sum; |
| 232 | - 127 mov rax, r12 | |
| 243 | + 156 mov rax, r12 | |
| 233 | 244 | 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 | |
| 238 | 250 | <span class="cmd">$ raz -S Dot.raz | sed -n '/^Dot.dot:/,/^$/p' | wc -l</span> |
| 239 | -55 | |
| 251 | +61 | |
| 240 | 252 | <span class="cmd">$ raz -S Dot2.raz | sed -n '/^Dot2.dot:/,/^$/p' | wc -l</span> |
| 241 | -44</code></pre> | |
| 253 | +50</code></pre> | |
| 242 | 254 | <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> |
| 243 | 255 | <h2>Comparing with C</h2> |
| 244 | 256 | <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 @@ | ||
| 246 | 258 | <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> |
| 247 | 259 | <pre class="output term"><code><span class="cmd">$ raz -o Dot Dot.raz</span> |
| 248 | 260 | <span class="cmd">$ objdump -d -M intel --no-show-raw-insn Dot | sed -n '/<Dot.dot>:/,/ret/p' | head -12</span> |
| 249 | -0000000000403aa0 <Dot.dot>: | |
| 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 <Dot.dot>: | |
| 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 | 273 | <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’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="syscalls.html">← System calls</a><a href="index.html">Contents</a><a class="next" href="volatile.html">Volatile →</a></div></article></div> |
| 262 | 274 | </main> |
| 263 | 275 | <footer>raz is under the ISC license.</footer> |
| @@ -65,7 +65,7 @@ | ||
| 65 | 65 | <span class="cmd">$ ldd Bare</span> |
| 66 | 66 | not a dynamic executable |
| 67 | 67 | <span class="cmd">$ ls -l Bare | awk '{print $5, "bytes"}'</span> |
| 68 | -26096 bytes</code></pre> | |
| 68 | +26032 bytes</code></pre> | |
| 69 | 69 | <p>It is statically linked: no interpreter, no shared libraries, no <code>libc</code>. <code>ldd</code> says so.</p> |
| 70 | 70 | <h2>What it does when it runs</h2> |
| 71 | 71 | <p><code>strace</code> lists every system call a program makes. This is the whole of it:</p> |
| @@ -97,8 +97,8 @@ | ||
| 97 | 97 | 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 |
| 98 | 98 | <span class="cmd">$ ldd Cprint | sed 's/ (0x[0-9a-f]*)//'</span> |
| 99 | 99 | linux-vdso.so.1 |
| 100 | - libc.so.6 => /usr/lib/libc.so.6 | |
| 101 | - /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2</code></pre> | |
| 100 | + libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 | |
| 101 | + /lib64/ld-linux-x86-64.so.2</code></pre> | |
| 102 | 102 | <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> |
| 103 | 103 | <h2>Only what is reached</h2> |
| 104 | 104 | <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 @@ | ||
| 134 | 134 | <pre class="output term"><code><span class="cmd">$ raz Lean.raz</span> |
| 135 | 135 | <span class="cmd">$ raz Heavy.raz</span> |
| 136 | 136 | <span class="cmd">$ ls -l Lean Heavy | awk '{print $9, $5}'</span> |
| 137 | -Heavy 28304 | |
| 138 | -Lean 28296</code></pre> | |
| 137 | +Heavy 28208 | |
| 138 | +Lean 28200</code></pre> | |
| 139 | 139 | <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> |
| 140 | 140 | <h2>Stripping</h2> |
| 141 | 141 | <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> |
| 142 | 142 | <pre class="output term"><code><span class="cmd">$ raz -s -o Lean-stripped Lean.raz</span> |
| 143 | 143 | <span class="cmd">$ ls -l Lean Lean-stripped | awk '{print $9, $5}'</span> |
| 144 | -Lean 28296 | |
| 145 | -Lean-stripped 6720 | |
| 144 | +Lean 28200 | |
| 145 | +Lean-stripped 6688 | |
| 146 | 146 | <span class="cmd">$ ./Lean-stripped</span> |
| 147 | 147 | same program</code></pre> |
| 148 | 148 | <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’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="allocators.html">← Writing an allocator</a><a href="index.html">Contents</a><a class="next" href="linkstatic.html">Threads without C, and keeping the two apart →</a></div></article></div> |
| @@ -52,7 +52,7 @@ | ||
| 52 | 52 | Sys.raz |
| 53 | 53 | Thread.linux-x86_64.raz |
| 54 | 54 | <span class="cmd">$ cd $RAZLIB && 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 | |
| 56 | 56 | Sys.raz:0</code></pre> |
| 57 | 57 | <h2>A module of your own</h2> |
| 58 | 58 | <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> |
Binary file; the patch has it.
| @@ -0,0 +1 @@ | ||
| 1 | +6b7b935d159a56710d0ca942516be40d00d5858bc7e532d533d47e993ef3ee5d raz-0.12.0-linux-x86_64.tar.gz |
Binary file; the patch has it.
| @@ -0,0 +1 @@ | ||
| 1 | +5b7caa843dea6cc97f9164e3dacb08e513f78604abcee2652085f7202eb3616f raz-0.13.0-linux-x86_64.tar.gz |
| @@ -42,7 +42,7 @@ | ||
| 42 | 42 | <div class="facts"> |
| 43 | 43 | <span><b>~10,700</b> lines of compiler</span> |
| 44 | 44 | <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> | |
| 46 | 46 | <span><b>static</b> executables, no run time to install</span> |
| 47 | 47 | </div> |
| 48 | 48 | |
| @@ -85,6 +85,7 @@ | ||
| 85 | 85 | <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> |
| 86 | 86 | <li>Arrays, checked slices, pointers, records laid out as C lays them out, packed records, enumerations, sets, and variants with <code>match</code>.</li> |
| 87 | 87 | <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> | |
| 88 | 89 | <li>Every variable starts at zero. Behaviour is defined everywhere but through pointers.</li> |
| 89 | 90 | <li>No garbage collector: memory comes from an allocator the program chooses and passes.</li> |
| 90 | 91 | <li>Several results, <code>ERROR</code> values and <code>defer</code>, in place of exceptions.</li> |
| @@ -108,28 +109,28 @@ | ||
| 108 | 109 | <p>The language is defined by <a href="spec.html">the report</a>, which is |
| 109 | 110 | short on purpose: whatever it doesn’t allow, raz doesn’t have. |
| 110 | 111 | 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 | |
| 112 | 113 | <a href="raz-lib.pdf">the library</a>.</p> |
| 113 | 114 | |
| 114 | 115 | <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>, | |
| 116 | 117 | its library, <code>lib/</code>, which it finds beside itself, the formatter |
| 117 | 118 | and the documentation tool as sources in <code>tools/</code>, and the manual |
| 118 | 119 | pages. It needs nothing else installed.</p> |
| 119 | 120 | <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> | |
| 122 | 123 | </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 | |
| 125 | 126 | $ raz run Hello.raz</code></pre> |
| 126 | 127 | <p>The link works since raz finds <code>lib/</code> beside its real path. The |
| 127 | 128 | manual pages are in <code>man/</code>: <code>man ./man/raz-tutorial.7</code>. |
| 128 | 129 | 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 | |
| 131 | 132 | <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> | |
| 133 | 134 | |
| 134 | 135 | </main> |
| 135 | 136 | <footer>raz is under the ISC license.</footer> |
Binary file; the patch has it.
Binary file; the patch has it.
| @@ -38,7 +38,7 @@ | ||
| 38 | 38 | <p>raz is a small systems language, first made for x86-64 Linux. A raz program can lay a record over a device’s registers, read and write a device’s memory exactly as written, take part in lock-free algorithms, make system calls and call C libraries. It has no garbage collector and no hidden run time: what the program does is what its text says.</p> |
| 39 | 39 | <p>This is the first volume of the report: the language, what an implementation chooses, and the grammar. The second, <code>doc/raz-lib.md</code>, is the library, which sections 3 and 10 and appendix A name.</p> |
| 40 | 40 | <p>This report defines the language. It is short on purpose. Whatever it doesn’t allow, raz doesn’t have.</p> |
| 41 | -<p>This is version 2026-10.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> | |
| 42 | 42 | <p>The language is frozen, softly, from this version on: it changes only to mend a defect, and its library grows only by what breaks no program written for it. What anyone wishes it had waits on a list, for a later thaw.</p> |
| 43 | 43 | <h1 id="a-first-look">1. A first look</h1> |
| 44 | 44 | <pre><code>module Hello; |
| @@ -65,7 +65,18 @@ | ||
| 65 | 65 | <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> |
| 66 | 66 | <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> |
| 67 | 67 | <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> | |
| 69 | 80 | <p>Messages give the line and the column, counted in characters (section 10).</p> |
| 70 | 81 | <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> |
| 71 | 82 | <p><strong>Operators and delimiters:</strong></p> |
| @@ -495,7 +506,7 @@ | ||
| 495 | 506 | | rect(w, h): area := w * h; |
| 496 | 507 | | dot: area := 0.0; |
| 497 | 508 | 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> | |
| 499 | 510 | <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> |
| 500 | 511 | <pre><code>match op of |
| 501 | 512 | | sPlus, sMinus: kind := additive; |
| @@ -509,7 +520,14 @@ | ||
| 509 | 520 | | '0' .. '9': digit := true; |
| 510 | 521 | | else: |
| 511 | 522 | 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 | +| "add", "plus": op := add; | |
| 527 | +| "sub": 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> | |
| 513 | 531 | <h2 id="order-of-evaluation">7.5 Order of evaluation</h2> |
| 514 | 532 | <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> |
| 515 | 533 | <h2 id="volatile">7.6 Volatile</h2> |
| @@ -680,7 +698,8 @@ | ||
| 680 | 698 | | digit { hexDigit } "H" |
| 681 | 699 | | digit { digit } "." digit { digit } [ "E" [ "+" | "-" ] digit { digit } ] . |
| 682 | 700 | char = "'" ( character | escape ) "'" . |
| 683 | -string = '"' { character | escape | "\x" anyHex anyHex } '"' . | |
| 701 | +string = '"' { character | escape | "\x" anyHex anyHex } '"' | |
| 702 | + | "`" { rawChar } "`" . | |
| 684 | 703 | escape = "\n" | "\t" | "\r" | "\0" | "\\" | "\'" | '\"' |
| 685 | 704 | | "\u{" anyHex { anyHex } "}" . |
| 686 | 705 | anyHex = hexDigit | "a" .. "f" . |
| @@ -765,7 +784,7 @@ | ||
| 765 | 784 | Results = Type | "(" Type { "," Type } ")" . |
| 766 | 785 | ExternDeclaration = "extern" ( "proc" IdentDef Signature |
| 767 | 786 | | "var" IdentDef ":" 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> | |
| 769 | 788 | <h1 id="the-library-volume-2">The library, volume 2</h1> |
| 770 | 789 | <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> |
| 771 | 790 | <p>Modules written in raz, in the compiler’s <code>lib</code> directory:</p> |
| @@ -783,7 +802,7 @@ | ||
| 783 | 802 | </tr> |
| 784 | 803 | <tr> |
| 785 | 804 | <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> | |
| 787 | 806 | </tr> |
| 788 | 807 | <tr> |
| 789 | 808 | <td><code>Out</code></td> |
| @@ -799,7 +818,7 @@ | ||
| 799 | 818 | </tr> |
| 800 | 819 | <tr> |
| 801 | 820 | <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> | |
| 803 | 822 | </tr> |
| 804 | 823 | <tr> |
| 805 | 824 | <td><code>Mem</code></td> |
| @@ -815,7 +834,7 @@ | ||
| 815 | 834 | </tr> |
| 816 | 835 | <tr> |
| 817 | 836 | <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> | |
| 819 | 838 | </tr> |
| 820 | 839 | <tr> |
| 821 | 840 | <td><code>Bytes</code></td> |
| @@ -902,7 +921,7 @@ | ||
| 902 | 921 | proc join*(t: ^Thread); (* waits for t to end, then frees its stack *) |
| 903 | 922 | proc lock*(m: ^Mutex); |
| 904 | 923 | 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> | |
| 906 | 925 | <pre><code>module Out; |
| 907 | 926 | proc str*(s: []U8); |
| 908 | 927 | proc char*(c: U8); |
| @@ -958,6 +977,7 @@ | ||
| 958 | 977 | tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *) |
| 959 | 978 | end; |
| 960 | 979 | Status* = (running, exited, killed, stopped); |
| 980 | + Peer* = record ... end; (* whom a datagram came from *) | |
| 961 | 981 | |
| 962 | 982 | proc write*(fd: INT; s: []U8): INT; (* the bytes written *) |
| 963 | 983 | proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) |
| @@ -1024,7 +1044,17 @@ | ||
| 1024 | 1044 | proc listen*(path: []U8): INT; (* a Unix socket made at path, taking connections *) |
| 1025 | 1045 | proc listenTcp*(addr: []U8; port: INT): INT; |
| 1026 | 1046 | 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> | |
| 1028 | 1058 | <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> |
| 1029 | 1059 | <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> |
| 1030 | 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> |
| @@ -1034,20 +1064,40 @@ | ||
| 1034 | 1064 | <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> |
| 1035 | 1065 | <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> |
| 1036 | 1066 | <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> | |
| 1038 | 1070 | <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> |
| 1039 | 1071 | <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> |
| 1040 | 1072 | <p><strong>Strings, buffers and collections.</strong></p> |
| 1041 | 1073 | <pre><code>module Str; |
| 1042 | 1074 | proc cmp*(a, b: []U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *) |
| 1043 | 1075 | proc eq*(a, b: []U8): BOOL; |
| 1076 | +proc eqFold*(a, b: []U8): BOOL; (* eq, an ASCII letter's capital and small the same *) | |
| 1044 | 1077 | proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); (* s's bytes in memory from a; false if a has none *) |
| 1045 | 1078 | proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *) |
| 1046 | 1079 | 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 *) | |
| 1047 | 1081 | 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't spaces, and what follows it *) | |
| 1088 | +proc lower*(s: []U8); (* s's ASCII capitals made small, in place *) | |
| 1089 | +proc upper*(s: []U8); (* s'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 *) | |
| 1048 | 1092 | proc encode*(c: I32): ([4]U8, INT); (* c in UTF-8: its bytes, and how many *) |
| 1049 | 1093 | 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 *) | |
| 1050 | 1098 | proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *) |
| 1099 | +proc toHex*(s: []U8): (U64, BOOL); (* s, in hexadecimal; false if it isn't a number a U64 holds *) | |
| 1100 | +proc fromInt*(n: I64): ([20]U8, INT); (* n in decimal: its bytes, and how many *) | |
| 1051 | 1101 | proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *) |
| 1052 | 1102 | proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *) |
| 1053 | 1103 | proc putLE*(s: []U8; v: U64); (* v into s, the lowest byte first *) |
| @@ -1117,12 +1167,30 @@ | ||
| 1117 | 1167 | proc atan2*(y, x: F64): F64; (* the angle of (x, y) from the x axis, from -pi to pi *) |
| 1118 | 1168 | proc bits*(x: F64): U64; (* the bits of x, as IEEE 754 lays them out *) |
| 1119 | 1169 | 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't one *)</code></pre> | |
| 1121 | 1172 | <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 >= 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, "\n"); (* the first line, and the others *) | |
| 1182 | +key, val, ok := Str.cut(line, "="); | |
| 1183 | +w, rest := Str.field(line); (* the words of a line *) | |
| 1184 | +while LEN(w) > 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> | |
| 1123 | 1190 | <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> |
| 1124 | 1191 | <pre><code>len := U32(Str.le(header[4..8])); |
| 1125 | 1192 | 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> | |
| 1126 | 1194 | <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> |
| 1127 | 1195 | <pre><code>i, k, v := Names.next(ADR(names), 0); |
| 1128 | 1196 | while i >= 0 do |
| @@ -70,10 +70,10 @@ | ||
| 70 | 70 | <span class="cmd">$ raz run Quiet.raz</span> |
| 71 | 71 | hello, again |
| 72 | 72 | <span class="cmd">$ ls -l Quiet | awk '{print $5, "bytes"}'</span> |
| 73 | -28304 bytes</code></pre> | |
| 73 | +28208 bytes</code></pre> | |
| 74 | 74 | <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> |
| 75 | 75 | <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> | |
| 77 | 77 | <div class="file"><span>Numbers.raz</span></div><pre class="code"><code><span class="kw">module</span> Numbers; |
| 78 | 78 | <span class="kw">import</span> Out; |
| 79 | 79 |
| @@ -35,7 +35,7 @@ | ||
| 35 | 35 | <li><a href="procedures.html">Procedures and several results</a> <span>Value and var parameters, several results, forward, and procedures as values.</span></li> |
| 36 | 36 | </ol> |
| 37 | 37 | <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> | |
| 39 | 39 | <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> |
| 40 | 40 | <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> |
| 41 | 41 | <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> |
| @@ -159,7 +159,7 @@ | ||
| 159 | 159 | <h2>Buffers that grow</h2> |
| 160 | 160 | <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> |
| 161 | 161 | <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; | |
| 163 | 163 | |
| 164 | 164 | <span class="kw">proc</span> main*; |
| 165 | 165 | <span class="kw">var</span> b: Buf.Buf; i: <span class="ty">INT</span>; |
| @@ -173,12 +173,21 @@ | ||
| 173 | 173 | <span class="kw">end</span>; |
| 174 | 174 | Out.str(Buf.bytes(<span class="ty">ADR</span>(b))); Out.ln; |
| 175 | 175 | Out.int(b.len); Out.str(<span class="string">" bytes\n"</span>); |
| 176 | + Buf.reset(<span class="ty">ADR</span>(b)); | |
| 177 | + Str.join(<span class="ty">ADR</span>(b), [<span class="string">"red"</span>, <span class="string">"green"</span>, <span class="string">"blue"</span>], <span class="string">", "</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">"a-b-c"</span>, <span class="string">"-"</span>, <span class="string">" and "</span>); | |
| 181 | + Out.str(Buf.bytes(<span class="ty">ADR</span>(b))); Out.ln; | |
| 176 | 182 | <span class="kw">end</span>; |
| 177 | 183 | |
| 178 | 184 | <span class="kw">end</span> Build.</code></pre> |
| 179 | 185 | <pre class="output"><code>item 1,item 2,item 3,item 4,item 5, |
| 180 | 186 | 35 bytes |
| 187 | +red, green, blue | |
| 188 | +a and b and c | |
| 181 | 189 | </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> | |
| 182 | 191 | <h2>Pages</h2> |
| 183 | 192 | <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’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="variants.html">← Enumerations, sets, variants and match</a><a href="index.html">Contents</a><a class="next" href="errors.html">Errors →</a></div></article></div> |
| 184 | 193 | </main> |
| @@ -51,7 +51,7 @@ | ||
| 51 | 51 | <span class="kw">end</span>; |
| 52 | 52 | |
| 53 | 53 | <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>; | |
| 55 | 55 | <span class="kw">begin</span> |
| 56 | 56 | <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>; |
| 57 | 57 | Out.int(sum(a)); Out.ln; <span class="comment">(* the array, as a slice *)</span> |
| @@ -60,9 +60,15 @@ | ||
| 60 | 60 | Out.str(s[<span class="number">7</span>..<span class="ty">LEN</span>(s)]); Out.ln; |
| 61 | 61 | <span class="kw">if</span> s[<span class="number">0</span>..<span class="number">5</span>] = <span class="string">"hello"</span> <span class="kw">then</span> Out.str(<span class="string">"it says hello\n"</span>); <span class="kw">end</span>; |
| 62 | 62 | Out.int(Str.find(s, <span class="string">"world"</span>)); Out.ln; |
| 63 | + _, val, ok := Str.cut(<span class="string">" size = 42 "</span>, <span class="string">"="</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>; | |
| 63 | 65 | name := <span class="string">"raz"</span>; <span class="comment">(* a copy, the program's own *)</span> |
| 64 | 66 | name[<span class="number">0</span>] := <span class="string">'R'</span>; |
| 65 | 67 | Out.str(name[<span class="number">0</span>..<span class="number">3</span>]); Out.ln; |
| 68 | + Out.str(<span class="string">` | |
| 69 | + C:\tmp\ "as it stands" | |
| 70 | + over lines | |
| 71 | + `</span>); | |
| 66 | 72 | <span class="kw">end</span>; |
| 67 | 73 | |
| 68 | 74 | <span class="kw">end</span> Slices.</code></pre> |
| @@ -71,7 +77,10 @@ | ||
| 71 | 77 | world |
| 72 | 78 | it says hello |
| 73 | 79 | 7 |
| 80 | +42 | |
| 74 | 81 | Raz |
| 82 | +C:\tmp\ "as it stands" | |
| 83 | + over lines | |
| 75 | 84 | </code></pre> |
| 76 | 85 | <h2>Arrays</h2> |
| 77 | 86 | <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 @@ | ||
| 152 | 161 | <pre class="output"><code>1 1 2 3 4 5 |
| 153 | 162 | </code></pre> |
| 154 | 163 | <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> | |
| 156 | 165 | <div class="file"><span>Words.raz</span></div><pre class="code"><code><span class="kw">module</span> Words; |
| 157 | 166 | <span class="kw">import</span> Out, Str; |
| 158 | 167 | |
| @@ -164,8 +173,12 @@ | ||
| 164 | 173 | <span class="kw">if</span> s[<span class="number">0</span>..<span class="number">5</span>] = <span class="string">"hello"</span> <span class="kw">then</span> Out.str(<span class="string">"it says hello\n"</span>); <span class="kw">end</span>; |
| 165 | 174 | Out.int(Str.find(s, <span class="string">"world"</span>)); Out.ln; <span class="comment">(* 7 *)</span> |
| 166 | 175 | Out.int(Str.find(s, <span class="string">"xyz"</span>)); Out.ln; <span class="comment">(* -1 *)</span> |
| 176 | + Out.int(Str.findLast(<span class="string">"abcabc"</span>, <span class="string">"bc"</span>)); Out.ln; <span class="comment">(* 4 *)</span> | |
| 167 | 177 | <span class="kw">if</span> Str.hasPrefix(s, <span class="string">"hell"</span>) <span class="kw">then</span> Out.str(<span class="string">"prefix\n"</span>); <span class="kw">end</span>; |
| 178 | + <span class="kw">if</span> Str.hasSuffix(s, <span class="string">"world"</span>) <span class="kw">then</span> Out.str(<span class="string">"suffix\n"</span>); <span class="kw">end</span>; | |
| 168 | 179 | Out.int(Str.cmp(<span class="string">"apple"</span>, <span class="string">"banana"</span>)); Out.ln; <span class="comment">(* -1 *)</span> |
| 180 | + <span class="kw">if</span> Str.eqFold(<span class="string">"RAZ"</span>, <span class="string">"raz"</span>) <span class="kw">then</span> Out.str(<span class="string">"the same, but for case\n"</span>); <span class="kw">end</span>; | |
| 181 | + Out.str(<span class="string">"["</span>); Out.str(Str.trim(<span class="string">" padded \t\n"</span>)); Out.str(<span class="string">"]\n"</span>); | |
| 169 | 182 | |
| 170 | 183 | n, ok := Str.toInt(<span class="string">"-1234"</span>); |
| 171 | 184 | <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 @@ | ||
| 175 | 188 | name := <span class="string">"raz"</span>; <span class="comment">(* a copy, the program's own *)</span> |
| 176 | 189 | name[<span class="number">0</span>] := <span class="string">'R'</span>; |
| 177 | 190 | 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; | |
| 178 | 193 | <span class="kw">end</span>; |
| 179 | 194 | |
| 180 | 195 | <span class="kw">end</span> Words.</code></pre> |
| @@ -182,48 +197,119 @@ | ||
| 182 | 197 | it says hello |
| 183 | 198 | 7 |
| 184 | 199 | -1 |
| 200 | +4 | |
| 185 | 201 | prefix |
| 202 | +suffix | |
| 186 | 203 | -1 |
| 204 | +the same, but for case | |
| 205 | +[padded] | |
| 187 | 206 | -1233 |
| 188 | 207 | not a number |
| 189 | 208 | Raz |
| 209 | +RAZ | |
| 190 | 210 | </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; | |
| 194 | 214 | <span class="kw">import</span> Out, Str; |
| 215 | + | |
| 216 | +<span class="kw">const</span> Text = <span class="string">"name = raz\nkind = language\nyear = 2026\n"</span>; | |
| 195 | 217 | |
| 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>; | |
| 199 | 220 | <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 < <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">"\n"</span>); | |
| 223 | + <span class="kw">while</span> more <span class="kw">do</span> | |
| 224 | + key, val, ok := Str.cut(line, <span class="string">"="</span>); | |
| 225 | + <span class="kw">if</span> ok <span class="kw">then</span> | |
| 226 | + Out.str(Str.trim(key)); Out.str(<span class="string">" is "</span>); Out.str(Str.trim(val)); Out.ln; | |
| 227 | + <span class="kw">end</span>; | |
| 228 | + line, rest, more := Str.cut(rest, <span class="string">"\n"</span>); | |
| 229 | + <span class="kw">end</span>; | |
| 230 | + | |
| 231 | + <span class="comment">(* word by word *)</span> | |
| 232 | + w, rest := Str.field(<span class="string">" the quick brown fox "</span>); | |
| 233 | + <span class="kw">while</span> <span class="ty">LEN</span>(w) > <span class="number">0</span> <span class="kw">do</span> | |
| 234 | + Out.str(<span class="string">"<"</span>); Out.str(w); Out.str(<span class="string">">"</span>); | |
| 235 | + w, rest := Str.field(rest); | |
| 206 | 236 | <span class="kw">end</span>; |
| 207 | - <span class="kw">return</span> n; | |
| 237 | + Out.ln; | |
| 208 | 238 | <span class="kw">end</span>; |
| 209 | 239 | |
| 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 | +<the><quick><brown><fox> | |
| 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\"notes".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 | + | |
| 210 | 263 | <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\"notes".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>; | |
| 212 | 285 | <span class="kw">begin</span> |
| 213 | 286 | s := <span class="string">"caf\u{E9} \u{3BB}"</span>; |
| 214 | 287 | Out.int(<span class="ty">LEN</span>(s)); Out.str(<span class="string">" bytes, "</span>); |
| 215 | - Out.int(chars(s)); Out.str(<span class="string">" characters\n"</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">" took "</span>); Out.int(size); Out.str(<span class="string">" bytes\n"</span>); | |
| 288 | + Out.int(Str.chars(s)); Out.str(<span class="string">" characters\n"</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 >= <span class="number">0</span> <span class="kw">do</span> | |
| 293 | + Out.int(<span class="ty">I64</span>(c)); Out.str(<span class="string">" "</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">" is last, in "</span>); Out.int(size); Out.str(<span class="string">" bytes\n"</span>); | |
| 218 | 300 | bytes, k := Str.encode(<span class="number">955</span>); |
| 219 | 301 | 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">"cut in the middle of a character\n"</span>); <span class="kw">end</span>; | |
| 220 | 303 | <span class="kw">end</span>; |
| 221 | 304 | |
| 222 | 305 | <span class="kw">end</span> Runes.</code></pre> |
| 223 | 306 | <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 | |
| 225 | 309 | λ |
| 310 | +cut in the middle of a character | |
| 226 | 311 | </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> | |
| 227 | 313 | <h2>Loops over lengths</h2> |
| 228 | 314 | <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> |
| 229 | 315 | <div class="file"><span>Loops.raz</span></div><pre class="code"><code><span class="kw">module</span> Loops; |
| @@ -70,6 +70,15 @@ | ||
| 70 | 70 | <span class="kw">end</span>; |
| 71 | 71 | <span class="kw">end</span>; |
| 72 | 72 | |
| 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">"red"</span>, <span class="string">"rose"</span>: <span class="kw">return</span> red; | |
| 77 | + | <span class="string">"green"</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 | + | |
| 73 | 82 | <span class="kw">proc</span> main*; |
| 74 | 83 | <span class="kw">var</span> shapes: [<span class="number">3</span>]Shape; i: <span class="ty">INT</span>; warm: Colours; c: Colour; |
| 75 | 84 | <span class="kw">begin</span> |
| @@ -83,6 +92,7 @@ | ||
| 83 | 92 | <span class="kw">for</span> c := red <span class="kw">to</span> blue <span class="kw">do</span> |
| 84 | 93 | <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>; |
| 85 | 94 | <span class="kw">end</span>; |
| 95 | + Out.int(<span class="ty">I64</span>(colour(<span class="string">"rose"</span>))); Out.int(<span class="ty">I64</span>(colour(<span class="string">"green"</span>))); Out.ln; | |
| 86 | 96 | <span class="kw">end</span>; |
| 87 | 97 | |
| 88 | 98 | <span class="kw">end</span> Shapes.</code></pre> |
| @@ -92,6 +102,7 @@ | ||
| 92 | 102 | digit letter other |
| 93 | 103 | 0 |
| 94 | 104 | 1 |
| 105 | +01 | |
| 95 | 106 | </code></pre> |
| 96 | 107 | <h2>Enumerations</h2> |
| 97 | 108 | <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 @@ | ||
| 261 | 272 | is space |
| 262 | 273 | _ is letter |
| 263 | 274 | y is letter |
| 264 | -</code></pre><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="records.html">← Records and pointers</a><a href="index.html">Contents</a><a class="next" href="memory.html">Memory and allocators →</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">"add"</span>, <span class="string">"plus"</span>, <span class="string">"+"</span>: <span class="kw">return</span> add; | |
| 287 | + | <span class="string">"sub"</span>, <span class="string">"minus"</span>, <span class="string">"-"</span>: <span class="kw">return</span> sub; | |
| 288 | + | <span class="string">"show"</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">"add"</span>, <span class="string">"plus"</span>, <span class="string">"sub"</span>, <span class="string">"show"</span>, <span class="string">"times"</span>, <span class="string">"-"</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">"?\n"</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’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="records.html">← Records and pointers</a><a href="index.html">Contents</a><a class="next" href="memory.html">Memory and allocators →</a></div></article></div> | |
| 265 | 314 | </main> |
| 266 | 315 | <footer>raz is under the ISC license.</footer> |
| 267 | 316 | </body> |