raz / conform / run / MatchStr.raz

42 lines · 993 B · raw · history · last change 264716cb

1(* match on a string: its labels string constants, compared as = compares strings *)
2module MatchStr;
3import Out;
4
5const Plus = "plus";
6
7type Op = (none, add, sub, mul);
8
9proc op(word: []const U8): Op;
10begin
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;
18end;
19
20proc main*;
21var a: [3]U8; s: []const U8;
22begin
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;
40end;
41
42end MatchStr.