3 \ Date: Mon, 27 Nov 95 13:10
5 \ (C) 1995 JOHNS HOPKINS UNIVERSITY / APPLIED PHYSICS LABORATORY
6 \ MAY BE DISTRIBUTED FREELY AS LONG AS THIS COPYRIGHT NOTICE REMAINS.
8 \ THIS PROGRAM TESTS THE CORE WORDS OF AN ANS FORTH SYSTEM.
9 \ THE PROGRAM ASSUMES A TWO'S COMPLEMENT IMPLEMENTATION WHERE
10 \ THE RANGE OF SIGNED NUMBERS IS -2^(N-1) ... 2^(N-1)-1 AND
11 \ THE RANGE OF UNSIGNED NUMBERS IS 0 ... 2^(N)-1.
12 \ I HAVEN'T FIGURED OUT HOW TO TEST KEY, QUIT, ABORT, OR ABORT"...
13 \ I ALSO HAVEN'T THOUGHT OF A WAY TO TEST ENVIRONMENT?...
18 \ ------------------------------------------------------------------------
19 TESTING BASIC ASSUMPTIONS
21 { -> } \ START WITH CLEAN SLATE
22 ( TEST IF ANY BITS ARE SET; ANSWER IN BASE 1 )
23 { : BITSSET? IF 0 0 ELSE 0 THEN ; -> }
24 { 0 BITSSET? -> 0 } ( ZERO IS ALL BITS CLEAR )
25 { 1 BITSSET? -> 0 0 } ( OTHER NUMBER HAVE AT LEAST ONE BIT )
26 { -1 BITSSET? -> 0 0 }
28 \ ------------------------------------------------------------------------
29 TESTING BOOLEANS: INVERT AND OR XOR
36 { 0 INVERT 1 AND -> 1 }
37 { 1 INVERT 1 AND -> 0 }
60 \ ------------------------------------------------------------------------
61 TESTING 2* 2/ LSHIFT RSHIFT
63 ( WE TRUST 1S, INVERT, AND BITSSET?; WE WILL CONFIRM RSHIFT LATER )
64 1S 1 RSHIFT INVERT CONSTANT MSB
65 { MSB BITSSET? -> 0 0 }
76 { 1S 2/ -> 1S } \ MSB PROPOGATED
78 { MSB 2/ MSB AND -> MSB }
83 { 1 F LSHIFT -> 8000 } \ BIGGEST GUARANTEED SHIFT
84 { 1S 1 LSHIFT 1 XOR -> 1S }
91 { 8000 F RSHIFT -> 1 } \ BIGGEST
92 { MSB 1 RSHIFT MSB AND -> 0 } \ RSHIFT ZERO FILLS MSBS
93 { MSB 1 RSHIFT 2* -> MSB }
95 \ ------------------------------------------------------------------------
96 TESTING COMPARISONS: 0= = 0< < > U< MIN MAX
97 0 INVERT CONSTANT MAX-UINT
98 0 INVERT 1 RSHIFT CONSTANT MAX-INT
99 0 INVERT 1 RSHIFT INVERT CONSTANT MIN-INT
100 0 INVERT 1 RSHIFT CONSTANT MID-UINT
101 0 INVERT 1 RSHIFT INVERT CONSTANT MID-UINT+1
110 { MAX-UINT 0= -> <FALSE> }
111 { MIN-INT 0= -> <FALSE> }
112 { MAX-INT 0= -> <FALSE> }
116 { -1 -1 = -> <TRUE> }
118 { -1 0 = -> <FALSE> }
120 { 0 -1 = -> <FALSE> }
124 { MIN-INT 0< -> <TRUE> }
126 { MAX-INT 0< -> <FALSE> }
132 { MIN-INT 0 < -> <TRUE> }
133 { MIN-INT MAX-INT < -> <TRUE> }
134 { 0 MAX-INT < -> <TRUE> }
139 { 0 -1 < -> <FALSE> }
140 { 1 -1 < -> <FALSE> }
141 { 0 MIN-INT < -> <FALSE> }
142 { MAX-INT MIN-INT < -> <FALSE> }
143 { MAX-INT 0 < -> <FALSE> }
147 { -1 0 > -> <FALSE> }
148 { -1 1 > -> <FALSE> }
149 { MIN-INT 0 > -> <FALSE> }
150 { MIN-INT MAX-INT > -> <FALSE> }
151 { 0 MAX-INT > -> <FALSE> }
158 { 0 MIN-INT > -> <TRUE> }
159 { MAX-INT MIN-INT > -> <TRUE> }
160 { MAX-INT 0 > -> <TRUE> }
164 { 0 MID-UINT U< -> <TRUE> }
165 { 0 MAX-UINT U< -> <TRUE> }
166 { MID-UINT MAX-UINT U< -> <TRUE> }
167 { 0 0 U< -> <FALSE> }
168 { 1 1 U< -> <FALSE> }
169 { 1 0 U< -> <FALSE> }
170 { 2 1 U< -> <FALSE> }
171 { MID-UINT 0 U< -> <FALSE> }
172 { MAX-UINT 0 U< -> <FALSE> }
173 { MAX-UINT MID-UINT U< -> <FALSE> }
179 { MIN-INT 0 MIN -> MIN-INT }
180 { MIN-INT MAX-INT MIN -> MIN-INT }
181 { 0 MAX-INT MIN -> 0 }
188 { 0 MIN-INT MIN -> MIN-INT }
189 { MAX-INT MIN-INT MIN -> MIN-INT }
190 { MAX-INT 0 MIN -> 0 }
196 { MIN-INT 0 MAX -> 0 }
197 { MIN-INT MAX-INT MAX -> MAX-INT }
198 { 0 MAX-INT MAX -> MAX-INT }
205 { 0 MIN-INT MAX -> 0 }
206 { MAX-INT MIN-INT MAX -> MAX-INT }
207 { MAX-INT 0 MAX -> MAX-INT }
209 \ ------------------------------------------------------------------------
210 TESTING STACK OPS: 2DROP 2DUP 2OVER 2SWAP ?DUP DEPTH DROP DUP OVER ROT SWAP
213 { 1 2 2DUP -> 1 2 1 2 }
214 { 1 2 3 4 2OVER -> 1 2 3 4 1 2 }
215 { 1 2 3 4 2SWAP -> 3 4 1 2 }
221 { 0 1 DEPTH -> 0 1 2 }
225 { 1 2 OVER -> 1 2 1 }
226 { 1 2 3 ROT -> 2 3 1 }
229 \ ------------------------------------------------------------------------
233 { : GR2 >R R@ R> DROP ; -> }
236 { 1S GR1 -> 1S } ( RETURN STACK HOLDS CELLS )
238 \ ------------------------------------------------------------------------
239 TESTING ADD/SUBTRACT: + - 1+ 1- ABS NEGATE
250 { MID-UINT 1 + -> MID-UINT+1 }
261 { MID-UINT+1 1 - -> MID-UINT }
266 { MID-UINT 1+ -> MID-UINT+1 }
271 { MID-UINT+1 1- -> MID-UINT }
282 { MIN-INT ABS -> MID-UINT+1 }
284 \ ------------------------------------------------------------------------
285 TESTING MULTIPLY: S>D * M* UM*
292 { MIN-INT S>D -> MIN-INT -1 }
293 { MAX-INT S>D -> MAX-INT 0 }
301 { -3 3 M* -> -9 S>D }
302 { 3 -3 M* -> -9 S>D }
303 { -3 -3 M* -> 9 S>D }
304 { 0 MIN-INT M* -> 0 S>D }
305 { 1 MIN-INT M* -> MIN-INT S>D }
306 { 2 MIN-INT M* -> 0 1S }
307 { 0 MAX-INT M* -> 0 S>D }
308 { 1 MAX-INT M* -> MAX-INT S>D }
309 { 2 MAX-INT M* -> MAX-INT 1 LSHIFT 0 }
310 { MIN-INT MIN-INT M* -> 0 MSB 1 RSHIFT }
311 { MAX-INT MIN-INT M* -> MSB MSB 2/ }
312 { MAX-INT MAX-INT M* -> 1 MSB 2/ INVERT }
314 { 0 0 * -> 0 } \ TEST IDENTITIES
324 { MID-UINT+1 1 RSHIFT 2 * -> MID-UINT+1 }
325 { MID-UINT+1 2 RSHIFT 4 * -> MID-UINT+1 }
326 { MID-UINT+1 1 RSHIFT MID-UINT+1 OR 2 * -> MID-UINT+1 }
335 { MID-UINT+1 1 RSHIFT 2 UM* -> MID-UINT+1 0 }
336 { MID-UINT+1 2 UM* -> 0 1 }
337 { MID-UINT+1 4 UM* -> 0 2 }
338 { 1S 2 UM* -> 1S 1 LSHIFT 1 }
339 { MAX-UINT MAX-UINT UM* -> 1 1 INVERT }
341 \ ------------------------------------------------------------------------
342 TESTING DIVIDE: FM/MOD SM/REM UM/MOD */ */MOD / /MOD MOD
344 { 0 S>D 1 FM/MOD -> 0 0 }
345 { 1 S>D 1 FM/MOD -> 0 1 }
346 { 2 S>D 1 FM/MOD -> 0 2 }
347 { -1 S>D 1 FM/MOD -> 0 -1 }
348 { -2 S>D 1 FM/MOD -> 0 -2 }
349 { 0 S>D -1 FM/MOD -> 0 0 }
350 { 1 S>D -1 FM/MOD -> 0 -1 }
351 { 2 S>D -1 FM/MOD -> 0 -2 }
352 { -1 S>D -1 FM/MOD -> 0 1 }
353 { -2 S>D -1 FM/MOD -> 0 2 }
354 { 2 S>D 2 FM/MOD -> 0 1 }
355 { -1 S>D -1 FM/MOD -> 0 1 }
356 { -2 S>D -2 FM/MOD -> 0 1 }
357 { 7 S>D 3 FM/MOD -> 1 2 }
358 { 7 S>D -3 FM/MOD -> -2 -3 }
359 { -7 S>D 3 FM/MOD -> 2 -3 }
360 { -7 S>D -3 FM/MOD -> -1 2 }
361 { MAX-INT S>D 1 FM/MOD -> 0 MAX-INT }
362 { MIN-INT S>D 1 FM/MOD -> 0 MIN-INT }
363 { MAX-INT S>D MAX-INT FM/MOD -> 0 1 }
364 { MIN-INT S>D MIN-INT FM/MOD -> 0 1 }
365 { 1S 1 4 FM/MOD -> 3 MAX-INT }
366 { 1 MIN-INT M* 1 FM/MOD -> 0 MIN-INT }
367 { 1 MIN-INT M* MIN-INT FM/MOD -> 0 1 }
368 { 2 MIN-INT M* 2 FM/MOD -> 0 MIN-INT }
369 { 2 MIN-INT M* MIN-INT FM/MOD -> 0 2 }
370 { 1 MAX-INT M* 1 FM/MOD -> 0 MAX-INT }
371 { 1 MAX-INT M* MAX-INT FM/MOD -> 0 1 }
372 { 2 MAX-INT M* 2 FM/MOD -> 0 MAX-INT }
373 { 2 MAX-INT M* MAX-INT FM/MOD -> 0 2 }
374 { MIN-INT MIN-INT M* MIN-INT FM/MOD -> 0 MIN-INT }
375 { MIN-INT MAX-INT M* MIN-INT FM/MOD -> 0 MAX-INT }
376 { MIN-INT MAX-INT M* MAX-INT FM/MOD -> 0 MIN-INT }
377 { MAX-INT MAX-INT M* MAX-INT FM/MOD -> 0 MAX-INT }
379 { 0 S>D 1 SM/REM -> 0 0 }
380 { 1 S>D 1 SM/REM -> 0 1 }
381 { 2 S>D 1 SM/REM -> 0 2 }
382 { -1 S>D 1 SM/REM -> 0 -1 }
383 { -2 S>D 1 SM/REM -> 0 -2 }
384 { 0 S>D -1 SM/REM -> 0 0 }
385 { 1 S>D -1 SM/REM -> 0 -1 }
386 { 2 S>D -1 SM/REM -> 0 -2 }
387 { -1 S>D -1 SM/REM -> 0 1 }
388 { -2 S>D -1 SM/REM -> 0 2 }
389 { 2 S>D 2 SM/REM -> 0 1 }
390 { -1 S>D -1 SM/REM -> 0 1 }
391 { -2 S>D -2 SM/REM -> 0 1 }
392 { 7 S>D 3 SM/REM -> 1 2 }
393 { 7 S>D -3 SM/REM -> 1 -2 }
394 { -7 S>D 3 SM/REM -> -1 -2 }
395 { -7 S>D -3 SM/REM -> -1 2 }
396 { MAX-INT S>D 1 SM/REM -> 0 MAX-INT }
397 { MIN-INT S>D 1 SM/REM -> 0 MIN-INT }
398 { MAX-INT S>D MAX-INT SM/REM -> 0 1 }
399 { MIN-INT S>D MIN-INT SM/REM -> 0 1 }
400 { 1S 1 4 SM/REM -> 3 MAX-INT }
401 { 2 MIN-INT M* 2 SM/REM -> 0 MIN-INT }
402 { 2 MIN-INT M* MIN-INT SM/REM -> 0 2 }
403 { 2 MAX-INT M* 2 SM/REM -> 0 MAX-INT }
404 { 2 MAX-INT M* MAX-INT SM/REM -> 0 2 }
405 { MIN-INT MIN-INT M* MIN-INT SM/REM -> 0 MIN-INT }
406 { MIN-INT MAX-INT M* MIN-INT SM/REM -> 0 MAX-INT }
407 { MIN-INT MAX-INT M* MAX-INT SM/REM -> 0 MIN-INT }
408 { MAX-INT MAX-INT M* MAX-INT SM/REM -> 0 MAX-INT }
410 { 0 0 1 UM/MOD -> 0 0 }
411 { 1 0 1 UM/MOD -> 0 1 }
412 { 1 0 2 UM/MOD -> 1 0 }
413 { 3 0 2 UM/MOD -> 1 1 }
414 { MAX-UINT 2 UM* 2 UM/MOD -> 0 MAX-UINT }
415 { MAX-UINT 2 UM* MAX-UINT UM/MOD -> 0 2 }
416 { MAX-UINT MAX-UINT UM* MAX-UINT UM/MOD -> 0 MAX-UINT }
419 [ -3 2 / -2 = INVERT ] LITERAL IF POSTPONE \ THEN ;
421 [ -3 2 / -1 = INVERT ] LITERAL IF POSTPONE \ THEN ;
423 \ THE SYSTEM MIGHT DO EITHER FLOORED OR SYMMETRIC DIVISION.
424 \ SINCE WE HAVE ALREADY TESTED M*, FM/MOD, AND SM/REM WE CAN USE THEM IN TEST.
425 IFFLOORED : T/MOD >R S>D R> FM/MOD ;
426 IFFLOORED : T/ T/MOD SWAP DROP ;
427 IFFLOORED : TMOD T/MOD DROP ;
428 IFFLOORED : T*/MOD >R M* R> FM/MOD ;
429 IFFLOORED : T*/ T*/MOD SWAP DROP ;
430 IFSYM : T/MOD >R S>D R> SM/REM ;
431 IFSYM : T/ T/MOD SWAP DROP ;
432 IFSYM : TMOD T/MOD DROP ;
433 IFSYM : T*/MOD >R M* R> SM/REM ;
434 IFSYM : T*/ T*/MOD SWAP DROP ;
436 { 0 1 /MOD -> 0 1 T/MOD }
437 { 1 1 /MOD -> 1 1 T/MOD }
438 { 2 1 /MOD -> 2 1 T/MOD }
439 { -1 1 /MOD -> -1 1 T/MOD }
440 { -2 1 /MOD -> -2 1 T/MOD }
441 { 0 -1 /MOD -> 0 -1 T/MOD }
442 { 1 -1 /MOD -> 1 -1 T/MOD }
443 { 2 -1 /MOD -> 2 -1 T/MOD }
444 { -1 -1 /MOD -> -1 -1 T/MOD }
445 { -2 -1 /MOD -> -2 -1 T/MOD }
446 { 2 2 /MOD -> 2 2 T/MOD }
447 { -1 -1 /MOD -> -1 -1 T/MOD }
448 { -2 -2 /MOD -> -2 -2 T/MOD }
449 { 7 3 /MOD -> 7 3 T/MOD }
450 { 7 -3 /MOD -> 7 -3 T/MOD }
451 { -7 3 /MOD -> -7 3 T/MOD }
452 { -7 -3 /MOD -> -7 -3 T/MOD }
453 { MAX-INT 1 /MOD -> MAX-INT 1 T/MOD }
454 { MIN-INT 1 /MOD -> MIN-INT 1 T/MOD }
455 { MAX-INT MAX-INT /MOD -> MAX-INT MAX-INT T/MOD }
456 { MIN-INT MIN-INT /MOD -> MIN-INT MIN-INT T/MOD }
461 { -1 1 / -> -1 1 T/ }
462 { -2 1 / -> -2 1 T/ }
463 { 0 -1 / -> 0 -1 T/ }
464 { 1 -1 / -> 1 -1 T/ }
465 { 2 -1 / -> 2 -1 T/ }
466 { -1 -1 / -> -1 -1 T/ }
467 { -2 -1 / -> -2 -1 T/ }
469 { -1 -1 / -> -1 -1 T/ }
470 { -2 -2 / -> -2 -2 T/ }
472 { 7 -3 / -> 7 -3 T/ }
473 { -7 3 / -> -7 3 T/ }
474 { -7 -3 / -> -7 -3 T/ }
475 { MAX-INT 1 / -> MAX-INT 1 T/ }
476 { MIN-INT 1 / -> MIN-INT 1 T/ }
477 { MAX-INT MAX-INT / -> MAX-INT MAX-INT T/ }
478 { MIN-INT MIN-INT / -> MIN-INT MIN-INT T/ }
480 { 0 1 MOD -> 0 1 TMOD }
481 { 1 1 MOD -> 1 1 TMOD }
482 { 2 1 MOD -> 2 1 TMOD }
483 { -1 1 MOD -> -1 1 TMOD }
484 { -2 1 MOD -> -2 1 TMOD }
485 { 0 -1 MOD -> 0 -1 TMOD }
486 { 1 -1 MOD -> 1 -1 TMOD }
487 { 2 -1 MOD -> 2 -1 TMOD }
488 { -1 -1 MOD -> -1 -1 TMOD }
489 { -2 -1 MOD -> -2 -1 TMOD }
490 { 2 2 MOD -> 2 2 TMOD }
491 { -1 -1 MOD -> -1 -1 TMOD }
492 { -2 -2 MOD -> -2 -2 TMOD }
493 { 7 3 MOD -> 7 3 TMOD }
494 { 7 -3 MOD -> 7 -3 TMOD }
495 { -7 3 MOD -> -7 3 TMOD }
496 { -7 -3 MOD -> -7 -3 TMOD }
497 { MAX-INT 1 MOD -> MAX-INT 1 TMOD }
498 { MIN-INT 1 MOD -> MIN-INT 1 TMOD }
499 { MAX-INT MAX-INT MOD -> MAX-INT MAX-INT TMOD }
500 { MIN-INT MIN-INT MOD -> MIN-INT MIN-INT TMOD }
502 { 0 2 1 */ -> 0 2 1 T*/ }
503 { 1 2 1 */ -> 1 2 1 T*/ }
504 { 2 2 1 */ -> 2 2 1 T*/ }
505 { -1 2 1 */ -> -1 2 1 T*/ }
506 { -2 2 1 */ -> -2 2 1 T*/ }
507 { 0 2 -1 */ -> 0 2 -1 T*/ }
508 { 1 2 -1 */ -> 1 2 -1 T*/ }
509 { 2 2 -1 */ -> 2 2 -1 T*/ }
510 { -1 2 -1 */ -> -1 2 -1 T*/ }
511 { -2 2 -1 */ -> -2 2 -1 T*/ }
512 { 2 2 2 */ -> 2 2 2 T*/ }
513 { -1 2 -1 */ -> -1 2 -1 T*/ }
514 { -2 2 -2 */ -> -2 2 -2 T*/ }
515 { 7 2 3 */ -> 7 2 3 T*/ }
516 { 7 2 -3 */ -> 7 2 -3 T*/ }
517 { -7 2 3 */ -> -7 2 3 T*/ }
518 { -7 2 -3 */ -> -7 2 -3 T*/ }
519 { MAX-INT 2 MAX-INT */ -> MAX-INT 2 MAX-INT T*/ }
520 { MIN-INT 2 MIN-INT */ -> MIN-INT 2 MIN-INT T*/ }
522 { 0 2 1 */MOD -> 0 2 1 T*/MOD }
523 { 1 2 1 */MOD -> 1 2 1 T*/MOD }
524 { 2 2 1 */MOD -> 2 2 1 T*/MOD }
525 { -1 2 1 */MOD -> -1 2 1 T*/MOD }
526 { -2 2 1 */MOD -> -2 2 1 T*/MOD }
527 { 0 2 -1 */MOD -> 0 2 -1 T*/MOD }
528 { 1 2 -1 */MOD -> 1 2 -1 T*/MOD }
529 { 2 2 -1 */MOD -> 2 2 -1 T*/MOD }
530 { -1 2 -1 */MOD -> -1 2 -1 T*/MOD }
531 { -2 2 -1 */MOD -> -2 2 -1 T*/MOD }
532 { 2 2 2 */MOD -> 2 2 2 T*/MOD }
533 { -1 2 -1 */MOD -> -1 2 -1 T*/MOD }
534 { -2 2 -2 */MOD -> -2 2 -2 T*/MOD }
535 { 7 2 3 */MOD -> 7 2 3 T*/MOD }
536 { 7 2 -3 */MOD -> 7 2 -3 T*/MOD }
537 { -7 2 3 */MOD -> -7 2 3 T*/MOD }
538 { -7 2 -3 */MOD -> -7 2 -3 T*/MOD }
539 { MAX-INT 2 MAX-INT */MOD -> MAX-INT 2 MAX-INT T*/MOD }
540 { MIN-INT 2 MIN-INT */MOD -> MIN-INT 2 MIN-INT T*/MOD }
542 \ ------------------------------------------------------------------------
543 TESTING HERE , @ ! CELL+ CELLS C, C@ C! CHARS 2@ 2! ALIGN ALIGNED +! ALLOT
549 { 1STA 2NDA U< -> <TRUE> } \ HERE MUST GROW WITH ALLOT
550 { 1STA 1+ -> 2NDA } \ ... BY ONE ADDRESS UNIT
551 ( MISSING TEST: NEGATIVE ALLOT )
557 { 1ST 2ND U< -> <TRUE> } \ HERE MUST GROW WITH ALLOT
558 { 1ST CELL+ -> 2ND } \ ... BY ONE CELL
559 { 1ST 1 CELLS + -> 2ND }
560 { 1ST @ 2ND @ -> 1 2 }
562 { 1ST @ 2ND @ -> 5 2 }
564 { 1ST @ 2ND @ -> 5 6 }
568 { 1S 1ST ! 1ST @ -> 1S } \ CAN STORE CELL-WIDE VALUE
574 { 1STC 2NDC U< -> <TRUE> } \ HERE MUST GROW WITH ALLOT
575 { 1STC CHAR+ -> 2NDC } \ ... BY ONE CHAR
576 { 1STC 1 CHARS + -> 2NDC }
577 { 1STC C@ 2NDC C@ -> 1 2 }
579 { 1STC C@ 2NDC C@ -> 3 2 }
581 { 1STC C@ 2NDC C@ -> 3 4 }
583 ALIGN 1 ALLOT HERE ALIGN HERE 3 CELLS ALLOT
584 CONSTANT A-ADDR CONSTANT UA-ADDR
585 { UA-ADDR ALIGNED -> A-ADDR }
586 { 1 A-ADDR C! A-ADDR C@ -> 1 }
587 { 1234 A-ADDR ! A-ADDR @ -> 1234 }
588 { 123 456 A-ADDR 2! A-ADDR 2@ -> 123 456 }
589 { 2 A-ADDR CHAR+ C! A-ADDR CHAR+ C@ -> 2 }
590 { 3 A-ADDR CELL+ C! A-ADDR CELL+ C@ -> 3 }
591 { 1234 A-ADDR CELL+ ! A-ADDR CELL+ @ -> 1234 }
592 { 123 456 A-ADDR CELL+ 2! A-ADDR CELL+ 2@ -> 123 456 }
595 0 SWAP BEGIN DUP WHILE DUP MSB AND IF >R 1+ R> THEN 2* REPEAT DROP ;
596 ( CHARACTERS >= 1 AU, <= SIZE OF CELL, >= 8 BITS )
597 { 1 CHARS 1 < -> <FALSE> }
598 { 1 CHARS 1 CELLS > -> <FALSE> }
599 ( TBD: HOW TO FIND NUMBER OF BITS? )
601 ( CELLS >= 1 AU, INTEGRAL MULTIPLE OF CHAR SIZE, >= 16 BITS )
602 { 1 CELLS 1 < -> <FALSE> }
603 { 1 CELLS 1 CHARS MOD -> 0 }
604 { 1S BITS 10 < -> <FALSE> }
609 { -1 1ST +! 1ST @ -> 0 }
611 \ ------------------------------------------------------------------------
612 TESTING CHAR [CHAR] [ ] BL S"
617 { : GC1 [CHAR] X ; -> }
618 { : GC2 [CHAR] HELLO ; -> }
621 { : GC3 [ GC1 ] LITERAL ; -> }
623 { : GC4 S" XY" ; -> }
624 { GC4 SWAP DROP -> 2 }
625 { GC4 DROP DUP C@ SWAP CHAR+ C@ -> 58 59 }
627 \ ------------------------------------------------------------------------
628 TESTING ' ['] FIND EXECUTE IMMEDIATE COUNT LITERAL POSTPONE STATE
631 { ' GT1 EXECUTE -> 123 }
632 { : GT2 ['] GT1 ; IMMEDIATE -> }
633 { GT2 EXECUTE -> 123 }
635 HERE 3 C, CHAR G C, CHAR T C, CHAR 1 C, CONSTANT GT1STRING
636 HERE 3 C, CHAR G C, CHAR T C, CHAR 2 C, CONSTANT GT2STRING
638 { GT1STRING FIND -> ' GT1 -1 }
639 { GT2STRING FIND -> ' GT2 1 }
640 ( HOW TO SEARCH FOR NON-EXISTENT WORD? )
641 { : GT3 GT2 LITERAL ; -> }
643 { GT1STRING COUNT -> GT1STRING CHAR+ 3 }
645 { : GT4 POSTPONE GT1 ; IMMEDIATE -> }
648 { : GT6 345 ; IMMEDIATE -> }
649 { : GT7 POSTPONE GT6 ; -> }
652 { : GT8 STATE @ ; IMMEDIATE -> }
654 { : GT9 GT8 LITERAL ; -> }
655 { GT9 0= -> <FALSE> }
657 \ ------------------------------------------------------------------------
658 TESTING IF ELSE THEN BEGIN WHILE REPEAT UNTIL RECURSE
660 { : GI1 IF 123 THEN ; -> }
661 { : GI2 IF 123 ELSE 234 THEN ; -> }
669 { : GI3 BEGIN DUP 5 < WHILE DUP 1+ REPEAT ; -> }
670 { 0 GI3 -> 0 1 2 3 4 5 }
675 { : GI4 BEGIN DUP 1+ DUP 5 > UNTIL ; -> }
680 { : GI5 BEGIN DUP 2 > WHILE DUP 5 < WHILE DUP 1+ REPEAT 123 ELSE 345 THEN ; -> }
683 { 3 GI5 -> 3 4 5 123 }
687 { : GI6 ( N -- 0,1,..N ) DUP IF DUP >R 1- RECURSE R> THEN ; -> }
692 { 4 GI6 -> 0 1 2 3 4 }
694 \ ------------------------------------------------------------------------
695 TESTING DO LOOP +LOOP I J UNLOOP LEAVE EXIT
697 { : GD1 DO I LOOP ; -> }
699 { 2 -1 GD1 -> -1 0 1 }
700 { MID-UINT+1 MID-UINT GD1 -> MID-UINT }
702 { : GD2 DO I -1 +LOOP ; -> }
703 { 1 4 GD2 -> 4 3 2 1 }
704 { -1 2 GD2 -> 2 1 0 -1 }
705 { MID-UINT MID-UINT+1 GD2 -> MID-UINT+1 MID-UINT }
707 { : GD3 DO 1 0 DO J LOOP LOOP ; -> }
709 { 2 -1 GD3 -> -1 0 1 }
710 { MID-UINT+1 MID-UINT GD3 -> MID-UINT }
712 { : GD4 DO 1 0 DO J LOOP -1 +LOOP ; -> }
713 { 1 4 GD4 -> 4 3 2 1 }
714 { -1 2 GD4 -> 2 1 0 -1 }
715 { MID-UINT MID-UINT+1 GD4 -> MID-UINT+1 MID-UINT }
717 { : GD5 123 SWAP 0 DO I 4 > IF DROP 234 LEAVE THEN LOOP ; -> }
722 { : GD6 ( PAT: {0 0},{0 0}{1 0}{1 1},{0 0}{1 0}{1 1}{2 0}{2 1}{2 2} )
724 I 1+ 0 DO I J + 3 = IF I UNLOOP I UNLOOP EXIT THEN 1+ LOOP
730 \ ------------------------------------------------------------------------
731 TESTING DEFINING WORDS: : ; CONSTANT VARIABLE CREATE DOES> >BODY
733 { 123 CONSTANT X123 -> }
735 { : EQU CONSTANT ; -> }
743 { : NOP : POSTPONE ; ; -> }
744 { NOP NOP1 NOP NOP2 -> }
748 { : DOES1 DOES> @ 1 + ; -> }
749 { : DOES2 DOES> @ 2 + ; -> }
752 { ' CR1 >BODY -> HERE }
760 { : WEIRD: CREATE DOES> 1 + DOES> 2 + ; -> }
762 { ' W1 >BODY -> HERE }
766 \ ------------------------------------------------------------------------
769 : GE1 S" 123" ; IMMEDIATE
770 : GE2 S" 123 1+" ; IMMEDIATE
771 : GE3 S" : GE4 345 ;" ;
772 : GE5 EVALUATE ; IMMEDIATE
774 { GE1 EVALUATE -> 123 } ( TEST EVALUATE IN INTERP. STATE )
775 { GE2 EVALUATE -> 124 }
779 { : GE6 GE1 GE5 ; -> } ( TEST EVALUATE IN COMPILE STATE )
781 { : GE7 GE2 GE5 ; -> }
784 \ ------------------------------------------------------------------------
785 TESTING SOURCE >IN WORD
787 : GS1 S" SOURCE" 2DUP EVALUATE
788 >R SWAP >R = R> R> = ;
789 { GS1 -> <TRUE> <TRUE> }
792 : RESCAN? -1 SCANS +! SCANS @ IF 0 >IN ! THEN ;
797 : GS2 5 SCANS ! S" 123 RESCAN?" EVALUATE ;
798 { GS2 -> 123 123 123 123 123 }
800 : GS3 WORD COUNT SWAP C@ ;
801 { BL GS3 HELLO -> 5 CHAR H }
802 { CHAR " GS3 GOODBYE" -> 7 CHAR G }
804 DROP -> 0 } \ BLANK LINE RETURN ZERO-LENGTH STRING
806 : GS4 SOURCE >IN ! DROP ;
810 \ ------------------------------------------------------------------------
811 TESTING <# # #S #> HOLD SIGN BASE >NUMBER HEX DECIMAL
813 : S= \ ( ADDR1 C1 ADDR2 C2 -- T/F ) COMPARE TWO STRINGS.
814 >R SWAP R@ = IF \ MAKE SURE STRINGS HAVE SAME LENGTH
815 R> ?DUP IF \ IF NON-EMPTY STRINGS
817 OVER C@ OVER C@ - IF 2DROP <FALSE> UNLOOP EXIT THEN
818 SWAP CHAR+ SWAP CHAR+
821 2DROP <TRUE> \ IF WE GET HERE, STRINGS MATCH
823 R> DROP 2DROP <FALSE> \ LENGTHS MISMATCH
826 : GP1 <# 41 HOLD 42 HOLD 0 0 #> S" BA" S= ;
829 : GP2 <# -1 SIGN 0 SIGN -1 SIGN 0 0 #> S" --" S= ;
832 : GP3 <# 1 0 # # #> S" 01" S= ;
835 : GP4 <# 1 0 #S #> S" 1" S= ;
838 24 CONSTANT MAX-BASE \ BASE 2 .. 36
840 0 0 INVERT BEGIN DUP WHILE >R 1+ R> 2* REPEAT DROP ;
841 COUNT-BITS 2* CONSTANT #BITS-UD \ NUMBER OF BITS IN UD
845 MAX-BASE 1+ 2 DO \ FOR EACH POSSIBLE BASE
846 I BASE ! \ TBD: ASSUMES BASE WORKS
847 I 0 <# #S #> S" 10" S= AND
854 MAX-UINT MAX-UINT <# #S #> \ MAXIMUM UD TO BINARY
855 R> BASE ! \ S: C-ADDR U
857 0 DO \ S: C-ADDR FLAG
858 OVER C@ [CHAR] 1 = AND \ ALL ONES
864 BASE @ >R MAX-BASE BASE !
868 1 = SWAP C@ I 30 + = AND AND
872 1 = SWAP C@ 41 I A - + = AND AND
880 : GN-STRING GN-BUF 1 ;
881 : GN-CONSUMED GN-BUF CHAR+ 0 ;
882 : GN' [CHAR] ' WORD CHAR+ C@ GN-BUF C! GN-STRING ;
884 { 0 0 GN' 0' >NUMBER -> 0 0 GN-CONSUMED }
885 { 0 0 GN' 1' >NUMBER -> 1 0 GN-CONSUMED }
886 { 1 0 GN' 1' >NUMBER -> BASE @ 1+ 0 GN-CONSUMED }
887 { 0 0 GN' -' >NUMBER -> 0 0 GN-STRING } \ SHOULD FAIL TO CONVERT THESE
888 { 0 0 GN' +' >NUMBER -> 0 0 GN-STRING }
889 { 0 0 GN' .' >NUMBER -> 0 0 GN-STRING }
892 BASE @ >R BASE ! >NUMBER R> BASE ! ;
894 { 0 0 GN' 2' 10 >NUMBER-BASED -> 2 0 GN-CONSUMED }
895 { 0 0 GN' 2' 2 >NUMBER-BASED -> 0 0 GN-STRING }
896 { 0 0 GN' F' 10 >NUMBER-BASED -> F 0 GN-CONSUMED }
897 { 0 0 GN' G' 10 >NUMBER-BASED -> 0 0 GN-STRING }
898 { 0 0 GN' G' MAX-BASE >NUMBER-BASED -> 10 0 GN-CONSUMED }
899 { 0 0 GN' Z' MAX-BASE >NUMBER-BASED -> 23 0 GN-CONSUMED }
901 : GN1 \ ( UD BASE -- UD' LEN ) UD SHOULD EQUAL UD' AND LEN SHOULD BE ZERO.
904 0 0 2SWAP >NUMBER SWAP DROP \ RETURN LENGTH ONLY
906 { 0 0 2 GN1 -> 0 0 0 }
907 { MAX-UINT 0 2 GN1 -> MAX-UINT 0 0 }
908 { MAX-UINT DUP 2 GN1 -> MAX-UINT DUP 0 }
909 { 0 0 MAX-BASE GN1 -> 0 0 0 }
910 { MAX-UINT 0 MAX-BASE GN1 -> MAX-UINT 0 0 }
911 { MAX-UINT DUP MAX-BASE GN1 -> MAX-UINT DUP 0 }
914 BASE @ >R HEX BASE @ DECIMAL BASE @ R> BASE ! ;
917 \ ------------------------------------------------------------------------
920 CREATE FBUF 00 C, 00 C, 00 C,
921 CREATE SBUF 12 C, 34 C, 56 C,
922 : SEEBUF FBUF C@ FBUF CHAR+ C@ FBUF CHAR+ CHAR+ C@ ;
924 { FBUF 0 20 FILL -> }
925 { SEEBUF -> 00 00 00 }
927 { FBUF 1 20 FILL -> }
928 { SEEBUF -> 20 00 00 }
930 { FBUF 3 20 FILL -> }
931 { SEEBUF -> 20 20 20 }
933 { FBUF FBUF 3 CHARS MOVE -> } \ BIZARRE SPECIAL CASE
934 { SEEBUF -> 20 20 20 }
936 { SBUF FBUF 0 CHARS MOVE -> }
937 { SEEBUF -> 20 20 20 }
939 { SBUF FBUF 1 CHARS MOVE -> }
940 { SEEBUF -> 12 20 20 }
942 { SBUF FBUF 3 CHARS MOVE -> }
943 { SEEBUF -> 12 34 56 }
945 { FBUF FBUF CHAR+ 2 CHARS MOVE -> }
946 { SEEBUF -> 12 12 34 }
948 { FBUF CHAR+ FBUF 2 CHARS MOVE -> }
949 { SEEBUF -> 12 34 34 }
951 \ ------------------------------------------------------------------------
952 TESTING OUTPUT: . ." CR EMIT SPACE SPACES TYPE U.
955 ." YOU SHOULD SEE THE STANDARD GRAPHIC CHARACTERS:" CR
956 41 BL DO I EMIT LOOP CR
957 61 41 DO I EMIT LOOP CR
958 7F 61 DO I EMIT LOOP CR
959 ." YOU SHOULD SEE 0-9 SEPARATED BY A SPACE:" CR
960 9 1+ 0 DO I . LOOP CR
961 ." YOU SHOULD SEE 0-9 (WITH NO SPACES):" CR
962 [CHAR] 9 1+ [CHAR] 0 DO I 0 SPACES EMIT LOOP CR
963 ." YOU SHOULD SEE A-G SEPARATED BY A SPACE:" CR
964 [CHAR] G 1+ [CHAR] A DO I EMIT SPACE LOOP CR
965 ." YOU SHOULD SEE 0-5 SEPARATED BY TWO SPACES:" CR
966 5 1+ 0 DO I [CHAR] 0 + EMIT 2 SPACES LOOP CR
967 ." YOU SHOULD SEE TWO SEPARATE LINES:" CR
968 S" LINE 1" TYPE CR S" LINE 2" TYPE CR
969 ." YOU SHOULD SEE THE NUMBER RANGES OF SIGNED AND UNSIGNED NUMBERS:" CR
970 ." SIGNED: " MIN-INT . MAX-INT . CR
971 ." UNSIGNED: " 0 U. MAX-UINT U. CR
976 \ ------------------------------------------------------------------------
977 TESTING INPUT: ACCEPT
979 CREATE ABUF 80 CHARS ALLOT
982 CR ." PLEASE TYPE UP TO 80 CHARACTERS:" CR
984 CR ." RECEIVED: " [CHAR] " EMIT
985 ABUF SWAP TYPE [CHAR] " EMIT CR
990 \ ------------------------------------------------------------------------
991 TESTING DICTIONARY SEARCH RULES
993 { : GDX 123 ; : GDX GDX 234 ; -> }