| 1 | module Results; |
| 2 | import Out; |
| 3 | import SYSTEM; |
| 4 | |
| 5 | const |
| 6 | NotFound*: ERROR = "not found"; |
| 7 | Empty: ERROR = "empty"; |
| 8 | Other: ERROR = "not found"; |
| 9 | |
| 10 | type Pair = record a, b: I64; end; |
| 11 | |
| 12 | var table: [4]I64; g: Pair; |
| 13 | |
| 14 | proc divmod(a, b: I64): (I64, I64); |
| 15 | begin |
| 16 | return a div b, a mod b; |
| 17 | end; |
| 18 | |
| 19 | proc find(key: I64): (I64, ERROR); |
| 20 | var i: I64; |
| 21 | begin |
| 22 | for i := 0 to 3 do |
| 23 | if table[i] = key then return i, nil; end; |
| 24 | end; |
| 25 | return -1, NotFound; |
| 26 | end; |
| 27 | |
| 28 | proc mixed(x: F64): (F64, []const U8, Pair, BOOL); |
| 29 | var p: Pair; |
| 30 | begin |
| 31 | p.a := 1; p.b := 2; |
| 32 | return x * 2.0, "two", p, true; |
| 33 | end; |
| 34 | |
| 35 | proc minmax(a, b: I64): (I64, I64); |
| 36 | code |
| 37 | mov rax, rdi; |
| 38 | mov rdx, rsi; |
| 39 | cmp rdi, rsi; |
| 40 | jle done; |
| 41 | mov rax, rsi; |
| 42 | mov rdx, rdi; |
| 43 | done: |
| 44 | ret; |
| 45 | end minmax; |
| 46 | |
| 47 | proc nothing: (I64, ERROR); |
| 48 | begin |
| 49 | end; |
| 50 | |
| 51 | proc main*; |
| 52 | var q, r, i: I64; e: ERROR; x: F64; s: []const U8; p: Pair; ok: BOOL; a: [3]I64; u: U8; |
| 53 | begin |
| 54 | q, r := divmod(17, 5); |
| 55 | Out.int(q); Out.str(" "); Out.int(r); Out.ln; |
| 56 | table := [10, 20, 30, 40]; |
| 57 | i, e := find(30); |
| 58 | Out.int(i); if e = nil then Out.str(" nil"); end; Out.ln; |
| 59 | i, e := find(7); |
| 60 | Out.int(i); Out.str(" "); Out.str(MSG(e)); |
| 61 | if e = NotFound then Out.str(" is NotFound"); end; |
| 62 | if e # Other then Out.str(", not Other"); end; |
| 63 | if e # Empty then Out.str(", not Empty"); end; |
| 64 | Out.ln; |
| 65 | Out.str("["); Out.str(MSG(nil)); Out.str("]"); Out.ln; |
| 66 | x, s, p, ok := mixed(1.5); |
| 67 | Out.int(I64(x * 10.0)); Out.str(" "); Out.str(s); Out.str(" "); Out.int(p.b); |
| 68 | if ok then Out.str(" ok"); end; Out.ln; |
| 69 | a[1], g.b := divmod(9, 4); |
| 70 | Out.int(a[1]); Out.str(" "); Out.int(g.b); Out.ln; |
| 71 | q, r := minmax(9, 3); |
| 72 | Out.int(q); Out.int(r); Out.ln; |
| 73 | _, _ := divmod(1, 1); |
| 74 | i, e := nothing(); |
| 75 | Out.int(i); if e = nil then Out.str(" zero"); end; Out.ln; |
| 76 | i := 5; |
| 77 | INC(i); INC(i, 10); DEC(i, 2); |
| 78 | a[2] := 7; DEC(a[2]); |
| 79 | u := 255; INC(u); |
| 80 | Out.int(i); Out.str(" "); Out.int(a[2]); Out.str(" "); Out.int(I64(u)); Out.ln; |
| 81 | end; |
| 82 | |
| 83 | end Results. |