| 1 | module TwoWords; |
| 2 | |
| 3 | (* Two results, and a slice, returned: with deferred statements, passed |
| 4 | on, through a procedure value, run off the end, and with a call in |
| 5 | the second. *) |
| 6 | import Out; |
| 7 | import SYSTEM; |
| 8 | |
| 9 | var log: INT; |
| 10 | |
| 11 | proc note; begin log := log * 10 + 1; end; |
| 12 | |
| 13 | proc pair(a, b: I32): (I32, I32); |
| 14 | begin |
| 15 | defer note; |
| 16 | return b, a; |
| 17 | end; |
| 18 | |
| 19 | proc mixed(n: INT): (BOOL, I64); |
| 20 | begin |
| 21 | if n < 0 then return false, -n; end; |
| 22 | return true, n * 3; |
| 23 | end; |
| 24 | |
| 25 | proc twice(n: INT): INT; begin return 2 * n; end; |
| 26 | |
| 27 | proc nested(n: INT): (INT, INT); |
| 28 | begin |
| 29 | return n, twice(n) + twice(n + 1); |
| 30 | end; |
| 31 | |
| 32 | proc passOn(n: INT): (INT, INT); |
| 33 | begin |
| 34 | defer note; |
| 35 | return nested(n); |
| 36 | end; |
| 37 | |
| 38 | proc tail(s: []const U8; i: INT): []const U8; |
| 39 | begin |
| 40 | defer note; |
| 41 | return s[i..LEN(s)]; |
| 42 | end; |
| 43 | |
| 44 | proc fallOff(n: INT): (INT, INT); |
| 45 | begin |
| 46 | if n > 0 then return 1, 2; end; |
| 47 | end; |
| 48 | |
| 49 | proc three(n: INT): (INT, INT, INT); begin return n, n + 1, n + 2; end; |
| 50 | |
| 51 | proc main*; |
| 52 | var x, y: I32; b: BOOL; v, p, q: INT; w: I64; h: proc(n: INT): (INT, INT); pv: ^INT; |
| 53 | begin |
| 54 | x, y := pair(-5, 7); Out.int(I64(x)); Out.str(" "); Out.int(I64(y)); Out.ln; |
| 55 | b, w := mixed(-4); if b then Out.str("T"); else Out.str("F"); end; Out.str(" "); Out.int(w); Out.ln; |
| 56 | b, w := mixed(4); if b then Out.str("T"); else Out.str("F"); end; Out.str(" "); Out.int(w); Out.ln; |
| 57 | p, q := passOn(5); Out.int(p); Out.str(" "); Out.int(q); Out.ln; |
| 58 | h := nested; p, q := h(1); Out.int(p); Out.str(" "); Out.int(q); Out.ln; |
| 59 | Out.str(tail("hello", 2)); Out.ln; |
| 60 | p, q := fallOff(0); Out.int(p); Out.str(" "); Out.int(q); Out.ln; |
| 61 | _, v := nested(3); Out.int(v); Out.str(" "); Out.int(log); Out.ln; |
| 62 | (* in order: the second result is the one left *) |
| 63 | v, v := nested(4); Out.int(v); Out.ln; |
| 64 | pv := SYSTEM.ADR(q); pv^, p := nested(5); Out.int(q); Out.str(" "); Out.int(p); Out.ln; |
| 65 | (* three results, which come back in memory, some dropped *) |
| 66 | _, p, q := three(1); Out.int(p); Out.str(" "); Out.int(q); Out.ln; |
| 67 | p, _, _ := three(7); Out.int(p); Out.ln; |
| 68 | end; |
| 69 | |
| 70 | end TwoWords. |