| 1 | (* A constant's slice part written as an array literal (report section |
| 2 | 4): each literal an array constant of its own, nested to any depth, |
| 3 | inside records and variants too. *) |
| 4 | module SliceLiteralConst; |
| 5 | import Out; |
| 6 | |
| 7 | type |
| 8 | Rule = record name: []const U8; keys: []const INT; end; |
| 9 | Shape = variant dot; poly(xs: []const INT); end; |
| 10 | |
| 11 | const |
| 12 | Rows: []const []const INT = [[1, 2], [3], []]; |
| 13 | Deep: []const []const []const U8 = [[['a', 'b'], "cd"], [], [['e']]]; |
| 14 | Rules: []const Rule = [{name: "up", keys: [107, 65]}, {name: "none", keys: []}]; |
| 15 | Shapes: [2]Shape = [poly([1, 2, 3]), dot]; |
| 16 | Words: []const []const U8 = [['h', 'i'], "there"]; |
| 17 | |
| 18 | proc sum(xs: []const INT): INT; |
| 19 | var i, s: INT; |
| 20 | begin |
| 21 | s := 0; |
| 22 | for i := 0 to LEN(xs) - 1 do s := s + xs[i]; end; |
| 23 | return s; |
| 24 | end; |
| 25 | |
| 26 | proc main*; |
| 27 | var i, j: INT; |
| 28 | begin |
| 29 | for i := 0 to LEN(Rows) - 1 do |
| 30 | Out.int(LEN(Rows[i])); Out.str(":"); Out.int(sum(Rows[i])); Out.str(" "); |
| 31 | end; |
| 32 | Out.ln; |
| 33 | for i := 0 to LEN(Deep) - 1 do |
| 34 | Out.str("["); |
| 35 | for j := 0 to LEN(Deep[i]) - 1 do Out.str(Deep[i][j]); Out.str(" "); end; |
| 36 | Out.str("]"); |
| 37 | end; |
| 38 | Out.ln; |
| 39 | for i := 0 to LEN(Rules) - 1 do |
| 40 | Out.str(Rules[i].name); Out.str(" "); Out.int(LEN(Rules[i].keys)); Out.str(" "); |
| 41 | Out.int(sum(Rules[i].keys)); Out.ln; |
| 42 | end; |
| 43 | match Shapes[0] of |
| 44 | | poly(xs): Out.int(sum(xs)); Out.ln; |
| 45 | | dot: |
| 46 | end; |
| 47 | Out.str(Words[0]); Out.str(" "); Out.str(Words[1]); Out.ln; |
| 48 | end; |
| 49 | |
| 50 | end SliceLiteralConst. |