raz / conform / run / TwoWords.raz

70 lines · 1.9 KiB · raw · history · last change 264716cb

1module 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. *)
6import Out;
7import SYSTEM;
8
9var log: INT;
10
11proc note; begin log := log * 10 + 1; end;
12
13proc pair(a, b: I32): (I32, I32);
14begin
15 defer note;
16 return b, a;
17end;
18
19proc mixed(n: INT): (BOOL, I64);
20begin
21 if n < 0 then return false, -n; end;
22 return true, n * 3;
23end;
24
25proc twice(n: INT): INT; begin return 2 * n; end;
26
27proc nested(n: INT): (INT, INT);
28begin
29 return n, twice(n) + twice(n + 1);
30end;
31
32proc passOn(n: INT): (INT, INT);
33begin
34 defer note;
35 return nested(n);
36end;
37
38proc tail(s: []const U8; i: INT): []const U8;
39begin
40 defer note;
41 return s[i..LEN(s)];
42end;
43
44proc fallOff(n: INT): (INT, INT);
45begin
46 if n > 0 then return 1, 2; end;
47end;
48
49proc three(n: INT): (INT, INT, INT); begin return n, n + 1, n + 2; end;
50
51proc main*;
52var x, y: I32; b: BOOL; v, p, q: INT; w: I64; h: proc(n: INT): (INT, INT); pv: ^INT;
53begin
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;
68end;
69
70end TwoWords.