3 ; \ We can now comment!
5 \ BASIC DEFINITIONS ----------------------------------------------------------------------
17 \ Translate a number of cells into memory units
18 \ (in our case 1 cell = 1 memory unit)
21 \ Since the smallest unit of memory in our system is 64 bits and since strings
22 \ are stored as arrays of 64 bit integers, the character store/fetch words are
23 \ just aliases of the standard store/fetch words.
33 : LITERAL IMMEDIATE ['] LIT , , ;
35 : ' BL WORD FIND DROP ;
47 BL WORD \ get the next word
48 FIND DROP \ find it in the dictionary
53 LATEST @ \ LATEST points to the word being compiled at the moment
54 >CFA \ get the codeword
58 \ CONTROL STRUCTURES ----------------------------------------------------------------------
61 ['] 0BRANCH , \ compile 0BRANCH
62 HERE \ save location of the offset on the stack
63 0 , \ compile a dummy offset
68 HERE SWAP - \ calculate the offset from the address saved on the stack
69 SWAP ! \ store the offset in the back-filled location
73 ['] BRANCH , \ definite branch to just over the false-part
74 HERE \ save location of the offset on the stack
75 0 , \ compile a dummy offset
76 SWAP \ now back-fill the original (IF) offset
77 DUP \ same as for THEN word above
83 HERE \ save location on the stack
87 ['] 0BRANCH , \ compile 0BRANCH
88 HERE - \ calculate the offset from the address saved on the stack
89 , \ compile the offset here
93 ['] BRANCH , \ compile BRANCH
94 HERE - \ calculate the offset back
95 , \ compile the offset here
99 ['] 0BRANCH , \ compile 0BRANCH
100 HERE \ save location of the offset2 on the stack
101 0 , \ compile a dummy offset2
105 ['] BRANCH , \ compile BRANCH
106 SWAP \ get the original offset (from BEGIN)
107 HERE - , \ and compile it after BRANCH
109 HERE SWAP - \ calculate the offset2
110 SWAP ! \ and back-fill it in the original location
114 ['] NOT , \ compile NOT (to reverse the test)
115 [COMPILE] IF \ continue by calling the normal IF
119 ['] LIT , -1 , [COMPILE] IF
121 ['] LIT , HERE 0 , ['] >R ,
126 ['] 2DUP , ['] - , [COMPILE] IF
128 ['] LIT , HERE 0 , ['] >R ,
138 ['] R> , ['] RDROP , ['] RDROP ,
139 ['] LIT , HERE 7 + , ['] DUP , ['] -ROT , ['] - , ['] SWAP , ['] ! ,
151 ['] DUP , \ Store copy of increment
153 ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] + , ['] 2DUP , ['] - ,
154 ['] SWAP , ['] >R , ['] SWAP , ['] >R , ['] SWAP , ['] >R ,
156 \ Condition differently depending on sign of increment
157 ['] SWAP , ['] 0>= , [COMPILE] IF
163 \ Branch back to begining of loop kernel
164 ['] 0BRANCH , HERE - ,
167 ['] RDROP , ['] RDROP , ['] RDROP ,
169 \ Record address of loop end for any LEAVEs to use
173 ['] 2DROP , \ Clean up if loop was entirely skipped (?DO)
183 \ CASE ------------------------------------------------------------------------
186 0 \ push 0 to mark the bottom of the stack
190 ['] OVER , \ compile OVER
192 [COMPILE] IF \ compile IF
193 ['] DROP , \ compile DROP
197 [COMPILE] ELSE \ ENDOF is the same as ELSE
201 ['] DROP , \ compile DROP
203 \ keep compiling THEN until we get to our zero marker
212 \ COMMENTS ----------------------------------------------------------------------
215 1 \ allowed nested parens by keeping track of depth
217 >IN @ #TIB @ >= IF \ End of TIB?
218 QUERY \ Get next line
221 TIB >IN @ + @ 1 >IN +!
222 DUP [CHAR] ( = IF \ open paren?
223 DROP \ drop the open paren
226 [CHAR] ) = IF \ close paren?
230 DUP 0= UNTIL \ continue until we reach matching close paren, depth 0
231 DROP \ drop the depth counter
234 ( Some more complicated stack examples, showing the stack notation. )
235 : NIP ( x y -- y ) SWAP DROP ;
236 : TUCK ( x y -- y x y ) DUP -ROT ;
237 : PICK ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
238 1+ ( add one because of 'u' on the stack )
239 PSP@ SWAP - ( add to the stack pointer )
242 : ROLL ( x_u x_u-1... x_0 u -- x_u-1 ... x_0 x_u )
243 1+ DUP PICK SWAP ( x_u x_u-1 ... x_0 x_u u+1 )
244 PSP@ 1- SWAP - PSP@ 2- SWAP
251 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
260 ( Standard words for manipulating BASE. )
261 : DECIMAL ( -- ) 10 BASE ! ;
262 : HEX ( -- ) 16 BASE ! ;
264 ( Compute absolute value. )
287 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
289 ( This is the underlying recursive definition of U. )
291 BASE @ /MOD ( width rem quot )
292 ?DUP IF ( if quotient <> 0 then )
293 RECURSE ( print the quotient )
296 ( print the remainder )
298 [CHAR] 0 ( decimal digits 0..9 )
300 10 - ( hex and beyond digits A..Z )
307 ( This word returns the width (in characters) of an unsigned number in the current base )
308 : UWIDTH ( u -- width )
309 BASE @ / ( rem quot )
310 ?DUP IF ( if quotient <> 0 then )
311 RECURSE 1+ ( return 1+recursive call )
320 UWIDTH ( width u uwidth )
321 ROT ( u uwidth width )
322 SWAP - ( u width-uwidth )
323 ( At this point if the requested width is narrower, we'll have a negative number on the stack.
324 Otherwise the number on the stack is the number of spaces to print. But SPACES won't print
325 a negative number of spaces anyway, so it's now safe to call SPACES ... )
327 ( ... and then call the underlying implementation of U. )
335 1 ( save a flag to remember that it was negative | width n 1 )
344 SWAP ( flag width u )
345 DUP ( flag width u u )
346 UWIDTH ( flag width u uwidth )
347 ROT ( flag u uwidth width )
348 SWAP - ( flag u width-uwidth )
353 IF ( was it negative? print the - character )
363 [CHAR] < EMIT DEPTH U. [CHAR] > EMIT SPACE
376 ( ? fetches the integer at an address and prints it. )
377 : ? ( addr -- ) @ . ;
379 ( c a b WITHIN returns true if a <= c and c < b )
396 ( STRINGS ---------------------------------------------------------------------- )
399 ( Block copy, however, is important and novel: )
400 : CMOVE ( src dest length -- )
406 -ROT OVER - ( length src (dest-src) )
407 -ROT DUP ROT + SWAP ( (dest-src) (src+length) src )
410 I @ ( (dest-src) i@ )
411 OVER I + ( (dest-src) i@ (dest-src+i) )
418 ( C, appends a byte to the current compiled word. )
424 : S" IMMEDIATE ( -- addr len )
425 STATE @ IF ( compiling? )
426 ['] LITSTRING , ( compile LITSTRING )
427 HERE ( save the address of the length word on the stack )
428 0 , ( dummy length - we don't know what it is yet )
431 >IN @ #TIB @ >= IF \ End of TIB?
432 QUERY \ Get next line
435 TIB >IN @ + @ 1 >IN +! \ Get char from TIB
439 C, ( copy character )
441 DROP ( drop the double quote character at the end )
442 DUP ( get the saved address of the length word )
443 HERE SWAP - ( calculate the length )
444 1- ( subtract 1 (because we measured from the start of the length word) )
445 SWAP ! ( and back-fill the length location )
446 ELSE ( immediate mode )
447 HERE ( get the start address of the temporary space )
450 >IN @ #TIB @ >= IF \ End of TIB?
451 QUERY \ Get next line
454 TIB >IN @ + @ 1 >IN +! \ Get char from TIB
458 OVER C! ( save next character )
459 1+ ( increment address )
461 DROP ( drop the final " character )
462 HERE - ( calculate the length )
463 HERE ( push the start address )
468 : ." IMMEDIATE ( -- )
469 [COMPILE] S" ( read the string, and compile LITSTRING, etc. )
470 ['] TYPE , ( compile the final TYPE )
475 >IN @ #TIB @ >= IF \ End of TIB?
476 QUERY \ Get next line
479 TIB >IN @ + @ 1 >IN +! \ Get char from TIB
482 DROP ( drop the double quote character )
483 EXIT ( return from this function )
489 ( Converts address of counted string into address of
490 start of string and length of string. )
491 : COUNT ( addr1 -- addr2 n )
495 ( CONSTANTS AND VARIABLES ------------------------------------------------------ )
503 H +! ( adds n to H, after this the old value of H is still on the stack )
508 1 CELLS ALLOT ( allocate 1 cell of memory, push the pointer to this memory )
516 : TO IMMEDIATE ( n -- )
517 BL WORD ( get the name of the value )
518 FIND DROP ( look it up in the dictionary )
519 >BODY ( get a pointer to the first data field (the 'LIT') )
520 STATE @ IF ( compiling? )
521 ['] LIT , ( compile LIT )
522 , ( compile the address of the value )
523 ['] ! , ( compile ! )
524 ELSE ( immediate mode )
525 ! ( update it straightaway )
529 ( x +TO VAL adds x to VAL )
531 BL WORD ( get the name of the value )
532 FIND DROP ( look it up in the dictionary )
533 >BODY ( get a pointer to the first data field (the 'LIT') )
534 STATE @ IF ( compiling? )
535 ['] LIT , ( compile LIT )
536 , ( compile the address of the value )
537 ['] +! , ( compile +! )
538 ELSE ( immediate mode )
539 +! ( update it straightaway )
543 ( Fill u ints, starting at a, with the value b )
555 ( PRINTING THE DICTIONARY ------------------------------------------------------ )
558 DUP @ ( get the flags/length byte )
559 F_LENMASK AND ( mask out the flags - just want the length )
562 DUP 0> ( length > 0? )
564 SWAP 1+ ( addr len -- len addr+1 )
565 DUP @ ( len addr -- len addr char | get the next character)
566 DUP 32 >= OVER 127 <= AND IF
567 EMIT ( len addr char -- len addr | and print it)
573 SWAP 1- ( len addr -- addr len-1 | subtract one from length )
575 2DROP ( len addr -- )
579 1+ ( skip over the link pointer )
580 @ ( get the flags/length byte )
581 F_HIDDEN AND ( mask the F_HIDDEN flag and return it (as a truth value) )
584 1+ ( skip over the link pointer )
585 @ ( get the flags/length byte )
586 F_IMMED AND ( mask the F_IMMED flag and return it (as a truth value) )
590 LATEST @ ( start at LATEST dictionary entry )
592 ?DUP ( while link pointer is not null )
594 DUP ?HIDDEN NOT IF ( ignore hidden words )
595 DUP 1+ .NAME ( but if not hidden, print the word )
598 @ ( dereference the link pointer - go to previous word )
603 ( DUMP ------------------------------------------------------------------------ )
608 ( DECOMPILER ------------------------------------------------------------------ )
620 BL WORD FIND ( find the dictionary entry to decompile )
623 ." Word '" COUNT TYPE ." ' not found in dictionary."
629 ( Now we search again, looking for the next word in the dictionary. This gives us
630 the length of the word that we will be decompiling. (Well, mostly it does). )
631 HERE ( address of the end of the last compiled word )
632 LATEST @ ( word last curr )
634 2 PICK ( word last curr word )
635 OVER ( word last curr word curr )
636 <> ( word last curr word<>curr? )
637 WHILE ( word last curr )
639 DUP @ ( word curr prev (which becomes: word last curr) )
642 DROP ( at this point, the stack is: start-of-word end-of-word )
643 SWAP ( end-of-word start-of-word )
648 [CHAR] : EMIT SPACE DUP 1+ .NAME SPACE
649 DUP ?IMMEDIATE IF ." IMMEDIATE " THEN CR
652 \ Variable definition
653 ." Variable " DUP 1+ .NAME CR
657 \ Constant definition
658 ." Constant " DUP 1+ .NAME CR
663 ." Primitive or word with unrecognized codeword." CR
669 >CFA >BODY ( get the data address, ie. points after DOCOL | end-of-word start-of-data )
671 ( now we start decompiling until we hit the end of the word )
675 DUP @ ( end start codeword )
678 ['] LIT OF ( is it LIT ? )
679 1+ DUP @ ( get next word which is the integer constant )
682 ['] LITSTRING OF ( is it LITSTRING ? )
683 [CHAR] S EMIT [CHAR] " EMIT SPACE ( print S"<space> )
684 1+ DUP @ ( get the length word )
685 SWAP 1+ SWAP ( end start+1 length )
686 2DUP TYPE ( print the string )
687 [CHAR] " EMIT SPACE ( finish the string with a final quote )
688 + ( end start+1+len, aligned )
689 1- ( because we're about to add 4 below )
691 ['] 0BRANCH OF ( is it 0BRANCH ? )
693 1+ DUP @ ( print the offset )
697 ['] BRANCH OF ( is it BRANCH ? )
699 1+ DUP @ ( print the offset )
703 ['] ['] OF ( is it ['] ? )
705 1+ DUP @ ( get the next codeword )
706 >NAME ( and force it to be printed as a dictionary entry )
709 ['] EXIT OF ( is it EXIT? )
710 ( We expect the last word to be EXIT, and if it is then we don't print it
711 because EXIT is normally implied by ;. EXIT can also appear in the middle
712 of words, and then it needs to be printed. )
713 2DUP ( end start end start )
714 1+ ( end start end start+1 )
715 <> IF ( end start | we're not at the end )
720 DUP ( in the default case we always need to DUP before using )
721 >NAME ( look up the codeword to get the dictionary entry )
722 .NAME SPACE ( and print it )
730 2DROP ( restore stack )
734 ( FORGET and HIDE ------------------------------------------------------------ )
737 BL WORD FIND >LFA ( find the word, gets the dictionary entry address )
738 DUP @ LATEST ! ( set LATEST to point to the previous word )
739 H ! ( and store H with the dictionary address )
743 BL WORD FIND DROP >NAME
744 DUP F_HIDDEN OR SWAP !
747 ( MEMORY ------------------------------------------------------------------ )
749 : UNUSED ( -- cells )