| 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. 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 |
| 13 | module Days; |
| 14 | import Out; |
| 15 | |
| 16 | type Day = (mon, tue, wed, thu, fri, sat, sun); |
| 17 | |
| 18 | proc name(d: Day): []const 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: []const 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): []const 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: []const 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 | ``` |
| 166 | |
| 167 | ## Matching strings |
| 168 | |
| 169 | On 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 |
| 172 | module Commands; |
| 173 | import Out; |
| 174 | |
| 175 | type Op = (none, add, sub, show); |
| 176 | |
| 177 | proc op(word: []const U8): Op; |
| 178 | begin |
| 179 | match word of |
| 180 | | "add", "plus", "+": return add; |
| 181 | | "sub", "minus", "-": return sub; |
| 182 | | "show": return show; |
| 183 | | else: return none; |
| 184 | end; |
| 185 | end; |
| 186 | |
| 187 | proc main*; |
| 188 | var total: INT; words: [6][]const U8; i: INT; |
| 189 | begin |
| 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; |
| 200 | end; |
| 201 | |
| 202 | end Commands. |
| 203 | ``` |
| 204 | |
| 205 | Two labels that are the same string are an error, as two arms for one case are. |