raz / commit bf6fdcb0

commit
bf6fdcb0e940630ed44d0f38d408c6de7f39ba93
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
ddd82658
site: the second tour is Close to the Metal; integers get a lesson

Beyond Go read as if it were about the Go language. The integer types
move out of Values and conversions into a lesson of their own: sizes,
literals, wrapping, widths and sign, division, shifts and bits.

patch · log

66 files changed, +2274 -1744

site/build.py modified +2 -2

@@ -42,7 +42,7 @@
4242 # folder in content/, folder in public/, name, the sentence under the name
4343 TOURS = [
4444 ("tour", "tour", "Raz, Set, Go", "tour-intro.html"),
45- ("advanced", "advanced", "Beyond Go", "advanced-intro.html"),
45+ ("advanced", "advanced", "Close to the Metal", "advanced-intro.html"),
4646 ]
4747
4848 KEYWORDS = set("""and begin break by code const defer div do else elsif end
@@ -216,7 +216,7 @@
216216 for key, href, label in [
217217 ("home", "index.html", "raz"),
218218 ("tour", "tour/index.html", "Raz, Set, Go"),
219- ("advanced", "advanced/index.html", "Beyond Go"),
219+ ("advanced", "advanced/index.html", "Close to the Metal"),
220220 ("spec", "spec.html", "Report"),
221221 ("get", "index.html#download", "Download")])
222222 return """<!doctype html>

site/content/advanced-intro.html (history) modified +1 -1

@@ -1,4 +1,4 @@
1-<h1>Beyond Go</h1>
1+<h1>Close to the Metal</h1>
22 <p>The second tour, for the part of raz that sits close to the machine. It
33 assumes you have taken <a href="../tour/index.html">Raz, Set, Go</a> or know
44 Pascal or C well enough not to need it.</p>

site/content/home.html (history) modified +3 -3

@@ -4,7 +4,7 @@
44 <em>Raz</em> is Polish for <em>one</em>, as in counting.</p>
55 <div class="cta">
66 <a class="btn primary" href="tour/index.html">Take the tour: Raz, Set, Go</a>
7- <a class="btn" href="advanced/index.html">Beyond Go</a>
7+ <a class="btn" href="advanced/index.html">Close to the Metal</a>
88 <a class="btn" href="#download">Download</a>
99 <a class="btn" href="spec.html">Read the report</a>
1010 </div>
@@ -53,8 +53,8 @@
5353
5454 <h2>Learn it</h2>
5555 <p><a href="tour/index.html"><strong>Raz, Set, Go</strong></a> is a tour of the
56-language in 14 lessons, each with programs you can run. <a
57-href="advanced/index.html"><strong>Beyond Go</strong></a> is the second tour:
56+language in 15 lessons, each with programs you can run. <a
57+href="advanced/index.html"><strong>Close to the Metal</strong></a> is the second tour:
5858 the library, programs with no run time, procedures in assembly, system calls,
5959 volatile accesses, atomics and lock-free code, in 18 more.</p>
6060

site/content/tour-intro.html (history) modified +1 -1

@@ -6,4 +6,4 @@
66 first steps; <strong>Set</strong>, how data is laid out; and
77 <strong>Go</strong>, making programs that run, fail and finish well. To follow
88 along, <a href="../index.html#download">get raz</a> and run any program with
9-<code>raz run Name.raz</code>. When you finish, <a href="../advanced/index.html">Beyond Go</a> goes down to the machine.</p>
9+<code>raz run Name.raz</code>. When you finish, <a href="../advanced/index.html">Close to the Metal</a> goes down to the machine.</p>

site/content/tour/03-integers.txt (history) added +273 -0

@@ -0,0 +1,273 @@
1+title: Integers
2+part: Raz
3+summary: The ten integer types, literals, wrapping, signed and unsigned, division, shifts and bits.
4+---
5+raz has ten integer types, and a program that works with bytes, addresses, hashes and file offsets uses most of them. Each is a different type, so each has rules, and the rules are the same everywhere: that is the point of having them.
6+
7+| Type | Size | Values |
8+|---|---|---|
9+| `I8` `I16` `I32` `I64` | 1, 2, 4, 8 bytes | signed, two's complement |
10+| `U8` `U16` `U32` `U64` | 1, 2, 4, 8 bytes | unsigned |
11+| `INT` | an address's: 8 on x86-64 | signed, as big as an address |
12+| `UINT` | an address's | unsigned, as big as an address |
13+
14+`SIZE(T)` says how many bytes a type takes, and an unsigned type's largest value is what you get by taking one from zero. This program asks each type.
15+
16+```raz Family run
17+module Family;
18+import Out;
19+
20+proc main*;
21+var a: U8; b: U16; c: U32; d: U64;
22+begin
23+ a := 0; a := a - 1;
24+ b := 0; b := b - 1;
25+ c := 0; c := c - 1;
26+ d := 0; d := d - 1;
27+ Out.str("U8 "); Out.int(SIZE(U8)); Out.str(" byte, largest "); Out.int(I64(a)); Out.ln;
28+ Out.str("U16 "); Out.int(SIZE(U16)); Out.str(" bytes, largest "); Out.int(I64(b)); Out.ln;
29+ Out.str("U32 "); Out.int(SIZE(U32)); Out.str(" bytes, largest "); Out.int(I64(c)); Out.ln;
30+ Out.str("U64 "); Out.int(SIZE(U64)); Out.str(" bytes, largest 0x"); Out.hex(d, 16); Out.ln;
31+ Out.str("INT "); Out.int(SIZE(INT)); Out.str(" bytes, as big as an address\n");
32+ Out.str("I8 "); Out.int(SIZE(I8)); Out.str(" byte, from -128 to 127\n");
33+end;
34+
35+end Family.
36+```
37+
38+## INT is the everyday one
39+
40+Lengths, indexes, counts and sizes are `INT`s. `LEN(s)` of a slice is an `INT`, and so is the loop variable that walks it. It is as big as an address, so it is the integer a pointer converts to, and the word the atomics work on. Reach for `INT` unless you have a reason: a byte is a `U8`, a character an `I32`, a file's size an `I64`, a hash a `U64`.
41+
42+`INT` and `I64` are two types even where they have the same size. An `INT` goes silently into an `I64` (and a `UINT` into a `U64`), which is why `Out.int(n)` and `Fmt.int(n)` take an `I64` and accept any `INT`. Nothing else converts unasked: any other integer is converted where it is written, as `Out.int(I64(b))`.
43+
44+## A number written in the program
45+
46+A number in the program text is untyped until it meets a type, and then it must fit. `0FFH` is hexadecimal (it starts with a digit and ends in `H`), a character in quotes is its code point, and `'\u{E9}'` is a code point written by number. A hexadecimal literal too big for an `I64` is a `U64`.
47+
48+```raz Literals run
49+module Literals;
50+import Out;
51+
52+proc main*;
53+var b: U8; w: I16; big: U64;
54+begin
55+ b := 0FFH; (* fits a U8 *)
56+ w := -300;
57+ big := 0FFFFFFFFFFFFFFFFH; (* above the largest I64: a U64 *)
58+ Out.int(I64(b)); Out.char(' ');
59+ Out.int(I64(w)); Out.char(' ');
60+ Out.int(I64('a')); Out.char(' ');
61+ Out.int(I64('\u{E9}')); Out.char(' ');
62+ Out.hex(big, 16); Out.ln;
63+end;
64+
65+end Literals.
66+```
67+
68+A constant that doesn't fit is an error, not a quiet cut. A conversion of a constant, which says you mean it, is allowed: `U8(300)` is 44.
69+
70+```raz Fits
71+module Fits;
72+
73+proc main*;
74+var b: U8; c: U8;
75+begin
76+ b := 300;
77+ c := U8(300);
78+end;
79+
80+end Fits.
81+```
82+
83+```sh
84+$ raz Fits.raz
85+```
86+
87+## Wrapping
88+
89+Arithmetic wraps at the type's width and never stops the program. `250 + 10` in a `U8` is 4; one more than the largest `I8` is the smallest. A unary minus wraps too: for an unsigned `x`, `-x` is `0 - x`. This is not undefined behaviour, as in C; it is what the report says, in every program, on every target, so a hash function can rely on it.
90+
91+```raz Wrap run
92+module Wrap;
93+import Out;
94+
95+proc main*;
96+var a: U8; b: I8; c: U32; d: U8;
97+begin
98+ a := 250; a := a + 10;
99+ Out.int(I64(a)); Out.ln; (* 4 *)
100+ b := 127; b := b + 1;
101+ Out.int(I64(b)); Out.ln; (* -128 *)
102+ c := 0; c := c - 1;
103+ Out.int(I64(c)); Out.ln; (* 4294967295 *)
104+ d := 1; d := -d;
105+ Out.int(I64(d)); Out.ln; (* 255: 0 - 1 *)
106+end;
107+
108+end Wrap.
109+```
110+
111+Because wrapping is defined, a hash that depends on it is portable. Here is the FNV-1a hash that `Str.hash` uses, written in a few lines; the multiplication overflows a `U64` on every byte, on purpose.
112+
113+```raz Fnv run
114+module Fnv;
115+import Out, Str;
116+
117+const Offset = 0CBF29CE484222325H;
118+const Prime = 100000001B3H;
119+
120+(* FNV-1a: xor each byte in, then multiply, all modulo 2^64 *)
121+proc fnv(s: []U8): U64;
122+var h: U64; i: INT;
123+begin
124+ h := Offset;
125+ for i := 0 to LEN(s) - 1 do
126+ h := (h xor U64(s[i])) * Prime;
127+ end;
128+ return h;
129+end;
130+
131+proc main*;
132+begin
133+ Out.hex(fnv("raz"), 16); Out.ln;
134+ Out.hex(Str.hash("raz"), 16); Out.ln;
135+ if fnv("raz") = Str.hash("raz") then Out.str("the same\n"); end;
136+end;
137+
138+end Fnv.
139+```
140+
141+## Widening, narrowing, and sign
142+
143+A conversion to a narrower integer cuts the value to the new width. To a wider one it extends by the sign for a signed source and with zeros for an unsigned one. That difference is the whole story of "why did my byte come out negative".
144+
145+```raz Widths run
146+module Widths;
147+import Out;
148+
149+proc main*;
150+var s: I8; u: U8;
151+begin
152+ s := -1;
153+ u := 255; (* the same eight bits *)
154+ Out.int(I64(s)); Out.char(' '); (* sign-extended: -1 *)
155+ Out.int(I64(u)); Out.char(' '); (* zero-extended: 255 *)
156+ Out.int(I64(U8(s))); Out.char(' '); (* the bits, read as unsigned: 255 *)
157+ Out.int(I64(I8(u))); Out.char(' '); (* and back: -1 *)
158+ Out.int(I64(U8(300 + INT(u)))); Out.ln; (* 555 cut to 8 bits: 43 *)
159+end;
160+
161+end Widths.
162+```
163+
164+Relations compare as signed or unsigned according to the type, so the same bits order differently:
165+
166+```raz Order run
167+module Order;
168+import Out;
169+
170+proc main*;
171+var s: I8; u: U8;
172+begin
173+ s := -1;
174+ u := U8(s); (* 255 *)
175+ if s < 0 then Out.str("s is below zero\n"); end;
176+ if u > 100 then Out.str("u is above 100\n"); end;
177+ if U8(s) = u then Out.str("but they are the same bits\n"); end;
178+end;
179+
180+end Order.
181+```
182+
183+## Division
184+
185+`div` rounds toward zero, and `mod` takes the sign of the dividend, as in C: `-7 div 2` is -3 and `-7 mod 2` is -1. Dividing by zero stops the program, and so does dividing the smallest value of a signed type by -1, whose result can't be represented. Neither is a silent wrong answer.
186+
187+```raz Divide run
188+module Divide;
189+import Out;
190+
191+proc main*;
192+var n, d: INT;
193+begin
194+ n := -7; d := 2;
195+ Out.int(n div d); Out.char(' '); Out.int(n mod d); Out.char(' '); (* -3 -1 *)
196+ n := 7; d := -2;
197+ Out.int(n div d); Out.char(' '); Out.int(n mod d); Out.ln; (* -3 1 *)
198+end;
199+
200+end Divide.
201+```
202+
203+## Shifts and bits
204+
205+`&` (and), `|` (or), `xor` and `~` (not) work on every bit. `x shl n` and `x shr n` shift by `n` modulo the width of `x`'s type: for a `U8`, `x shl 9` is `x shl 1`. `shr` brings in copies of the sign bit for signed types, so it divides by two keeping the sign, and zeros for unsigned ones. The result has `x`'s type, cut to its width: in a `U8`, `0C0H shl 1` is `80H`.
206+
207+```raz Shifts run
208+module Shifts;
209+import Out;
210+
211+proc main*;
212+var u: U8; s: I8;
213+begin
214+ u := 0C0H;
215+ Out.hex(U64(u shl 1), 2); Out.char(' '); (* 80: cut to 8 bits *)
216+ Out.hex(U64(u shr 4), 2); Out.char(' '); (* 0C: zeros come in *)
217+ Out.hex(U64(u shl 9), 2); Out.ln; (* 9 mod 8 = 1: 80 *)
218+ s := -64;
219+ Out.int(I64(s shr 2)); Out.char(' '); (* -16: the sign comes in *)
220+ Out.int(I64(s shr 7)); Out.ln; (* -1 *)
221+end;
222+
223+end Shifts.
224+```
225+
226+A packed value is the usual use of all this: several small numbers in one word. This puts a colour in the bytes of a `U32` and takes it out again.
227+
228+```raz Colour run
229+module Colour;
230+import Out;
231+
232+(* red, green and blue, each 0 to 255, in the low 24 bits *)
233+proc pack(r, g, b: U8): U32;
234+begin
235+ return (U32(r) shl 16) | (U32(g) shl 8) | U32(b);
236+end;
237+
238+proc red(c: U32): U8;
239+begin
240+ return U8(c shr 16);
241+end;
242+
243+proc green(c: U32): U8;
244+begin
245+ return U8((c shr 8) & 0FFH);
246+end;
247+
248+proc blue(c: U32): U8;
249+begin
250+ return U8(c & 0FFH);
251+end;
252+
253+proc main*;
254+var c: U32;
255+begin
256+ c := pack(0FFH, 80H, 10H);
257+ Out.str("0x"); Out.hex(U64(c), 6); Out.ln;
258+ Out.int(I64(red(c))); Out.char(' ');
259+ Out.int(I64(green(c))); Out.char(' ');
260+ Out.int(I64(blue(c))); Out.ln;
261+end;
262+
263+end Colour.
264+```
265+
266+## Which one to use
267+
268+- **`INT`** for lengths, indexes, counts, loop variables, and anything you'd write `int` for in C.
269+- **`U8`** for bytes, and for a string's elements (`[]U8`). **`I32`** for a character, a code point.
270+- **`U32`, `U64`** for hashes, bit sets, masks and addresses you do arithmetic on.
271+- **`I64`** for what must not shrink on a smaller target: a file's size or position, a time in nanoseconds, a number read from text.
272+- **`I8`, `I16`, `U16`** when a format or a layout says so, in a record or an array, to save space or match a protocol. Convert at the edge and compute in `INT`.
273+- **`UINT`** rarely: for a count that must use the whole address range, or an address.

site/content/tour/03-values.txt deleted +0 -110

@@ -1,110 +0,0 @@
1-title: Values and conversions
2-part: Raz
3-summary: Sized integers that wrap, reals, bits, and nothing converts unasked.
4----
5-```raz Values run
6-```
7-
8-## Integers
9-
10-The integers are `I8`, `I16`, `I32`, `I64` and `U8`, `U16`, `U32`, `U64`, and `INT` and `UINT`, as big as an address. `INT` is the everyday one: lengths, indexes and counts are `INT`s.
11-
12-Arithmetic wraps at the type's width and never stops the program, except dividing by zero. That is why `250 + 10` in a `U8` is 4. `div` divides toward zero, and `mod` has the sign of its left side.
13-
14-```raz Wrap run
15-module Wrap;
16-import Out;
17-
18-proc main*;
19-var a: U8; b: I8; c: U32; d: INT;
20-begin
21- a := 250; a := a + 10;
22- Out.int(I64(a)); Out.ln; (* 4 *)
23- b := 127; b := b + 1;
24- Out.int(I64(b)); Out.ln; (* -128 *)
25- c := 0; c := c - 1;
26- Out.int(I64(c)); Out.ln; (* 4294967295 *)
27- d := -7;
28- Out.int(d div 2); Out.str(" "); (* -3 *)
29- Out.int(d mod 2); Out.ln; (* -1 *)
30-end;
31-
32-end Wrap.
33-```
34-
35-## Conversions are written
36-
37-Two types never mix: with `i: I64` and a slice `s`, `i < LEN(s)` is an error. A type's name converts: `F64(n)`, `I64(b)`, `U8(x)`. Converting to a narrower integer cuts it; to a wider one extends it by its sign (signed) or with zeros (unsigned). The one silent conversion between numbers is an `INT` into an `I64`, which loses nothing.
38-
39-A number written in the program is untyped until it meets a type; `0FFH` is hexadecimal, and `1.5` is a real, which an integer never becomes without a conversion.
40-
41-```raz Mix
42-module Mix;
43-import Out;
44-
45-proc main*;
46-var i: I64; s: []U8;
47-begin
48- s := "four";
49- i := 3;
50- if i < LEN(s) then Out.str("smaller\n"); end;
51-end;
52-
53-end Mix.
54-```
55-
56-```sh
57-$ raz Mix.raz
58-```
59-
60-The fix is the conversion, written where it happens: `if i < I64(LEN(s))`. It costs nothing at run time and says what is meant.
61-
62-## Bits and booleans
63-
64-`#`, not `<>`, is "different". `and`, `or` and `not` are for `BOOL`s, and bind below the relations, so `i < n and a[i] # 0` needs no parentheses. `&`, `|`, `xor`, `~`, `shl` and `shr` are for the bits of integers. There is no conversion between `BOOL` and an integer: say what you mean with `if`.
65-
66-```raz Bits run
67-module Bits;
68-import Out;
69-
70-proc main*;
71-var flags, mask: U8;
72-begin
73- flags := 0;
74- flags := flags | 1; (* set bit 0 *)
75- flags := flags | (1 shl 3); (* set bit 3 *)
76- mask := 0FH;
77- Out.str("flags 0x"); Out.hex(U64(flags), 2); Out.ln;
78- Out.str("and 0x"); Out.hex(U64(flags & mask), 2); Out.ln;
79- Out.str("xor 0x"); Out.hex(U64(flags xor 0FFH), 2); Out.ln;
80- Out.str("not 0x"); Out.hex(U64(~flags), 2); Out.ln;
81- if (flags & 8) # 0 then Out.str("bit 3 is set\n"); end;
82-end;
83-
84-end Bits.
85-```
86-
87-## Reals
88-
89-`F32` and `F64` are the reals. A real constant is written with a point, `1.5` or `2.0E-3`. `Fmt.real(x, places)` writes one with that many digits after the point, and `Math` has the functions.
90-
91-```raz Reals run
92-module Reals;
93-import Fmt, Math;
94-
95-proc main*;
96-var n: INT; x: F64; y: F32;
97-begin
98- n := 22;
99- x := F64(n) / 7.0;
100- y := F32(x);
101- Fmt.print("22/7 = {}\n", [Fmt.real(x, 6)]);
102- Fmt.print("as F32 = {}\n", [Fmt.real(F64(y), 6)]);
103- Fmt.print("sqrt 2 = {}, pi = {}\n", [Fmt.real(Math.sqrt(2.0), 5), Fmt.real(Math.pi, 5)]);
104- Fmt.print("truncated: {} {}\n", [Fmt.int(I64(2.9)), Fmt.int(I64(-2.9))]);
105-end;
106-
107-end Reals.
108-```
109-
110-A real converts to an integer by rounding toward zero. Every variable starts at zero, a real's `0.0` and a `BOOL`'s `false` included, so there is no uninitialised value to be surprised by.

site/content/tour/04-procedures.txt deleted +0 -161

@@ -1,161 +0,0 @@
1-title: Procedures and several results
2-part: Raz
3-summary: Value and var parameters, several results, forward, and procedures as values.
4----
5-```raz Procs run
6-```
7-
8-A parameter is a copy, unless it is `var`, when it is the caller's variable itself. A procedure may return several results, which a multiple assignment takes apart; the blank, `_`, drops one.
9-
10-## Value and `var` parameters
11-
12-A value parameter is the procedure's own: it may change it, and the caller's variable stays as it was. A `var` parameter is the caller's variable, and its argument must be a variable of exactly that type.
13-
14-```raz Params run
15-module Params;
16-import Out;
17-
18-proc bump(n: INT); (* changes its own copy *)
19-begin
20- n := n + 1;
21-end;
22-
23-proc bumpIn(var n: INT); (* changes the caller's *)
24-begin
25- n := n + 1;
26-end;
27-
28-proc fill(var a: [4]INT; v: INT); (* an array is copied unless var *)
29-var i: INT;
30-begin
31- for i := 0 to 3 do a[i] := v; end;
32-end;
33-
34-proc main*;
35-var x: INT; a: [4]INT;
36-begin
37- x := 10;
38- bump(x);
39- Out.int(x); Out.ln; (* 10 *)
40- bumpIn(x);
41- Out.int(x); Out.ln; (* 11 *)
42- fill(a, 7);
43- Out.int(a[0] + a[3]); Out.ln; (* 14 *)
44-end;
45-
46-end Params.
47-```
48-
49-## Several results
50-
51-Instead of an output parameter, a procedure says what it returns: `(INT, INT)`, or any list of types. The caller takes them apart with a multiple assignment. A call that returns several values may stand on the right of one, alone as a statement, or alone after `return`, which passes them on as they are.
52-
53-```raz Several run
54-module Several;
55-import Out;
56-
57-proc divmod(a, b: INT): (INT, INT);
58-begin
59- return a div b, a mod b;
60-end;
61-
62-proc minmax(a, b: INT): (INT, INT);
63-begin
64- if a < b then return a, b; end;
65- return b, a;
66-end;
67-
68-proc spread(a, b: INT): (INT, INT);
69-begin
70- return minmax(a, b); (* passes both results on *)
71-end;
72-
73-proc main*;
74-var q, r, lo, hi: INT;
75-begin
76- q, r := divmod(100, 7);
77- Out.int(q); Out.str(" rest "); Out.int(r); Out.ln;
78- lo, hi := spread(9, 4);
79- Out.int(lo); Out.str(" .. "); Out.int(hi); Out.ln;
80- _, r := divmod(-7, 2); (* drop the quotient *)
81- Out.int(r); Out.ln;
82-end;
83-
84-end Several.
85-```
86-
87-## Recursion, and `forward`
88-
89-A name is declared before it is used, and procedures don't nest. When two procedures need each other, `forward` declares the first so that its body can come later.
90-
91-```raz Recur run
92-module Recur;
93-import Out;
94-
95-proc gcd(a, b: INT): INT;
96-begin
97- if b = 0 then return a; end;
98- return gcd(b, a mod b);
99-end;
100-
101-proc isOdd(n: INT): BOOL; forward;
102-
103-proc isEven(n: INT): BOOL;
104-begin
105- if n = 0 then return true; end;
106- return isOdd(n - 1);
107-end;
108-
109-proc isOdd(n: INT): BOOL;
110-begin
111- if n = 0 then return false; end;
112- return isEven(n - 1);
113-end;
114-
115-proc main*;
116-begin
117- Out.int(gcd(84, 36)); Out.ln;
118- if isEven(10) and isOdd(7) then Out.str("parity works\n"); end;
119-end;
120-
121-end Recur.
122-```
123-
124-## Procedures are values
125-
126-A procedure used without parentheses is a procedure value, of a procedure type such as `proc(a, b: INT): INT`. It can be passed, stored in a variable or a record, and called. This is raz's only dynamic dispatch, and it is as plain as it looks.
127-
128-```raz Table run
129-module Table;
130-import Out;
131-
132-type Op = proc(a, b: INT): INT;
133-
134-proc add(a, b: INT): INT; begin return a + b; end;
135-proc sub(a, b: INT): INT; begin return a - b; end;
136-proc mul(a, b: INT): INT; begin return a * b; end;
137-
138-proc apply(f: Op; a, b: INT): INT;
139-begin
140- return f(a, b);
141-end;
142-
143-proc main*;
144-var ops: [3]Op; names: [3][]U8; i: INT;
145-begin
146- ops := [add, sub, mul];
147- names := ["add", "sub", "mul"];
148- for i := 0 to 2 do
149- Out.str(names[i]); Out.str(": ");
150- Out.int(apply(ops[i], 12, 5)); Out.ln;
151- end;
152-end;
153-
154-end Table.
155-```
156-
157-A procedure without parameters leaves out the parentheses where it is called, as `Out.ln` does. That means `f` alone is the procedure and `f()` would not be a call, so be careful when passing one around.
158-
159-## Dropped results
160-
161-A call that drops a result, as `Sys.write(fd, s);` does, is warned of; `_ := Sys.write(fd, s);` says it is meant. The compiler also warns of a variable that is assigned and never read, and of a name declared and never used, unless it starts with `_`.

site/content/tour/04-values.txt (history) added +99 -0

@@ -0,0 +1,99 @@
1+title: Values and conversions
2+part: Raz
3+summary: Nothing converts unasked; what converts to what; booleans and reals.
4+---
5+This program uses several of the basic types at once, with each conversion written where it happens.
6+
7+```raz Values run
8+```
9+
10+## Conversions are written
11+
12+Two types never mix: with `i: I64` and a slice `s`, `i < LEN(s)` is an error. A type's name converts: `F64(n)`, `I64(b)`, `U8(x)`. The one silent conversion between numbers is an `INT` into an `I64`, which loses nothing. The [lesson on integers](integers.html) says what each width conversion does.
13+
14+A number written in the program is untyped until it meets a type, and `1.5` is a real, which an integer never becomes without a conversion.
15+
16+```raz Mix
17+module Mix;
18+import Out;
19+
20+proc main*;
21+var i: I64; s: []U8;
22+begin
23+ s := "four";
24+ i := 3;
25+ if i < LEN(s) then Out.str("smaller\n"); end;
26+end;
27+
28+end Mix.
29+```
30+
31+```sh
32+$ raz Mix.raz
33+```
34+
35+The fix is the conversion, written where it happens: `if i < I64(LEN(s))`. It costs nothing at run time and says what is meant.
36+
37+## What converts to what
38+
39+Everything else that converts does so with the type's name, and the rules are short:
40+
41+| From | To | Result |
42+|---|---|---|
43+| integer | integer | cut to the new width, or extended by the sign (signed) or zeros (unsigned) |
44+| integer | `F32`, `F64` | the nearest real |
45+| `F32`, `F64` | integer | rounded toward zero |
46+| `F32` | `F64` | the same value; the other way, the nearest `F32` |
47+| enumeration | integer | its position; and back, if there is a name at that position |
48+| set | integer | its bits; and back |
49+| `INT`, `UINT` | pointer | the address; and back |
50+
51+There is no conversion between `BOOL` and an integer: say what you mean with `if`. A `BOOL` is `true` or `false`, and `and`, `or` and `not` are for `BOOL`s: they bind below the relations, so `i < n and a[i] # 0` needs no parentheses, and `and` and `or` evaluate their right operand only when it decides the result. `#`, not `<>`, is "different".
52+
53+```raz Truth run
54+module Truth;
55+import Out;
56+
57+proc positive(n: INT): BOOL;
58+begin
59+ return n > 0;
60+end;
61+
62+proc main*;
63+var a: [3]INT; i: INT; found: BOOL;
64+begin
65+ a := [4, 0, 9];
66+ i := 0;
67+ while (i < LEN(a)) and (a[i] # 0) do INC(i); end; (* never reads a[3] *)
68+ Out.int(i); Out.ln;
69+ found := positive(i) or positive(-1);
70+ if found and not positive(0) then Out.str("true and true\n"); end;
71+end;
72+
73+end Truth.
74+```
75+
76+## Reals
77+
78+`F32` and `F64` are the reals. A real constant is written with a point, `1.5` or `2.0E-3`. `Fmt.real(x, places)` writes one with that many digits after the point, and `Math` has the functions.
79+
80+```raz Reals run
81+module Reals;
82+import Fmt, Math;
83+
84+proc main*;
85+var n: INT; x: F64; y: F32;
86+begin
87+ n := 22;
88+ x := F64(n) / 7.0;
89+ y := F32(x);
90+ Fmt.print("22/7 = {}\n", [Fmt.real(x, 6)]);
91+ Fmt.print("as F32 = {}\n", [Fmt.real(F64(y), 6)]);
92+ Fmt.print("sqrt 2 = {}, pi = {}\n", [Fmt.real(Math.sqrt(2.0), 5), Fmt.real(Math.pi, 5)]);
93+ Fmt.print("truncated: {} {}\n", [Fmt.int(I64(2.9)), Fmt.int(I64(-2.9))]);
94+end;
95+
96+end Reals.
97+```
98+
99+A real converts to an integer by rounding toward zero. Every variable starts at zero, a real's `0.0` and a `BOOL`'s `false` included, so there is no uninitialised value to be surprised by.

site/content/tour/05-procedures.txt (history) added +161 -0

@@ -0,0 +1,161 @@
1+title: Procedures and several results
2+part: Raz
3+summary: Value and var parameters, several results, forward, and procedures as values.
4+---
5+```raz Procs run
6+```
7+
8+A parameter is a copy, unless it is `var`, when it is the caller's variable itself. A procedure may return several results, which a multiple assignment takes apart; the blank, `_`, drops one.
9+
10+## Value and `var` parameters
11+
12+A value parameter is the procedure's own: it may change it, and the caller's variable stays as it was. A `var` parameter is the caller's variable, and its argument must be a variable of exactly that type.
13+
14+```raz Params run
15+module Params;
16+import Out;
17+
18+proc bump(n: INT); (* changes its own copy *)
19+begin
20+ n := n + 1;
21+end;
22+
23+proc bumpIn(var n: INT); (* changes the caller's *)
24+begin
25+ n := n + 1;
26+end;
27+
28+proc fill(var a: [4]INT; v: INT); (* an array is copied unless var *)
29+var i: INT;
30+begin
31+ for i := 0 to 3 do a[i] := v; end;
32+end;
33+
34+proc main*;
35+var x: INT; a: [4]INT;
36+begin
37+ x := 10;
38+ bump(x);
39+ Out.int(x); Out.ln; (* 10 *)
40+ bumpIn(x);
41+ Out.int(x); Out.ln; (* 11 *)
42+ fill(a, 7);
43+ Out.int(a[0] + a[3]); Out.ln; (* 14 *)
44+end;
45+
46+end Params.
47+```
48+
49+## Several results
50+
51+Instead of an output parameter, a procedure says what it returns: `(INT, INT)`, or any list of types. The caller takes them apart with a multiple assignment. A call that returns several values may stand on the right of one, alone as a statement, or alone after `return`, which passes them on as they are.
52+
53+```raz Several run
54+module Several;
55+import Out;
56+
57+proc divmod(a, b: INT): (INT, INT);
58+begin
59+ return a div b, a mod b;
60+end;
61+
62+proc minmax(a, b: INT): (INT, INT);
63+begin
64+ if a < b then return a, b; end;
65+ return b, a;
66+end;
67+
68+proc spread(a, b: INT): (INT, INT);
69+begin
70+ return minmax(a, b); (* passes both results on *)
71+end;
72+
73+proc main*;
74+var q, r, lo, hi: INT;
75+begin
76+ q, r := divmod(100, 7);
77+ Out.int(q); Out.str(" rest "); Out.int(r); Out.ln;
78+ lo, hi := spread(9, 4);
79+ Out.int(lo); Out.str(" .. "); Out.int(hi); Out.ln;
80+ _, r := divmod(-7, 2); (* drop the quotient *)
81+ Out.int(r); Out.ln;
82+end;
83+
84+end Several.
85+```
86+
87+## Recursion, and `forward`
88+
89+A name is declared before it is used, and procedures don't nest. When two procedures need each other, `forward` declares the first so that its body can come later.
90+
91+```raz Recur run
92+module Recur;
93+import Out;
94+
95+proc gcd(a, b: INT): INT;
96+begin
97+ if b = 0 then return a; end;
98+ return gcd(b, a mod b);
99+end;
100+
101+proc isOdd(n: INT): BOOL; forward;
102+
103+proc isEven(n: INT): BOOL;
104+begin
105+ if n = 0 then return true; end;
106+ return isOdd(n - 1);
107+end;
108+
109+proc isOdd(n: INT): BOOL;
110+begin
111+ if n = 0 then return false; end;
112+ return isEven(n - 1);
113+end;
114+
115+proc main*;
116+begin
117+ Out.int(gcd(84, 36)); Out.ln;
118+ if isEven(10) and isOdd(7) then Out.str("parity works\n"); end;
119+end;
120+
121+end Recur.
122+```
123+
124+## Procedures are values
125+
126+A procedure used without parentheses is a procedure value, of a procedure type such as `proc(a, b: INT): INT`. It can be passed, stored in a variable or a record, and called. This is raz's only dynamic dispatch, and it is as plain as it looks.
127+
128+```raz Table run
129+module Table;
130+import Out;
131+
132+type Op = proc(a, b: INT): INT;
133+
134+proc add(a, b: INT): INT; begin return a + b; end;
135+proc sub(a, b: INT): INT; begin return a - b; end;
136+proc mul(a, b: INT): INT; begin return a * b; end;
137+
138+proc apply(f: Op; a, b: INT): INT;
139+begin
140+ return f(a, b);
141+end;
142+
143+proc main*;
144+var ops: [3]Op; names: [3][]U8; i: INT;
145+begin
146+ ops := [add, sub, mul];
147+ names := ["add", "sub", "mul"];
148+ for i := 0 to 2 do
149+ Out.str(names[i]); Out.str(": ");
150+ Out.int(apply(ops[i], 12, 5)); Out.ln;
151+ end;
152+end;
153+
154+end Table.
155+```
156+
157+A procedure without parameters leaves out the parentheses where it is called, as `Out.ln` does. That means `f` alone is the procedure and `f()` would not be a call, so be careful when passing one around.
158+
159+## Dropped results
160+
161+A call that drops a result, as `Sys.write(fd, s);` does, is warned of; `_ := Sys.write(fd, s);` says it is meant. The compiler also warns of a variable that is assigned and never read, and of a name declared and never used, unless it starts with `_`.

site/content/tour/05-slices.txt deleted +0 -181

@@ -1,181 +0,0 @@
1-title: Arrays, slices and strings
2-part: Set
3-summary: Five in a row, a window onto them, bytes in UTF-8, and checks on every index.
4----
5-```raz Slices run
6-```
7-
8-## Arrays
9-
10-`[5]INT` is an array: five `INT`s in a row, copied when it is assigned or passed. Its length is part of its type, so `[5]INT` and `[6]INT` are different types, and `LEN(a)` of an array is a constant. Array literals are written in brackets, and need a type from the place they are used.
11-
12-```raz Arrays run
13-module Arrays;
14-import Out;
15-
16-proc main*;
17-var a, b: [4]INT; grid: [3][3]INT; i, j: INT;
18-begin
19- a := [1, 2, 3, 4];
20- b := a; (* a copy *)
21- b[0] := 99;
22- Out.int(a[0]); Out.char(' '); Out.int(b[0]); Out.ln;
23- if a # b then Out.str("they differ now\n"); end;
24-
25- for i := 0 to 2 do
26- for j := 0 to 2 do grid[i][j] := i * 3 + j; end;
27- end;
28- Out.int(grid[2][1]); Out.ln; (* 7 *)
29- Out.int(LEN(grid)); Out.char('x'); Out.int(LEN(grid[0])); Out.ln;
30-end;
31-
32-end Arrays.
33-```
34-
35-## Slices
36-
37-`[]INT` is a slice: a pointer to elements that live elsewhere, and their number, `LEN(s)`. `a[lo..hi]` is the slice of the elements from `lo` up to, but not including, `hi`, and an array goes silently where a slice is wanted. A slice doesn't copy anything: changing an element of it changes the array it looks at.
38-
39-```raz Windows run
40-module Windows;
41-import Out;
42-
43-proc zero(s: []INT);
44-var i: INT;
45-begin
46- for i := 0 to LEN(s) - 1 do s[i] := 0; end;
47-end;
48-
49-proc show(s: []INT);
50-var i: INT;
51-begin
52- for i := 0 to LEN(s) - 1 do Out.int(s[i]); Out.char(' '); end;
53- Out.ln;
54-end;
55-
56-proc main*;
57-var a: [8]INT; i: INT;
58-begin
59- for i := 0 to 7 do a[i] := i + 1; end;
60- zero(a[2..5]); (* a window onto a *)
61- show(a); (* 1 2 0 0 0 6 7 8 *)
62- show(a[6..8]);
63- show(a[3..3]); (* empty: prints just a line feed *)
64-end;
65-
66-end Windows.
67-```
68-
69-Every index and every slice bound is checked, so a slice can never reach past what it was made from; the lesson on failures shows what that looks like.
70-
71-## `COPY`
72-
73-`COPY(d, s)` copies the elements of `s` to the start of `d`, as one block, and is right when they overlap. `LEN(s)` must be at most `LEN(d)`.
74-
75-```raz Moves run
76-module Moves;
77-import Out;
78-
79-proc main*;
80-var a: [6]INT; i: INT;
81-begin
82- a := [1, 2, 3, 4, 5, 0];
83- COPY(a[1..6], a[0..5]); (* shift everything one place up *)
84- for i := 0 to 5 do Out.int(a[i]); Out.char(' '); end;
85- Out.ln;
86-end;
87-
88-end Moves.
89-```
90-
91-## Strings
92-
93-A string is a slice of bytes, `[]U8`, in UTF-8, and a string constant's bytes are constant. A `[8]U8` made from one is a copy you can change. Arrays and slices of integers compare with `=` and `#`, so strings do too. `Str` has the rest: ordering, finding, hashing, prefixes, UTF-8 and reading a number.
94-
95-```raz Words run
96-module Words;
97-import Out, Str;
98-
99-proc main*;
100-var s: []U8; n: I64; ok: BOOL; name: [8]U8;
101-begin
102- s := "hello, world";
103- Out.str(s[7..LEN(s)]); Out.ln;
104- if s[0..5] = "hello" then Out.str("it says hello\n"); end;
105- Out.int(Str.find(s, "world")); Out.ln; (* 7 *)
106- Out.int(Str.find(s, "xyz")); Out.ln; (* -1 *)
107- if Str.hasPrefix(s, "hell") then Out.str("prefix\n"); end;
108- Out.int(Str.cmp("apple", "banana")); Out.ln; (* -1 *)
109-
110- n, ok := Str.toInt("-1234");
111- if ok then Out.int(n + 1); Out.ln; end;
112- n, ok := Str.toInt("12x");
113- if not ok then Out.str("not a number\n"); end;
114-
115- name := "raz"; (* a copy, the program's own *)
116- name[0] := 'R';
117- Out.str(name[0..3]); Out.ln;
118-end;
119-
120-end Words.
121-```
122-
123-## UTF-8
124-
125-A character is an `I32`, its code point. `Str.decode` reads the first character of a string and says how many bytes it took; `Str.encode` goes the other way. Byte length and character count are different things, and the program is the one that says which it wants.
126-
127-```raz Runes run
128-module Runes;
129-import Out, Str;
130-
131-(* the characters of s, not its bytes *)
132-proc chars(s: []U8): INT;
133-var i, n, size: INT;
134-begin
135- i := 0;
136- n := 0;
137- while i < LEN(s) do
138- _, size := Str.decode(s[i..LEN(s)]);
139- INC(i, size);
140- INC(n);
141- end;
142- return n;
143-end;
144-
145-proc main*;
146-var s: []U8; c: I32; size: INT; bytes: [4]U8; k: INT;
147-begin
148- s := "caf\u{E9} \u{3BB}";
149- Out.int(LEN(s)); Out.str(" bytes, ");
150- Out.int(chars(s)); Out.str(" characters\n");
151- c, size := Str.decode(s[3..LEN(s)]);
152- Out.int(I64(c)); Out.str(" took "); Out.int(size); Out.str(" bytes\n");
153- bytes, k := Str.encode(955);
154- Out.str(bytes[0..k]); Out.ln;
155-end;
156-
157-end Runes.
158-```
159-
160-## Loops over lengths
161-
162-A `for` loop takes its bounds once, and never steps past the last: with an empty slice, `0 to LEN(s) - 1` doesn't run at all, and the variable can't wrap. `for` takes a constant step with `by`, and `while` and `repeat ... until` do the rest.
163-
164-```raz Loops run
165-module Loops;
166-import Out;
167-
168-proc main*;
169-var i: INT;
170-begin
171- for i := 10 to 0 by -5 do Out.int(i); Out.char(' '); end;
172- Out.ln;
173- i := 1;
174- while i < 100 do i := i * 3; end;
175- Out.int(i); Out.ln;
176- repeat DEC(i, 30); until i < 0;
177- Out.int(i); Out.ln;
178-end;
179-
180-end Loops.
181-```

site/content/tour/06-records.txt deleted +0 -138

@@ -1,138 +0,0 @@
1-title: Records and pointers
2-part: Set
3-summary: Laid out as C lays out a struct, packed records, pointers and nil.
4----
5-```raz Boxes run
6-```
7-
8-A record is laid out as C lays out a struct. `{lo: ..., hi: ...}` builds one, naming its fields; the ones it leaves out are zero. `^Box` is a pointer to a `Box`, `ADR(b)` the address of `b`, and `b^` what a pointer points at. A pointer is never the record: it is `b^.lo`, never `b.lo`, so you always see where the memory is reached through an address.
9-
10-## Copies and literals
11-
12-Assigning a record copies it whole, as with an array. A record is compared field by field by writing the comparison, not by `=`.
13-
14-```raz Points run
15-module Points;
16-import Out;
17-
18-type Point = record x, y: INT; end;
19-
20-proc show(p: Point);
21-begin
22- Out.char('('); Out.int(p.x); Out.str(", "); Out.int(p.y); Out.char(')');
23-end;
24-
25-proc mirror(p: Point): Point;
26-begin
27- return {x: p.y, y: p.x};
28-end;
29-
30-proc main*;
31-var a, b: Point;
32-begin
33- a := {x: 3, y: 4};
34- b := a; (* a copy *)
35- b.x := 30;
36- show(a); show(b); show(mirror(a)); Out.ln;
37- a := {y: 9}; (* x is zero *)
38- show(a); Out.ln;
39-end;
40-
41-end Points.
42-```
43-
44-## Layout
45-
46-A record's layout is C's: each field at the next multiple of its own alignment, and the whole padded to a multiple of its largest alignment. `SIZE(T)` says how many bytes. A `packed record` has no padding, which is what a file format or a wire protocol wants.
47-
48-```raz Layout run
49-module Layout;
50-import Out;
51-
52-type
53- Loose = record a: U8; b: U32; c: U8; end;
54- Tight = packed record a: U8; b: U32; c: U8; end;
55-
56-proc main*;
57-begin
58- Out.str("loose: "); Out.int(SIZE(Loose)); Out.ln; (* 12 *)
59- Out.str("tight: "); Out.int(SIZE(Tight)); Out.ln; (* 6 *)
60- Out.str("INT: "); Out.int(SIZE(INT)); Out.ln;
61- Out.str("[5]I16: "); Out.int(SIZE([5]I16)); Out.ln;
62-end;
63-
64-end Layout.
65-```
66-
67-A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with `ADR`.
68-
69-## Pointers
70-
71-`ADR(x)` takes the address of a variable, `^T` is a pointer to a `T`, and `p^` is what it points at. A pointer that points nowhere is `nil`, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory.
72-
73-```raz Pointers run
74-module Pointers;
75-import Out;
76-
77-type Counter = record hits: INT; name: []U8; end;
78-
79-proc hit(c: ^Counter);
80-begin
81- if c = nil then return; end;
82- INC(c^.hits);
83-end;
84-
85-proc swap(a, b: ^INT);
86-var t: INT;
87-begin
88- t := a^;
89- a^ := b^;
90- b^ := t;
91-end;
92-
93-proc main*;
94-var c: Counter; x, y: INT; p: ^INT;
95-begin
96- c.name := "front door";
97- hit(ADR(c)); hit(ADR(c)); hit(nil);
98- Out.str(c.name); Out.str(": "); Out.int(c.hits); Out.ln;
99- x := 1; y := 2;
100- swap(ADR(x), ADR(y));
101- Out.int(x); Out.char(' '); Out.int(y); Out.ln;
102- p := ADR(x);
103- p^ := p^ + 40;
104- Out.int(x); Out.ln;
105-end;
106-
107-end Pointers.
108-```
109-
110-## Pointers that link
111-
112-A record may hold a pointer to its own type, which is how lists and trees are made. Memory for them comes from an allocator, which is the next lesson; here the nodes simply live in an array.
113-
114-```raz Chain run
115-module Chain;
116-import Out;
117-
118-type Node = record next: ^Node; val: INT; end;
119-
120-proc main*;
121-var nodes: [4]Node; head, n: ^Node; i: INT;
122-begin
123- head := nil;
124- for i := 0 to 3 do
125- nodes[i].val := (i + 1) * 10;
126- nodes[i].next := head; (* each new one in front *)
127- head := ADR(nodes[i]);
128- end;
129- n := head;
130- while n # nil do
131- Out.int(n^.val); Out.char(' ');
132- n := n^.next;
133- end;
134- Out.ln;
135-end;
136-
137-end Chain.
138-```

site/content/tour/06-slices.txt (history) added +181 -0

@@ -0,0 +1,181 @@
1+title: Arrays, slices and strings
2+part: Set
3+summary: Five in a row, a window onto them, bytes in UTF-8, and checks on every index.
4+---
5+```raz Slices run
6+```
7+
8+## Arrays
9+
10+`[5]INT` is an array: five `INT`s in a row, copied when it is assigned or passed. Its length is part of its type, so `[5]INT` and `[6]INT` are different types, and `LEN(a)` of an array is a constant. Array literals are written in brackets, and need a type from the place they are used.
11+
12+```raz Arrays run
13+module Arrays;
14+import Out;
15+
16+proc main*;
17+var a, b: [4]INT; grid: [3][3]INT; i, j: INT;
18+begin
19+ a := [1, 2, 3, 4];
20+ b := a; (* a copy *)
21+ b[0] := 99;
22+ Out.int(a[0]); Out.char(' '); Out.int(b[0]); Out.ln;
23+ if a # b then Out.str("they differ now\n"); end;
24+
25+ for i := 0 to 2 do
26+ for j := 0 to 2 do grid[i][j] := i * 3 + j; end;
27+ end;
28+ Out.int(grid[2][1]); Out.ln; (* 7 *)
29+ Out.int(LEN(grid)); Out.char('x'); Out.int(LEN(grid[0])); Out.ln;
30+end;
31+
32+end Arrays.
33+```
34+
35+## Slices
36+
37+`[]INT` is a slice: a pointer to elements that live elsewhere, and their number, `LEN(s)`. `a[lo..hi]` is the slice of the elements from `lo` up to, but not including, `hi`, and an array goes silently where a slice is wanted. A slice doesn't copy anything: changing an element of it changes the array it looks at.
38+
39+```raz Windows run
40+module Windows;
41+import Out;
42+
43+proc zero(s: []INT);
44+var i: INT;
45+begin
46+ for i := 0 to LEN(s) - 1 do s[i] := 0; end;
47+end;
48+
49+proc show(s: []INT);
50+var i: INT;
51+begin
52+ for i := 0 to LEN(s) - 1 do Out.int(s[i]); Out.char(' '); end;
53+ Out.ln;
54+end;
55+
56+proc main*;
57+var a: [8]INT; i: INT;
58+begin
59+ for i := 0 to 7 do a[i] := i + 1; end;
60+ zero(a[2..5]); (* a window onto a *)
61+ show(a); (* 1 2 0 0 0 6 7 8 *)
62+ show(a[6..8]);
63+ show(a[3..3]); (* empty: prints just a line feed *)
64+end;
65+
66+end Windows.
67+```
68+
69+Every index and every slice bound is checked, so a slice can never reach past what it was made from; the lesson on failures shows what that looks like.
70+
71+## `COPY`
72+
73+`COPY(d, s)` copies the elements of `s` to the start of `d`, as one block, and is right when they overlap. `LEN(s)` must be at most `LEN(d)`.
74+
75+```raz Moves run
76+module Moves;
77+import Out;
78+
79+proc main*;
80+var a: [6]INT; i: INT;
81+begin
82+ a := [1, 2, 3, 4, 5, 0];
83+ COPY(a[1..6], a[0..5]); (* shift everything one place up *)
84+ for i := 0 to 5 do Out.int(a[i]); Out.char(' '); end;
85+ Out.ln;
86+end;
87+
88+end Moves.
89+```
90+
91+## Strings
92+
93+A string is a slice of bytes, `[]U8`, in UTF-8, and a string constant's bytes are constant. A `[8]U8` made from one is a copy you can change. Arrays and slices of integers compare with `=` and `#`, so strings do too. `Str` has the rest: ordering, finding, hashing, prefixes, UTF-8 and reading a number.
94+
95+```raz Words run
96+module Words;
97+import Out, Str;
98+
99+proc main*;
100+var s: []U8; n: I64; ok: BOOL; name: [8]U8;
101+begin
102+ s := "hello, world";
103+ Out.str(s[7..LEN(s)]); Out.ln;
104+ if s[0..5] = "hello" then Out.str("it says hello\n"); end;
105+ Out.int(Str.find(s, "world")); Out.ln; (* 7 *)
106+ Out.int(Str.find(s, "xyz")); Out.ln; (* -1 *)
107+ if Str.hasPrefix(s, "hell") then Out.str("prefix\n"); end;
108+ Out.int(Str.cmp("apple", "banana")); Out.ln; (* -1 *)
109+
110+ n, ok := Str.toInt("-1234");
111+ if ok then Out.int(n + 1); Out.ln; end;
112+ n, ok := Str.toInt("12x");
113+ if not ok then Out.str("not a number\n"); end;
114+
115+ name := "raz"; (* a copy, the program's own *)
116+ name[0] := 'R';
117+ Out.str(name[0..3]); Out.ln;
118+end;
119+
120+end Words.
121+```
122+
123+## UTF-8
124+
125+A character is an `I32`, its code point. `Str.decode` reads the first character of a string and says how many bytes it took; `Str.encode` goes the other way. Byte length and character count are different things, and the program is the one that says which it wants.
126+
127+```raz Runes run
128+module Runes;
129+import Out, Str;
130+
131+(* the characters of s, not its bytes *)
132+proc chars(s: []U8): INT;
133+var i, n, size: INT;
134+begin
135+ i := 0;
136+ n := 0;
137+ while i < LEN(s) do
138+ _, size := Str.decode(s[i..LEN(s)]);
139+ INC(i, size);
140+ INC(n);
141+ end;
142+ return n;
143+end;
144+
145+proc main*;
146+var s: []U8; c: I32; size: INT; bytes: [4]U8; k: INT;
147+begin
148+ s := "caf\u{E9} \u{3BB}";
149+ Out.int(LEN(s)); Out.str(" bytes, ");
150+ Out.int(chars(s)); Out.str(" characters\n");
151+ c, size := Str.decode(s[3..LEN(s)]);
152+ Out.int(I64(c)); Out.str(" took "); Out.int(size); Out.str(" bytes\n");
153+ bytes, k := Str.encode(955);
154+ Out.str(bytes[0..k]); Out.ln;
155+end;
156+
157+end Runes.
158+```
159+
160+## Loops over lengths
161+
162+A `for` loop takes its bounds once, and never steps past the last: with an empty slice, `0 to LEN(s) - 1` doesn't run at all, and the variable can't wrap. `for` takes a constant step with `by`, and `while` and `repeat ... until` do the rest.
163+
164+```raz Loops run
165+module Loops;
166+import Out;
167+
168+proc main*;
169+var i: INT;
170+begin
171+ for i := 10 to 0 by -5 do Out.int(i); Out.char(' '); end;
172+ Out.ln;
173+ i := 1;
174+ while i < 100 do i := i * 3; end;
175+ Out.int(i); Out.ln;
176+ repeat DEC(i, 30); until i < 0;
177+ Out.int(i); Out.ln;
178+end;
179+
180+end Loops.
181+```

site/content/tour/07-records.txt (history) added +138 -0

@@ -0,0 +1,138 @@
1+title: Records and pointers
2+part: Set
3+summary: Laid out as C lays out a struct, packed records, pointers and nil.
4+---
5+```raz Boxes run
6+```
7+
8+A record is laid out as C lays out a struct. `{lo: ..., hi: ...}` builds one, naming its fields; the ones it leaves out are zero. `^Box` is a pointer to a `Box`, `ADR(b)` the address of `b`, and `b^` what a pointer points at. A pointer is never the record: it is `b^.lo`, never `b.lo`, so you always see where the memory is reached through an address.
9+
10+## Copies and literals
11+
12+Assigning a record copies it whole, as with an array. A record is compared field by field by writing the comparison, not by `=`.
13+
14+```raz Points run
15+module Points;
16+import Out;
17+
18+type Point = record x, y: INT; end;
19+
20+proc show(p: Point);
21+begin
22+ Out.char('('); Out.int(p.x); Out.str(", "); Out.int(p.y); Out.char(')');
23+end;
24+
25+proc mirror(p: Point): Point;
26+begin
27+ return {x: p.y, y: p.x};
28+end;
29+
30+proc main*;
31+var a, b: Point;
32+begin
33+ a := {x: 3, y: 4};
34+ b := a; (* a copy *)
35+ b.x := 30;
36+ show(a); show(b); show(mirror(a)); Out.ln;
37+ a := {y: 9}; (* x is zero *)
38+ show(a); Out.ln;
39+end;
40+
41+end Points.
42+```
43+
44+## Layout
45+
46+A record's layout is C's: each field at the next multiple of its own alignment, and the whole padded to a multiple of its largest alignment. `SIZE(T)` says how many bytes. A `packed record` has no padding, which is what a file format or a wire protocol wants.
47+
48+```raz Layout run
49+module Layout;
50+import Out;
51+
52+type
53+ Loose = record a: U8; b: U32; c: U8; end;
54+ Tight = packed record a: U8; b: U32; c: U8; end;
55+
56+proc main*;
57+begin
58+ Out.str("loose: "); Out.int(SIZE(Loose)); Out.ln; (* 12 *)
59+ Out.str("tight: "); Out.int(SIZE(Tight)); Out.ln; (* 6 *)
60+ Out.str("INT: "); Out.int(SIZE(INT)); Out.ln;
61+ Out.str("[5]I16: "); Out.int(SIZE([5]I16)); Out.ln;
62+end;
63+
64+end Layout.
65+```
66+
67+A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with `ADR`.
68+
69+## Pointers
70+
71+`ADR(x)` takes the address of a variable, `^T` is a pointer to a `T`, and `p^` is what it points at. A pointer that points nowhere is `nil`, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory.
72+
73+```raz Pointers run
74+module Pointers;
75+import Out;
76+
77+type Counter = record hits: INT; name: []U8; end;
78+
79+proc hit(c: ^Counter);
80+begin
81+ if c = nil then return; end;
82+ INC(c^.hits);
83+end;
84+
85+proc swap(a, b: ^INT);
86+var t: INT;
87+begin
88+ t := a^;
89+ a^ := b^;
90+ b^ := t;
91+end;
92+
93+proc main*;
94+var c: Counter; x, y: INT; p: ^INT;
95+begin
96+ c.name := "front door";
97+ hit(ADR(c)); hit(ADR(c)); hit(nil);
98+ Out.str(c.name); Out.str(": "); Out.int(c.hits); Out.ln;
99+ x := 1; y := 2;
100+ swap(ADR(x), ADR(y));
101+ Out.int(x); Out.char(' '); Out.int(y); Out.ln;
102+ p := ADR(x);
103+ p^ := p^ + 40;
104+ Out.int(x); Out.ln;
105+end;
106+
107+end Pointers.
108+```
109+
110+## Pointers that link
111+
112+A record may hold a pointer to its own type, which is how lists and trees are made. Memory for them comes from an allocator, which is the next lesson; here the nodes simply live in an array.
113+
114+```raz Chain run
115+module Chain;
116+import Out;
117+
118+type Node = record next: ^Node; val: INT; end;
119+
120+proc main*;
121+var nodes: [4]Node; head, n: ^Node; i: INT;
122+begin
123+ head := nil;
124+ for i := 0 to 3 do
125+ nodes[i].val := (i + 1) * 10;
126+ nodes[i].next := head; (* each new one in front *)
127+ head := ADR(nodes[i]);
128+ end;
129+ n := head;
130+ while n # nil do
131+ Out.int(n^.val); Out.char(' ');
132+ n := n^.next;
133+ end;
134+ Out.ln;
135+end;
136+
137+end Chain.
138+```

site/content/tour/07-variants.txt deleted +0 -165

@@ -1,165 +0,0 @@
1-title: Enumerations, sets, variants and match
2-part: Set
3-summary: Names in order, sets as bits, and cases the compiler makes you handle.
4----
5-```raz Shapes run
6-```
7-
8-## Enumerations
9-
10-An enumeration is a list of names, in order. They convert to their position and back, and a `for` loop can run over them. An integer that has no name at its position is a failed check, never a silent wrong value.
11-
12-```raz Days run
13-module Days;
14-import Out;
15-
16-type Day = (mon, tue, wed, thu, fri, sat, sun);
17-
18-proc name(d: Day): []U8;
19-begin
20- match d of
21- | mon: return "Monday";
22- | tue: return "Tuesday";
23- | wed: return "Wednesday";
24- | thu: return "Thursday";
25- | fri: return "Friday";
26- | sat: return "Saturday";
27- | sun: return "Sunday";
28- end;
29-end;
30-
31-proc main*;
32-var d: Day;
33-begin
34- for d := mon to sun do
35- Out.int(I64(d)); Out.str(" "); Out.str(name(d));
36- if d >= sat then Out.str(" (weekend)"); end;
37- Out.ln;
38- end;
39- d := Day(4);
40- Out.str(name(d)); Out.ln;
41-end;
42-
43-end Days.
44-```
45-
46-## Sets
47-
48-A `set of` an enumeration holds any choice of its names, in the bits of a word. `in` tests membership; `+` is union, `*` intersection and `-` difference; `{}` is the empty set. Sets are how Unix permissions, option flags and key modifiers are best written.
49-
50-```raz Perms run
51-module Perms;
52-import Out;
53-
54-type
55- Perm = (x, w, r); (* bits 0, 1, 2, as chmod has them *)
56- Perms = set of Perm;
57-
58-proc show(p: Perms);
59-begin
60- if r in p then Out.char('r'); else Out.char('-'); end;
61- if w in p then Out.char('w'); else Out.char('-'); end;
62- if x in p then Out.char('x'); else Out.char('-'); end;
63-end;
64-
65-proc main*;
66-var user, group, mine: Perms;
67-begin
68- user := {r, w, x};
69- group := {r, x};
70- show(user); Out.char(' '); show(group); Out.ln;
71- show(user * group); Out.char(' '); (* both: r-x *)
72- show(user - group); Out.char(' '); (* only user: -w- *)
73- show(user - {w}); Out.ln; (* r-x *)
74- mine := group;
75- if mine = {r, x} then Out.str("same as group\n"); end;
76- Out.int(I64(U8(user))); Out.ln; (* 7: a set is its bits *)
77-end;
78-
79-end Perms.
80-```
81-
82-## Variants
83-
84-A variant holds one of its cases, each with fields of its own, and `match` is the only way to read them: it runs the arm of the case the variant holds, naming its fields. Every case has an arm, unless an `| else:` comes last, so adding a case to the type makes every `match` that forgot it a compile error.
85-
86-```raz Tokens run
87-module Tokens;
88-import Out;
89-
90-type
91- Token = variant
92- number(n: INT);
93- plus;
94- times;
95- word(s: []U8);
96- end;
97-
98-proc describe(t: Token);
99-begin
100- match t of
101- | number(n): Out.str("number "); Out.int(n);
102- | plus: Out.str("plus");
103- | times: Out.str("times");
104- | word(s): Out.str("word '"); Out.str(s); Out.char('\'');
105- end;
106- Out.ln;
107-end;
108-
109-(* evaluates tokens in reverse Polish; assumes the input is well formed *)
110-proc eval(ts: []Token): INT;
111-var stack: [16]INT; top, i, b: INT;
112-begin
113- top := 0;
114- for i := 0 to LEN(ts) - 1 do
115- match ts[i] of
116- | number(n): stack[top] := n; INC(top);
117- | plus: b := stack[top - 1]; DEC(top); stack[top - 1] := stack[top - 1] + b;
118- | times: b := stack[top - 1]; DEC(top); stack[top - 1] := stack[top - 1] * b;
119- | else:
120- end;
121- end;
122- return stack[0];
123-end;
124-
125-proc main*;
126-var ts: [5]Token; i: INT;
127-begin
128- ts := [number(3), number(4), plus, number(5), times]; (* (3 + 4) * 5 *)
129- for i := 0 to 4 do describe(ts[i]); end;
130- Out.int(eval(ts)); Out.ln;
131- describe(word("hello"));
132-end;
133-
134-end Tokens.
135-```
136-
137-## Matching integers
138-
139-On an integer, `match` takes constants and ranges, and must have an `| else:`. Characters are integers, so it is also how a scanner is written.
140-
141-```raz Classify run
142-module Classify;
143-import Out;
144-
145-proc kind(c: I32): []U8;
146-begin
147- match c of
148- | ' ', '\t', '\n': return "space";
149- | '0' .. '9': return "digit";
150- | 'a' .. 'z', 'A' .. 'Z', '_': return "letter";
151- | else: return "other";
152- end;
153-end;
154-
155-proc main*;
156-var s: []U8; i: INT;
157-begin
158- s := "x1 + _y";
159- for i := 0 to LEN(s) - 1 do
160- Out.char(s[i]); Out.str(" is "); Out.str(kind(I32(s[i]))); Out.ln;
161- end;
162-end;
163-
164-end Classify.
165-```

site/content/tour/08-memory.txt deleted +0 -119

@@ -1,119 +0,0 @@
1-title: Memory and allocators
2-part: Set
3-summary: No garbage collector; you choose where memory comes from, and what happens when it runs out.
4----
5-```raz Stack run
6-```
7-
8-raz has no garbage collector, and no allocator of its own: memory comes from an allocator, a `Mem.Allocator`, which the program chooses and passes. `NEW(n, a)` sets `n` to a fresh, zeroed `Node` from `a`, or to `nil` if `a` has no room. `Heap.heap` is the general one, which `Mem.free` gives memory back to. An `Arena` hands out memory from one block and frees it all at once, as the last lines above do.
9-
10-## Why an allocator is a parameter
11-
12-A procedure that allocates takes the allocator it uses, so its caller decides where the memory comes from and when it goes: the heap for something that lives long, an arena for everything one request needs. Nothing in the library allocates behind your back.
13-
14-```raz Pool run
15-module Pool;
16-import Out, Mem, Arena;
17-
18-type Item = record id: INT; next: ^Item; end;
19-
20-(* n items, linked; or nil if the allocator ran dry (what came is lost with it) *)
21-proc make(n: INT; a: ^Mem.Allocator): ^Item;
22-var head, it: ^Item; i: INT;
23-begin
24- head := nil;
25- for i := 1 to n do
26- NEW(it, a);
27- if it = nil then return nil; end;
28- it^.id := i;
29- it^.next := head;
30- head := it;
31- end;
32- return head;
33-end;
34-
35-proc count(it: ^Item): INT;
36-var n: INT;
37-begin
38- n := 0;
39- while it # nil do INC(n); it := it^.next; end;
40- return n;
41-end;
42-
43-proc main*;
44-var ar: Arena.Arena; small: Arena.Arena; list: ^Item;
45-begin
46- if not Arena.init(ADR(ar), 1000000) then return; end;
47- list := make(1000, ADR(ar.base));
48- Out.int(count(list)); Out.str(" items from a big arena\n");
49- Arena.reset(ADR(ar)); (* every allocation forgotten *)
50- list := make(10, ADR(ar.base));
51- Out.int(count(list)); Out.str(" after a reset\n");
52- Arena.done(ADR(ar));
53-
54- if not Arena.init(ADR(small), 4096) then return; end;
55- list := make(1000, ADR(small.base));
56- if list = nil then Out.str("a small arena ran out, and said so\n"); end;
57- Arena.done(ADR(small));
58-end;
59-
60-end Pool.
61-```
62-
63-The run-out case is an ordinary `nil` to test, not an exception that unwinds past your code.
64-
65-## The heap
66-
67-`Heap.heap` serves any size and takes memory back with `Mem.free`. Freeing a block twice, or one that was never given out, stops the program with a message instead of corrupting the heap. It is safe to call from several threads.
68-
69-```raz Free run
70-module Free;
71-import Out, Mem, Heap;
72-
73-type Cell = record val: INT; end;
74-
75-proc main*;
76-var a, b: ^Cell; i, total: INT;
77-begin
78- for i := 1 to 5 do
79- NEW(a, Heap.heap);
80- a^.val := i * i;
81- total := total + a^.val;
82- Mem.free(Heap.heap, ^U8(a)); (* freed at once: its block is reused *)
83- end;
84- NEW(b, Heap.heap);
85- Out.int(total); Out.char(' '); Out.int(b^.val); Out.ln; (* a new block is zeroed *)
86- Mem.free(Heap.heap, ^U8(b));
87-end;
88-
89-end Free.
90-```
91-
92-## Buffers that grow
93-
94-`Buf` 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.
95-
96-```raz Build run
97-module Build;
98-import Out, Buf, Heap;
99-
100-proc main*;
101-var b: Buf.Buf; i: INT;
102-begin
103- Buf.init(ADR(b), Heap.heap);
104- defer Buf.done(ADR(b));
105- for i := 1 to 5 do
106- Buf.str(ADR(b), "item ");
107- Buf.add(ADR(b), U8('0' + i));
108- Buf.add(ADR(b), ',');
109- end;
110- Out.str(Buf.bytes(ADR(b))); Out.ln;
111- Out.int(b.len); Out.str(" bytes\n");
112-end;
113-
114-end Build.
115-```
116-
117-## Pages
118-
119-Under the allocators are pages from the system: `Mem.pages(size)` gives zeroed memory in whole pages, or `nil`, and `Mem.unmap` gives it back. Most programs never need to call it directly.

site/content/tour/08-variants.txt (history) added +165 -0

@@ -0,0 +1,165 @@
1+title: Enumerations, sets, variants and match
2+part: Set
3+summary: Names in order, sets as bits, and cases the compiler makes you handle.
4+---
5+```raz Shapes run
6+```
7+
8+## Enumerations
9+
10+An enumeration is a list of names, in order. They convert to their position and back, and a `for` loop can run over them. An integer that has no name at its position is a failed check, never a silent wrong value.
11+
12+```raz Days run
13+module Days;
14+import Out;
15+
16+type Day = (mon, tue, wed, thu, fri, sat, sun);
17+
18+proc name(d: Day): []U8;
19+begin
20+ match d of
21+ | mon: return "Monday";
22+ | tue: return "Tuesday";
23+ | wed: return "Wednesday";
24+ | thu: return "Thursday";
25+ | fri: return "Friday";
26+ | sat: return "Saturday";
27+ | sun: return "Sunday";
28+ end;
29+end;
30+
31+proc main*;
32+var d: Day;
33+begin
34+ for d := mon to sun do
35+ Out.int(I64(d)); Out.str(" "); Out.str(name(d));
36+ if d >= sat then Out.str(" (weekend)"); end;
37+ Out.ln;
38+ end;
39+ d := Day(4);
40+ Out.str(name(d)); Out.ln;
41+end;
42+
43+end Days.
44+```
45+
46+## Sets
47+
48+A `set of` an enumeration holds any choice of its names, in the bits of a word. `in` tests membership; `+` is union, `*` intersection and `-` difference; `{}` is the empty set. Sets are how Unix permissions, option flags and key modifiers are best written.
49+
50+```raz Perms run
51+module Perms;
52+import Out;
53+
54+type
55+ Perm = (x, w, r); (* bits 0, 1, 2, as chmod has them *)
56+ Perms = set of Perm;
57+
58+proc show(p: Perms);
59+begin
60+ if r in p then Out.char('r'); else Out.char('-'); end;
61+ if w in p then Out.char('w'); else Out.char('-'); end;
62+ if x in p then Out.char('x'); else Out.char('-'); end;
63+end;
64+
65+proc main*;
66+var user, group, mine: Perms;
67+begin
68+ user := {r, w, x};
69+ group := {r, x};
70+ show(user); Out.char(' '); show(group); Out.ln;
71+ show(user * group); Out.char(' '); (* both: r-x *)
72+ show(user - group); Out.char(' '); (* only user: -w- *)
73+ show(user - {w}); Out.ln; (* r-x *)
74+ mine := group;
75+ if mine = {r, x} then Out.str("same as group\n"); end;
76+ Out.int(I64(U8(user))); Out.ln; (* 7: a set is its bits *)
77+end;
78+
79+end Perms.
80+```
81+
82+## Variants
83+
84+A variant holds one of its cases, each with fields of its own, and `match` is the only way to read them: it runs the arm of the case the variant holds, naming its fields. Every case has an arm, unless an `| else:` comes last, so adding a case to the type makes every `match` that forgot it a compile error.
85+
86+```raz Tokens run
87+module Tokens;
88+import Out;
89+
90+type
91+ Token = variant
92+ number(n: INT);
93+ plus;
94+ times;
95+ word(s: []U8);
96+ end;
97+
98+proc describe(t: Token);
99+begin
100+ match t of
101+ | number(n): Out.str("number "); Out.int(n);
102+ | plus: Out.str("plus");
103+ | times: Out.str("times");
104+ | word(s): Out.str("word '"); Out.str(s); Out.char('\'');
105+ end;
106+ Out.ln;
107+end;
108+
109+(* evaluates tokens in reverse Polish; assumes the input is well formed *)
110+proc eval(ts: []Token): INT;
111+var stack: [16]INT; top, i, b: INT;
112+begin
113+ top := 0;
114+ for i := 0 to LEN(ts) - 1 do
115+ match ts[i] of
116+ | number(n): stack[top] := n; INC(top);
117+ | plus: b := stack[top - 1]; DEC(top); stack[top - 1] := stack[top - 1] + b;
118+ | times: b := stack[top - 1]; DEC(top); stack[top - 1] := stack[top - 1] * b;
119+ | else:
120+ end;
121+ end;
122+ return stack[0];
123+end;
124+
125+proc main*;
126+var ts: [5]Token; i: INT;
127+begin
128+ ts := [number(3), number(4), plus, number(5), times]; (* (3 + 4) * 5 *)
129+ for i := 0 to 4 do describe(ts[i]); end;
130+ Out.int(eval(ts)); Out.ln;
131+ describe(word("hello"));
132+end;
133+
134+end Tokens.
135+```
136+
137+## Matching integers
138+
139+On an integer, `match` takes constants and ranges, and must have an `| else:`. Characters are integers, so it is also how a scanner is written.
140+
141+```raz Classify run
142+module Classify;
143+import Out;
144+
145+proc kind(c: I32): []U8;
146+begin
147+ match c of
148+ | ' ', '\t', '\n': return "space";
149+ | '0' .. '9': return "digit";
150+ | 'a' .. 'z', 'A' .. 'Z', '_': return "letter";
151+ | else: return "other";
152+ end;
153+end;
154+
155+proc main*;
156+var s: []U8; i: INT;
157+begin
158+ s := "x1 + _y";
159+ for i := 0 to LEN(s) - 1 do
160+ Out.char(s[i]); Out.str(" is "); Out.str(kind(I32(s[i]))); Out.ln;
161+ end;
162+end;
163+
164+end Classify.
165+```

site/content/tour/09-errors.txt deleted +0 -140

@@ -1,140 +0,0 @@
1-title: Errors
2-part: Go
3-summary: A second result, compared by identity; passing errors up; and no exceptions.
4----
5-```raz Errors run
6-```
7-
8-A procedure that can fail returns an `ERROR` beside its result, and `nil` when it didn't. Each `ERROR` constant is an error of its own, compared with `=` by which declaration made it, never by its message; `MSG(err)` is the message. An exported one, as `Empty` is here, lets the callers tell it from the others. raz has no exceptions, so every failure is visible in the signature and handled where the call is written.
9-
10-## Handling them by kind
11-
12-Because an error is compared by identity, a caller can handle the cases it knows and pass on the rest.
13-
14-```raz Parse run
15-module Parse;
16-import Out, Str;
17-
18-const
19- Empty*: ERROR = "nothing to read";
20- Bad*: ERROR = "not a number";
21- Range*: ERROR = "out of range";
22-
23-(* a percentage, 0 to 100 *)
24-proc percent(s: []U8): (INT, ERROR);
25-var n: I64; ok: BOOL;
26-begin
27- if LEN(s) = 0 then return 0, Empty; end;
28- n, ok := Str.toInt(s);
29- if not ok then return 0, Bad; end;
30- if (n < 0) or (n > 100) then return 0, Range; end;
31- return INT(n), nil;
32-end;
33-
34-proc main*;
35-var inputs: [4][]U8; i, n: INT; err: ERROR;
36-begin
37- inputs := ["42", "", "forty", "140"];
38- for i := 0 to 3 do
39- n, err := percent(inputs[i]);
40- if err = nil then
41- Out.int(n); Out.str("%");
42- elsif err = Empty then
43- Out.str("(skipped)");
44- else
45- Out.str("error: "); Out.str(MSG(err));
46- end;
47- Out.ln;
48- end;
49-end;
50-
51-end Parse.
52-```
53-
54-## Passing errors up
55-
56-When a procedure can't do anything useful with an error, it returns it, so the first caller that can decide gets it. There is no hidden unwinding: the `return` is right there.
57-
58-```raz Layers run
59-module Layers;
60-import Out, Str;
61-
62-const Missing*: ERROR = "key missing";
63-
64-(* the number after "n=" in a line like "n=17" *)
65-proc value(line: []U8): (INT, ERROR);
66-var n: I64; ok: BOOL;
67-begin
68- if not Str.hasPrefix(line, "n=") then return 0, Missing; end;
69- n, ok := Str.toInt(line[2..LEN(line)]);
70- if not ok then return 0, Missing; end;
71- return INT(n), nil;
72-end;
73-
74-(* the sum of the values of two lines *)
75-proc sum(a, b: []U8): (INT, ERROR);
76-var x, y: INT; err: ERROR;
77-begin
78- x, err := value(a);
79- if err # nil then return 0, err; end; (* passed up *)
80- y, err := value(b);
81- if err # nil then return 0, err; end;
82- return x + y, nil;
83-end;
84-
85-proc main*;
86-var n: INT; err: ERROR;
87-begin
88- n, err := sum("n=40", "n=2");
89- if err = nil then Out.int(n); Out.ln; end;
90- n, err := sum("n=40", "x=2");
91- if err # nil then Out.str("sum failed: "); Out.str(MSG(err)); Out.ln; end;
92-end;
93-
94-end Layers.
95-```
96-
97-## When the library reports errors
98-
99-Library procedures that can fail say so in the same way: `Thread.spawn` returns an `ERROR`; `Sys` calls return a negative `errno` as an `INT`, with `0` or a positive count for success; `Files.read` returns a `BOOL`. The first is the language's way; the others are the system's, left as they are.
100-
101-```raz Probe run
102-module Probe;
103-import Out, Sys;
104-
105-proc main*;
106-var fd: INT;
107-begin
108- fd := Sys.openRead("/no/such/file");
109- if fd < 0 then
110- Out.str("open failed, errno "); Out.int(-fd); Out.ln; (* 2: ENOENT *)
111- end;
112- fd := Sys.openRead("/dev/null");
113- if fd >= 0 then
114- Out.str("open worked\n");
115- Sys.close(fd);
116- end;
117-end;
118-
119-end Probe.
120-```
121-
122-## When there is nothing to do but stop
123-
124-A program that has found a state it can't carry on from, such as a broken invariant, says so and ends with `Rt.stop`: the message goes to standard error and the status is 2, the same as a failed check.
125-
126-```raz Giveup
127-module Giveup;
128-import Rt;
129-
130-proc main*;
131-begin
132- Rt.stop("the table is corrupt");
133-end;
134-
135-end Giveup.
136-```
137-
138-```sh
139-$ raz run Giveup.raz; echo "status $?"
140-```

site/content/tour/09-memory.txt (history) added +119 -0

@@ -0,0 +1,119 @@
1+title: Memory and allocators
2+part: Set
3+summary: No garbage collector; you choose where memory comes from, and what happens when it runs out.
4+---
5+```raz Stack run
6+```
7+
8+raz has no garbage collector, and no allocator of its own: memory comes from an allocator, a `Mem.Allocator`, which the program chooses and passes. `NEW(n, a)` sets `n` to a fresh, zeroed `Node` from `a`, or to `nil` if `a` has no room. `Heap.heap` is the general one, which `Mem.free` gives memory back to. An `Arena` hands out memory from one block and frees it all at once, as the last lines above do.
9+
10+## Why an allocator is a parameter
11+
12+A procedure that allocates takes the allocator it uses, so its caller decides where the memory comes from and when it goes: the heap for something that lives long, an arena for everything one request needs. Nothing in the library allocates behind your back.
13+
14+```raz Pool run
15+module Pool;
16+import Out, Mem, Arena;
17+
18+type Item = record id: INT; next: ^Item; end;
19+
20+(* n items, linked; or nil if the allocator ran dry (what came is lost with it) *)
21+proc make(n: INT; a: ^Mem.Allocator): ^Item;
22+var head, it: ^Item; i: INT;
23+begin
24+ head := nil;
25+ for i := 1 to n do
26+ NEW(it, a);
27+ if it = nil then return nil; end;
28+ it^.id := i;
29+ it^.next := head;
30+ head := it;
31+ end;
32+ return head;
33+end;
34+
35+proc count(it: ^Item): INT;
36+var n: INT;
37+begin
38+ n := 0;
39+ while it # nil do INC(n); it := it^.next; end;
40+ return n;
41+end;
42+
43+proc main*;
44+var ar: Arena.Arena; small: Arena.Arena; list: ^Item;
45+begin
46+ if not Arena.init(ADR(ar), 1000000) then return; end;
47+ list := make(1000, ADR(ar.base));
48+ Out.int(count(list)); Out.str(" items from a big arena\n");
49+ Arena.reset(ADR(ar)); (* every allocation forgotten *)
50+ list := make(10, ADR(ar.base));
51+ Out.int(count(list)); Out.str(" after a reset\n");
52+ Arena.done(ADR(ar));
53+
54+ if not Arena.init(ADR(small), 4096) then return; end;
55+ list := make(1000, ADR(small.base));
56+ if list = nil then Out.str("a small arena ran out, and said so\n"); end;
57+ Arena.done(ADR(small));
58+end;
59+
60+end Pool.
61+```
62+
63+The run-out case is an ordinary `nil` to test, not an exception that unwinds past your code.
64+
65+## The heap
66+
67+`Heap.heap` serves any size and takes memory back with `Mem.free`. Freeing a block twice, or one that was never given out, stops the program with a message instead of corrupting the heap. It is safe to call from several threads.
68+
69+```raz Free run
70+module Free;
71+import Out, Mem, Heap;
72+
73+type Cell = record val: INT; end;
74+
75+proc main*;
76+var a, b: ^Cell; i, total: INT;
77+begin
78+ for i := 1 to 5 do
79+ NEW(a, Heap.heap);
80+ a^.val := i * i;
81+ total := total + a^.val;
82+ Mem.free(Heap.heap, ^U8(a)); (* freed at once: its block is reused *)
83+ end;
84+ NEW(b, Heap.heap);
85+ Out.int(total); Out.char(' '); Out.int(b^.val); Out.ln; (* a new block is zeroed *)
86+ Mem.free(Heap.heap, ^U8(b));
87+end;
88+
89+end Free.
90+```
91+
92+## Buffers that grow
93+
94+`Buf` 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.
95+
96+```raz Build run
97+module Build;
98+import Out, Buf, Heap;
99+
100+proc main*;
101+var b: Buf.Buf; i: INT;
102+begin
103+ Buf.init(ADR(b), Heap.heap);
104+ defer Buf.done(ADR(b));
105+ for i := 1 to 5 do
106+ Buf.str(ADR(b), "item ");
107+ Buf.add(ADR(b), U8('0' + i));
108+ Buf.add(ADR(b), ',');
109+ end;
110+ Out.str(Buf.bytes(ADR(b))); Out.ln;
111+ Out.int(b.len); Out.str(" bytes\n");
112+end;
113+
114+end Build.
115+```
116+
117+## Pages
118+
119+Under the allocators are pages from the system: `Mem.pages(size)` gives zeroed memory in whole pages, or `nil`, and `Mem.unmap` gives it back. Most programs never need to call it directly.

site/content/tour/10-defer.txt deleted +0 -107

@@ -1,107 +0,0 @@
1-title: Defer
2-part: Go
3-summary: Clean up beside the line that needs it, in the right order, on every way out.
4----
5-```raz Defers run
6-```
7-
8-`defer s;` runs `s` when the program leaves the statements the `defer` stands in: at their end, or by a `return` or a `break`. Deferred statements run the last first, and only those whose `defer` was reached. So a file is closed beside the line that opens it, however the procedure ends.
9-
10-## The order, and a `return` value
11-
12-A `return e;` evaluates `e` before the deferred statements run, so the value is what it was at the return.
13-
14-```raz Order run
15-module Order;
16-import Out;
17-
18-var counter: INT;
19-
20-proc next(): INT;
21-begin
22- defer INC(counter);
23- return counter; (* the old value is returned; then counter grows *)
24-end;
25-
26-proc nest;
27-begin
28- defer Out.str("1 ");
29- defer Out.str("2 ");
30- defer Out.str("3 ");
31- Out.str("body: ");
32-end;
33-
34-proc main*;
35-var a, b: INT;
36-begin
37- a := next();
38- b := next();
39- Out.int(a); Out.char(' '); Out.int(b); Out.char(' '); Out.int(counter); Out.ln;
40- nest;
41- Out.ln;
42-end;
43-
44-end Order.
45-```
46-
47-## In a loop
48-
49-A `defer` stands in the statement list that holds it, so in a loop body it runs at the end of each pass, and on a `break`.
50-
51-```raz Passes run
52-module Passes;
53-import Out;
54-
55-proc main*;
56-var i: INT;
57-begin
58- for i := 1 to 4 do
59- Out.str("pass "); Out.int(i);
60- defer Out.str(" done\n");
61- if i = 3 then break; end;
62- Out.str(" ...");
63- end;
64-end;
65-
66-end Passes.
67-```
68-
69-## Giving back what was taken
70-
71-The point is that acquiring and releasing sit next to each other, so a new early `return` added later can't leak. Here an arena and a buffer are both released on every path.
72-
73-```raz Report run
74-module Report;
75-import Out, Buf, Heap;
76-
77-(* a line, built in a buffer; false (nothing written) if n is negative *)
78-proc line(n: INT): BOOL;
79-var b: Buf.Buf;
80-begin
81- if n < 0 then return false; end;
82- Buf.init(ADR(b), Heap.heap);
83- defer Buf.done(ADR(b)); (* freed on every way out below *)
84- Buf.str(ADR(b), "n is ");
85- if n > 9 then
86- Buf.str(ADR(b), "large");
87- Out.str(Buf.bytes(ADR(b))); Out.ln;
88- return true;
89- end;
90- Buf.str(ADR(b), "small");
91- Out.str(Buf.bytes(ADR(b))); Out.ln;
92- return true;
93-end;
94-
95-proc main*;
96-begin
97- _ := line(3);
98- _ := line(30);
99- if not line(-1) then Out.str("rejected\n"); end;
100-end;
101-
102-end Report.
103-```
104-
105-## What defer does not do
106-
107-A deferred statement can't contain a `return`, `break` or `defer` that would leave it. A failed check, `Rt.exit` and `Sys.exit` end the program without running the deferred statements still pending, so deferred cleanup is for the program's resources, not for what must happen at exit; that is what `Rt.atExit` is for.

site/content/tour/10-errors.txt (history) added +140 -0

@@ -0,0 +1,140 @@
1+title: Errors
2+part: Go
3+summary: A second result, compared by identity; passing errors up; and no exceptions.
4+---
5+```raz Errors run
6+```
7+
8+A procedure that can fail returns an `ERROR` beside its result, and `nil` when it didn't. Each `ERROR` constant is an error of its own, compared with `=` by which declaration made it, never by its message; `MSG(err)` is the message. An exported one, as `Empty` is here, lets the callers tell it from the others. raz has no exceptions, so every failure is visible in the signature and handled where the call is written.
9+
10+## Handling them by kind
11+
12+Because an error is compared by identity, a caller can handle the cases it knows and pass on the rest.
13+
14+```raz Parse run
15+module Parse;
16+import Out, Str;
17+
18+const
19+ Empty*: ERROR = "nothing to read";
20+ Bad*: ERROR = "not a number";
21+ Range*: ERROR = "out of range";
22+
23+(* a percentage, 0 to 100 *)
24+proc percent(s: []U8): (INT, ERROR);
25+var n: I64; ok: BOOL;
26+begin
27+ if LEN(s) = 0 then return 0, Empty; end;
28+ n, ok := Str.toInt(s);
29+ if not ok then return 0, Bad; end;
30+ if (n < 0) or (n > 100) then return 0, Range; end;
31+ return INT(n), nil;
32+end;
33+
34+proc main*;
35+var inputs: [4][]U8; i, n: INT; err: ERROR;
36+begin
37+ inputs := ["42", "", "forty", "140"];
38+ for i := 0 to 3 do
39+ n, err := percent(inputs[i]);
40+ if err = nil then
41+ Out.int(n); Out.str("%");
42+ elsif err = Empty then
43+ Out.str("(skipped)");
44+ else
45+ Out.str("error: "); Out.str(MSG(err));
46+ end;
47+ Out.ln;
48+ end;
49+end;
50+
51+end Parse.
52+```
53+
54+## Passing errors up
55+
56+When a procedure can't do anything useful with an error, it returns it, so the first caller that can decide gets it. There is no hidden unwinding: the `return` is right there.
57+
58+```raz Layers run
59+module Layers;
60+import Out, Str;
61+
62+const Missing*: ERROR = "key missing";
63+
64+(* the number after "n=" in a line like "n=17" *)
65+proc value(line: []U8): (INT, ERROR);
66+var n: I64; ok: BOOL;
67+begin
68+ if not Str.hasPrefix(line, "n=") then return 0, Missing; end;
69+ n, ok := Str.toInt(line[2..LEN(line)]);
70+ if not ok then return 0, Missing; end;
71+ return INT(n), nil;
72+end;
73+
74+(* the sum of the values of two lines *)
75+proc sum(a, b: []U8): (INT, ERROR);
76+var x, y: INT; err: ERROR;
77+begin
78+ x, err := value(a);
79+ if err # nil then return 0, err; end; (* passed up *)
80+ y, err := value(b);
81+ if err # nil then return 0, err; end;
82+ return x + y, nil;
83+end;
84+
85+proc main*;
86+var n: INT; err: ERROR;
87+begin
88+ n, err := sum("n=40", "n=2");
89+ if err = nil then Out.int(n); Out.ln; end;
90+ n, err := sum("n=40", "x=2");
91+ if err # nil then Out.str("sum failed: "); Out.str(MSG(err)); Out.ln; end;
92+end;
93+
94+end Layers.
95+```
96+
97+## When the library reports errors
98+
99+Library procedures that can fail say so in the same way: `Thread.spawn` returns an `ERROR`; `Sys` calls return a negative `errno` as an `INT`, with `0` or a positive count for success; `Files.read` returns a `BOOL`. The first is the language's way; the others are the system's, left as they are.
100+
101+```raz Probe run
102+module Probe;
103+import Out, Sys;
104+
105+proc main*;
106+var fd: INT;
107+begin
108+ fd := Sys.openRead("/no/such/file");
109+ if fd < 0 then
110+ Out.str("open failed, errno "); Out.int(-fd); Out.ln; (* 2: ENOENT *)
111+ end;
112+ fd := Sys.openRead("/dev/null");
113+ if fd >= 0 then
114+ Out.str("open worked\n");
115+ Sys.close(fd);
116+ end;
117+end;
118+
119+end Probe.
120+```
121+
122+## When there is nothing to do but stop
123+
124+A program that has found a state it can't carry on from, such as a broken invariant, says so and ends with `Rt.stop`: the message goes to standard error and the status is 2, the same as a failed check.
125+
126+```raz Giveup
127+module Giveup;
128+import Rt;
129+
130+proc main*;
131+begin
132+ Rt.stop("the table is corrupt");
133+end;
134+
135+end Giveup.
136+```
137+
138+```sh
139+$ raz run Giveup.raz; echo "status $?"
140+```

site/content/tour/11-checks.txt deleted +0 -130

@@ -1,130 +0,0 @@
1-title: When it goes wrong
2-part: Go
3-summary: Every index is checked, a failure says where and with what values, and how to read it.
4----
5-```raz Fail fail
6-```
7-
8-Every index and slice is checked. A failed check says where it failed and with what values, then the calls under way with their variables, and ends the program with status 2. The three `0`s are what it printed before the fourth pass went one past the end.
9-
10-## Reading the message
11-
12-The first line is the place of the offending expression: file, line and column, then what failed and the numbers involved. Each `in` line after it is a call still under way, innermost first, with the basic-typed variables it held. That is usually enough to see the bug without a debugger.
13-
14-## Other failures
15-
16-The same shape of message tells of a `nil` pointer, a division by zero, a bad slice and a stack overflow. Each of them stops the program with status 2, or by the signal if the machine caught it, and never runs on with a wrong value.
17-
18-```raz Nilp fail
19-module Nilp;
20-import Out;
21-
22-type Node = record val: INT; next: ^Node; end;
23-
24-proc last(n: ^Node): INT;
25-begin
26- while n^.next # nil do n := n^.next; end;
27- return n^.val;
28-end;
29-
30-proc main*;
31-var a, b: Node;
32-begin
33- a.val := 1; b.val := 2;
34- a.next := ADR(b);
35- Out.int(last(ADR(a))); Out.ln;
36- Out.int(last(nil)); Out.ln;
37-end;
38-
39-end Nilp.
40-```
41-
42-```raz Zero fail
43-module Zero;
44-import Out;
45-
46-proc ratio(a, b: INT): INT;
47-begin
48- return a div b;
49-end;
50-
51-proc main*;
52-var n: INT;
53-begin
54- Out.int(ratio(10, 3)); Out.ln;
55- Out.int(ratio(10, n)); Out.ln; (* n is zero: every variable starts at zero *)
56-end;
57-
58-end Zero.
59-```
60-
61-```raz Slicing fail
62-module Slicing;
63-import Out;
64-
65-proc main*;
66-var a: [5]INT; s: []INT; hi: INT;
67-begin
68- hi := 7;
69- s := a[2..5];
70- Out.int(LEN(s)); Out.ln;
71- s := a[2..hi]; (* past the end *)
72- Out.int(LEN(s)); Out.ln;
73-end;
74-
75-end Slicing.
76-```
77-
78-In every case the output before the failure was written first. `Out`'s buffer is flushed by the failure, so what the program printed is never lost to the crash.
79-
80-## A constant can fail at compile time
81-
82-A division by a zero constant, or a constant index out of range, is not a run-time failure at all; the compiler says it at the line, so you never ship it.
83-
84-```raz Early
85-module Early;
86-import Out;
87-
88-proc main*;
89-var a: [3]INT;
90-begin
91- a[3] := 1;
92- Out.int(10 div 0); Out.ln;
93-end;
94-
95-end Early.
96-```
97-
98-```sh
99-$ raz Early.raz
100-```
101-
102-## Looking at where you are
103-
104-`Rt.trace` prints the calls under way, as a failure does, whenever you ask. It writes to standard error and the program goes on. It is the cheapest debugging tool there is.
105-
106-```raz Where run
107-module Where;
108-import Rt;
109-
110-proc inner(depth: INT);
111-begin
112- if depth = 0 then Rt.trace; return; end;
113- inner(depth - 1);
114-end;
115-
116-proc main*;
117-begin
118- inner(2);
119-end;
120-
121-end Where.
122-```
123-
124-## Leaving the checks out
125-
126-A module that is sure of its indexes may say `check off;` after its imports and links, for speed. The module's own indexing and slicing is then unchecked, so an index out of range reads or writes whatever it reaches. Every other check stays (`nil` and division), and callers of the module keep theirs. Use it for a measured inner loop, never as a first step.
127-
128-## For gdb and the profiler
129-
130-The program carries names and line numbers, so `gdb` can break at `Fail.raz:6` and show raz source. `RAZ_PROFILE=1` in the environment makes a program sample itself every millisecond and say at the end where its time went, by procedure and by line.

site/content/tour/11-defer.txt (history) added +107 -0

@@ -0,0 +1,107 @@
1+title: Defer
2+part: Go
3+summary: Clean up beside the line that needs it, in the right order, on every way out.
4+---
5+```raz Defers run
6+```
7+
8+`defer s;` runs `s` when the program leaves the statements the `defer` stands in: at their end, or by a `return` or a `break`. Deferred statements run the last first, and only those whose `defer` was reached. So a file is closed beside the line that opens it, however the procedure ends.
9+
10+## The order, and a `return` value
11+
12+A `return e;` evaluates `e` before the deferred statements run, so the value is what it was at the return.
13+
14+```raz Order run
15+module Order;
16+import Out;
17+
18+var counter: INT;
19+
20+proc next(): INT;
21+begin
22+ defer INC(counter);
23+ return counter; (* the old value is returned; then counter grows *)
24+end;
25+
26+proc nest;
27+begin
28+ defer Out.str("1 ");
29+ defer Out.str("2 ");
30+ defer Out.str("3 ");
31+ Out.str("body: ");
32+end;
33+
34+proc main*;
35+var a, b: INT;
36+begin
37+ a := next();
38+ b := next();
39+ Out.int(a); Out.char(' '); Out.int(b); Out.char(' '); Out.int(counter); Out.ln;
40+ nest;
41+ Out.ln;
42+end;
43+
44+end Order.
45+```
46+
47+## In a loop
48+
49+A `defer` stands in the statement list that holds it, so in a loop body it runs at the end of each pass, and on a `break`.
50+
51+```raz Passes run
52+module Passes;
53+import Out;
54+
55+proc main*;
56+var i: INT;
57+begin
58+ for i := 1 to 4 do
59+ Out.str("pass "); Out.int(i);
60+ defer Out.str(" done\n");
61+ if i = 3 then break; end;
62+ Out.str(" ...");
63+ end;
64+end;
65+
66+end Passes.
67+```
68+
69+## Giving back what was taken
70+
71+The point is that acquiring and releasing sit next to each other, so a new early `return` added later can't leak. Here an arena and a buffer are both released on every path.
72+
73+```raz Report run
74+module Report;
75+import Out, Buf, Heap;
76+
77+(* a line, built in a buffer; false (nothing written) if n is negative *)
78+proc line(n: INT): BOOL;
79+var b: Buf.Buf;
80+begin
81+ if n < 0 then return false; end;
82+ Buf.init(ADR(b), Heap.heap);
83+ defer Buf.done(ADR(b)); (* freed on every way out below *)
84+ Buf.str(ADR(b), "n is ");
85+ if n > 9 then
86+ Buf.str(ADR(b), "large");
87+ Out.str(Buf.bytes(ADR(b))); Out.ln;
88+ return true;
89+ end;
90+ Buf.str(ADR(b), "small");
91+ Out.str(Buf.bytes(ADR(b))); Out.ln;
92+ return true;
93+end;
94+
95+proc main*;
96+begin
97+ _ := line(3);
98+ _ := line(30);
99+ if not line(-1) then Out.str("rejected\n"); end;
100+end;
101+
102+end Report.
103+```
104+
105+## What defer does not do
106+
107+A deferred statement can't contain a `return`, `break` or `defer` that would leave it. A failed check, `Rt.exit` and `Sys.exit` end the program without running the deferred statements still pending, so deferred cleanup is for the program's resources, not for what must happen at exit; that is what `Rt.atExit` is for.

site/content/tour/12-checks.txt (history) added +130 -0

@@ -0,0 +1,130 @@
1+title: When it goes wrong
2+part: Go
3+summary: Every index is checked, a failure says where and with what values, and how to read it.
4+---
5+```raz Fail fail
6+```
7+
8+Every index and slice is checked. A failed check says where it failed and with what values, then the calls under way with their variables, and ends the program with status 2. The three `0`s are what it printed before the fourth pass went one past the end.
9+
10+## Reading the message
11+
12+The first line is the place of the offending expression: file, line and column, then what failed and the numbers involved. Each `in` line after it is a call still under way, innermost first, with the basic-typed variables it held. That is usually enough to see the bug without a debugger.
13+
14+## Other failures
15+
16+The same shape of message tells of a `nil` pointer, a division by zero, a bad slice and a stack overflow. Each of them stops the program with status 2, or by the signal if the machine caught it, and never runs on with a wrong value.
17+
18+```raz Nilp fail
19+module Nilp;
20+import Out;
21+
22+type Node = record val: INT; next: ^Node; end;
23+
24+proc last(n: ^Node): INT;
25+begin
26+ while n^.next # nil do n := n^.next; end;
27+ return n^.val;
28+end;
29+
30+proc main*;
31+var a, b: Node;
32+begin
33+ a.val := 1; b.val := 2;
34+ a.next := ADR(b);
35+ Out.int(last(ADR(a))); Out.ln;
36+ Out.int(last(nil)); Out.ln;
37+end;
38+
39+end Nilp.
40+```
41+
42+```raz Zero fail
43+module Zero;
44+import Out;
45+
46+proc ratio(a, b: INT): INT;
47+begin
48+ return a div b;
49+end;
50+
51+proc main*;
52+var n: INT;
53+begin
54+ Out.int(ratio(10, 3)); Out.ln;
55+ Out.int(ratio(10, n)); Out.ln; (* n is zero: every variable starts at zero *)
56+end;
57+
58+end Zero.
59+```
60+
61+```raz Slicing fail
62+module Slicing;
63+import Out;
64+
65+proc main*;
66+var a: [5]INT; s: []INT; hi: INT;
67+begin
68+ hi := 7;
69+ s := a[2..5];
70+ Out.int(LEN(s)); Out.ln;
71+ s := a[2..hi]; (* past the end *)
72+ Out.int(LEN(s)); Out.ln;
73+end;
74+
75+end Slicing.
76+```
77+
78+In every case the output before the failure was written first. `Out`'s buffer is flushed by the failure, so what the program printed is never lost to the crash.
79+
80+## A constant can fail at compile time
81+
82+A division by a zero constant, or a constant index out of range, is not a run-time failure at all; the compiler says it at the line, so you never ship it.
83+
84+```raz Early
85+module Early;
86+import Out;
87+
88+proc main*;
89+var a: [3]INT;
90+begin
91+ a[3] := 1;
92+ Out.int(10 div 0); Out.ln;
93+end;
94+
95+end Early.
96+```
97+
98+```sh
99+$ raz Early.raz
100+```
101+
102+## Looking at where you are
103+
104+`Rt.trace` prints the calls under way, as a failure does, whenever you ask. It writes to standard error and the program goes on. It is the cheapest debugging tool there is.
105+
106+```raz Where run
107+module Where;
108+import Rt;
109+
110+proc inner(depth: INT);
111+begin
112+ if depth = 0 then Rt.trace; return; end;
113+ inner(depth - 1);
114+end;
115+
116+proc main*;
117+begin
118+ inner(2);
119+end;
120+
121+end Where.
122+```
123+
124+## Leaving the checks out
125+
126+A module that is sure of its indexes may say `check off;` after its imports and links, for speed. The module's own indexing and slicing is then unchecked, so an index out of range reads or writes whatever it reaches. Every other check stays (`nil` and division), and callers of the module keep theirs. Use it for a measured inner loop, never as a first step.
127+
128+## For gdb and the profiler
129+
130+The program carries names and line numbers, so `gdb` can break at `Fail.raz:6` and show raz source. `RAZ_PROFILE=1` in the environment makes a program sample itself every millisecond and say at the end where its time went, by procedure and by line.

site/content/tour/12-generics.txt deleted +0 -104

@@ -1,104 +0,0 @@
1-title: Generic modules
2-part: Go
3-summary: A module that takes types and procedures; the library's List, Map and Sort.
4----
5-A module may take types, and the procedures it needs on them, as parameters. `Biggest.raz`:
6-
7-```raz Biggest
8-```
9-
10-and a program that uses it:
11-
12-```raz Generics run
13-```
14-
15-Each import with its own arguments makes an instance: the module compiled again, with `T` standing for the type given, and `less` calling the procedure given. The arguments may name what the importing module declares later, as `before` is here. There is no run-time cost for being generic, and no boxing: `Ints.max` is exactly the code you would write by hand for `INT`.
16-
17-## Writing your own
18-
19-A generic module is an ordinary module with parameters in its header. Here is a stack of anything, which takes the allocator it grows from.
20-
21-```raz Stk
22-module Stk(T);
23-import Mem;
24-
25-type
26- Stack* = record
27- items: ^T; (* an array of cap Ts *)
28- cap*, len*: INT;
29- a: ^Mem.Allocator;
30- end;
31-
32-proc init*(s: ^Stack; a: ^Mem.Allocator);
33-begin
34- s^.a := a;
35- s^.items := nil;
36- s^.cap := 0;
37- s^.len := 0;
38-end;
39-
40-(* v on top; false if the allocator has no room *)
41-proc push*(s: ^Stack; v: T): BOOL;
42-var bigger: ^T; i, cap: INT; src, dst: ^T;
43-begin
44- if s^.len = s^.cap then
45- if s^.cap = 0 then cap := 4; else cap := s^.cap * 2; end;
46- bigger := ^T(Mem.alloc(s^.a, cap * SIZE(T), 8));
47- if bigger = nil then return false; end;
48- for i := 0 to s^.len - 1 do
49- src := ^T(INT(s^.items) + i * SIZE(T));
50- dst := ^T(INT(bigger) + i * SIZE(T));
51- dst^ := src^;
52- end;
53- if s^.items # nil then Mem.free(s^.a, ^U8(s^.items)); end;
54- s^.items := bigger;
55- s^.cap := cap;
56- end;
57- dst := ^T(INT(s^.items) + s^.len * SIZE(T));
58- dst^ := v;
59- INC(s^.len);
60- return true;
61-end;
62-
63-(* the top value, taken off; false if empty *)
64-proc pop*(s: ^Stack): (T, BOOL);
65-var z: T; p: ^T;
66-begin
67- if s^.len = 0 then return z, false; end;
68- DEC(s^.len);
69- p := ^T(INT(s^.items) + s^.len * SIZE(T));
70- return p^, true;
71-end;
72-
73-end Stk.
74-```
75-
76-```raz Uses run
77-module Uses;
78-import Out, Heap, Nums := Stk(INT), Pairs := Stk(Pair);
79-
80-type Pair = record a, b: INT; end;
81-
82-proc main*;
83-var n: Nums.Stack; p: Pairs.Stack; i, v: INT; ok: BOOL; pr: Pair;
84-begin
85- Nums.init(ADR(n), Heap.heap);
86- Pairs.init(ADR(p), Heap.heap);
87- for i := 1 to 10 do _ := Nums.push(ADR(n), i * i); end;
88- _ := Pairs.push(ADR(p), {a: 3, b: 4});
89- v, ok := Nums.pop(ADR(n));
90- Out.int(v); Out.str(" of "); Out.int(n.len + 1); Out.ln;
91- pr, ok := Pairs.pop(ADR(p));
92- Out.int(pr.a * pr.b); Out.ln;
93- _, ok := Pairs.pop(ADR(p));
94- if not ok then Out.str("empty\n"); end;
95-end;
96-
97-end Uses.
98-```
99-
100-Two instances (`Nums` and `Pairs`) are two separate modules, compiled with their own `T`. Its pointer arithmetic is the one place where the program does what C does and leaves the checks behind; a library would keep that inside, as this does.
101-
102-## The library's generics
103-
104-The library's `List`, `Map` and `Sort` are generic modules. A `Map` takes its key hash and equality as procedure arguments: `import Count := Map([]U8, INT, Str.hash, Str.eq);`. The advanced tour uses each of them in a real program.

site/content/tour/13-c.txt deleted +0 -89

@@ -1,89 +0,0 @@
1-title: Talking to C
2-part: Go
3-summary: Call a C library, be called back, share variables, and mind the two buffers.
4----
5-```raz Qsort run
6-```
7-
8-An `extern` procedure is found by its name in the C library, or in a library a `link` line names. Its parameters are integers, reals, pointers and procedures, as C declares them; a raz procedure of such types can be given to C to call back, as `order` is. A record goes to C by pointer, never by value.
9-
10-## What using C costs
11-
12-A program that uses C is linked against it dynamically and starts through it, so that C's own set-up and clean-up run; one that doesn't is a static executable with nothing in it but its own code. A program uses C if any module it uses has an `extern` or a `link` of a library.
13-
14-## Varargs
15-
16-`...` after an `extern`'s parameters takes any number of further arguments, as `printf` wants. A string literal goes as a C string, an integer constant as an `INT`, a real constant as an `F64`. An `F32` is an error there, since C promotes it; write `F64(x)`.
17-
18-```raz Say run
19-module Say;
20-import Out;
21-
22-extern proc printf(format: ^U8; ...): I32;
23-extern proc fflush(f: ^U8): I32;
24-extern proc puts(s: ^U8): I32;
25-
26-proc main*;
27-var n: INT; x: F32;
28-begin
29- n := 42;
30- x := 2.5;
31- Out.str("raz first\n");
32- Out.flush; (* Out has a buffer; so does C *)
33- _ := printf("C says %ld and %.2f and %s\n", n, F64(x), "hello");
34- _ := puts("and puts");
35- _ := fflush(nil); (* nil: every C stream *)
36- Out.str("raz again\n");
37-end;
38-
39-end Say.
40-```
41-
42-`Out` and C's `printf` keep two buffers, so a program that writes with both flushes one before it writes with the other, or the text comes out of order.
43-
44-## Libraries, and C's variables
45-
46-`link "libm.so.6";` names a shared library to load at run time. An `extern var` is C's variable itself: reading it reads C's, assigning it writes C's.
47-
48-```raz Env run
49-module Env;
50-import Out;
51-
52-extern var environ: ^^U8;
53-extern proc strlen(s: ^U8): UINT;
54-extern proc getenv(name: ^U8): ^U8;
55-
56-proc main*;
57-var p: ^U8; n: INT;
58-begin
59- n := 0;
60- while environ[n] # nil do INC(n); end;
61- if n > 0 then Out.str("the environment is not empty\n"); end;
62- p := getenv("RAZ_NO_SUCH_VARIABLE");
63- if p = nil then Out.str("not set\n"); end;
64- Out.int(INT(strlen("six"))); Out.ln;
65-end;
66-
67-end Env.
68-```
69-
70-## Callbacks
71-
72-Every raz procedure uses the System V calling convention, so any with parameters and a result of C's types may be given to C to call, as `qsort` is given `order` above. The procedure may not be a closure: it has only its arguments, so state travels through the pointer C hands back or a module variable.
73-
74-```raz Roots run
75-module Roots;
76-import Fmt;
77-
78-link "libm.so.6";
79-
80-extern proc cbrt(x: F64): F64;
81-extern proc hypot(x, y: F64): F64;
82-
83-proc main*;
84-begin
85- Fmt.print("{} {}\n", [Fmt.real(cbrt(27.0), 3), Fmt.real(hypot(3.0, 4.0), 3)]);
86-end;
87-
88-end Roots.
89-```

site/content/tour/13-generics.txt (history) added +104 -0

@@ -0,0 +1,104 @@
1+title: Generic modules
2+part: Go
3+summary: A module that takes types and procedures; the library's List, Map and Sort.
4+---
5+A module may take types, and the procedures it needs on them, as parameters. `Biggest.raz`:
6+
7+```raz Biggest
8+```
9+
10+and a program that uses it:
11+
12+```raz Generics run
13+```
14+
15+Each import with its own arguments makes an instance: the module compiled again, with `T` standing for the type given, and `less` calling the procedure given. The arguments may name what the importing module declares later, as `before` is here. There is no run-time cost for being generic, and no boxing: `Ints.max` is exactly the code you would write by hand for `INT`.
16+
17+## Writing your own
18+
19+A generic module is an ordinary module with parameters in its header. Here is a stack of anything, which takes the allocator it grows from.
20+
21+```raz Stk
22+module Stk(T);
23+import Mem;
24+
25+type
26+ Stack* = record
27+ items: ^T; (* an array of cap Ts *)
28+ cap*, len*: INT;
29+ a: ^Mem.Allocator;
30+ end;
31+
32+proc init*(s: ^Stack; a: ^Mem.Allocator);
33+begin
34+ s^.a := a;
35+ s^.items := nil;
36+ s^.cap := 0;
37+ s^.len := 0;
38+end;
39+
40+(* v on top; false if the allocator has no room *)
41+proc push*(s: ^Stack; v: T): BOOL;
42+var bigger: ^T; i, cap: INT; src, dst: ^T;
43+begin
44+ if s^.len = s^.cap then
45+ if s^.cap = 0 then cap := 4; else cap := s^.cap * 2; end;
46+ bigger := ^T(Mem.alloc(s^.a, cap * SIZE(T), 8));
47+ if bigger = nil then return false; end;
48+ for i := 0 to s^.len - 1 do
49+ src := ^T(INT(s^.items) + i * SIZE(T));
50+ dst := ^T(INT(bigger) + i * SIZE(T));
51+ dst^ := src^;
52+ end;
53+ if s^.items # nil then Mem.free(s^.a, ^U8(s^.items)); end;
54+ s^.items := bigger;
55+ s^.cap := cap;
56+ end;
57+ dst := ^T(INT(s^.items) + s^.len * SIZE(T));
58+ dst^ := v;
59+ INC(s^.len);
60+ return true;
61+end;
62+
63+(* the top value, taken off; false if empty *)
64+proc pop*(s: ^Stack): (T, BOOL);
65+var z: T; p: ^T;
66+begin
67+ if s^.len = 0 then return z, false; end;
68+ DEC(s^.len);
69+ p := ^T(INT(s^.items) + s^.len * SIZE(T));
70+ return p^, true;
71+end;
72+
73+end Stk.
74+```
75+
76+```raz Uses run
77+module Uses;
78+import Out, Heap, Nums := Stk(INT), Pairs := Stk(Pair);
79+
80+type Pair = record a, b: INT; end;
81+
82+proc main*;
83+var n: Nums.Stack; p: Pairs.Stack; i, v: INT; ok: BOOL; pr: Pair;
84+begin
85+ Nums.init(ADR(n), Heap.heap);
86+ Pairs.init(ADR(p), Heap.heap);
87+ for i := 1 to 10 do _ := Nums.push(ADR(n), i * i); end;
88+ _ := Pairs.push(ADR(p), {a: 3, b: 4});
89+ v, ok := Nums.pop(ADR(n));
90+ Out.int(v); Out.str(" of "); Out.int(n.len + 1); Out.ln;
91+ pr, ok := Pairs.pop(ADR(p));
92+ Out.int(pr.a * pr.b); Out.ln;
93+ _, ok := Pairs.pop(ADR(p));
94+ if not ok then Out.str("empty\n"); end;
95+end;
96+
97+end Uses.
98+```
99+
100+Two instances (`Nums` and `Pairs`) are two separate modules, compiled with their own `T`. Its pointer arithmetic is the one place where the program does what C does and leaves the checks behind; a library would keep that inside, as this does.
101+
102+## The library's generics
103+
104+The library's `List`, `Map` and `Sort` are generic modules. A `Map` takes its key hash and equality as procedure arguments: `import Count := Map([]U8, INT, Str.hash, Str.eq);`. The advanced tour uses each of them in a real program.

site/content/tour/14-c.txt (history) added +89 -0

@@ -0,0 +1,89 @@
1+title: Talking to C
2+part: Go
3+summary: Call a C library, be called back, share variables, and mind the two buffers.
4+---
5+```raz Qsort run
6+```
7+
8+An `extern` procedure is found by its name in the C library, or in a library a `link` line names. Its parameters are integers, reals, pointers and procedures, as C declares them; a raz procedure of such types can be given to C to call back, as `order` is. A record goes to C by pointer, never by value.
9+
10+## What using C costs
11+
12+A program that uses C is linked against it dynamically and starts through it, so that C's own set-up and clean-up run; one that doesn't is a static executable with nothing in it but its own code. A program uses C if any module it uses has an `extern` or a `link` of a library.
13+
14+## Varargs
15+
16+`...` after an `extern`'s parameters takes any number of further arguments, as `printf` wants. A string literal goes as a C string, an integer constant as an `INT`, a real constant as an `F64`. An `F32` is an error there, since C promotes it; write `F64(x)`.
17+
18+```raz Say run
19+module Say;
20+import Out;
21+
22+extern proc printf(format: ^U8; ...): I32;
23+extern proc fflush(f: ^U8): I32;
24+extern proc puts(s: ^U8): I32;
25+
26+proc main*;
27+var n: INT; x: F32;
28+begin
29+ n := 42;
30+ x := 2.5;
31+ Out.str("raz first\n");
32+ Out.flush; (* Out has a buffer; so does C *)
33+ _ := printf("C says %ld and %.2f and %s\n", n, F64(x), "hello");
34+ _ := puts("and puts");
35+ _ := fflush(nil); (* nil: every C stream *)
36+ Out.str("raz again\n");
37+end;
38+
39+end Say.
40+```
41+
42+`Out` and C's `printf` keep two buffers, so a program that writes with both flushes one before it writes with the other, or the text comes out of order.
43+
44+## Libraries, and C's variables
45+
46+`link "libm.so.6";` names a shared library to load at run time. An `extern var` is C's variable itself: reading it reads C's, assigning it writes C's.
47+
48+```raz Env run
49+module Env;
50+import Out;
51+
52+extern var environ: ^^U8;
53+extern proc strlen(s: ^U8): UINT;
54+extern proc getenv(name: ^U8): ^U8;
55+
56+proc main*;
57+var p: ^U8; n: INT;
58+begin
59+ n := 0;
60+ while environ[n] # nil do INC(n); end;
61+ if n > 0 then Out.str("the environment is not empty\n"); end;
62+ p := getenv("RAZ_NO_SUCH_VARIABLE");
63+ if p = nil then Out.str("not set\n"); end;
64+ Out.int(INT(strlen("six"))); Out.ln;
65+end;
66+
67+end Env.
68+```
69+
70+## Callbacks
71+
72+Every raz procedure uses the System V calling convention, so any with parameters and a result of C's types may be given to C to call, as `qsort` is given `order` above. The procedure may not be a closure: it has only its arguments, so state travels through the pointer C hands back or a module variable.
73+
74+```raz Roots run
75+module Roots;
76+import Fmt;
77+
78+link "libm.so.6";
79+
80+extern proc cbrt(x: F64): F64;
81+extern proc hypot(x, y: F64): F64;
82+
83+proc main*;
84+begin
85+ Fmt.print("{} {}\n", [Fmt.real(cbrt(27.0), 3), Fmt.real(hypot(3.0, 4.0), 3)]);
86+end;
87+
88+end Roots.
89+```

site/content/tour/14-threads.txt deleted +0 -120

@@ -1,120 +0,0 @@
1-title: Threads
2-part: Go
3-summary: Native threads without C, atomics, a mutex, and why the counter needs both.
4----
5-```raz Hits run
6-```
7-
8-`Thread.spawn` runs a procedure in a thread of its own, with a stack of its own, and `Thread.join` waits for it to end. `XADD`, `CAS` and `XCHG` change a word atomically; a `Thread.Mutex` keeps a longer section to one thread at a time. `Out` and `Heap` lock themselves.
9-
10-## What goes wrong without them
11-
12-Two threads that each read a variable, add one and write it back can lose one of the additions. A plain `INC` on a variable shared by threads has no particular value afterwards; the report says so. `XADD` is the atomic add: it returns the old value and adds in one indivisible step.
13-
14-```raz Race run
15-module Race;
16-import Out, Thread;
17-
18-var
19- safe: INT; (* added to by XADD *)
20- plain: INT; (* added to by INC: unsafe, never printed *)
21-
22-proc work(_arg: ^U8);
23-var i: INT;
24-begin
25- for i := 1 to 100000 do
26- _ := XADD(ADR(safe), 1);
27- INC(plain); (* unsafe: lost additions *)
28- end;
29-end;
30-
31-proc main*;
32-var t: [4]^Thread.Thread; i: INT; err: ERROR;
33-begin
34- for i := 0 to 3 do
35- t[i], err := Thread.spawn(work, nil);
36- if err # nil then Out.str(MSG(err)); Out.ln; return; end;
37- end;
38- for i := 0 to 3 do Thread.join(t[i]); end;
39- Out.str("safe "); Out.int(safe); Out.ln;
40-end;
41-
42-end Race.
43-```
44-
45-The atomic count is always 400000. The plain one is usually lower, and different from run to run, which is why this page doesn't print it.
46-
47-## A mutex for longer sections
48-
49-An atomic is one word. When several variables must change together, take a `Thread.Mutex`; it sleeps while it waits, so it suits sections that take a while. Declare it as a module variable, since zero is free.
50-
51-```raz Bank run
52-module Bank;
53-import Out, Thread;
54-
55-var
56- m: Thread.Mutex;
57- balance, deposits: INT; (* kept in step, under m *)
58-
59-proc deposit(_arg: ^U8);
60-var i: INT;
61-begin
62- for i := 1 to 10000 do
63- Thread.lock(ADR(m));
64- balance := balance + 5;
65- INC(deposits);
66- Thread.unlock(ADR(m));
67- end;
68-end;
69-
70-proc main*;
71-var t: [3]^Thread.Thread; i: INT; err: ERROR;
72-begin
73- for i := 0 to 2 do
74- t[i], err := Thread.spawn(deposit, nil);
75- if err # nil then Out.str(MSG(err)); Out.ln; return; end;
76- end;
77- for i := 0 to 2 do Thread.join(t[i]); end;
78- Out.int(deposits); Out.char(' '); Out.int(balance); Out.ln;
79-end;
80-
81-end Bank.
82-```
83-
84-## Handing work out
85-
86-A thread's procedure takes one pointer argument. Pass a record's address to give each thread its own job and its own place to put the answer; then no one shares anything.
87-
88-```raz Jobs run
89-module Jobs;
90-import Out, Thread;
91-
92-type Job = record lo, hi: INT; sum: INT; end;
93-
94-proc work(arg: ^U8);
95-var j: ^Job; i: INT;
96-begin
97- j := ^Job(arg);
98- for i := j^.lo to j^.hi - 1 do j^.sum := j^.sum + i; end;
99-end;
100-
101-proc main*;
102-var jobs: [4]Job; t: [4]^Thread.Thread; i, total: INT; err: ERROR;
103-begin
104- for i := 0 to 3 do
105- jobs[i].lo := i * 250000;
106- jobs[i].hi := (i + 1) * 250000;
107- t[i], err := Thread.spawn(work, ^U8(ADR(jobs[i])));
108- if err # nil then Out.str(MSG(err)); Out.ln; return; end;
109- end;
110- for i := 0 to 3 do
111- Thread.join(t[i]);
112- total := total + jobs[i].sum;
113- end;
114- Out.int(total); Out.ln;
115-end;
116-
117-end Jobs.
118-```
119-
120-`Thread` makes its own threads, without C, so a program that uses it can't use C; one that does makes its threads with C's `pthread_create` instead. The advanced tour goes further into atomics, memory ordering and lock-free code.

site/content/tour/15-threads.txt (history) added +120 -0

@@ -0,0 +1,120 @@
1+title: Threads
2+part: Go
3+summary: Native threads without C, atomics, a mutex, and why the counter needs both.
4+---
5+```raz Hits run
6+```
7+
8+`Thread.spawn` runs a procedure in a thread of its own, with a stack of its own, and `Thread.join` waits for it to end. `XADD`, `CAS` and `XCHG` change a word atomically; a `Thread.Mutex` keeps a longer section to one thread at a time. `Out` and `Heap` lock themselves.
9+
10+## What goes wrong without them
11+
12+Two threads that each read a variable, add one and write it back can lose one of the additions. A plain `INC` on a variable shared by threads has no particular value afterwards; the report says so. `XADD` is the atomic add: it returns the old value and adds in one indivisible step.
13+
14+```raz Race run
15+module Race;
16+import Out, Thread;
17+
18+var
19+ safe: INT; (* added to by XADD *)
20+ plain: INT; (* added to by INC: unsafe, never printed *)
21+
22+proc work(_arg: ^U8);
23+var i: INT;
24+begin
25+ for i := 1 to 100000 do
26+ _ := XADD(ADR(safe), 1);
27+ INC(plain); (* unsafe: lost additions *)
28+ end;
29+end;
30+
31+proc main*;
32+var t: [4]^Thread.Thread; i: INT; err: ERROR;
33+begin
34+ for i := 0 to 3 do
35+ t[i], err := Thread.spawn(work, nil);
36+ if err # nil then Out.str(MSG(err)); Out.ln; return; end;
37+ end;
38+ for i := 0 to 3 do Thread.join(t[i]); end;
39+ Out.str("safe "); Out.int(safe); Out.ln;
40+end;
41+
42+end Race.
43+```
44+
45+The atomic count is always 400000. The plain one is usually lower, and different from run to run, which is why this page doesn't print it.
46+
47+## A mutex for longer sections
48+
49+An atomic is one word. When several variables must change together, take a `Thread.Mutex`; it sleeps while it waits, so it suits sections that take a while. Declare it as a module variable, since zero is free.
50+
51+```raz Bank run
52+module Bank;
53+import Out, Thread;
54+
55+var
56+ m: Thread.Mutex;
57+ balance, deposits: INT; (* kept in step, under m *)
58+
59+proc deposit(_arg: ^U8);
60+var i: INT;
61+begin
62+ for i := 1 to 10000 do
63+ Thread.lock(ADR(m));
64+ balance := balance + 5;
65+ INC(deposits);
66+ Thread.unlock(ADR(m));
67+ end;
68+end;
69+
70+proc main*;
71+var t: [3]^Thread.Thread; i: INT; err: ERROR;
72+begin
73+ for i := 0 to 2 do
74+ t[i], err := Thread.spawn(deposit, nil);
75+ if err # nil then Out.str(MSG(err)); Out.ln; return; end;
76+ end;
77+ for i := 0 to 2 do Thread.join(t[i]); end;
78+ Out.int(deposits); Out.char(' '); Out.int(balance); Out.ln;
79+end;
80+
81+end Bank.
82+```
83+
84+## Handing work out
85+
86+A thread's procedure takes one pointer argument. Pass a record's address to give each thread its own job and its own place to put the answer; then no one shares anything.
87+
88+```raz Jobs run
89+module Jobs;
90+import Out, Thread;
91+
92+type Job = record lo, hi: INT; sum: INT; end;
93+
94+proc work(arg: ^U8);
95+var j: ^Job; i: INT;
96+begin
97+ j := ^Job(arg);
98+ for i := j^.lo to j^.hi - 1 do j^.sum := j^.sum + i; end;
99+end;
100+
101+proc main*;
102+var jobs: [4]Job; t: [4]^Thread.Thread; i, total: INT; err: ERROR;
103+begin
104+ for i := 0 to 3 do
105+ jobs[i].lo := i * 250000;
106+ jobs[i].hi := (i + 1) * 250000;
107+ t[i], err := Thread.spawn(work, ^U8(ADR(jobs[i])));
108+ if err # nil then Out.str(MSG(err)); Out.ln; return; end;
109+ end;
110+ for i := 0 to 3 do
111+ Thread.join(t[i]);
112+ total := total + jobs[i].sum;
113+ end;
114+ Out.int(total); Out.ln;
115+end;
116+
117+end Jobs.
118+```
119+
120+`Thread` makes its own threads, without C, so a program that uses it can't use C; one that does makes its threads with C's `pthread_create` instead. The advanced tour goes further into atomics, memory ordering and lock-free code.

site/public/advanced/allocators.html (history) modified +3 -3

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>Writing an allocator - Beyond Go</title>
6+<title>Writing an allocator - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

site/public/advanced/asm.html (history) modified +3 -3

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>Procedures in assembly - Beyond Go</title>
6+<title>Procedures in assembly - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

site/public/advanced/atomics.html (history) modified +3 -3

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>Atomics - Beyond Go</title>
6+<title>Atomics - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

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

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>Strings and bytes - Beyond Go</title>
6+<title>Strings and bytes - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html" aria-current="page">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

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

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>Lists, maps and sorting - Beyond Go</title>
6+<title>Lists, maps and sorting - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html" aria-current="page">Lists, maps and sorting</a></li>

site/public/advanced/files.html (history) modified +3 -3

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>Files and the command line - Beyond Go</title>
6+<title>Files and the command line - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

site/public/advanced/fmt.html (history) modified +3 -3

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>Output and formatting - Beyond Go</title>
6+<title>Output and formatting - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html" aria-current="page">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

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

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>Beyond Go</title>
6+<title>Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="contents"><h1>Beyond Go</h1>
12+<div class="contents"><h1>Close to the Metal</h1>
1313 <p>The second tour, for the part of raz that sits close to the machine. It
1414 assumes you have taken <a href="../tour/index.html">Raz, Set, Go</a> or know
1515 Pascal or C well enough not to need it.</p>

site/public/advanced/lifecycle.html (history) modified +3 -3

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>Starting, ending, and signals - Beyond Go</title>
6+<title>Starting, ending, and signals - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

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

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>Threads without C, and keeping the two apart - Beyond Go</title>
6+<title>Threads without C, and keeping the two apart - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

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

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>What the compiler makes - Beyond Go</title>
6+<title>What the compiler makes - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

site/public/advanced/lockfree.html (history) modified +3 -3

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>A lock-free stack - Beyond Go</title>
6+<title>A lock-free stack - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

site/public/advanced/ordering.html (history) modified +3 -3

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>Ordering and publication - Beyond Go</title>
6+<title>Ordering and publication - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

site/public/advanced/processes.html (history) modified +3 -3

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>Time, randomness and other programs - Beyond Go</title>
6+<title>Time, randomness and other programs - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

site/public/advanced/ring.html (history) modified +3 -3

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>A ring buffer between threads - Beyond Go</title>
6+<title>A ring buffer between threads - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

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

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>A program with no run time - Beyond Go</title>
6+<title>A program with no run time - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

site/public/advanced/syscalls.html (history) modified +3 -3

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>System calls - Beyond Go</title>
6+<title>System calls - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

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

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>One module, one file per target - Beyond Go</title>
6+<title>One module, one file per target - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

site/public/advanced/volatile.html (history) modified +3 -3

@@ -3,13 +3,13 @@
33 <head>
44 <meta charset="utf-8">
55 <meta name="viewport" content="width=device-width, initial-scale=1">
6-<title>Volatile - Beyond Go</title>
6+<title>Volatile - Close to the Metal</title>
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
12-<div class="tour"><aside><h2>Beyond Go</h2><h3>The library</h3><ol start='1'>
12+<div class="tour"><aside><h2>Close to the Metal</h2><h3>The library</h3><ol start='1'>
1313 <li><a href="fmt.html">Output and formatting</a></li>
1414 <li><a href="bytes.html">Strings and bytes</a></li>
1515 <li><a href="collections.html">Lists, maps and sorting</a></li>

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

@@ -7,7 +7,7 @@
77 <link rel="stylesheet" href="style.css">
88 </head>
99 <body>
10-<header><nav><a href="index.html" aria-current="page">raz</a><a href="tour/index.html">Raz, Set, Go</a><a href="advanced/index.html">Beyond Go</a><a href="spec.html">Report</a><a href="index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="index.html" aria-current="page">raz</a><a href="tour/index.html">Raz, Set, Go</a><a href="advanced/index.html">Close to the Metal</a><a href="spec.html">Report</a><a href="index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <section class="hero">
1313 <h1>raz</h1>
@@ -15,7 +15,7 @@
1515 <em>Raz</em> is Polish for <em>one</em>, as in counting.</p>
1616 <div class="cta">
1717 <a class="btn primary" href="tour/index.html">Take the tour: Raz, Set, Go</a>
18- <a class="btn" href="advanced/index.html">Beyond Go</a>
18+ <a class="btn" href="advanced/index.html">Close to the Metal</a>
1919 <a class="btn" href="#download">Download</a>
2020 <a class="btn" href="spec.html">Read the report</a>
2121 </div>
@@ -90,8 +90,8 @@
9090
9191 <h2>Learn it</h2>
9292 <p><a href="tour/index.html"><strong>Raz, Set, Go</strong></a> is a tour of the
93-language in 14 lessons, each with programs you can run. <a
94-href="advanced/index.html"><strong>Beyond Go</strong></a> is the second tour:
93+language in 15 lessons, each with programs you can run. <a
94+href="advanced/index.html"><strong>Close to the Metal</strong></a> is the second tour:
9595 the library, programs with no run time, procedures in assembly, system calls,
9696 volatile accesses, atomics and lock-free code, in 18 more.</p>
9797

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

@@ -7,7 +7,7 @@
77 <link rel="stylesheet" href="style.css">
88 </head>
99 <body>
10-<header><nav><a href="index.html">raz</a><a href="tour/index.html">Raz, Set, Go</a><a href="advanced/index.html">Beyond Go</a><a href="spec.html" aria-current="page">Report</a><a href="index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="index.html">raz</a><a href="tour/index.html">Raz, Set, Go</a><a href="advanced/index.html">Close to the Metal</a><a href="spec.html" aria-current="page">Report</a><a href="index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="spec"><ul>
1313 <li><a href="#a-first-look" id="toc-a-first-look">1. A first

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

@@ -7,28 +7,29 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html">Hello, world</a></li>
1414 <li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html">Values and conversions</a></li>
1617 <li><a href="procedures.html">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html">Arrays, slices and strings</a></li>
2021 <li><a href="records.html">Records and pointers</a></li>
2122 <li><a href="variants.html">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html">Errors</a></li>
2627 <li><a href="defer.html">Defer</a></li>
2728 <li><a href="checks.html">When it goes wrong</a></li>
2829 <li><a href="generics.html">Generic modules</a></li>
2930 <li><a href="c.html" aria-current="page">Talking to C</a></li>
3031 <li><a href="threads.html">Threads</a></li>
31-</ol></aside><article><p class="kicker">Go &middot; lesson 13 of 14</p><h1>Talking to C</h1>
32+</ol></aside><article><p class="kicker">Go &middot; lesson 14 of 15</p><h1>Talking to C</h1>
3233 <div class="file"><span>Qsort.raz</span></div><pre class="code"><code><span class="kw">module</span> Qsort;
3334 <span class="kw">import</span> Out, Fmt;
3435

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

@@ -7,28 +7,29 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html">Hello, world</a></li>
1414 <li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html">Values and conversions</a></li>
1617 <li><a href="procedures.html">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html">Arrays, slices and strings</a></li>
2021 <li><a href="records.html">Records and pointers</a></li>
2122 <li><a href="variants.html">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html">Errors</a></li>
2627 <li><a href="defer.html">Defer</a></li>
2728 <li><a href="checks.html" aria-current="page">When it goes wrong</a></li>
2829 <li><a href="generics.html">Generic modules</a></li>
2930 <li><a href="c.html">Talking to C</a></li>
3031 <li><a href="threads.html">Threads</a></li>
31-</ol></aside><article><p class="kicker">Go &middot; lesson 11 of 14</p><h1>When it goes wrong</h1>
32+</ol></aside><article><p class="kicker">Go &middot; lesson 12 of 15</p><h1>When it goes wrong</h1>
3233 <div class="file"><span>Fail.raz</span></div><pre class="code"><code><span class="kw">module</span> Fail;
3334 <span class="kw">import</span> Out;
3435

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

@@ -7,28 +7,29 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html">Hello, world</a></li>
1414 <li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html">Values and conversions</a></li>
1617 <li><a href="procedures.html">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html">Arrays, slices and strings</a></li>
2021 <li><a href="records.html">Records and pointers</a></li>
2122 <li><a href="variants.html">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html">Errors</a></li>
2627 <li><a href="defer.html" aria-current="page">Defer</a></li>
2728 <li><a href="checks.html">When it goes wrong</a></li>
2829 <li><a href="generics.html">Generic modules</a></li>
2930 <li><a href="c.html">Talking to C</a></li>
3031 <li><a href="threads.html">Threads</a></li>
31-</ol></aside><article><p class="kicker">Go &middot; lesson 10 of 14</p><h1>Defer</h1>
32+</ol></aside><article><p class="kicker">Go &middot; lesson 11 of 15</p><h1>Defer</h1>
3233 <div class="file"><span>Defers.raz</span></div><pre class="code"><code><span class="kw">module</span> Defers;
3334 <span class="kw">import</span> Out, Sys;
3435

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

@@ -7,28 +7,29 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html">Hello, world</a></li>
1414 <li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html">Values and conversions</a></li>
1617 <li><a href="procedures.html">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html">Arrays, slices and strings</a></li>
2021 <li><a href="records.html">Records and pointers</a></li>
2122 <li><a href="variants.html">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html" aria-current="page">Errors</a></li>
2627 <li><a href="defer.html">Defer</a></li>
2728 <li><a href="checks.html">When it goes wrong</a></li>
2829 <li><a href="generics.html">Generic modules</a></li>
2930 <li><a href="c.html">Talking to C</a></li>
3031 <li><a href="threads.html">Threads</a></li>
31-</ol></aside><article><p class="kicker">Go &middot; lesson 9 of 14</p><h1>Errors</h1>
32+</ol></aside><article><p class="kicker">Go &middot; lesson 10 of 15</p><h1>Errors</h1>
3233 <div class="file"><span>Errors.raz</span></div><pre class="code"><code><span class="kw">module</span> Errors;
3334 <span class="kw">import</span> Out, Str;
3435

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

@@ -7,28 +7,29 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html">Hello, world</a></li>
1414 <li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html">Values and conversions</a></li>
1617 <li><a href="procedures.html">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html">Arrays, slices and strings</a></li>
2021 <li><a href="records.html">Records and pointers</a></li>
2122 <li><a href="variants.html">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html">Errors</a></li>
2627 <li><a href="defer.html">Defer</a></li>
2728 <li><a href="checks.html">When it goes wrong</a></li>
2829 <li><a href="generics.html" aria-current="page">Generic modules</a></li>
2930 <li><a href="c.html">Talking to C</a></li>
3031 <li><a href="threads.html">Threads</a></li>
31-</ol></aside><article><p class="kicker">Go &middot; lesson 12 of 14</p><h1>Generic modules</h1>
32+</ol></aside><article><p class="kicker">Go &middot; lesson 13 of 15</p><h1>Generic modules</h1>
3233 <p>A module may take types, and the procedures it needs on them, as parameters. <code>Biggest.raz</code>:</p>
3334 <div class="file"><span>Biggest.raz</span></div><pre class="code"><code><span class="kw">module</span> Biggest(T; less: <span class="kw">proc</span>(a, b: T): <span class="ty">BOOL</span>);
3435

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

@@ -7,28 +7,29 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html" aria-current="page">Hello, world</a></li>
1414 <li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html">Values and conversions</a></li>
1617 <li><a href="procedures.html">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html">Arrays, slices and strings</a></li>
2021 <li><a href="records.html">Records and pointers</a></li>
2122 <li><a href="variants.html">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html">Errors</a></li>
2627 <li><a href="defer.html">Defer</a></li>
2728 <li><a href="checks.html">When it goes wrong</a></li>
2829 <li><a href="generics.html">Generic modules</a></li>
2930 <li><a href="c.html">Talking to C</a></li>
3031 <li><a href="threads.html">Threads</a></li>
31-</ol></aside><article><p class="kicker">Raz &middot; lesson 1 of 14</p><h1>Hello, world</h1>
32+</ol></aside><article><p class="kicker">Raz &middot; lesson 1 of 15</p><h1>Hello, world</h1>
3233 <p>Every raz program is a module with a <code>main</code> procedure. This is the smallest one that does something.</p>
3334 <div class="file"><span>Hello.raz</span></div><pre class="code"><code><span class="kw">module</span> Hello;
3435 <span class="kw">import</span> Out;

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

@@ -7,7 +7,7 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="contents"><h1>Raz, Set, Go</h1>
1313 <p>A tour of raz, in the spirit of <em>A Tour of Go</em>. Each lesson is one
@@ -17,20 +17,21 @@
1717 first steps; <strong>Set</strong>, how data is laid out; and
1818 <strong>Go</strong>, making programs that run, fail and finish well. To follow
1919 along, <a href="../index.html#download">get raz</a> and run any program with
20-<code>raz run Name.raz</code>. When you finish, <a href="../advanced/index.html">Beyond Go</a> goes down to the machine.</p>
20+<code>raz run Name.raz</code>. When you finish, <a href="../advanced/index.html">Close to the Metal</a> goes down to the machine.</p>
2121 <h2>Raz</h2><ol start='1'>
2222 <li><a href="hello.html">Hello, world</a> <span>A module, an import, a procedure; running, printing, exit status and arguments.</span></li>
2323 <li><a href="modules.html">Modules</a> <span>Exports, renaming, the order bodies run in, and what the compiler refuses.</span></li>
24-<li><a href="values.html">Values and conversions</a> <span>Sized integers that wrap, reals, bits, and nothing converts unasked.</span></li>
24+<li><a href="integers.html">Integers</a> <span>The ten integer types, literals, wrapping, signed and unsigned, division, shifts and bits.</span></li>
25+<li><a href="values.html">Values and conversions</a> <span>Nothing converts unasked; what converts to what; booleans and reals.</span></li>
2526 <li><a href="procedures.html">Procedures and several results</a> <span>Value and var parameters, several results, forward, and procedures as values.</span></li>
2627 </ol>
27-<h2>Set</h2><ol start='5'>
28+<h2>Set</h2><ol start='6'>
2829 <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>
2930 <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>
3031 <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>
3132 <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>
3233 </ol>
33-<h2>Go</h2><ol start='9'>
34+<h2>Go</h2><ol start='10'>
3435 <li><a href="errors.html">Errors</a> <span>A second result, compared by identity; passing errors up; and no exceptions.</span></li>
3536 <li><a href="defer.html">Defer</a> <span>Clean up beside the line that needs it, in the right order, on every way out.</span></li>
3637 <li><a href="checks.html">When it goes wrong</a> <span>Every index is checked, a failure says where and with what values, and how to read it.</span></li>

site/public/tour/integers.html (history) added +277 -0

@@ -0,0 +1,277 @@
1+<!doctype html>
2+<html lang="en">
3+<head>
4+<meta charset="utf-8">
5+<meta name="viewport" content="width=device-width, initial-scale=1">
6+<title>Integers - Raz, Set, Go</title>
7+<link rel="stylesheet" href="../style.css">
8+</head>
9+<body>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
11+<main>
12+<div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
13+<li><a href="hello.html">Hello, world</a></li>
14+<li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html" aria-current="page">Integers</a></li>
16+<li><a href="values.html">Values and conversions</a></li>
17+<li><a href="procedures.html">Procedures and several results</a></li>
18+</ol>
19+<h3>Set</h3><ol start='6'>
20+<li><a href="slices.html">Arrays, slices and strings</a></li>
21+<li><a href="records.html">Records and pointers</a></li>
22+<li><a href="variants.html">Enumerations, sets, variants and match</a></li>
23+<li><a href="memory.html">Memory and allocators</a></li>
24+</ol>
25+<h3>Go</h3><ol start='10'>
26+<li><a href="errors.html">Errors</a></li>
27+<li><a href="defer.html">Defer</a></li>
28+<li><a href="checks.html">When it goes wrong</a></li>
29+<li><a href="generics.html">Generic modules</a></li>
30+<li><a href="c.html">Talking to C</a></li>
31+<li><a href="threads.html">Threads</a></li>
32+</ol></aside><article><p class="kicker">Raz &middot; lesson 3 of 15</p><h1>Integers</h1>
33+<p>raz has ten integer types, and a program that works with bytes, addresses, hashes and file offsets uses most of them. Each is a different type, so each has rules, and the rules are the same everywhere: that is the point of having them.</p>
34+<p>| Type | Size | Values | |---|---|---| | <code>I8</code> <code>I16</code> <code>I32</code> <code>I64</code> | 1, 2, 4, 8 bytes | signed, two's complement | | <code>U8</code> <code>U16</code> <code>U32</code> <code>U64</code> | 1, 2, 4, 8 bytes | unsigned | | <code>INT</code> | an address's: 8 on x86-64 | signed, as big as an address | | <code>UINT</code> | an address's | unsigned, as big as an address |</p>
35+<p><code>SIZE(T)</code> says how many bytes a type takes, and an unsigned type's largest value is what you get by taking one from zero. This program asks each type.</p>
36+<div class="file"><span>Family.raz</span></div><pre class="code"><code><span class="kw">module</span> Family;
37+<span class="kw">import</span> Out;
38+
39+<span class="kw">proc</span> main*;
40+<span class="kw">var</span> a: <span class="ty">U8</span>; b: <span class="ty">U16</span>; c: <span class="ty">U32</span>; d: <span class="ty">U64</span>;
41+<span class="kw">begin</span>
42+ a := <span class="number">0</span>; a := a - <span class="number">1</span>;
43+ b := <span class="number">0</span>; b := b - <span class="number">1</span>;
44+ c := <span class="number">0</span>; c := c - <span class="number">1</span>;
45+ d := <span class="number">0</span>; d := d - <span class="number">1</span>;
46+ Out.str(<span class="string">&quot;U8 &quot;</span>); Out.int(<span class="ty">SIZE</span>(<span class="ty">U8</span>)); Out.str(<span class="string">&quot; byte, largest &quot;</span>); Out.int(<span class="ty">I64</span>(a)); Out.ln;
47+ Out.str(<span class="string">&quot;U16 &quot;</span>); Out.int(<span class="ty">SIZE</span>(<span class="ty">U16</span>)); Out.str(<span class="string">&quot; bytes, largest &quot;</span>); Out.int(<span class="ty">I64</span>(b)); Out.ln;
48+ Out.str(<span class="string">&quot;U32 &quot;</span>); Out.int(<span class="ty">SIZE</span>(<span class="ty">U32</span>)); Out.str(<span class="string">&quot; bytes, largest &quot;</span>); Out.int(<span class="ty">I64</span>(c)); Out.ln;
49+ Out.str(<span class="string">&quot;U64 &quot;</span>); Out.int(<span class="ty">SIZE</span>(<span class="ty">U64</span>)); Out.str(<span class="string">&quot; bytes, largest 0x&quot;</span>); Out.hex(d, <span class="number">16</span>); Out.ln;
50+ Out.str(<span class="string">&quot;INT &quot;</span>); Out.int(<span class="ty">SIZE</span>(<span class="ty">INT</span>)); Out.str(<span class="string">&quot; bytes, as big as an address\n&quot;</span>);
51+ Out.str(<span class="string">&quot;I8 &quot;</span>); Out.int(<span class="ty">SIZE</span>(<span class="ty">I8</span>)); Out.str(<span class="string">&quot; byte, from -128 to 127\n&quot;</span>);
52+<span class="kw">end</span>;
53+
54+<span class="kw">end</span> Family.</code></pre>
55+<pre class="output"><code>U8 1 byte, largest 255
56+U16 2 bytes, largest 65535
57+U32 4 bytes, largest 4294967295
58+U64 8 bytes, largest 0xFFFFFFFFFFFFFFFF
59+INT 8 bytes, as big as an address
60+I8 1 byte, from -128 to 127
61+</code></pre>
62+<h2>INT is the everyday one</h2>
63+<p>Lengths, indexes, counts and sizes are <code>INT</code>s. <code>LEN(s)</code> of a slice is an <code>INT</code>, and so is the loop variable that walks it. It is as big as an address, so it is the integer a pointer converts to, and the word the atomics work on. Reach for <code>INT</code> unless you have a reason: a byte is a <code>U8</code>, a character an <code>I32</code>, a file's size an <code>I64</code>, a hash a <code>U64</code>.</p>
64+<p><code>INT</code> and <code>I64</code> are two types even where they have the same size. An <code>INT</code> goes silently into an <code>I64</code> (and a <code>UINT</code> into a <code>U64</code>), which is why <code>Out.int(n)</code> and <code>Fmt.int(n)</code> take an <code>I64</code> and accept any <code>INT</code>. Nothing else converts unasked: any other integer is converted where it is written, as <code>Out.int(I64(b))</code>.</p>
65+<h2>A number written in the program</h2>
66+<p>A number in the program text is untyped until it meets a type, and then it must fit. <code>0FFH</code> is hexadecimal (it starts with a digit and ends in <code>H</code>), a character in quotes is its code point, and <code>'\u{E9}'</code> is a code point written by number. A hexadecimal literal too big for an <code>I64</code> is a <code>U64</code>.</p>
67+<div class="file"><span>Literals.raz</span></div><pre class="code"><code><span class="kw">module</span> Literals;
68+<span class="kw">import</span> Out;
69+
70+<span class="kw">proc</span> main*;
71+<span class="kw">var</span> b: <span class="ty">U8</span>; w: <span class="ty">I16</span>; big: <span class="ty">U64</span>;
72+<span class="kw">begin</span>
73+ b := <span class="number">0FFH</span>; <span class="comment">(* fits a U8 *)</span>
74+ w := -<span class="number">300</span>;
75+ big := <span class="number">0FFFFFFFFFFFFFFFFH</span>; <span class="comment">(* above the largest I64: a U64 *)</span>
76+ Out.int(<span class="ty">I64</span>(b)); Out.char(<span class="string">&#x27; &#x27;</span>);
77+ Out.int(<span class="ty">I64</span>(w)); Out.char(<span class="string">&#x27; &#x27;</span>);
78+ Out.int(<span class="ty">I64</span>(<span class="string">&#x27;a&#x27;</span>)); Out.char(<span class="string">&#x27; &#x27;</span>);
79+ Out.int(<span class="ty">I64</span>(<span class="string">&#x27;\u{E9}&#x27;</span>)); Out.char(<span class="string">&#x27; &#x27;</span>);
80+ Out.hex(big, <span class="number">16</span>); Out.ln;
81+<span class="kw">end</span>;
82+
83+<span class="kw">end</span> Literals.</code></pre>
84+<pre class="output"><code>255 -300 97 233 FFFFFFFFFFFFFFFF
85+</code></pre>
86+<p>A constant that doesn't fit is an error, not a quiet cut. A conversion of a constant, which says you mean it, is allowed: <code>U8(300)</code> is 44.</p>
87+<div class="file"><span>Fits.raz</span></div><pre class="code"><code><span class="kw">module</span> Fits;
88+
89+<span class="kw">proc</span> main*;
90+<span class="kw">var</span> b: <span class="ty">U8</span>; c: <span class="ty">U8</span>;
91+<span class="kw">begin</span>
92+ b := <span class="number">300</span>;
93+ c := <span class="ty">U8</span>(<span class="number">300</span>);
94+<span class="kw">end</span>;
95+
96+<span class="kw">end</span> Fits.</code></pre>
97+<pre class="output term"><code><span class="cmd">$ raz Fits.raz</span>
98+Fits.raz:6:10: error: 300 doesn&#x27;t fit U8
99+ 6 | b := 300;
100+ | ^^^
101+raz: 1 error</code></pre>
102+<h2>Wrapping</h2>
103+<p>Arithmetic wraps at the type's width and never stops the program. <code>250 + 10</code> in a <code>U8</code> is 4; one more than the largest <code>I8</code> is the smallest. A unary minus wraps too: for an unsigned <code>x</code>, <code>-x</code> is <code>0 - x</code>. This is not undefined behaviour, as in C; it is what the report says, in every program, on every target, so a hash function can rely on it.</p>
104+<div class="file"><span>Wrap.raz</span></div><pre class="code"><code><span class="kw">module</span> Wrap;
105+<span class="kw">import</span> Out;
106+
107+<span class="kw">proc</span> main*;
108+<span class="kw">var</span> a: <span class="ty">U8</span>; b: <span class="ty">I8</span>; c: <span class="ty">U32</span>; d: <span class="ty">U8</span>;
109+<span class="kw">begin</span>
110+ a := <span class="number">250</span>; a := a + <span class="number">10</span>;
111+ Out.int(<span class="ty">I64</span>(a)); Out.ln; <span class="comment">(* 4 *)</span>
112+ b := <span class="number">127</span>; b := b + <span class="number">1</span>;
113+ Out.int(<span class="ty">I64</span>(b)); Out.ln; <span class="comment">(* -128 *)</span>
114+ c := <span class="number">0</span>; c := c - <span class="number">1</span>;
115+ Out.int(<span class="ty">I64</span>(c)); Out.ln; <span class="comment">(* 4294967295 *)</span>
116+ d := <span class="number">1</span>; d := -d;
117+ Out.int(<span class="ty">I64</span>(d)); Out.ln; <span class="comment">(* 255: 0 - 1 *)</span>
118+<span class="kw">end</span>;
119+
120+<span class="kw">end</span> Wrap.</code></pre>
121+<pre class="output"><code>4
122+-128
123+4294967295
124+255
125+</code></pre>
126+<p>Because wrapping is defined, a hash that depends on it is portable. Here is the FNV-1a hash that <code>Str.hash</code> uses, written in a few lines; the multiplication overflows a <code>U64</code> on every byte, on purpose.</p>
127+<div class="file"><span>Fnv.raz</span></div><pre class="code"><code><span class="kw">module</span> Fnv;
128+<span class="kw">import</span> Out, Str;
129+
130+<span class="kw">const</span> Offset = <span class="number">0CBF29CE484222325H</span>;
131+<span class="kw">const</span> Prime = <span class="number">100000001B3H</span>;
132+
133+<span class="comment">(* FNV-1a: xor each byte in, then multiply, all modulo 2^64 *)</span>
134+<span class="kw">proc</span> fnv(s: []<span class="ty">U8</span>): <span class="ty">U64</span>;
135+<span class="kw">var</span> h: <span class="ty">U64</span>; i: <span class="ty">INT</span>;
136+<span class="kw">begin</span>
137+ h := Offset;
138+ <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(s) - <span class="number">1</span> <span class="kw">do</span>
139+ h := (h <span class="kw">xor</span> <span class="ty">U64</span>(s[i])) * Prime;
140+ <span class="kw">end</span>;
141+ <span class="kw">return</span> h;
142+<span class="kw">end</span>;
143+
144+<span class="kw">proc</span> main*;
145+<span class="kw">begin</span>
146+ Out.hex(fnv(<span class="string">&quot;raz&quot;</span>), <span class="number">16</span>); Out.ln;
147+ Out.hex(Str.hash(<span class="string">&quot;raz&quot;</span>), <span class="number">16</span>); Out.ln;
148+ <span class="kw">if</span> fnv(<span class="string">&quot;raz&quot;</span>) = Str.hash(<span class="string">&quot;raz&quot;</span>) <span class="kw">then</span> Out.str(<span class="string">&quot;the same\n&quot;</span>); <span class="kw">end</span>;
149+<span class="kw">end</span>;
150+
151+<span class="kw">end</span> Fnv.</code></pre>
152+<pre class="output"><code>89F6BC1960FF4912
153+89F6BC1960FF4912
154+the same
155+</code></pre>
156+<h2>Widening, narrowing, and sign</h2>
157+<p>A conversion to a narrower integer cuts the value to the new width. To a wider one it extends by the sign for a signed source and with zeros for an unsigned one. That difference is the whole story of "why did my byte come out negative".</p>
158+<div class="file"><span>Widths.raz</span></div><pre class="code"><code><span class="kw">module</span> Widths;
159+<span class="kw">import</span> Out;
160+
161+<span class="kw">proc</span> main*;
162+<span class="kw">var</span> s: <span class="ty">I8</span>; u: <span class="ty">U8</span>;
163+<span class="kw">begin</span>
164+ s := -<span class="number">1</span>;
165+ u := <span class="number">255</span>; <span class="comment">(* the same eight bits *)</span>
166+ Out.int(<span class="ty">I64</span>(s)); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="comment">(* sign-extended: -1 *)</span>
167+ Out.int(<span class="ty">I64</span>(u)); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="comment">(* zero-extended: 255 *)</span>
168+ Out.int(<span class="ty">I64</span>(<span class="ty">U8</span>(s))); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="comment">(* the bits, read as unsigned: 255 *)</span>
169+ Out.int(<span class="ty">I64</span>(<span class="ty">I8</span>(u))); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="comment">(* and back: -1 *)</span>
170+ Out.int(<span class="ty">I64</span>(<span class="ty">U8</span>(<span class="number">300</span> + <span class="ty">INT</span>(u)))); Out.ln; <span class="comment">(* 555 cut to 8 bits: 43 *)</span>
171+<span class="kw">end</span>;
172+
173+<span class="kw">end</span> Widths.</code></pre>
174+<pre class="output"><code>-1 255 255 -1 43
175+</code></pre>
176+<p>Relations compare as signed or unsigned according to the type, so the same bits order differently:</p>
177+<div class="file"><span>Order.raz</span></div><pre class="code"><code><span class="kw">module</span> Order;
178+<span class="kw">import</span> Out;
179+
180+<span class="kw">proc</span> main*;
181+<span class="kw">var</span> s: <span class="ty">I8</span>; u: <span class="ty">U8</span>;
182+<span class="kw">begin</span>
183+ s := -<span class="number">1</span>;
184+ u := <span class="ty">U8</span>(s); <span class="comment">(* 255 *)</span>
185+ <span class="kw">if</span> s &lt; <span class="number">0</span> <span class="kw">then</span> Out.str(<span class="string">&quot;s is below zero\n&quot;</span>); <span class="kw">end</span>;
186+ <span class="kw">if</span> u &gt; <span class="number">100</span> <span class="kw">then</span> Out.str(<span class="string">&quot;u is above 100\n&quot;</span>); <span class="kw">end</span>;
187+ <span class="kw">if</span> <span class="ty">U8</span>(s) = u <span class="kw">then</span> Out.str(<span class="string">&quot;but they are the same bits\n&quot;</span>); <span class="kw">end</span>;
188+<span class="kw">end</span>;
189+
190+<span class="kw">end</span> Order.</code></pre>
191+<pre class="output"><code>s is below zero
192+u is above 100
193+but they are the same bits
194+</code></pre>
195+<h2>Division</h2>
196+<p><code>div</code> rounds toward zero, and <code>mod</code> takes the sign of the dividend, as in C: <code>-7 div 2</code> is -3 and <code>-7 mod 2</code> is -1. Dividing by zero stops the program, and so does dividing the smallest value of a signed type by -1, whose result can't be represented. Neither is a silent wrong answer.</p>
197+<div class="file"><span>Divide.raz</span></div><pre class="code"><code><span class="kw">module</span> Divide;
198+<span class="kw">import</span> Out;
199+
200+<span class="kw">proc</span> main*;
201+<span class="kw">var</span> n, d: <span class="ty">INT</span>;
202+<span class="kw">begin</span>
203+ n := -<span class="number">7</span>; d := <span class="number">2</span>;
204+ Out.int(n <span class="kw">div</span> d); Out.char(<span class="string">&#x27; &#x27;</span>); Out.int(n <span class="kw">mod</span> d); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="comment">(* -3 -1 *)</span>
205+ n := <span class="number">7</span>; d := -<span class="number">2</span>;
206+ Out.int(n <span class="kw">div</span> d); Out.char(<span class="string">&#x27; &#x27;</span>); Out.int(n <span class="kw">mod</span> d); Out.ln; <span class="comment">(* -3 1 *)</span>
207+<span class="kw">end</span>;
208+
209+<span class="kw">end</span> Divide.</code></pre>
210+<pre class="output"><code>-3 -1 -3 1
211+</code></pre>
212+<h2>Shifts and bits</h2>
213+<p><code>&amp;</code> (and), <code>|</code> (or), <code>xor</code> and <code>~</code> (not) work on every bit. <code>x shl n</code> and <code>x shr n</code> shift by <code>n</code> modulo the width of <code>x</code>'s type: for a <code>U8</code>, <code>x shl 9</code> is <code>x shl 1</code>. <code>shr</code> brings in copies of the sign bit for signed types, so it divides by two keeping the sign, and zeros for unsigned ones. The result has <code>x</code>'s type, cut to its width: in a <code>U8</code>, <code>0C0H shl 1</code> is <code>80H</code>.</p>
214+<div class="file"><span>Shifts.raz</span></div><pre class="code"><code><span class="kw">module</span> Shifts;
215+<span class="kw">import</span> Out;
216+
217+<span class="kw">proc</span> main*;
218+<span class="kw">var</span> u: <span class="ty">U8</span>; s: <span class="ty">I8</span>;
219+<span class="kw">begin</span>
220+ u := <span class="number">0C0H</span>;
221+ Out.hex(<span class="ty">U64</span>(u <span class="kw">shl</span> <span class="number">1</span>), <span class="number">2</span>); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="comment">(* 80: cut to 8 bits *)</span>
222+ Out.hex(<span class="ty">U64</span>(u <span class="kw">shr</span> <span class="number">4</span>), <span class="number">2</span>); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="comment">(* 0C: zeros come in *)</span>
223+ Out.hex(<span class="ty">U64</span>(u <span class="kw">shl</span> <span class="number">9</span>), <span class="number">2</span>); Out.ln; <span class="comment">(* 9 mod 8 = 1: 80 *)</span>
224+ s := -<span class="number">64</span>;
225+ Out.int(<span class="ty">I64</span>(s <span class="kw">shr</span> <span class="number">2</span>)); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="comment">(* -16: the sign comes in *)</span>
226+ Out.int(<span class="ty">I64</span>(s <span class="kw">shr</span> <span class="number">7</span>)); Out.ln; <span class="comment">(* -1 *)</span>
227+<span class="kw">end</span>;
228+
229+<span class="kw">end</span> Shifts.</code></pre>
230+<pre class="output"><code>80 0C 80
231+-16 -1
232+</code></pre>
233+<p>A packed value is the usual use of all this: several small numbers in one word. This puts a colour in the bytes of a <code>U32</code> and takes it out again.</p>
234+<div class="file"><span>Colour.raz</span></div><pre class="code"><code><span class="kw">module</span> Colour;
235+<span class="kw">import</span> Out;
236+
237+<span class="comment">(* red, green and blue, each 0 to 255, in the low 24 bits *)</span>
238+<span class="kw">proc</span> pack(r, g, b: <span class="ty">U8</span>): <span class="ty">U32</span>;
239+<span class="kw">begin</span>
240+ <span class="kw">return</span> (<span class="ty">U32</span>(r) <span class="kw">shl</span> <span class="number">16</span>) | (<span class="ty">U32</span>(g) <span class="kw">shl</span> <span class="number">8</span>) | <span class="ty">U32</span>(b);
241+<span class="kw">end</span>;
242+
243+<span class="kw">proc</span> red(c: <span class="ty">U32</span>): <span class="ty">U8</span>;
244+<span class="kw">begin</span>
245+ <span class="kw">return</span> <span class="ty">U8</span>(c <span class="kw">shr</span> <span class="number">16</span>);
246+<span class="kw">end</span>;
247+
248+<span class="kw">proc</span> green(c: <span class="ty">U32</span>): <span class="ty">U8</span>;
249+<span class="kw">begin</span>
250+ <span class="kw">return</span> <span class="ty">U8</span>((c <span class="kw">shr</span> <span class="number">8</span>) &amp; <span class="number">0FFH</span>);
251+<span class="kw">end</span>;
252+
253+<span class="kw">proc</span> blue(c: <span class="ty">U32</span>): <span class="ty">U8</span>;
254+<span class="kw">begin</span>
255+ <span class="kw">return</span> <span class="ty">U8</span>(c &amp; <span class="number">0FFH</span>);
256+<span class="kw">end</span>;
257+
258+<span class="kw">proc</span> main*;
259+<span class="kw">var</span> c: <span class="ty">U32</span>;
260+<span class="kw">begin</span>
261+ c := pack(<span class="number">0FFH</span>, <span class="number">80H</span>, <span class="number">10H</span>);
262+ Out.str(<span class="string">&quot;0x&quot;</span>); Out.hex(<span class="ty">U64</span>(c), <span class="number">6</span>); Out.ln;
263+ Out.int(<span class="ty">I64</span>(red(c))); Out.char(<span class="string">&#x27; &#x27;</span>);
264+ Out.int(<span class="ty">I64</span>(green(c))); Out.char(<span class="string">&#x27; &#x27;</span>);
265+ Out.int(<span class="ty">I64</span>(blue(c))); Out.ln;
266+<span class="kw">end</span>;
267+
268+<span class="kw">end</span> Colour.</code></pre>
269+<pre class="output"><code>0xFF8010
270+255 128 16
271+</code></pre>
272+<h2>Which one to use</h2>
273+<ul><li><strong><code>INT</code></strong> for lengths, indexes, counts, loop variables, and anything you'd write <code>int</code> for in C.</li><li><strong><code>U8</code></strong> for bytes, and for a string's elements (<code>[]U8</code>). <strong><code>I32</code></strong> for a character, a code point.</li><li><strong><code>U32</code>, <code>U64</code></strong> for hashes, bit sets, masks and addresses you do arithmetic on.</li><li><strong><code>I64</code></strong> for what must not shrink on a smaller target: a file's size or position, a time in nanoseconds, a number read from text.</li><li><strong><code>I8</code>, <code>I16</code>, <code>U16</code></strong> when a format or a layout says so, in a record or an array, to save space or match a protocol. Convert at the edge and compute in <code>INT</code>.</li><li><strong><code>UINT</code></strong> rarely: for a count that must use the whole address range, or an address.</li></ul><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="modules.html">&larr; Modules</a><a href="index.html">Contents</a><a class="next" href="values.html">Values and conversions &rarr;</a></div></article></div>
274+</main>
275+<footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
276+</body>
277+</html>

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

@@ -7,28 +7,29 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html">Hello, world</a></li>
1414 <li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html">Values and conversions</a></li>
1617 <li><a href="procedures.html">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html">Arrays, slices and strings</a></li>
2021 <li><a href="records.html">Records and pointers</a></li>
2122 <li><a href="variants.html">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html" aria-current="page">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html">Errors</a></li>
2627 <li><a href="defer.html">Defer</a></li>
2728 <li><a href="checks.html">When it goes wrong</a></li>
2829 <li><a href="generics.html">Generic modules</a></li>
2930 <li><a href="c.html">Talking to C</a></li>
3031 <li><a href="threads.html">Threads</a></li>
31-</ol></aside><article><p class="kicker">Set &middot; lesson 8 of 14</p><h1>Memory and allocators</h1>
32+</ol></aside><article><p class="kicker">Set &middot; lesson 9 of 15</p><h1>Memory and allocators</h1>
3233 <div class="file"><span>Stack.raz</span></div><pre class="code"><code><span class="kw">module</span> Stack;
3334 <span class="kw">import</span> Out, Mem, Heap, Arena;
3435

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

@@ -7,28 +7,29 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html">Hello, world</a></li>
1414 <li><a href="modules.html" aria-current="page">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html">Values and conversions</a></li>
1617 <li><a href="procedures.html">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html">Arrays, slices and strings</a></li>
2021 <li><a href="records.html">Records and pointers</a></li>
2122 <li><a href="variants.html">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html">Errors</a></li>
2627 <li><a href="defer.html">Defer</a></li>
2728 <li><a href="checks.html">When it goes wrong</a></li>
2829 <li><a href="generics.html">Generic modules</a></li>
2930 <li><a href="c.html">Talking to C</a></li>
3031 <li><a href="threads.html">Threads</a></li>
31-</ol></aside><article><p class="kicker">Raz &middot; lesson 2 of 14</p><h1>Modules</h1>
32+</ol></aside><article><p class="kicker">Raz &middot; lesson 2 of 15</p><h1>Modules</h1>
3233 <p>A program is a set of modules, one to a file, and a module's interface is everything it marks with <code>*</code>. Everything else is private to it. Here is one, saved as <code>Greet.raz</code>:</p>
3334 <div class="file"><span>Greet.raz</span></div><pre class="code"><code><span class="kw">module</span> Greet;
3435 <span class="kw">import</span> Out;
@@ -182,7 +183,7 @@
182183 11 | var made: INT; (* only Shape may read or write it *)
183184 | ^^^^
184185 raz: 2 errors</code></pre>
185-<p>The compiler goes on to find the others, up to 20, and says what it can: where a name was declared, or a name spelled almost alike. A module with errors writes no executable.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="hello.html">&larr; Hello, world</a><a href="index.html">Contents</a><a class="next" href="values.html">Values and conversions &rarr;</a></div></article></div>
186+<p>The compiler goes on to find the others, up to 20, and says what it can: where a name was declared, or a name spelled almost alike. A module with errors writes no executable.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="hello.html">&larr; Hello, world</a><a href="index.html">Contents</a><a class="next" href="integers.html">Integers &rarr;</a></div></article></div>
186187 </main>
187188 <footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
188189 </body>

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

@@ -7,28 +7,29 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html">Hello, world</a></li>
1414 <li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html">Values and conversions</a></li>
1617 <li><a href="procedures.html" aria-current="page">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html">Arrays, slices and strings</a></li>
2021 <li><a href="records.html">Records and pointers</a></li>
2122 <li><a href="variants.html">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html">Errors</a></li>
2627 <li><a href="defer.html">Defer</a></li>
2728 <li><a href="checks.html">When it goes wrong</a></li>
2829 <li><a href="generics.html">Generic modules</a></li>
2930 <li><a href="c.html">Talking to C</a></li>
3031 <li><a href="threads.html">Threads</a></li>
31-</ol></aside><article><p class="kicker">Raz &middot; lesson 4 of 14</p><h1>Procedures and several results</h1>
32+</ol></aside><article><p class="kicker">Raz &middot; lesson 5 of 15</p><h1>Procedures and several results</h1>
3233 <div class="file"><span>Procs.raz</span></div><pre class="code"><code><span class="kw">module</span> Procs;
3334 <span class="kw">import</span> Out;
3435

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

@@ -7,28 +7,29 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html">Hello, world</a></li>
1414 <li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html">Values and conversions</a></li>
1617 <li><a href="procedures.html">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html">Arrays, slices and strings</a></li>
2021 <li><a href="records.html" aria-current="page">Records and pointers</a></li>
2122 <li><a href="variants.html">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html">Errors</a></li>
2627 <li><a href="defer.html">Defer</a></li>
2728 <li><a href="checks.html">When it goes wrong</a></li>
2829 <li><a href="generics.html">Generic modules</a></li>
2930 <li><a href="c.html">Talking to C</a></li>
3031 <li><a href="threads.html">Threads</a></li>
31-</ol></aside><article><p class="kicker">Set &middot; lesson 6 of 14</p><h1>Records and pointers</h1>
32+</ol></aside><article><p class="kicker">Set &middot; lesson 7 of 15</p><h1>Records and pointers</h1>
3233 <div class="file"><span>Boxes.raz</span></div><pre class="code"><code><span class="kw">module</span> Boxes;
3334 <span class="kw">import</span> Fmt;
3435

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

@@ -7,28 +7,29 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html">Hello, world</a></li>
1414 <li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html">Values and conversions</a></li>
1617 <li><a href="procedures.html">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html" aria-current="page">Arrays, slices and strings</a></li>
2021 <li><a href="records.html">Records and pointers</a></li>
2122 <li><a href="variants.html">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html">Errors</a></li>
2627 <li><a href="defer.html">Defer</a></li>
2728 <li><a href="checks.html">When it goes wrong</a></li>
2829 <li><a href="generics.html">Generic modules</a></li>
2930 <li><a href="c.html">Talking to C</a></li>
3031 <li><a href="threads.html">Threads</a></li>
31-</ol></aside><article><p class="kicker">Set &middot; lesson 5 of 14</p><h1>Arrays, slices and strings</h1>
32+</ol></aside><article><p class="kicker">Set &middot; lesson 6 of 15</p><h1>Arrays, slices and strings</h1>
3233 <div class="file"><span>Slices.raz</span></div><pre class="code"><code><span class="kw">module</span> Slices;
3334 <span class="kw">import</span> Out, Str;
3435

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

@@ -7,28 +7,29 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html">Hello, world</a></li>
1414 <li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html">Values and conversions</a></li>
1617 <li><a href="procedures.html">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html">Arrays, slices and strings</a></li>
2021 <li><a href="records.html">Records and pointers</a></li>
2122 <li><a href="variants.html">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html">Errors</a></li>
2627 <li><a href="defer.html">Defer</a></li>
2728 <li><a href="checks.html">When it goes wrong</a></li>
2829 <li><a href="generics.html">Generic modules</a></li>
2930 <li><a href="c.html">Talking to C</a></li>
3031 <li><a href="threads.html" aria-current="page">Threads</a></li>
31-</ol></aside><article><p class="kicker">Go &middot; lesson 14 of 14</p><h1>Threads</h1>
32+</ol></aside><article><p class="kicker">Go &middot; lesson 15 of 15</p><h1>Threads</h1>
3233 <div class="file"><span>Hits.raz</span></div><pre class="code"><code><span class="kw">module</span> Hits;
3334 <span class="kw">import</span> Out, Thread;
3435
@@ -168,7 +169,7 @@
168169 <span class="kw">end</span> Jobs.</code></pre>
169170 <pre class="output"><code>499999500000
170171 </code></pre>
171-<p><code>Thread</code> makes its own threads, without C, so a program that uses it can't use C; one that does makes its threads with C's <code>pthread_create</code> instead. The advanced tour goes further into atomics, memory ordering and lock-free code.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="c.html">&larr; Talking to C</a><a href="index.html">Contents</a><a class="next" href="../advanced/index.html">Beyond Go &rarr;</a></div></article></div>
172+<p><code>Thread</code> makes its own threads, without C, so a program that uses it can't use C; one that does makes its threads with C's <code>pthread_create</code> instead. The advanced tour goes further into atomics, memory ordering and lock-free code.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="c.html">&larr; Talking to C</a><a href="index.html">Contents</a><a class="next" href="../advanced/index.html">Close to the Metal &rarr;</a></div></article></div>
172173 </main>
173174 <footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
174175 </body>

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

@@ -7,28 +7,30 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html">Hello, world</a></li>
1414 <li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html" aria-current="page">Values and conversions</a></li>
1617 <li><a href="procedures.html">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html">Arrays, slices and strings</a></li>
2021 <li><a href="records.html">Records and pointers</a></li>
2122 <li><a href="variants.html">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html">Errors</a></li>
2627 <li><a href="defer.html">Defer</a></li>
2728 <li><a href="checks.html">When it goes wrong</a></li>
2829 <li><a href="generics.html">Generic modules</a></li>
2930 <li><a href="c.html">Talking to C</a></li>
3031 <li><a href="threads.html">Threads</a></li>
31-</ol></aside><article><p class="kicker">Raz &middot; lesson 3 of 14</p><h1>Values and conversions</h1>
32+</ol></aside><article><p class="kicker">Raz &middot; lesson 4 of 15</p><h1>Values and conversions</h1>
33+<p>This program uses several of the basic types at once, with each conversion written where it happens.</p>
3234 <div class="file"><span>Values.raz</span></div><pre class="code"><code><span class="kw">module</span> Values;
3335 <span class="kw">import</span> Fmt;
3436
@@ -52,35 +54,9 @@
5254 <span class="kw">end</span> Values.</code></pre>
5355 <pre class="output"><code>300 4 300 42.857 233 true
5456 </code></pre>
55-<h2>Integers</h2>
56-<p>The integers are <code>I8</code>, <code>I16</code>, <code>I32</code>, <code>I64</code> and <code>U8</code>, <code>U16</code>, <code>U32</code>, <code>U64</code>, and <code>INT</code> and <code>UINT</code>, as big as an address. <code>INT</code> is the everyday one: lengths, indexes and counts are <code>INT</code>s.</p>
57-<p>Arithmetic wraps at the type's width and never stops the program, except dividing by zero. That is why <code>250 + 10</code> in a <code>U8</code> is 4. <code>div</code> divides toward zero, and <code>mod</code> has the sign of its left side.</p>
58-<div class="file"><span>Wrap.raz</span></div><pre class="code"><code><span class="kw">module</span> Wrap;
59-<span class="kw">import</span> Out;
60-
61-<span class="kw">proc</span> main*;
62-<span class="kw">var</span> a: <span class="ty">U8</span>; b: <span class="ty">I8</span>; c: <span class="ty">U32</span>; d: <span class="ty">INT</span>;
63-<span class="kw">begin</span>
64- a := <span class="number">250</span>; a := a + <span class="number">10</span>;
65- Out.int(<span class="ty">I64</span>(a)); Out.ln; <span class="comment">(* 4 *)</span>
66- b := <span class="number">127</span>; b := b + <span class="number">1</span>;
67- Out.int(<span class="ty">I64</span>(b)); Out.ln; <span class="comment">(* -128 *)</span>
68- c := <span class="number">0</span>; c := c - <span class="number">1</span>;
69- Out.int(<span class="ty">I64</span>(c)); Out.ln; <span class="comment">(* 4294967295 *)</span>
70- d := -<span class="number">7</span>;
71- Out.int(d <span class="kw">div</span> <span class="number">2</span>); Out.str(<span class="string">&quot; &quot;</span>); <span class="comment">(* -3 *)</span>
72- Out.int(d <span class="kw">mod</span> <span class="number">2</span>); Out.ln; <span class="comment">(* -1 *)</span>
73-<span class="kw">end</span>;
74-
75-<span class="kw">end</span> Wrap.</code></pre>
76-<pre class="output"><code>4
77--128
78-4294967295
79--3 -1
80-</code></pre>
8157 <h2>Conversions are written</h2>
82-<p>Two types never mix: with <code>i: I64</code> and a slice <code>s</code>, <code>i &lt; LEN(s)</code> is an error. A type's name converts: <code>F64(n)</code>, <code>I64(b)</code>, <code>U8(x)</code>. Converting to a narrower integer cuts it; to a wider one extends it by its sign (signed) or with zeros (unsigned). The one silent conversion between numbers is an <code>INT</code> into an <code>I64</code>, which loses nothing.</p>
83-<p>A number written in the program is untyped until it meets a type; <code>0FFH</code> is hexadecimal, and <code>1.5</code> is a real, which an integer never becomes without a conversion.</p>
58+<p>Two types never mix: with <code>i: I64</code> and a slice <code>s</code>, <code>i &lt; LEN(s)</code> is an error. A type's name converts: <code>F64(n)</code>, <code>I64(b)</code>, <code>U8(x)</code>. The one silent conversion between numbers is an <code>INT</code> into an <code>I64</code>, which loses nothing. The <a href="integers.html">lesson on integers</a> says what each width conversion does.</p>
59+<p>A number written in the program is untyped until it meets a type, and <code>1.5</code> is a real, which an integer never becomes without a conversion.</p>
8460 <div class="file"><span>Mix.raz</span></div><pre class="code"><code><span class="kw">module</span> Mix;
8561 <span class="kw">import</span> Out;
8662
@@ -100,31 +76,32 @@
10076 help: convert one to the other&#x27;s type, as in I64(LEN(s))
10177 raz: 1 error</code></pre>
10278 <p>The fix is the conversion, written where it happens: <code>if i &lt; I64(LEN(s))</code>. It costs nothing at run time and says what is meant.</p>
103-<h2>Bits and booleans</h2>
104-<p><code>#</code>, not <code>&lt;&gt;</code>, is "different". <code>and</code>, <code>or</code> and <code>not</code> are for <code>BOOL</code>s, and bind below the relations, so <code>i &lt; n and a[i] # 0</code> needs no parentheses. <code>&amp;</code>, <code>|</code>, <code>xor</code>, <code>~</code>, <code>shl</code> and <code>shr</code> are for the bits of integers. There is no conversion between <code>BOOL</code> and an integer: say what you mean with <code>if</code>.</p>
105-<div class="file"><span>Bits.raz</span></div><pre class="code"><code><span class="kw">module</span> Bits;
79+<h2>What converts to what</h2>
80+<p>Everything else that converts does so with the type's name, and the rules are short:</p>
81+<p>| From | To | Result | |---|---|---| | integer | integer | cut to the new width, or extended by the sign (signed) or zeros (unsigned) | | integer | <code>F32</code>, <code>F64</code> | the nearest real | | <code>F32</code>, <code>F64</code> | integer | rounded toward zero | | <code>F32</code> | <code>F64</code> | the same value; the other way, the nearest <code>F32</code> | | enumeration | integer | its position; and back, if there is a name at that position | | set | integer | its bits; and back | | <code>INT</code>, <code>UINT</code> | pointer | the address; and back |</p>
82+<p>There is no conversion between <code>BOOL</code> and an integer: say what you mean with <code>if</code>. A <code>BOOL</code> is <code>true</code> or <code>false</code>, and <code>and</code>, <code>or</code> and <code>not</code> are for <code>BOOL</code>s: they bind below the relations, so <code>i &lt; n and a[i] # 0</code> needs no parentheses, and <code>and</code> and <code>or</code> evaluate their right operand only when it decides the result. <code>#</code>, not <code>&lt;&gt;</code>, is "different".</p>
83+<div class="file"><span>Truth.raz</span></div><pre class="code"><code><span class="kw">module</span> Truth;
10684 <span class="kw">import</span> Out;
10785
86+<span class="kw">proc</span> positive(n: <span class="ty">INT</span>): <span class="ty">BOOL</span>;
87+<span class="kw">begin</span>
88+ <span class="kw">return</span> n &gt; <span class="number">0</span>;
89+<span class="kw">end</span>;
90+
10891 <span class="kw">proc</span> main*;
109-<span class="kw">var</span> flags, mask: <span class="ty">U8</span>;
92+<span class="kw">var</span> a: [<span class="number">3</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>; found: <span class="ty">BOOL</span>;
11093 <span class="kw">begin</span>
111- flags := <span class="number">0</span>;
112- flags := flags | <span class="number">1</span>; <span class="comment">(* set bit 0 *)</span>
113- flags := flags | (<span class="number">1</span> <span class="kw">shl</span> <span class="number">3</span>); <span class="comment">(* set bit 3 *)</span>
114- mask := <span class="number">0FH</span>;
115- Out.str(<span class="string">&quot;flags 0x&quot;</span>); Out.hex(<span class="ty">U64</span>(flags), <span class="number">2</span>); Out.ln;
116- Out.str(<span class="string">&quot;and 0x&quot;</span>); Out.hex(<span class="ty">U64</span>(flags &amp; mask), <span class="number">2</span>); Out.ln;
117- Out.str(<span class="string">&quot;xor 0x&quot;</span>); Out.hex(<span class="ty">U64</span>(flags <span class="kw">xor</span> <span class="number">0FFH</span>), <span class="number">2</span>); Out.ln;
118- Out.str(<span class="string">&quot;not 0x&quot;</span>); Out.hex(<span class="ty">U64</span>(~flags), <span class="number">2</span>); Out.ln;
119- <span class="kw">if</span> (flags &amp; <span class="number">8</span>) # <span class="number">0</span> <span class="kw">then</span> Out.str(<span class="string">&quot;bit 3 is set\n&quot;</span>); <span class="kw">end</span>;
94+ a := [<span class="number">4</span>, <span class="number">0</span>, <span class="number">9</span>];
95+ i := <span class="number">0</span>;
96+ <span class="kw">while</span> (i &lt; <span class="ty">LEN</span>(a)) <span class="kw">and</span> (a[i] # <span class="number">0</span>) <span class="kw">do</span> <span class="ty">INC</span>(i); <span class="kw">end</span>; <span class="comment">(* never reads a[3] *)</span>
97+ Out.int(i); Out.ln;
98+ found := positive(i) <span class="kw">or</span> positive(-<span class="number">1</span>);
99+ <span class="kw">if</span> found <span class="kw">and</span> <span class="kw">not</span> positive(<span class="number">0</span>) <span class="kw">then</span> Out.str(<span class="string">&quot;true and true\n&quot;</span>); <span class="kw">end</span>;
120100 <span class="kw">end</span>;
121101
122-<span class="kw">end</span> Bits.</code></pre>
123-<pre class="output"><code>flags 0x09
124-and 0x09
125-xor 0xF6
126-not 0xF6
127-bit 3 is set
102+<span class="kw">end</span> Truth.</code></pre>
103+<pre class="output"><code>1
104+true and true
128105 </code></pre>
129106 <h2>Reals</h2>
130107 <p><code>F32</code> and <code>F64</code> are the reals. A real constant is written with a point, <code>1.5</code> or <code>2.0E-3</code>. <code>Fmt.real(x, places)</code> writes one with that many digits after the point, and <code>Math</code> has the functions.</p>
@@ -149,7 +126,7 @@
149126 sqrt 2 = 1.41421, pi = 3.14159
150127 truncated: 2 -2
151128 </code></pre>
152-<p>A real converts to an integer by rounding toward zero. Every variable starts at zero, a real's <code>0.0</code> and a <code>BOOL</code>'s <code>false</code> included, so there is no uninitialised value to be surprised by.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="modules.html">&larr; Modules</a><a href="index.html">Contents</a><a class="next" href="procedures.html">Procedures and several results &rarr;</a></div></article></div>
129+<p>A real converts to an integer by rounding toward zero. Every variable starts at zero, a real's <code>0.0</code> and a <code>BOOL</code>'s <code>false</code> included, so there is no uninitialised value to be surprised by.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="integers.html">&larr; Integers</a><a href="index.html">Contents</a><a class="next" href="procedures.html">Procedures and several results &rarr;</a></div></article></div>
153130 </main>
154131 <footer>raz is under the ISC license. <a href="https://github.com/ldiaz94/raz">Source</a>.</footer>
155132 </body>

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

@@ -7,28 +7,29 @@
77 <link rel="stylesheet" href="../style.css">
88 </head>
99 <body>
10-<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Beyond Go</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
10+<header><nav><a href="../index.html">raz</a><a href="../tour/index.html" aria-current="page">Raz, Set, Go</a><a href="../advanced/index.html">Close to the Metal</a><a href="../spec.html">Report</a><a href="../index.html#download">Download</a><a class="repo" href="https://github.com/ldiaz94/raz">GitHub</a></nav></header>
1111 <main>
1212 <div class="tour"><aside><h2>Raz, Set, Go</h2><h3>Raz</h3><ol start='1'>
1313 <li><a href="hello.html">Hello, world</a></li>
1414 <li><a href="modules.html">Modules</a></li>
15+<li><a href="integers.html">Integers</a></li>
1516 <li><a href="values.html">Values and conversions</a></li>
1617 <li><a href="procedures.html">Procedures and several results</a></li>
1718 </ol>
18-<h3>Set</h3><ol start='5'>
19+<h3>Set</h3><ol start='6'>
1920 <li><a href="slices.html">Arrays, slices and strings</a></li>
2021 <li><a href="records.html">Records and pointers</a></li>
2122 <li><a href="variants.html" aria-current="page">Enumerations, sets, variants and match</a></li>
2223 <li><a href="memory.html">Memory and allocators</a></li>
2324 </ol>
24-<h3>Go</h3><ol start='9'>
25+<h3>Go</h3><ol start='10'>
2526 <li><a href="errors.html">Errors</a></li>
2627 <li><a href="defer.html">Defer</a></li>
2728 <li><a href="checks.html">When it goes wrong</a></li>
2829 <li><a href="generics.html">Generic modules</a></li>
2930 <li><a href="c.html">Talking to C</a></li>
3031 <li><a href="threads.html">Threads</a></li>
31-</ol></aside><article><p class="kicker">Set &middot; lesson 7 of 14</p><h1>Enumerations, sets, variants and match</h1>
32+</ol></aside><article><p class="kicker">Set &middot; lesson 8 of 15</p><h1>Enumerations, sets, variants and match</h1>
3233 <div class="file"><span>Shapes.raz</span></div><pre class="code"><code><span class="kw">module</span> Shapes;
3334 <span class="kw">import</span> Out, Fmt, Math;
3435