raz / site / content / tour / 08-variants.txt

205 lines · 5.2 KiB · raw · history · last change a5145f64

1title: Enumerations, sets, variants and match
2part: Set
3summary: Names in order, sets as bits, and cases the compiler makes you handle.
4---
5```raz Shapes run
6```
7
8## Enumerations
9
10An enumeration is a list of names, in order. They convert to their position and back, and a `for` loop can run over them. A name may be written with its type, `Day.mon`, and two enumerations of a module may share a name, `Style.bold` and `Elem.bold`, which is then always written so. An integer that has no name at its position is a failed check, never a silent wrong value.
11
12```raz Days run
13module Days;
14import Out;
15
16type Day = (mon, tue, wed, thu, fri, sat, sun);
17
18proc name(d: Day): []const U8;
19begin
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;
29end;
30
31proc main*;
32var d: Day;
33begin
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;
41end;
42
43end Days.
44```
45
46## Sets
47
48A `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
51module Perms;
52import Out;
53
54type
55 Perm = (x, w, r); (* bits 0, 1, 2, as chmod has them *)
56 Perms = set of Perm;
57
58proc show(p: Perms);
59begin
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;
63end;
64
65proc main*;
66var user, group, mine: Perms;
67begin
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 *)
77end;
78
79end Perms.
80```
81
82## Variants
83
84A 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
87module Tokens;
88import Out;
89
90type
91 Token = variant
92 number(n: INT);
93 plus;
94 times;
95 word(s: []const U8);
96 end;
97
98proc describe(t: Token);
99begin
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;
107end;
108
109(* evaluates tokens in reverse Polish; assumes the input is well formed *)
110proc eval(ts: []Token): INT;
111var stack: [16]INT; top, i, b: INT;
112begin
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];
123end;
124
125proc main*;
126var ts: [5]Token; i: INT;
127begin
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"));
132end;
133
134end Tokens.
135```
136
137## Matching integers
138
139On 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
142module Classify;
143import Out;
144
145proc kind(c: I32): []const U8;
146begin
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;
153end;
154
155proc main*;
156var s: []const U8; i: INT;
157begin
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;
162end;
163
164end Classify.
165```
166
167## Matching strings
168
169On a string, `match` takes string constants, several to an arm if need be, and compares them as `=` does. It too must have an `| else:`. It is how a program reads a command, an option or a keyword without a chain of `elsif`s.
170
171```raz Commands run
172module Commands;
173import Out;
174
175type Op = (none, add, sub, show);
176
177proc op(word: []const U8): Op;
178begin
179 match word of
180 | "add", "plus", "+": return add;
181 | "sub", "minus", "-": return sub;
182 | "show": return show;
183 | else: return none;
184 end;
185end;
186
187proc main*;
188var total: INT; words: [6][]const U8; i: INT;
189begin
190 words := ["add", "plus", "sub", "show", "times", "-"];
191 for i := 0 to 5 do
192 match op(words[i]) of
193 | add: INC(total);
194 | sub: DEC(total);
195 | show: Out.int(total); Out.ln;
196 | none: Out.str(words[i]); Out.str("?\n");
197 end;
198 end;
199 Out.int(total); Out.ln;
200end;
201
202end Commands.
203```
204
205Two labels that are the same string are an error, as two arms for one case are.