comparison ppc/test/ptr.p @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 type
2 tree = pointer to node;
3 node = record left, right: tree end;
4
5 proc build(n: integer): tree;
6 var t: tree;
7 begin
8 if n <= 1 then
9 return nil
10 else
11 new(t);
12 t^.left := build(n-2);
13 t^.right := build(n-1);
14 return t
15 end
16 end;
17
18 proc print(t:tree);
19 begin
20 if t = nil then
21 print_char('.')
22 else
23 print_char('(');
24 print(t^.left);
25 print(t^.right);
26 print_char(')')
27 end
28 end;
29
30 proc display(t: tree);
31 var buf: array 20 of char;
32
33 proc indent(i: integer);
34 var j: integer;
35 begin
36 j := 0;
37 while j <= i do print_char(' '); print_char(buf[j]); j := j+1 end
38 end;
39
40 proc disp(i: integer; t: tree);
41 begin
42 if t = nil then
43 print_char('$');
44 newline()
45 else
46 print_char('+'); print_char('-');
47 buf[i] := '|';
48 disp(i+1, t^.right);
49 indent(i-1);
50 print_char(' '); print_char(' '); print_char('\');
51 newline();
52 buf[i] := ' ';
53 indent(i); print_char(' ');
54 disp(i+1, t^.left)
55 end
56 end;
57
58 begin
59 print_char('-'); disp(0, t)
60 end;
61
62 proc count(t:tree): integer;
63 begin
64 if t = nil then
65 return 1
66 else
67 return count(t^.left) + count(t^.right)
68 end
69 end;
70
71 var p: tree;
72
73 begin
74 p := build(7);
75 print(p); newline();
76 (* newline(); display(p); newline(); *)
77 print_string("Count = "); print_num(count(p)); newline()
78 end.
79
80 (*<<
81 (((.(..))((..)(.(..))))(((..)(.(..)))((.(..))((..)(.(..))))))
82 Count = 21
83 >>*)
84
85 (*[[
86 MODULE Main 0 0
87 IMPORT Lib 0
88 ENDHDR
89
90 PROC _build 4 0 0
91 ! if n <= 1 then
92 LDLW 16
93 CONST 1
94 JGT L3
95 ! return nil
96 CONST 0
97 RETURNW
98 LABEL L3
99 ! new(t);
100 CONST 8
101 CONST 0
102 GLOBAL lib.new
103 PCALLW 1
104 STLW -4
105 ! t^.left := build(n-2);
106 LDLW 16
107 CONST 2
108 MINUS
109 CONST 0
110 GLOBAL _build
111 PCALLW 1
112 LDLW -4
113 STOREW
114 ! t^.right := build(n-1);
115 LDLW 16
116 CONST 1
117 MINUS
118 CONST 0
119 GLOBAL _build
120 PCALLW 1
121 LDLW -4
122 STNW 4
123 ! return t
124 LDLW -4
125 RETURNW
126 END
127
128 PROC _print 0 0 0
129 ! if t = nil then
130 LDLW 16
131 JEQZ L5
132 JUMP L6
133 LABEL L5
134 ! print_char('.')
135 CONST 46
136 CONST 0
137 GLOBAL lib.print_char
138 PCALL 1
139 JUMP L7
140 LABEL L6
141 ! print_char('(');
142 CONST 40
143 CONST 0
144 GLOBAL lib.print_char
145 PCALL 1
146 ! print(t^.left);
147 LDLW 16
148 LOADW
149 CONST 0
150 GLOBAL _print
151 PCALL 1
152 ! print(t^.right);
153 LDLW 16
154 LDNW 4
155 CONST 0
156 GLOBAL _print
157 PCALL 1
158 ! print_char(')')
159 CONST 41
160 CONST 0
161 GLOBAL lib.print_char
162 PCALL 1
163 LABEL L7
164 RETURN
165 END
166
167 PROC _display 20 0 0
168 ! print_char('-'); disp(0, t)
169 CONST 45
170 CONST 0
171 GLOBAL lib.print_char
172 PCALL 1
173 LDLW 16
174 CONST 0
175 LOCAL 0
176 GLOBAL _disp
177 PCALL 2
178 RETURN
179 END
180
181 PROC _indent 4 0 0
182 ! j := 0;
183 CONST 0
184 STLW -4
185 ! while j <= i do print_char(' '); print_char(buf[j]); j := j+1 end
186 JUMP L9
187 LABEL L8
188 CONST 32
189 CONST 0
190 GLOBAL lib.print_char
191 PCALL 1
192 LDLW 12
193 CONST -20
194 OFFSET
195 LDLW -4
196 LDIC
197 CONST 0
198 GLOBAL lib.print_char
199 PCALL 1
200 LDLW -4
201 CONST 1
202 PLUS
203 STLW -4
204 LABEL L9
205 LDLW -4
206 LDLW 16
207 JLEQ L8
208 RETURN
209 END
210
211 PROC _disp 0 0 0
212 ! if t = nil then
213 LDLW 20
214 JEQZ L11
215 JUMP L12
216 LABEL L11
217 ! print_char('$');
218 CONST 36
219 CONST 0
220 GLOBAL lib.print_char
221 PCALL 1
222 ! newline()
223 CONST 0
224 GLOBAL lib.newline
225 PCALL 0
226 JUMP L13
227 LABEL L12
228 ! print_char('+'); print_char('-');
229 CONST 43
230 CONST 0
231 GLOBAL lib.print_char
232 PCALL 1
233 CONST 45
234 CONST 0
235 GLOBAL lib.print_char
236 PCALL 1
237 ! buf[i] := '|';
238 CONST 124
239 LDLW 12
240 CONST -20
241 OFFSET
242 LDLW 16
243 STIC
244 ! disp(i+1, t^.right);
245 LDLW 20
246 LDNW 4
247 LDLW 16
248 CONST 1
249 PLUS
250 LDLW 12
251 GLOBAL _disp
252 PCALL 2
253 ! indent(i-1);
254 LDLW 16
255 CONST 1
256 MINUS
257 LDLW 12
258 GLOBAL _indent
259 PCALL 1
260 ! print_char(' '); print_char(' '); print_char('\');
261 CONST 32
262 CONST 0
263 GLOBAL lib.print_char
264 PCALL 1
265 CONST 32
266 CONST 0
267 GLOBAL lib.print_char
268 PCALL 1
269 CONST 92
270 CONST 0
271 GLOBAL lib.print_char
272 PCALL 1
273 ! newline();
274 CONST 0
275 GLOBAL lib.newline
276 PCALL 0
277 ! buf[i] := ' ';
278 CONST 32
279 LDLW 12
280 CONST -20
281 OFFSET
282 LDLW 16
283 STIC
284 ! indent(i); print_char(' ');
285 LDLW 16
286 LDLW 12
287 GLOBAL _indent
288 PCALL 1
289 CONST 32
290 CONST 0
291 GLOBAL lib.print_char
292 PCALL 1
293 ! disp(i+1, t^.left)
294 LDLW 20
295 LOADW
296 LDLW 16
297 CONST 1
298 PLUS
299 LDLW 12
300 GLOBAL _disp
301 PCALL 2
302 LABEL L13
303 RETURN
304 END
305
306 PROC _count 0 0 0
307 ! if t = nil then
308 LDLW 16
309 JEQZ L14
310 JUMP L15
311 LABEL L14
312 ! return 1
313 CONST 1
314 RETURNW
315 LABEL L15
316 ! return count(t^.left) + count(t^.right)
317 LDLW 16
318 LOADW
319 CONST 0
320 GLOBAL _count
321 PCALLW 1
322 LDLW 16
323 LDNW 4
324 CONST 0
325 GLOBAL _count
326 PCALLW 1
327 PLUS
328 RETURNW
329 END
330
331 PROC MAIN 0 0 0
332 ! p := build(7);
333 CONST 7
334 CONST 0
335 GLOBAL _build
336 PCALLW 1
337 STGW _p
338 ! print(p); newline();
339 LDGW _p
340 CONST 0
341 GLOBAL _print
342 PCALL 1
343 CONST 0
344 GLOBAL lib.newline
345 PCALL 0
346 ! print_string("Count = "); print_num(count(p)); newline()
347 CONST 8
348 GLOBAL g1
349 CONST 0
350 GLOBAL lib.print_string
351 PCALL 2
352 LDGW _p
353 CONST 0
354 GLOBAL _count
355 PCALLW 1
356 CONST 0
357 GLOBAL lib.print_num
358 PCALL 1
359 CONST 0
360 GLOBAL lib.newline
361 PCALL 0
362 RETURN
363 END
364
365 GLOVAR _p 4
366 ! String "Count = "
367 DEFINE g1
368 STRING 436F756E74203D2000
369
370 ! End
371 ]]*)