| 1 | (* match on a string: its labels string constants, compared as = compares strings *) |
| 2 | module MatchStr; |
| 3 | import Out; |
| 4 | |
| 5 | const Plus = "plus"; |
| 6 | |
| 7 | type Op = (none, add, sub, mul); |
| 8 | |
| 9 | proc op(word: []const U8): Op; |
| 10 | begin |
| 11 | match word of |
| 12 | | "add", Plus, `+`: return add; |
| 13 | | "sub", "-": return sub; |
| 14 | | "mul": return mul; |
| 15 | | "": return none; |
| 16 | | else: return none; |
| 17 | end; |
| 18 | end; |
| 19 | |
| 20 | proc main*; |
| 21 | var a: [3]U8; s: []const U8; |
| 22 | begin |
| 23 | Out.int(I64(op("add"))); Out.int(I64(op("plus"))); Out.int(I64(op("+"))); |
| 24 | Out.int(I64(op("sub"))); Out.int(I64(op("-"))); Out.int(I64(op("mul"))); |
| 25 | Out.int(I64(op("mu"))); Out.int(I64(op("mull"))); Out.int(I64(op(""))); Out.ln; |
| 26 | a := "sub"; |
| 27 | match a of |
| 28 | | "sub": Out.str("array\n"); |
| 29 | | else: |
| 30 | end; |
| 31 | s := a[0..2]; |
| 32 | match s of |
| 33 | | "su": Out.str("slice\n"); |
| 34 | | else: Out.str("no\n"); |
| 35 | end; |
| 36 | match "abc" of |
| 37 | | "abc": Out.str("constant\n"); |
| 38 | | else: |
| 39 | end; |
| 40 | end; |
| 41 | |
| 42 | end MatchStr. |