| 1 | (* Calls of procedures whose body is only return e mean what any call |
| 2 | means: each argument evaluated once, left to right, and converted as |
| 3 | for a call; a var or const parameter the variable itself; the result |
| 4 | of the procedure's type. *) |
| 5 | module Inline; |
| 6 | import Out, Inl; |
| 7 | |
| 8 | type Pair = record a, b: INT; end; |
| 9 | |
| 10 | var trace: [16]INT; numTrace: INT; |
| 11 | |
| 12 | (* v, noted in trace: an argument with a side effect *) |
| 13 | proc note(v: INT): INT; |
| 14 | begin |
| 15 | trace[numTrace] := v; |
| 16 | INC(numTrace); |
| 17 | return v; |
| 18 | end; |
| 19 | |
| 20 | proc sub(a, b: INT): INT; |
| 21 | begin |
| 22 | return a - b; |
| 23 | end; |
| 24 | |
| 25 | proc square(a: INT): INT; |
| 26 | begin |
| 27 | return a * a; |
| 28 | end; |
| 29 | |
| 30 | proc subSquares(a, b: INT): INT; |
| 31 | begin |
| 32 | return sub(square(a), square(b)); |
| 33 | end; |
| 34 | |
| 35 | proc isLower(c: U8): BOOL; |
| 36 | begin |
| 37 | return U8(c - 97) < 26; |
| 38 | end; |
| 39 | |
| 40 | proc wide(x: U8): INT; |
| 41 | begin |
| 42 | return x; |
| 43 | end; |
| 44 | |
| 45 | proc low(x: INT): U8; |
| 46 | begin |
| 47 | return U8(x & 255); |
| 48 | end; |
| 49 | |
| 50 | proc add8(a, b: U8): U8; |
| 51 | begin |
| 52 | return a + b; |
| 53 | end; |
| 54 | |
| 55 | proc half(r: F64): F64; |
| 56 | begin |
| 57 | return r / 2.0; |
| 58 | end; |
| 59 | |
| 60 | proc sumOf(const p: Pair): INT; |
| 61 | begin |
| 62 | return p.a + p.b; |
| 63 | end; |
| 64 | |
| 65 | proc incr(var n: INT): INT; |
| 66 | begin |
| 67 | INC(n); |
| 68 | return n; |
| 69 | end; |
| 70 | |
| 71 | proc bumped(var n: INT): INT; |
| 72 | begin |
| 73 | return incr(n); |
| 74 | end; |
| 75 | |
| 76 | proc second(s: []const U8): U8; |
| 77 | begin |
| 78 | return s[1]; |
| 79 | end; |
| 80 | |
| 81 | proc odd(n: INT): BOOL; forward; |
| 82 | |
| 83 | (* recursive through odd, which is made in place here and there *) |
| 84 | proc even(n: INT): BOOL; |
| 85 | begin |
| 86 | return (n = 0) or odd(n - 1); |
| 87 | end; |
| 88 | |
| 89 | proc odd(n: INT): BOOL; |
| 90 | begin |
| 91 | return (n # 0) and even(n - 1); |
| 92 | end; |
| 93 | |
| 94 | proc show(s: []const U8; n: INT); |
| 95 | begin |
| 96 | Out.str(s); Out.str(" "); Out.int(n); Out.ln; |
| 97 | end; |
| 98 | |
| 99 | proc main*; |
| 100 | var i, n: INT; s: []const U8; p: Pair; f: proc(a, b: INT): INT; c: U8; |
| 101 | begin |
| 102 | show("sub", sub(note(10), note(3))); |
| 103 | show("order", trace[0] * 10 + trace[1]); |
| 104 | show("count", numTrace); |
| 105 | show("nested", subSquares(note(5), note(4))); |
| 106 | show("count", numTrace); |
| 107 | s := "abcD"; |
| 108 | i := 0; |
| 109 | while (i < LEN(s)) and isLower(s[i]) do INC(i); end; |
| 110 | show("lower", i); |
| 111 | c := 200; |
| 112 | show("wide", wide(c) + wide(c)); |
| 113 | show("low", INT(low(-1))); |
| 114 | n := add8(200, 100); |
| 115 | show("add8", n); |
| 116 | if half(3.0) = 1.5 then show("half", 1); end; |
| 117 | p := {a: 3, b: 4}; |
| 118 | show("const", sumOf(p)); |
| 119 | show("literal", sumOf({a: 1, b: 2})); |
| 120 | n := 7; |
| 121 | show("var", bumped(n)); |
| 122 | show("var after", n); |
| 123 | show("slice", INT(second(s))); |
| 124 | if even(10) and odd(7) and not even(3) then show("even", 1); end; |
| 125 | f := sub; |
| 126 | show("value", f(9, 2)); |
| 127 | show("imported", Inl.twice(Inl.plusHidden(1))); |
| 128 | show("generic", Inl.boxed(41)); |
| 129 | end; |
| 130 | end Inline. |