annotate ppc/test/record.p @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
mike@0 1 type string = array 10 of char;
mike@0 2
mike@0 3 type rec = record name: string; age: integer end;
mike@0 4
mike@0 5 var
mike@0 6 db: array 20 of rec;
mike@0 7 N: integer;
mike@0 8
mike@0 9 proc equal(x, y: string): boolean;
mike@0 10 var i: integer;
mike@0 11 begin
mike@0 12 i := 0;
mike@0 13 while i < 10 do
mike@0 14 if x[i] <> y[i] then
mike@0 15 return false
mike@0 16 end;
mike@0 17 i := i+1
mike@0 18 end;
mike@0 19 return true
mike@0 20 end;
mike@0 21
mike@0 22 proc store(n: string; a: integer);
mike@0 23 begin
mike@0 24 db[N].name := n;
mike@0 25 db[N].age := a;
mike@0 26 N := N+1
mike@0 27 end;
mike@0 28
mike@0 29 proc recall(n: string): integer;
mike@0 30 var i: integer;
mike@0 31 begin
mike@0 32 i := 0;
mike@0 33 while i < N do
mike@0 34 if equal(db[i].name, n) then
mike@0 35 return db[i].age
mike@0 36 end;
mike@0 37 i := i+1
mike@0 38 end;
mike@0 39 return 999
mike@0 40 end;
mike@0 41
mike@0 42 begin
mike@0 43 N := 0;
mike@0 44
mike@0 45 store("bill ", 23);
mike@0 46 store("george ", 34);
mike@0 47
mike@0 48 print_num(recall("george ")); newline();
mike@0 49 print_num(recall("fred ")); newline()
mike@0 50 end.
mike@0 51
mike@0 52 (*<<
mike@0 53 34
mike@0 54 999
mike@0 55 >>*)
mike@0 56
mike@0 57 (*[[
mike@0 58 MODULE Main 0 0
mike@0 59 IMPORT Lib 0
mike@0 60 ENDHDR
mike@0 61
mike@0 62 PROC _equal 4 0 0
mike@0 63 ! i := 0;
mike@0 64 CONST 0
mike@0 65 STLW -4
mike@0 66 ! while i < 10 do
mike@0 67 JUMP L6
mike@0 68 LABEL L5
mike@0 69 ! if x[i] <> y[i] then
mike@0 70 LDLW 16
mike@0 71 LDLW -4
mike@0 72 LDIC
mike@0 73 LDLW 20
mike@0 74 LDLW -4
mike@0 75 LDIC
mike@0 76 JEQ L10
mike@0 77 ! return false
mike@0 78 CONST 0
mike@0 79 RETURNW
mike@0 80 LABEL L10
mike@0 81 ! i := i+1
mike@0 82 LDLW -4
mike@0 83 CONST 1
mike@0 84 PLUS
mike@0 85 STLW -4
mike@0 86 LABEL L6
mike@0 87 LDLW -4
mike@0 88 CONST 10
mike@0 89 JLT L5
mike@0 90 ! return true
mike@0 91 CONST 1
mike@0 92 RETURNW
mike@0 93 END
mike@0 94
mike@0 95 PROC _store 0 0 0
mike@0 96 ! db[N].name := n;
mike@0 97 GLOBAL _db
mike@0 98 LDGW _N
mike@0 99 CONST 16
mike@0 100 TIMES
mike@0 101 OFFSET
mike@0 102 LDLW 16
mike@0 103 CONST 10
mike@0 104 FIXCOPY
mike@0 105 ! db[N].age := a;
mike@0 106 LDLW 20
mike@0 107 GLOBAL _db
mike@0 108 LDGW _N
mike@0 109 CONST 16
mike@0 110 TIMES
mike@0 111 OFFSET
mike@0 112 STNW 12
mike@0 113 ! N := N+1
mike@0 114 LDGW _N
mike@0 115 CONST 1
mike@0 116 PLUS
mike@0 117 STGW _N
mike@0 118 RETURN
mike@0 119 END
mike@0 120
mike@0 121 PROC _recall 4 0 0
mike@0 122 ! i := 0;
mike@0 123 CONST 0
mike@0 124 STLW -4
mike@0 125 ! while i < N do
mike@0 126 JUMP L12
mike@0 127 LABEL L11
mike@0 128 ! if equal(db[i].name, n) then
mike@0 129 LDLW 16
mike@0 130 GLOBAL _db
mike@0 131 LDLW -4
mike@0 132 CONST 16
mike@0 133 TIMES
mike@0 134 OFFSET
mike@0 135 CONST 0
mike@0 136 GLOBAL _equal
mike@0 137 PCALLW 2
mike@0 138 JNEQZ L14
mike@0 139 JUMP L16
mike@0 140 LABEL L14
mike@0 141 ! return db[i].age
mike@0 142 GLOBAL _db
mike@0 143 LDLW -4
mike@0 144 CONST 16
mike@0 145 TIMES
mike@0 146 OFFSET
mike@0 147 LDNW 12
mike@0 148 RETURNW
mike@0 149 LABEL L16
mike@0 150 ! i := i+1
mike@0 151 LDLW -4
mike@0 152 CONST 1
mike@0 153 PLUS
mike@0 154 STLW -4
mike@0 155 LABEL L12
mike@0 156 LDLW -4
mike@0 157 LDGW _N
mike@0 158 JLT L11
mike@0 159 ! return 999
mike@0 160 CONST 999
mike@0 161 RETURNW
mike@0 162 END
mike@0 163
mike@0 164 PROC MAIN 0 0 0
mike@0 165 ! N := 0;
mike@0 166 CONST 0
mike@0 167 STGW _N
mike@0 168 ! store("bill ", 23);
mike@0 169 CONST 23
mike@0 170 GLOBAL g1
mike@0 171 CONST 0
mike@0 172 GLOBAL _store
mike@0 173 PCALL 2
mike@0 174 ! store("george ", 34);
mike@0 175 CONST 34
mike@0 176 GLOBAL g2
mike@0 177 CONST 0
mike@0 178 GLOBAL _store
mike@0 179 PCALL 2
mike@0 180 ! print_num(recall("george ")); newline();
mike@0 181 GLOBAL g3
mike@0 182 CONST 0
mike@0 183 GLOBAL _recall
mike@0 184 PCALLW 1
mike@0 185 CONST 0
mike@0 186 GLOBAL lib.print_num
mike@0 187 PCALL 1
mike@0 188 CONST 0
mike@0 189 GLOBAL lib.newline
mike@0 190 PCALL 0
mike@0 191 ! print_num(recall("fred ")); newline()
mike@0 192 GLOBAL g4
mike@0 193 CONST 0
mike@0 194 GLOBAL _recall
mike@0 195 PCALLW 1
mike@0 196 CONST 0
mike@0 197 GLOBAL lib.print_num
mike@0 198 PCALL 1
mike@0 199 CONST 0
mike@0 200 GLOBAL lib.newline
mike@0 201 PCALL 0
mike@0 202 RETURN
mike@0 203 END
mike@0 204
mike@0 205 GLOVAR _db 320
mike@0 206 GLOVAR _N 4
mike@0 207 ! String "bill "
mike@0 208 DEFINE g1
mike@0 209 STRING 62696C6C20202020202000
mike@0 210
mike@0 211 ! String "george "
mike@0 212 DEFINE g2
mike@0 213 STRING 67656F7267652020202000
mike@0 214
mike@0 215 ! String "george "
mike@0 216 DEFINE g3
mike@0 217 STRING 67656F7267652020202000
mike@0 218
mike@0 219 ! String "fred "
mike@0 220 DEFINE g4
mike@0 221 STRING 6672656420202020202000
mike@0 222
mike@0 223 ! End
mike@0 224 ]]*)