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.
35 : LITERAL IMMEDIATE ['] LIT , , ;
37 : ' BL WORD FIND >CFA ;
49 BL WORD \ get the next word
50 FIND \ find it in the dictionary
51 >CFA \ get its codeword
56 LATEST @ \ LATEST points to the word being compiled at the moment
57 >CFA \ get the codeword
61 \ CONTROL STRUCTURES ----------------------------------------------------------------------
64 ['] 0BRANCH , \ compile 0BRANCH
65 HERE \ save location of the offset on the stack
66 0 , \ compile a dummy offset
71 HERE SWAP - \ calculate the offset from the address saved on the stack
72 SWAP ! \ store the offset in the back-filled location
76 ['] BRANCH , \ definite branch to just over the false-part
77 HERE \ save location of the offset on the stack
78 0 , \ compile a dummy offset
79 SWAP \ now back-fill the original (IF) offset
80 DUP \ same as for THEN word above
86 HERE \ save location on the stack
90 ['] 0BRANCH , \ compile 0BRANCH
91 HERE - \ calculate the offset from the address saved on the stack
92 , \ compile the offset here
96 ['] BRANCH , \ compile BRANCH
97 HERE - \ calculate the offset back
98 , \ compile the offset here
102 ['] 0BRANCH , \ compile 0BRANCH
103 HERE \ save location of the offset2 on the stack
104 0 , \ compile a dummy offset2
108 ['] BRANCH , \ compile BRANCH
109 SWAP \ get the original offset (from BEGIN)
110 HERE - , \ and compile it after BRANCH
112 HERE SWAP - \ calculate the offset2
113 SWAP ! \ and back-fill it in the original location
117 ['] NOT , \ compile NOT (to reverse the test)
118 [COMPILE] IF \ continue by calling the normal IF
122 ['] LIT , -1 , [COMPILE] IF
124 ['] LIT , HERE 0 , ['] >R ,
129 ['] 2DUP , ['] - , [COMPILE] IF
131 ['] LIT , HERE 0 , ['] >R ,
141 ['] R> , ['] RDROP , ['] RDROP ,
142 ['] LIT , HERE 7 + , ['] DUP , ['] -ROT , ['] - , ['] SWAP , ['] ! ,
154 ['] DUP , \ Store copy of increment
156 ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] + , ['] 2DUP , ['] - ,
157 ['] SWAP , ['] >R , ['] SWAP , ['] >R , ['] SWAP , ['] >R ,
159 \ Condition differently depending on sign of increment
160 ['] SWAP , ['] 0>= , [COMPILE] IF
166 \ Branch back to begining of loop kernel
167 ['] 0BRANCH , HERE - ,
170 ['] RDROP , ['] RDROP , ['] RDROP ,
172 \ Record address of loop end for any LEAVEs to use
176 ['] 2DROP , \ Clean up if loop was entirely skipped (?DO)
186 \ CASE ------------------------------------------------------------------------
189 0 \ push 0 to mark the bottom of the stack
193 ['] OVER , \ compile OVER
195 [COMPILE] IF \ compile IF
196 ['] DROP , \ compile DROP
200 [COMPILE] ELSE \ ENDOF is the same as ELSE
204 ['] DROP , \ compile DROP
206 \ keep compiling THEN until we get to our zero marker
215 \ COMMENTS ----------------------------------------------------------------------
218 1 \ allowed nested parens by keeping track of depth
220 >IN @ #TIB @ >= IF \ End of TIB?
221 QUERY \ Get next line
224 TIB >IN @ + @ 1 >IN +!
225 DUP [CHAR] ( = IF \ open paren?
226 DROP \ drop the open paren
229 [CHAR] ) = IF \ close paren?
233 DUP 0= UNTIL \ continue until we reach matching close paren, depth 0
234 DROP \ drop the depth counter
237 ( Some more complicated stack examples, showing the stack notation. )
238 : NIP ( x y -- y ) SWAP DROP ;
239 : TUCK ( x y -- y x y ) DUP -ROT ;
240 : PICK ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
241 1+ ( add one because of 'u' on the stack )
242 PSP@ SWAP - ( add to the stack pointer )
245 : ROLL ( x_u x_u-1... x_0 u -- x_u-1 ... x_0 x_u )
246 1+ DUP PICK SWAP ( x_u x_u-1 ... x_0 x_u u+1 )
247 PSP@ 1- SWAP - PSP@ 2- SWAP
254 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
263 ( Standard words for manipulating BASE. )
264 : DECIMAL ( -- ) 10 BASE ! ;
265 : HEX ( -- ) 16 BASE ! ;
267 ( Compute absolute value. )
290 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
292 ( This is the underlying recursive definition of U. )
294 BASE @ /MOD ( width rem quot )
295 ?DUP IF ( if quotient <> 0 then )
296 RECURSE ( print the quotient )
299 ( print the remainder )
301 [CHAR] 0 ( decimal digits 0..9 )
303 10 - ( hex and beyond digits A..Z )
310 ( This word returns the width (in characters) of an unsigned number in the current base )
311 : UWIDTH ( u -- width )
312 BASE @ / ( rem quot )
313 ?DUP IF ( if quotient <> 0 then )
314 RECURSE 1+ ( return 1+recursive call )
323 UWIDTH ( width u uwidth )
324 ROT ( u uwidth width )
325 SWAP - ( u width-uwidth )
326 ( At this point if the requested width is narrower, we'll have a negative number on the stack.
327 Otherwise the number on the stack is the number of spaces to print. But SPACES won't print
328 a negative number of spaces anyway, so it's now safe to call SPACES ... )
330 ( ... and then call the underlying implementation of U. )
338 1 ( save a flag to remember that it was negative | width n 1 )
347 SWAP ( flag width u )
348 DUP ( flag width u u )
349 UWIDTH ( flag width u uwidth )
350 ROT ( flag u uwidth width )
351 SWAP - ( flag u width-uwidth )
356 IF ( was it negative? print the - character )
366 [CHAR] < EMIT DEPTH U. [CHAR] > EMIT SPACE
379 ( ? fetches the integer at an address and prints it. )
380 : ? ( addr -- ) @ . ;
382 ( c a b WITHIN returns true if a <= c and c < b )
399 ( STRINGS ---------------------------------------------------------------------- )
402 ( Block copy, however, is important and novel: )
403 : CMOVE ( src dest length -- )
409 -ROT OVER - ( length src (dest-src) )
410 -ROT DUP ROT + SWAP ( (dest-src) (src+length) src )
413 I @ ( (dest-src) i@ )
414 OVER I + ( (dest-src) i@ (dest-src+i) )
421 ( C, appends a byte to the current compiled word. )
427 : S" IMMEDIATE ( -- addr len )
428 STATE @ IF ( compiling? )
429 ['] LITSTRING , ( compile LITSTRING )
430 HERE ( save the address of the length word on the stack )
431 0 , ( dummy length - we don't know what it is yet )
434 >IN @ #TIB @ >= IF \ End of TIB?
435 QUERY \ Get next line
438 TIB >IN @ + @ 1 >IN +! \ Get char from TIB
442 C, ( copy character )
444 DROP ( drop the double quote character at the end )
445 DUP ( get the saved address of the length word )
446 HERE SWAP - ( calculate the length )
447 1- ( subtract 1 (because we measured from the start of the length word) )
448 SWAP ! ( and back-fill the length location )
449 ELSE ( immediate mode )
450 HERE ( get the start address of the temporary space )
453 >IN @ #TIB @ >= IF \ End of TIB?
454 QUERY \ Get next line
457 TIB >IN @ + @ 1 >IN +! \ Get char from TIB
461 OVER C! ( save next character )
462 1+ ( increment address )
464 DROP ( drop the final " character )
465 HERE - ( calculate the length )
466 HERE ( push the start address )
471 : ." IMMEDIATE ( -- )
472 [COMPILE] S" ( read the string, and compile LITSTRING, etc. )
473 ['] TYPE , ( compile the final TYPE )
478 >IN @ #TIB @ >= IF \ End of TIB?
479 QUERY \ Get next line
482 TIB >IN @ + @ 1 >IN +! \ Get char from TIB
485 DROP ( drop the double quote character )
486 EXIT ( return from this function )
492 ( Converts address of counted string into address of
493 start of string and length of string. )
494 : COUNT ( addr1 -- addr2 n )
498 ( CONSTANTS AND VARIABLES ------------------------------------------------------ )
501 BL WORD HEADER ( make dictionary entry (the name follows CONSTANT) )
502 DOCOL , ( append DOCOL (the codeword field of this word) )
503 ['] LIT , ( append the codeword LIT )
504 , ( append the value on the top of the stack )
505 ['] EXIT , ( append the codeword EXIT )
509 H +! ( adds n to H, after this the old value of H is still on the stack )
515 1 CELLS ALLOT ( allocate 1 cell of memory, push the pointer to this memory )
519 BL WORD HEADER ( make the dictionary entry (the name follows VALUE) )
520 DOCOL , ( append DOCOL )
521 ['] LIT , ( append the codeword LIT )
522 , ( append the initial value )
523 ['] EXIT , ( append the codeword EXIT )
526 : TO IMMEDIATE ( n -- )
527 BL WORD ( get the name of the value )
528 FIND ( look it up in the dictionary )
529 >PFA ( get a pointer to the first data field (the 'LIT') )
530 1+ ( increment to point at the value )
531 STATE @ IF ( compiling? )
532 ['] LIT , ( compile LIT )
533 , ( compile the address of the value )
534 ['] ! , ( compile ! )
535 ELSE ( immediate mode )
536 ! ( update it straightaway )
540 ( x +TO VAL adds x to VAL )
542 BL WORD ( get the name of the value )
543 FIND ( look it up in the dictionary )
544 >PFA ( get a pointer to the first data field (the 'LIT') )
545 1+ ( increment to point at the value )
546 STATE @ IF ( compiling? )
547 ['] LIT , ( compile LIT )
548 , ( compile the address of the value )
549 ['] +! , ( compile +! )
550 ELSE ( immediate mode )
551 +! ( update it straightaway )
555 ( Fill u ints, starting at a, with the value b )
567 ( PRINTING THE DICTIONARY ------------------------------------------------------ )
570 1+ ( skip over the link pointer )
571 DUP @ ( get the flags/length byte )
572 F_LENMASK AND ( mask out the flags - just want the length )
575 DUP 0> ( length > 0? )
577 SWAP 1+ ( addr len -- len addr+1 )
578 DUP @ ( len addr -- len addr char | get the next character)
579 DUP 32 >= OVER 127 <= AND IF
580 EMIT ( len addr char -- len addr | and print it)
586 SWAP 1- ( len addr -- addr len-1 | subtract one from length )
588 2DROP ( len addr -- )
592 1+ ( skip over the link pointer )
593 @ ( get the flags/length byte )
594 F_HIDDEN AND ( mask the F_HIDDEN flag and return it (as a truth value) )
597 1+ ( skip over the link pointer )
598 @ ( get the flags/length byte )
599 F_IMMED AND ( mask the F_IMMED flag and return it (as a truth value) )
603 LATEST @ ( start at LATEST dictionary entry )
605 ?DUP ( while link pointer is not null )
607 DUP ?HIDDEN NOT IF ( ignore hidden words )
608 DUP ID. ( but if not hidden, print the word )
611 @ ( dereference the link pointer - go to previous word )
617 ( FORGET ---------------------------------------------------------------------- )
620 BL WORD FIND ( find the word, gets the dictionary entry address )
621 DUP @ LATEST ! ( set LATEST to point to the previous word )
622 H ! ( and store H with the dictionary address )
625 ( DUMP ------------------------------------------------------------------------ )
630 ( DECOMPILER ------------------------------------------------------------------ )
633 LATEST @ ( start at LATEST dictionary entry )
635 ?DUP ( while link pointer is not null )
637 2DUP SWAP ( cfa curr curr cfa )
638 < IF ( current dictionary entry < cfa? )
639 NIP ( leave curr dictionary entry on the stack )
642 @ ( follow link pointer back )
644 DROP ( restore stack )
645 0 ( sorry, nothing found )
649 BL WORD DUP FIND ( find the dictionary entry to decompile )
652 ." Word '" COUNT TYPE ." ' not found in dictionary."
658 ( Now we search again, looking for the next word in the dictionary. This gives us
659 the length of the word that we will be decompiling. (Well, mostly it does). )
660 HERE ( address of the end of the last compiled word )
661 LATEST @ ( word last curr )
663 2 PICK ( word last curr word )
664 OVER ( word last curr word curr )
665 <> ( word last curr word<>curr? )
666 WHILE ( word last curr )
668 DUP @ ( word curr prev (which becomes: word last curr) )
671 DROP ( at this point, the stack is: start-of-word end-of-word )
672 SWAP ( end-of-word start-of-word )
677 [CHAR] : EMIT SPACE DUP ID. SPACE
678 DUP ?IMMEDIATE IF ." IMMEDIATE " THEN CR
681 \ Variable definition
682 ." Variable " DUP ID. CR
686 \ Constant definition
687 ." Constant " DUP ID. CR
692 ." Primitive or word with unrecognized codeword." CR
696 ( begin the definition with : NAME [IMMEDIATE] )
697 ( [CHAR] : EMIT SPACE DUP ID. SPACE
698 DUP ?IMMEDIATE IF ." IMMEDIATE " THEN CR 4 )
702 >PFA ( get the data address, ie. points after DOCOL | end-of-word start-of-data )
704 ( now we start decompiling until we hit the end of the word )
708 DUP @ ( end start codeword )
711 ['] LIT OF ( is it LIT ? )
712 1+ DUP @ ( get next word which is the integer constant )
715 ['] LITSTRING OF ( is it LITSTRING ? )
716 [CHAR] S EMIT [CHAR] " EMIT SPACE ( print S"<space> )
717 1+ DUP @ ( get the length word )
718 SWAP 1+ SWAP ( end start+1 length )
719 2DUP TYPE ( print the string )
720 [CHAR] " EMIT SPACE ( finish the string with a final quote )
721 + ( end start+1+len, aligned )
722 1- ( because we're about to add 4 below )
724 ['] 0BRANCH OF ( is it 0BRANCH ? )
726 1+ DUP @ ( print the offset )
730 ['] BRANCH OF ( is it BRANCH ? )
732 1+ DUP @ ( print the offset )
736 ['] ['] OF ( is it ['] ? )
738 1+ DUP @ ( get the next codeword )
739 CFA> ( and force it to be printed as a dictionary entry )
742 ['] EXIT OF ( is it EXIT? )
743 ( We expect the last word to be EXIT, and if it is then we don't print it
744 because EXIT is normally implied by ;. EXIT can also appear in the middle
745 of words, and then it needs to be printed. )
746 2DUP ( end start end start )
747 1+ ( end start end start+1 )
748 <> IF ( end start | we're not at the end )
753 DUP ( in the default case we always need to DUP before using )
754 CFA> ( look up the codeword to get the dictionary entry )
755 ID. SPACE ( and print it )
763 2DROP ( restore stack )
767 ( MEMORY ------------------------------------------------------------------ )
769 : UNUSED ( -- cells )