1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
\ checks that postpone works correctly with words with special
\ compilation semantics
\ by M. Anton Ertl 1996
\ This file is based on John Hayes' core.fr (coretest.fs), which has
\ the following copyright notice:
\ (C) 1995 JOHNS HOPKINS UNIVERSITY / APPLIED PHYSICS LABORATORY
\ MAY BE DISTRIBUTED FREELY AS LONG AS THIS COPYRIGHT NOTICE REMAINS.
\ my contributions to this file are in the public domain
\ you have to load John Hayes' tester.fs (=tester.fr) and coretest.fs
\ (core.fr) first
\ These tests are especially useful for showing that state-smart
\ implementations of words with special compilation semantics,
\ combined with a straight-forward implementation of POSTPONE (and
\ [COMPILE]) do not conform to the ANS Forth standard. The essential
\ sentences in the standad are:
\ 6.1.2033 POSTPONE CORE
\ ...
\ Compilation: ( <spaces>name -- )
\ Skip leading space delimiters. Parse name delimited by a space. Find
\ name. Append the compilation semantics of name to the current
\ definition.
\ 6.2.2530 [COMPILE] bracket-compile CORE EXT
\ ...
\ Compilation: ( <spaces>name -- )
\ Skip leading space delimiters. Parse name delimited by a space. Find
\ name. If name has other than default compilation semantics, append
\ them to the current definition;...
\ Note that the compilation semantics are appended, not some
\ state-dependent semantics.
\ first I test against a non-ANS solution suggested by Bernd Paysan
: state@-now ( -- f )
state @ ; immediate
: state@ ( -- f )
POSTPONE state@-now ;
{ state@ -> state @ }
\ here I test POSTPONE with all core words with special compilation
\ semantics.
testing postpone (
: postpone-(
postpone ( ;
{ : pp1 [ postpone-( does nothing ) ] ; -> }
{ here pp1 -> here }
testing postpone +loop
: postpone-+loop
postpone +loop ;
{ : PGD2 DO I -1 [ POSTPONE-+LOOP ] ; -> }
{ 1 4 PGD2 -> 4 3 2 1 }
{ -1 2 PGD2 -> 2 1 0 -1 }
{ MID-UINT MID-UINT+1 PGD2 -> MID-UINT+1 MID-UINT }
{ : PGD4 DO 1 0 DO J LOOP -1 [ POSTPONE-+LOOP ] ; -> }
{ 1 4 PGD4 -> 4 3 2 1 }
{ -1 2 PGD4 -> 2 1 0 -1 }
{ MID-UINT MID-UINT+1 PGD4 -> MID-UINT+1 MID-UINT }
testing postpone ."
: postpone-."
postpone ." ;
: pdq2 [ postpone-." you should see this later. " ] cr ;
: pdq1 [ postpone-." you should see this first. " ] cr ;
{ pdq1 pdq2 -> }
testing postpone ;
: postpone-;
postpone ; ;
{ : psc [ postpone-; -> }
{ psc -> }
testing postpone abort"
: postpone-abort"
postpone abort" ;
{ : paq1 [ postpone-abort" this should not abort" ] ; -> }
testing postpone begin
: postpone-begin
postpone begin ;
{ : PB3 [ POSTPONE-BEGIN ] DUP 5 < WHILE DUP 1+ REPEAT ; -> }
{ 0 PB3 -> 0 1 2 3 4 5 }
{ 4 PB3 -> 4 5 }
{ 5 PB3 -> 5 }
{ 6 PB3 -> 6 }
{ : PB4 [ POSTPONE-BEGIN ] DUP 1+ DUP 5 > UNTIL ; -> }
{ 3 PB4 -> 3 4 5 6 }
{ 5 PB4 -> 5 6 }
{ 6 PB4 -> 6 7 }
{ : PB5 [ POSTPONE-BEGIN ] DUP 2 > WHILE DUP 5 < WHILE DUP 1+ REPEAT 123 ELSE 345 THEN ; -> }
{ 1 PB5 -> 1 345 }
{ 2 PB5 -> 2 345 }
{ 3 PB5 -> 3 4 5 123 }
{ 4 PB5 -> 4 5 123 }
{ 5 PB5 -> 5 123 }
testing postpone do
: postpone-do
postpone do ;
{ : PDO1 [ POSTPONE-DO ] I LOOP ; -> }
{ 4 1 PDO1 -> 1 2 3 }
{ 2 -1 PDO1 -> -1 0 1 }
{ MID-UINT+1 MID-UINT PDO1 -> MID-UINT }
{ : PDO2 [ POSTPONE-DO ] I -1 +LOOP ; -> }
{ 1 4 PDO2 -> 4 3 2 1 }
{ -1 2 PDO2 -> 2 1 0 -1 }
{ MID-UINT MID-UINT+1 PDO2 -> MID-UINT+1 MID-UINT }
{ : PDO3 [ POSTPONE-DO ] 1 0 [ POSTPONE-DO ] J LOOP LOOP ; -> }
{ 4 1 PDO3 -> 1 2 3 }
{ 2 -1 PDO3 -> -1 0 1 }
{ MID-UINT+1 MID-UINT PDO3 -> MID-UINT }
{ : PDO4 [ POSTPONE-DO ] 1 0 [ POSTPONE-DO ] J LOOP -1 +LOOP ; -> }
{ 1 4 PDO4 -> 4 3 2 1 }
{ -1 2 PDO4 -> 2 1 0 -1 }
{ MID-UINT MID-UINT+1 PDO4 -> MID-UINT+1 MID-UINT }
{ : PDO5 123 SWAP 0 [ POSTPONE-DO ] I 4 > IF DROP 234 LEAVE THEN LOOP ; -> }
{ 1 PDO5 -> 123 }
{ 5 PDO5 -> 123 }
{ 6 PDO5 -> 234 }
{ : PDO6 ( PAT: {0 0},{0 0}{1 0}{1 1},{0 0}{1 0}{1 1}{2 0}{2 1}{2 2} )
0 SWAP 0 [ POSTPONE-DO ]
I 1+ 0 [ POSTPONE-DO ] I J + 3 = IF I UNLOOP I UNLOOP EXIT THEN 1+ LOOP
LOOP ; -> }
{ 1 PDO6 -> 1 }
{ 2 PDO6 -> 3 }
{ 3 PDO6 -> 4 1 2 }
testing postpone does>
: postpone-does>
postpone does> ;
{ : PDOES1 [ POSTPONE-DOES> ] @ 1 + ; -> }
{ : PDOES2 [ POSTPONE-DOES> ] @ 2 + ; -> }
{ CREATE PCR1 -> }
{ PCR1 -> HERE }
{ ' PCR1 >BODY -> HERE }
{ 1 , -> }
{ PCR1 @ -> 1 }
{ PDOES1 -> }
{ PCR1 -> 2 }
{ PDOES2 -> }
{ PCR1 -> 3 }
{ : pWEIRD: CREATE [ POSTPONE-DOES> ] 1 + [ POSTPONE-DOES> ] 2 + ; -> }
{ pWEIRD: PW1 -> }
{ ' PW1 >BODY -> HERE }
{ PW1 -> HERE 1 + }
{ PW1 -> HERE 2 + }
testing postpone else
: postpone-else
postpone else ;
{ : PELSE1 IF 123 [ postpone-ELSE ] 234 THEN ; -> }
{ 0 PELSE1 -> 234 }
{ 1 PELSE1 -> 123 }
{ : PELSE2 BEGIN DUP 2 > WHILE DUP 5 < WHILE DUP 1+ REPEAT 123 [ postpone-ELSE ] 345 THEN ; -> }
{ 1 PELSE2 -> 1 345 }
{ 2 PELSE2 -> 2 345 }
{ 3 PELSE2 -> 3 4 5 123 }
{ 4 PELSE2 -> 4 5 123 }
{ 5 PELSE2 -> 5 123 }
testing postpone if
: postpone-if
postpone if ;
{ : PIF1 [ POSTPONE-IF ] 123 THEN ; -> }
{ : PIF2 [ POSTPONE-IF ] 123 ELSE 234 THEN ; -> }
{ 0 PIF1 -> }
{ 1 PIF1 -> 123 }
{ -1 PIF1 -> 123 }
{ 0 PIF2 -> 234 }
{ 1 PIF2 -> 123 }
{ -1 PIF1 -> 123 }
{ : PIF6 ( N -- 0,1,..N ) DUP [ POSTPONE-IF ] DUP >R 1- RECURSE R> THEN ; -> }
{ 0 PIF6 -> 0 }
{ 1 PIF6 -> 0 1 }
{ 2 PIF6 -> 0 1 2 }
{ 3 PIF6 -> 0 1 2 3 }
{ 4 PIF6 -> 0 1 2 3 4 }
testing postpone literal
: postpone-literal
postpone literal ;
{ : plit [ 42 postpone-literal ] ; -> }
{ plit -> 42 }
testing postpone loop
: postpone-loop
postpone loop ;
{ : PLOOP1 DO I [ POSTPONE-LOOP ] ; -> }
{ 4 1 PLOOP1 -> 1 2 3 }
{ 2 -1 PLOOP1 -> -1 0 1 }
{ MID-UINT+1 MID-UINT PLOOP1 -> MID-UINT }
{ : PLOOP3 DO 1 0 DO J [ POSTPONE-LOOP ] [ POSTPONE-LOOP ] ; -> }
{ 4 1 PLOOP3 -> 1 2 3 }
{ 2 -1 PLOOP3 -> -1 0 1 }
{ MID-UINT+1 MID-UINT PLOOP3 -> MID-UINT }
{ : PLOOP4 DO 1 0 DO J [ POSTPONE-LOOP ] -1 +LOOP ; -> }
{ 1 4 PLOOP4 -> 4 3 2 1 }
{ -1 2 PLOOP4 -> 2 1 0 -1 }
{ MID-UINT MID-UINT+1 PLOOP4 -> MID-UINT+1 MID-UINT }
{ : PLOOP5 123 SWAP 0 DO I 4 > IF DROP 234 LEAVE THEN [ POSTPONE-LOOP ] ; -> }
{ 1 PLOOP5 -> 123 }
{ 5 PLOOP5 -> 123 }
{ 6 PLOOP5 -> 234 }
{ : PLOOP6 ( PAT: {0 0},{0 0}{1 0}{1 1},{0 0}{1 0}{1 1}{2 0}{2 1}{2 2} )
0 SWAP 0 DO
I 1+ 0 DO I J + 3 = IF I UNLOOP I UNLOOP EXIT THEN 1+ [ POSTPONE-LOOP ]
[ POSTPONE-LOOP ] ; -> }
{ 1 PLOOP6 -> 1 }
{ 2 PLOOP6 -> 3 }
{ 3 PLOOP6 -> 4 1 2 }
testing postpone postpone
: postpone-postpone
postpone postpone ;
{ : PPP1 123 ; -> }
{ : PPP4 [ POSTPONE-POSTPONE PPP1 ] ; IMMEDIATE -> }
{ : PPP5 PPP4 ; -> }
{ PPP5 -> 123 }
{ : PPP6 345 ; IMMEDIATE -> }
{ : PPP7 [ POSTPONE-POSTPONE PPP6 ] ; -> }
{ PPP7 -> 345 }
testing postpone recurse
: postpone-recurse
postpone recurse ;
{ : GREC ( N -- 0,1,..N ) DUP IF DUP >R 1- [ postpone-RECURSE ] R> THEN ; -> }
{ 0 GREC -> 0 }
{ 1 GREC -> 0 1 }
{ 2 GREC -> 0 1 2 }
{ 3 GREC -> 0 1 2 3 }
{ 4 GREC -> 0 1 2 3 4 }
testing postpone repeat
: postpone-repeat
postpone repeat ;
{ : PREP3 BEGIN DUP 5 < WHILE DUP 1+ [ POSTPONE-REPEAT ] ; -> }
{ 0 PREP3 -> 0 1 2 3 4 5 }
{ 4 PREP3 -> 4 5 }
{ 5 PREP3 -> 5 }
{ 6 PREP3 -> 6 }
{ : PREP5 BEGIN DUP 2 > WHILE DUP 5 < WHILE DUP 1+ [ POSTPONE-REPEAT ] 123 ELSE 345 THEN ; -> }
{ 1 PREP5 -> 1 345 }
{ 2 PREP5 -> 2 345 }
{ 3 PREP5 -> 3 4 5 123 }
{ 4 PREP5 -> 4 5 123 }
{ 5 PREP5 -> 5 123 }
testing postpone S"
: postpone-s"
postpone s" ;
{ : PSQ4 [ postpone-S" XY" ] ; -> }
{ PSQ4 SWAP DROP -> 2 }
{ PSQ4 DROP DUP C@ SWAP CHAR+ C@ -> 58 59 }
testing postpone then
: postpone-then
postpone then ;
{ : PTH1 IF 123 [ POSTPONE-THEN ] ; -> }
{ : PTH2 IF 123 ELSE 234 [ POSTPONE-THEN ] ; -> }
{ 0 PTH1 -> }
{ 1 PTH1 -> 123 }
{ -1 PTH1 -> 123 }
{ 0 PTH2 -> 234 }
{ 1 PTH2 -> 123 }
{ -1 PTH1 -> 123 }
{ : PTH5 BEGIN DUP 2 > WHILE DUP 5 < WHILE DUP 1+ REPEAT 123 ELSE 345 [ POSTPONE-THEN ] ; -> }
{ 1 PTH5 -> 1 345 }
{ 2 PTH5 -> 2 345 }
{ 3 PTH5 -> 3 4 5 123 }
{ 4 PTH5 -> 4 5 123 }
{ 5 PTH5 -> 5 123 }
{ : PTH6 ( N -- 0,1,..N ) DUP IF DUP >R 1- RECURSE R> [ POSTPONE-THEN ] ; -> }
{ 0 PTH6 -> 0 }
{ 1 PTH6 -> 0 1 }
{ 2 PTH6 -> 0 1 2 }
{ 3 PTH6 -> 0 1 2 3 }
{ 4 PTH6 -> 0 1 2 3 4 }
testing postpone until
: postpone-until
postpone until ;
{ : PUNT4 BEGIN DUP 1+ DUP 5 > [ postpone-UNTIL ] ; -> }
{ 3 PUNT4 -> 3 4 5 6 }
{ 5 PUNT4 -> 5 6 }
{ 6 PUNT4 -> 6 7 }
testing postpone while
: postpone-while
postpone while ;
{ : PWH3 BEGIN DUP 5 < [ POSTPONE-WHILE ] DUP 1+ REPEAT ; -> }
{ 0 PWH3 -> 0 1 2 3 4 5 }
{ 4 PWH3 -> 4 5 }
{ 5 PWH3 -> 5 }
{ 6 PWH3 -> 6 }
{ : PWH5 BEGIN DUP 2 > [ POSTPONE-WHILE ] DUP 5 < [ POSTPONE-WHILE ] DUP 1+ REPEAT 123 ELSE 345 THEN ; -> }
{ 1 PWH5 -> 1 345 }
{ 2 PWH5 -> 2 345 }
{ 3 PWH5 -> 3 4 5 123 }
{ 4 PWH5 -> 4 5 123 }
{ 5 PWH5 -> 5 123 }
testing postpone [
: postpone-[
postpone [ ;
{ here postpone-[ -> here }
testing postpone [']
: postpone-[']
postpone ['] ;
{ : PTICK1 123 ; -> }
{ : PTICK2 [ postpone-['] PTICK1 ] ; IMMEDIATE -> }
{ PTICK2 EXECUTE -> 123 }
testing postpone [char]
: postpone-[char]
postpone [char] ;
{ : PCHAR1 [ postpone-[CHAR] X ] ; -> }
{ : PCHAR2 [ postpone-[CHAR] HELLO ] ; -> }
{ PCHAR1 -> 58 }
{ PCHAR2 -> 48 }
|