| 1 | module Shapes; |
| 2 | import Out; |
| 3 | |
| 4 | type |
| 5 | Shape* = variant |
| 6 | circle(r: F64); |
| 7 | rect(w, h: F64); |
| 8 | label(text: []const U8; size: I64); |
| 9 | dot; |
| 10 | end; |
| 11 | |
| 12 | proc area(s: Shape): I64; |
| 13 | var a: F64; |
| 14 | begin |
| 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); |
| 22 | end; |
| 23 | |
| 24 | proc grow(var s: Shape); |
| 25 | begin |
| 26 | match s of |
| 27 | | rect(w, h): w := w * 2.0; |
| 28 | | else: |
| 29 | end; |
| 30 | end; |
| 31 | |
| 32 | proc main*; |
| 33 | var s, t: Shape; shapes: [4]Shape; i: I64; z: Shape; |
| 34 | begin |
| 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; |
| 61 | end; |
| 62 | |
| 63 | end Shapes. |