| 1 | (* Every variable starts at zero: a module's, and a procedure's even |
| 2 | where the stack held other values before. *) |
| 3 | module ZeroStart; |
| 4 | import Out; |
| 5 | |
| 6 | type |
| 7 | Colour = (red, green, blue); |
| 8 | Flags = set of Colour; |
| 9 | Rec = record n: I64; ok: BOOL; p: ^Rec; end; |
| 10 | Shape = variant first(x: I64); second(y: I64); end; |
| 11 | |
| 12 | var |
| 13 | n: I64; u: U8; ok: BOOL; p: ^Rec; f: proc; e: ERROR; s: []const U8; |
| 14 | c: Colour; fl: Flags; r: Rec; a: [3]I64; v: Shape; |
| 15 | |
| 16 | proc line(name: []const U8; zero: BOOL); |
| 17 | begin |
| 18 | Out.str(name); |
| 19 | if zero then Out.str(" zero"); else Out.str(" NOT ZERO"); end; |
| 20 | Out.ln; |
| 21 | end; |
| 22 | |
| 23 | proc which(name: []const U8; v: Shape); |
| 24 | begin |
| 25 | Out.str(name); |
| 26 | match v of |
| 27 | | first(x): Out.str(" first "); Out.int(x); |
| 28 | | second(y): Out.str(" second "); Out.int(y); |
| 29 | end; |
| 30 | Out.ln; |
| 31 | end; |
| 32 | |
| 33 | (* leaves its frame full of other values *) |
| 34 | proc dirty; |
| 35 | var pad: [64]I64; i: INT; |
| 36 | begin |
| 37 | for i := 0 to 63 do pad[i] := 7; end; |
| 38 | end; |
| 39 | |
| 40 | proc locals; |
| 41 | var |
| 42 | n: I64; u: U8; ok: BOOL; p: ^Rec; f: proc; e: ERROR; s: []const U8; |
| 43 | c: Colour; fl: Flags; r: Rec; a: [3]I64; v: Shape; |
| 44 | begin |
| 45 | line("local number", (n = 0) and (u = 0)); |
| 46 | line("local bool", not ok); |
| 47 | line("local pointers", (p = nil) and (f = nil) and (e = nil)); |
| 48 | line("local slice", LEN(s) = 0); |
| 49 | line("local enumeration and set", (c = red) and not (red in fl) and not (blue in fl)); |
| 50 | line("local record", (r.n = 0) and not r.ok and (r.p = nil)); |
| 51 | line("local array", (a[0] = 0) and (a[1] = 0) and (a[2] = 0)); |
| 52 | which("local variant", v); |
| 53 | end; |
| 54 | |
| 55 | proc main*; |
| 56 | begin |
| 57 | line("number", (n = 0) and (u = 0)); |
| 58 | line("bool", not ok); |
| 59 | line("pointers", (p = nil) and (f = nil) and (e = nil)); |
| 60 | line("slice", LEN(s) = 0); |
| 61 | line("enumeration and set", (c = red) and not (red in fl) and not (blue in fl)); |
| 62 | line("record", (r.n = 0) and not r.ok and (r.p = nil)); |
| 63 | line("array", (a[0] = 0) and (a[1] = 0) and (a[2] = 0)); |
| 64 | which("variant", v); |
| 65 | dirty; |
| 66 | locals; |
| 67 | end; |
| 68 | |
| 69 | end ZeroStart. |