raz / test / run / Shapes.raz

63 lines · 1.2 KiB · raw · history · last change 264716cb

1module Shapes;
2import Out;
3
4type
5 Shape* = variant
6 circle(r: F64);
7 rect(w, h: F64);
8 label(text: []const U8; size: I64);
9 dot;
10 end;
11
12proc area(s: Shape): I64;
13var a: F64;
14begin
15 match s of
16 | circle(r): a := 3.0 * r * r;
17 | rect(w, h): a := w * h;
18 | label: a := 0.0;
19 | dot: a := 1.0;
20 end;
21 return I64(a);
22end;
23
24proc grow(var s: Shape);
25begin
26 match s of
27 | rect(w, h): w := w * 2.0;
28 | else:
29 end;
30end;
31
32proc main*;
33var s, t: Shape; shapes: [4]Shape; i: I64; z: Shape;
34begin
35 s := circle(2.0);
36 Out.int(area(s)); Out.ln;
37 s := rect(3.0, 4.0);
38 Out.int(area(s)); Out.ln;
39 grow(s);
40 Out.int(area(s)); Out.ln;
41 t := s;
42 s := dot;
43 Out.int(area(s)); Out.str(" "); Out.int(area(t)); Out.ln;
44 shapes[0] := circle(1.0);
45 shapes[1] := label("hi", 12);
46 shapes[2] := dot;
47 shapes[3] := rect(1.5, 2.0);
48 for i := 0 to 3 do
49 match shapes[i] of
50 | label(text, size): Out.str(text); Out.int(size);
51 | else: Out.int(area(shapes[i]));
52 end;
53 Out.str(" ");
54 end;
55 Out.ln;
56 match z of
57 | circle(r): Out.str("zero is a circle of "); Out.int(I64(r)); Out.ln;
58 | else: Out.str("?");
59 end;
60 Out.int(SIZE(Shape)); Out.ln;
61end;
62
63end Shapes.