raz / conform / run / VariantConst.raz

38 lines · 927 B · raw · history · last change 264716cb

1module VariantConst;
2import Out;
3
4type
5 Shape = variant circle(r: F64); rect(w, h: INT); dot; named(name: []const U8; n: U8); end;
6 Rule = record tag: INT; s: Shape; end;
7
8const
9 Shapes: []Shape = [circle(1.5), rect(2, 3), dot, named("box", 7)];
10 One: Shape = rect(4, 5);
11 Rules: [2]Rule = [{tag: 1, s: dot}, {tag: 2, s: named("x", 1)}];
12
13proc show(const s: Shape);
14begin
15 match s of
16 | circle(r): Out.str("circle "); Out.int(I64(r * 2.0));
17 | rect(w, h): Out.str("rect "); Out.int(w); Out.char(' '); Out.int(h);
18 | dot: Out.str("dot");
19 | named(name, n): Out.str(name); Out.char(' '); Out.int(I64(n));
20 end;
21 Out.ln;
22end;
23
24proc main*;
25var i: INT; s: Shape;
26begin
27 for i := 0 to LEN(Shapes) - 1 do show(Shapes[i]); end;
28 show(One);
29 show(Rules[1].s);
30 s := One;
31 show(s);
32 match One of
33 | rect(w, h): Out.int(w * h); Out.ln;
34 | else:
35 end;
36end;
37
38end VariantConst.