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.
28 : DEPTH PSP@ PSP0 @ - ;
33 : LITERAL IMMEDIATE ['] LIT , , ;
35 : ' BL WORD FIND >CFA ;
47 BL WORD \ get the next word
48 FIND \ find it in the dictionary
49 >CFA \ get its codeword
54 LATEST @ \ LATEST points to the word being compiled at the moment
55 >CFA \ get the codeword
59 \ CONTROL STRUCTURES ----------------------------------------------------------------------
62 ['] 0BRANCH , \ compile 0BRANCH
63 HERE @ \ save location of the offset on the stack
64 0 , \ compile a dummy offset
69 HERE @ SWAP - \ calculate the offset from the address saved on the stack
70 SWAP ! \ store the offset in the back-filled location
74 ['] BRANCH , \ definite branch to just over the false-part
75 HERE @ \ save location of the offset on the stack
76 0 , \ compile a dummy offset
77 SWAP \ now back-fill the original (IF) offset
78 DUP \ same as for THEN word above
84 HERE @ \ save location on the stack
88 ['] 0BRANCH , \ compile 0BRANCH
89 HERE @ - \ calculate the offset from the address saved on the stack
90 , \ compile the offset here
94 ['] BRANCH , \ compile BRANCH
95 HERE @ - \ calculate the offset back
96 , \ compile the offset here
100 ['] 0BRANCH , \ compile 0BRANCH
101 HERE @ \ save location of the offset2 on the stack
102 0 , \ compile a dummy offset2
106 ['] BRANCH , \ compile BRANCH
107 SWAP \ get the original offset (from BEGIN)
108 HERE @ - , \ and compile it after BRANCH
110 HERE @ SWAP - \ calculate the offset2
111 SWAP ! \ and back-fill it in the original location
115 ['] NOT , \ compile NOT (to reverse the test)
116 [COMPILE] IF \ continue by calling the normal IF
120 ['] LIT , -1 , [COMPILE] IF
122 ['] LIT , HERE @ 0 , ['] >R ,
127 ['] 2DUP , ['] - , [COMPILE] IF
129 ['] LIT , HERE @ 0 , ['] >R ,
139 ['] R> , ['] RDROP , ['] RDROP ,
140 ['] LIT , HERE @ 7 + , ['] DUP , ['] -ROT , ['] - , ['] SWAP , ['] ! ,
152 ['] DUP , \ Store copy of increment
154 ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] + , ['] 2DUP , ['] - ,
155 ['] SWAP , ['] >R , ['] SWAP , ['] >R , ['] SWAP , ['] >R ,
157 \ Condition differently depending on sign of increment
158 ['] SWAP , ['] 0>= , [COMPILE] IF
164 \ Branch back to begining of loop kernel
165 ['] 0BRANCH , HERE @ - ,
168 ['] RDROP , ['] RDROP , ['] RDROP ,
170 \ Record address of loop end for any LEAVEs to use
174 ['] 2DROP , \ Clean up if loop was entirely skipped (?DO)
184 \ CASE ------------------------------------------------------------------------
187 0 \ push 0 to mark the bottom of the stack
191 ['] OVER , \ compile OVER
193 [COMPILE] IF \ compile IF
194 ['] DROP , \ compile DROP
198 [COMPILE] ELSE \ ENDOF is the same as ELSE
202 ['] DROP , \ compile DROP
204 \ keep compiling THEN until we get to our zero marker
213 \ COMMENTS ----------------------------------------------------------------------
216 1 \ allowed nested parens by keeping track of depth
218 >IN @ #TIB @ >= IF \ End of TIB?
219 QUERY \ Get next line
222 TIB >IN @ + @ 1 >IN +!
223 DUP [CHAR] ( = IF \ open paren?
224 DROP \ drop the open paren
227 [CHAR] ) = IF \ close paren?
231 DUP 0= UNTIL \ continue until we reach matching close paren, depth 0
232 DROP \ drop the depth counter
235 ( Some more complicated stack examples, showing the stack notation. )
236 : NIP ( x y -- y ) SWAP DROP ;
237 : TUCK ( x y -- y x y ) DUP -ROT ;
238 : PICK ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
239 1+ ( add one because of 'u' on the stack )
240 PSP@ SWAP - ( add to the stack pointer )
243 : ROLL ( x_u x_u-1... x_0 u -- x_u-1 ... x_0 x_u )
244 1+ DUP PICK SWAP ( x_u x_u-1 ... x_0 x_u u+1 )
245 PSP@ 1- SWAP - PSP@ 2- SWAP
252 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
261 ( Standard words for manipulating BASE. )
262 : DECIMAL ( -- ) 10 BASE ! ;
263 : HEX ( -- ) 16 BASE ! ;
265 ( Compute absolute value. )
288 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
290 ( This is the underlying recursive definition of U. )
292 BASE @ /MOD ( width rem quot )
293 ?DUP IF ( if quotient <> 0 then )
294 RECURSE ( print the quotient )
297 ( print the remainder )
299 [CHAR] 0 ( decimal digits 0..9 )
301 10 - ( hex and beyond digits A..Z )
308 ( This word returns the width (in characters) of an unsigned number in the current base )
309 : UWIDTH ( u -- width )
310 BASE @ / ( rem quot )
311 ?DUP IF ( if quotient <> 0 then )
312 RECURSE 1+ ( return 1+recursive call )
321 UWIDTH ( width u uwidth )
322 ROT ( u uwidth width )
323 SWAP - ( u width-uwidth )
324 ( At this point if the requested width is narrower, we'll have a negative number on the stack.
325 Otherwise the number on the stack is the number of spaces to print. But SPACES won't print
326 a negative number of spaces anyway, so it's now safe to call SPACES ... )
328 ( ... and then call the underlying implementation of U. )
336 1 ( save a flag to remember that it was negative | width n 1 )
345 SWAP ( flag width u )
346 DUP ( flag width u u )
347 UWIDTH ( flag width u uwidth )
348 ROT ( flag u uwidth width )
349 SWAP - ( flag u width-uwidth )
354 IF ( was it negative? print the - character )
364 [CHAR] < EMIT DEPTH U. [CHAR] > EMIT SPACE
377 ( ? fetches the integer at an address and prints it. )
378 : ? ( addr -- ) @ . ;
380 ( c a b WITHIN returns true if a <= c and c < b )
397 ( STRINGS ---------------------------------------------------------------------- )
400 ( Block copy, however, is important and novel: )
401 : CMOVE ( src dest length -- )
407 -ROT OVER - ( length src (dest-src) )
408 -ROT DUP ROT + SWAP ( (dest-src) (src+length) src )
411 I @ ( (dest-src) i@ )
412 OVER I + ( (dest-src) i@ (dest-src+i) )
419 ( C, appends a byte to the current compiled word. )
425 : S" IMMEDIATE ( -- addr len )
426 STATE @ IF ( compiling? )
427 ['] LITSTRING , ( compile LITSTRING )
428 HERE @ ( save the address of the length word on the stack )
429 0 , ( dummy length - we don't know what it is yet )
432 >IN @ #TIB @ >= IF \ End of TIB?
433 QUERY \ Get next line
436 TIB >IN @ + @ 1 >IN +! \ Get char from TIB
440 C, ( copy character )
442 DROP ( drop the double quote character at the end )
443 DUP ( get the saved address of the length word )
444 HERE @ SWAP - ( calculate the length )
445 1- ( subtract 1 (because we measured from the start of the length word) )
446 SWAP ! ( and back-fill the length location )
447 ELSE ( immediate mode )
448 HERE @ ( get the start address of the temporary space )
451 >IN @ #TIB @ >= IF \ End of TIB?
452 QUERY \ Get next line
455 TIB >IN @ + @ 1 >IN +! \ Get char from TIB
459 OVER C! ( save next character )
460 1+ ( increment address )
462 DROP ( drop the final " character )
463 HERE @ - ( calculate the length )
464 HERE @ ( push the start address )
469 : ." IMMEDIATE ( -- )
470 [COMPILE] S" ( read the string, and compile LITSTRING, etc. )
471 ['] TYPE , ( compile the final TYPE )
476 >IN @ #TIB @ >= IF \ End of TIB?
477 QUERY \ Get next line
480 TIB >IN @ + @ 1 >IN +! \ Get char from TIB
483 DROP ( drop the double quote character )
484 EXIT ( return from this function )
490 ( Converts address of counted string into address of
491 start of string and length of string. )
492 : COUNT ( addr1 -- addr2 n )
496 ( CONSTANTS AND VARIABLES ------------------------------------------------------ )
499 BL WORD HEADER ( make dictionary entry (the name follows CONSTANT) )
500 DOCOL , ( append DOCOL (the codeword field of this word) )
501 ['] LIT , ( append the codeword LIT )
502 , ( append the value on the top of the stack )
503 ['] EXIT , ( append the codeword EXIT )
507 HERE +! ( adds n to HERE, after this the old value of HERE is still on the stack )
513 1 CELLS ALLOT ( allocate 1 cell of memory, push the pointer to this memory )
517 BL WORD HEADER ( make the dictionary entry (the name follows VALUE) )
518 DOCOL , ( append DOCOL )
519 ['] LIT , ( append the codeword LIT )
520 , ( append the initial value )
521 ['] EXIT , ( append the codeword EXIT )
524 : TO IMMEDIATE ( n -- )
525 BL WORD ( get the name of the value )
526 FIND ( look it up in the dictionary )
527 >PFA ( get a pointer to the first data field (the 'LIT') )
528 1+ ( increment to point at the value )
529 STATE @ IF ( compiling? )
530 ['] LIT , ( compile LIT )
531 , ( compile the address of the value )
532 ['] ! , ( compile ! )
533 ELSE ( immediate mode )
534 ! ( update it straightaway )
538 ( x +TO VAL adds x to VAL )
540 BL WORD ( get the name of the value )
541 FIND ( look it up in the dictionary )
542 >PFA ( get a pointer to the first data field (the 'LIT') )
543 1+ ( increment to point at the value )
544 STATE @ IF ( compiling? )
545 ['] LIT , ( compile LIT )
546 , ( compile the address of the value )
547 ['] +! , ( compile +! )
548 ELSE ( immediate mode )
549 +! ( update it straightaway )
553 ( Fill u ints, starting at a, with the value b )
565 ( PRINTING THE DICTIONARY ------------------------------------------------------ )
568 1+ ( skip over the link pointer )
569 DUP @ ( get the flags/length byte )
570 F_LENMASK AND ( mask out the flags - just want the length )
573 DUP 0> ( length > 0? )
575 SWAP 1+ ( addr len -- len addr+1 )
576 DUP @ ( len addr -- len addr char | get the next character)
577 DUP 32 >= OVER 127 <= AND IF
578 EMIT ( len addr char -- len addr | and print it)
584 SWAP 1- ( len addr -- addr len-1 | subtract one from length )
586 2DROP ( len addr -- )
590 1+ ( skip over the link pointer )
591 @ ( get the flags/length byte )
592 F_HIDDEN AND ( mask the F_HIDDEN flag and return it (as a truth value) )
595 1+ ( skip over the link pointer )
596 @ ( get the flags/length byte )
597 F_IMMED AND ( mask the F_IMMED flag and return it (as a truth value) )
601 LATEST @ ( start at LATEST dictionary entry )
603 ?DUP ( while link pointer is not null )
605 DUP ?HIDDEN NOT IF ( ignore hidden words )
606 DUP ID. ( but if not hidden, print the word )
609 @ ( dereference the link pointer - go to previous word )
615 ( FORGET ---------------------------------------------------------------------- )
618 BL WORD FIND ( find the word, gets the dictionary entry address )
619 DUP @ LATEST ! ( set LATEST to point to the previous word )
620 HERE ! ( and store HERE with the dictionary address )
623 ( DUMP ------------------------------------------------------------------------ )
628 ( DECOMPILER ------------------------------------------------------------------ )
631 LATEST @ ( start at LATEST dictionary entry )
633 ?DUP ( while link pointer is not null )
635 2DUP SWAP ( cfa curr curr cfa )
636 < IF ( current dictionary entry < cfa? )
637 NIP ( leave curr dictionary entry on the stack )
640 @ ( follow link pointer back )
642 DROP ( restore stack )
643 0 ( sorry, nothing found )
647 BL WORD DUP FIND ( find the dictionary entry to decompile )
650 ." Word '" COUNT TYPE ." ' not found in dictionary."
656 ( Now we search again, looking for the next word in the dictionary. This gives us
657 the length of the word that we will be decompiling. (Well, mostly it does). )
658 HERE @ ( address of the end of the last compiled word )
659 LATEST @ ( word last curr )
661 2 PICK ( word last curr word )
662 OVER ( word last curr word curr )
663 <> ( word last curr word<>curr? )
664 WHILE ( word last curr )
666 DUP @ ( word curr prev (which becomes: word last curr) )
669 DROP ( at this point, the stack is: start-of-word end-of-word )
670 SWAP ( end-of-word start-of-word )
675 [CHAR] : EMIT SPACE DUP ID. SPACE
676 DUP ?IMMEDIATE IF ." IMMEDIATE " THEN CR
679 \ Variable definition
680 ." Variable " DUP ID. CR
684 \ Constant definition
685 ." Constant " DUP ID. CR
690 ." Primitive or word with unrecognized codeword." CR
694 ( begin the definition with : NAME [IMMEDIATE] )
695 ( [CHAR] : EMIT SPACE DUP ID. SPACE
696 DUP ?IMMEDIATE IF ." IMMEDIATE " THEN CR 4 )
700 >PFA ( get the data address, ie. points after DOCOL | end-of-word start-of-data )
702 ( now we start decompiling until we hit the end of the word )
706 DUP @ ( end start codeword )
709 ['] LIT OF ( is it LIT ? )
710 1+ DUP @ ( get next word which is the integer constant )
713 ['] LITSTRING OF ( is it LITSTRING ? )
714 [CHAR] S EMIT [CHAR] " EMIT SPACE ( print S"<space> )
715 1+ DUP @ ( get the length word )
716 SWAP 1+ SWAP ( end start+1 length )
717 2DUP TYPE ( print the string )
718 [CHAR] " EMIT SPACE ( finish the string with a final quote )
719 + ( end start+1+len, aligned )
720 1- ( because we're about to add 4 below )
722 ['] 0BRANCH OF ( is it 0BRANCH ? )
724 1+ DUP @ ( print the offset )
728 ['] BRANCH OF ( is it BRANCH ? )
730 1+ DUP @ ( print the offset )
734 ['] ['] OF ( is it ['] ? )
736 1+ DUP @ ( get the next codeword )
737 CFA> ( and force it to be printed as a dictionary entry )
740 ['] EXIT OF ( is it EXIT? )
741 ( We expect the last word to be EXIT, and if it is then we don't print it
742 because EXIT is normally implied by ;. EXIT can also appear in the middle
743 of words, and then it needs to be printed. )
744 2DUP ( end start end start )
745 1+ ( end start end start+1 )
746 <> IF ( end start | we're not at the end )
751 DUP ( in the default case we always need to DUP before using )
752 CFA> ( look up the codeword to get the dictionary entry )
753 ID. SPACE ( and print it )
761 2DROP ( restore stack )
765 ( MEMORY ------------------------------------------------------------------ )
767 : UNUSED ( -- cells )