| 1 | module Kinds; |
| 2 | import Out; |
| 3 | |
| 4 | type |
| 5 | Color* = (red, green, blue, black); |
| 6 | Colors* = set of Color; |
| 7 | Cell = record ch: U8; attr: U8; fg: Color; end; |
| 8 | Point = record x, y: I64; name: []const U8; end; |
| 9 | Line = record a, b: Point; end; |
| 10 | |
| 11 | var log: [8]U8; nlog: I64; |
| 12 | |
| 13 | proc note(c: U8); |
| 14 | begin |
| 15 | log[nlog] := c; |
| 16 | INC(nlog); |
| 17 | end; |
| 18 | |
| 19 | proc show(s: Colors); |
| 20 | var c: Color; |
| 21 | begin |
| 22 | for c := red to black do |
| 23 | if c in s then Out.int(I64(c)); end; |
| 24 | end; |
| 25 | Out.str(" "); |
| 26 | end; |
| 27 | |
| 28 | proc name(c: Color): []const U8; |
| 29 | begin |
| 30 | match c of |
| 31 | | red: return "red"; |
| 32 | | green: return "green"; |
| 33 | | else: return "other"; |
| 34 | end; |
| 35 | end; |
| 36 | |
| 37 | proc deferred(n: I64): I64; |
| 38 | var i: I64; |
| 39 | begin |
| 40 | defer note('A'); |
| 41 | defer note('B'); |
| 42 | for i := 1 to 3 do |
| 43 | defer note('L'); |
| 44 | if i = 2 then break; end; |
| 45 | end; |
| 46 | if n > 0 then |
| 47 | defer note('C'); |
| 48 | return n * 10; |
| 49 | end; |
| 50 | note('D'); |
| 51 | return -1; |
| 52 | end; |
| 53 | |
| 54 | proc mid(a, b: Point): Point; |
| 55 | begin |
| 56 | return {x: (a.x + b.x) div 2, y: (a.y + b.y) div 2, name: "mid"}; |
| 57 | end; |
| 58 | |
| 59 | proc main*; |
| 60 | var s, t: Colors; c: Color; cell: Cell; p: Point; l: Line; r: I64; one: Colors; |
| 61 | begin |
| 62 | s := {red, blue}; |
| 63 | t := {blue, black}; |
| 64 | show(s + t); show(s * t); show(s - t); show({}); |
| 65 | if red in s then Out.str("in "); end; |
| 66 | if not (green in s) then Out.str("out "); end; |
| 67 | if s * t <= t then Out.str("sub "); end; |
| 68 | if s + t >= s then Out.str("sup "); end; |
| 69 | if s # t then Out.str("ne "); end; |
| 70 | if s = {blue, red} then Out.str("eq"); end; |
| 71 | Out.ln; |
| 72 | c := green; |
| 73 | one := {c, black}; |
| 74 | show(one); |
| 75 | Out.int(I64(U8(one))); Out.str(" "); |
| 76 | Out.int(SIZE(Colors)); Out.str(" "); |
| 77 | Out.int(I64(Color(2))); Out.str(" "); |
| 78 | if c < blue then Out.str("lt "); end; |
| 79 | Out.str(name(c)); Out.str(" "); Out.str(name(black)); Out.ln; |
| 80 | cell := {ch: 'x', fg: blue}; |
| 81 | Out.int(I64(cell.ch)); Out.str(" "); Out.int(I64(cell.attr)); Out.str(" "); Out.int(I64(cell.fg)); Out.ln; |
| 82 | p := mid({x: 2, y: 4}, {y: 10, x: 6}); |
| 83 | Out.int(p.x); Out.int(p.y); Out.str(p.name); Out.ln; |
| 84 | l := {a: {x: 1, y: 2}, b: p}; |
| 85 | Out.int(l.a.y); Out.int(l.b.x); Out.ln; |
| 86 | r := deferred(5); |
| 87 | Out.int(r); Out.str(" "); Out.str(log[0..nlog]); Out.ln; |
| 88 | nlog := 0; |
| 89 | r := deferred(0); |
| 90 | Out.int(r); Out.str(" "); Out.str(log[0..nlog]); Out.ln; |
| 91 | Out.int(I64(Color(I64(c) + 2))); |
| 92 | c := Color(nlog); |
| 93 | end; |
| 94 | |
| 95 | end Kinds. |