annotate ppc/test/ptr.p @ 1:b5139af1a420 tip basis

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