raz / conform / run / Inline.raz

130 lines · 2.4 KiB · raw · history · last change 264716cb

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. *)
5module Inline;
6import Out, Inl;
7
8type Pair = record a, b: INT; end;
9
10var trace: [16]INT; numTrace: INT;
11
12(* v, noted in trace: an argument with a side effect *)
13proc note(v: INT): INT;
14begin
15 trace[numTrace] := v;
16 INC(numTrace);
17 return v;
18end;
19
20proc sub(a, b: INT): INT;
21begin
22 return a - b;
23end;
24
25proc square(a: INT): INT;
26begin
27 return a * a;
28end;
29
30proc subSquares(a, b: INT): INT;
31begin
32 return sub(square(a), square(b));
33end;
34
35proc isLower(c: U8): BOOL;
36begin
37 return U8(c - 97) < 26;
38end;
39
40proc wide(x: U8): INT;
41begin
42 return x;
43end;
44
45proc low(x: INT): U8;
46begin
47 return U8(x & 255);
48end;
49
50proc add8(a, b: U8): U8;
51begin
52 return a + b;
53end;
54
55proc half(r: F64): F64;
56begin
57 return r / 2.0;
58end;
59
60proc sumOf(const p: Pair): INT;
61begin
62 return p.a + p.b;
63end;
64
65proc incr(var n: INT): INT;
66begin
67 INC(n);
68 return n;
69end;
70
71proc bumped(var n: INT): INT;
72begin
73 return incr(n);
74end;
75
76proc second(s: []const U8): U8;
77begin
78 return s[1];
79end;
80
81proc odd(n: INT): BOOL; forward;
82
83(* recursive through odd, which is made in place here and there *)
84proc even(n: INT): BOOL;
85begin
86 return (n = 0) or odd(n - 1);
87end;
88
89proc odd(n: INT): BOOL;
90begin
91 return (n # 0) and even(n - 1);
92end;
93
94proc show(s: []const U8; n: INT);
95begin
96 Out.str(s); Out.str(" "); Out.int(n); Out.ln;
97end;
98
99proc main*;
100var i, n: INT; s: []const U8; p: Pair; f: proc(a, b: INT): INT; c: U8;
101begin
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));
129end;
130end Inline.