4 ; \ We can now comment!
6 \ BASIC DEFINITIONS ----------------------------------------------------------------------
18 \ Translate a number of cells into memory units
19 \ (in our case 1 cell = 1 memory unit)
22 \ Since the smallest unit of memory in our system is 64 bits and since strings
23 \ are stored as arrays of 64 bit integers, the character store/fetch words are
24 \ just aliases of the standard store/fetch words.
29 : DEPTH PSP@ PSP0 @ - ;
34 : LITERAL IMMEDIATE ['] LIT , , ;
50 WORD \ get the next word
51 FIND \ find it in the dictionary
52 >CFA \ get its codeword
57 LATEST @ \ LATEST points to the word being compiled at the moment
58 >CFA \ get the codeword
62 : DEBUGON TRUE DEBUG ! ;
63 : DEBUGOFF FALSE DEBUG ! ;
65 \ CONTROL STRUCTURES ----------------------------------------------------------------------
68 ['] 0BRANCH , \ compile 0BRANCH
69 HERE @ \ save location of the offset on the stack
70 0 , \ compile a dummy offset
75 HERE @ SWAP - \ calculate the offset from the address saved on the stack
76 SWAP ! \ store the offset in the back-filled location
80 ['] BRANCH , \ definite branch to just over the false-part
81 HERE @ \ save location of the offset on the stack
82 0 , \ compile a dummy offset
83 SWAP \ now back-fill the original (IF) offset
84 DUP \ same as for THEN word above
90 HERE @ \ save location on the stack
94 ['] 0BRANCH , \ compile 0BRANCH
95 HERE @ - \ calculate the offset from the address saved on the stack
96 , \ compile the offset here
100 ['] BRANCH , \ compile BRANCH
101 HERE @ - \ calculate the offset back
102 , \ compile the offset here
106 ['] 0BRANCH , \ compile 0BRANCH
107 HERE @ \ save location of the offset2 on the stack
108 0 , \ compile a dummy offset2
112 ['] BRANCH , \ compile BRANCH
113 SWAP \ get the original offset (from BEGIN)
114 HERE @ - , \ and compile it after BRANCH
116 HERE @ SWAP - \ calculate the offset2
117 SWAP ! \ and back-fill it in the original location
121 ['] NOT , \ compile NOT (to reverse the test)
122 [COMPILE] IF \ continue by calling the normal IF
126 ['] LIT , -1 , [COMPILE] IF
128 ['] LIT , HERE @ 0 , ['] >R ,
133 ['] 2DUP , ['] - , [COMPILE] IF
135 ['] LIT , HERE @ 0 , ['] >R ,
145 ['] R> , ['] RDROP , ['] RDROP ,
146 ['] LIT , HERE @ 7 + , ['] DUP , ['] -ROT , ['] - , ['] SWAP , ['] ! ,
157 ['] DUP , \ Store copy of increment
159 ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] + , ['] 2DUP , ['] - ,
160 ['] SWAP , ['] >R , ['] SWAP , ['] >R , ['] SWAP , ['] >R ,
162 \ Condition differently depending on sign of increment
163 ['] SWAP , ['] 0>= , [COMPILE] IF
169 \ Branch back to begining of loop kernel
170 ['] 0BRANCH , HERE @ - ,
173 ['] RDROP , ['] RDROP , ['] RDROP ,
175 \ Record address of loop end for any LEAVEs to use
179 ['] 2DROP , \ Clean up if loop was entirely skipped (?DO)
189 \ CASE ------------------------------------------------------------------------
192 0 \ push 0 to mark the bottom of the stack
196 ['] OVER , \ compile OVER
198 [COMPILE] IF \ compile IF
199 ['] DROP , \ compile DROP
203 [COMPILE] ELSE \ ENDOF is the same as ELSE
207 ['] DROP , \ compile DROP
209 \ keep compiling THEN until we get to our zero marker
218 \ COMMENTS ----------------------------------------------------------------------
221 1 \ allowed nested parens by keeping track of depth
223 KEY \ read next character
224 DUP [CHAR] ( = IF \ open paren?
225 DROP \ drop the open paren
228 [CHAR] ) = IF \ close paren?
232 DUP 0= UNTIL \ continue until we reach matching close paren, depth 0
233 DROP \ drop the depth counter
236 ( Some more complicated stack examples, showing the stack notation. )
237 : NIP ( x y -- y ) SWAP DROP ;
238 : TUCK ( x y -- y x y ) DUP -ROT ;
239 : PICK ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
240 1+ ( add one because of 'u' on the stack )
241 PSP@ SWAP - ( add to the stack pointer )
244 : ROLL ( x_u x_u-1... x_0 u -- x_u-1 ... x_0 x_u )
245 1+ DUP PICK SWAP ( x_u x_u-1 ... x_0 x_u u+1 )
246 PSP@ 1- SWAP - PSP@ 2- SWAP
253 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
262 ( Standard words for manipulating BASE. )
263 : DECIMAL ( -- ) 10 BASE ! ;
264 : HEX ( -- ) 16 BASE ! ;
266 ( Compute absolute value. )
289 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
291 ( This is the underlying recursive definition of U. )
293 BASE @ /MOD ( width rem quot )
294 ?DUP IF ( if quotient <> 0 then )
295 RECURSE ( print the quotient )
298 ( print the remainder )
300 [CHAR] 0 ( decimal digits 0..9 )
302 10 - ( hex and beyond digits A..Z )
309 ( This word returns the width (in characters) of an unsigned number in the current base )
310 : UWIDTH ( u -- width )
311 BASE @ / ( rem quot )
312 ?DUP IF ( if quotient <> 0 then )
313 RECURSE 1+ ( return 1+recursive call )
322 UWIDTH ( width u uwidth )
323 ROT ( u uwidth width )
324 SWAP - ( u width-uwidth )
325 ( At this point if the requested width is narrower, we'll have a negative number on the stack.
326 Otherwise the number on the stack is the number of spaces to print. But SPACES won't print
327 a negative number of spaces anyway, so it's now safe to call SPACES ... )
329 ( ... and then call the underlying implementation of U. )
337 1 ( save a flag to remember that it was negative | width n 1 )
346 SWAP ( flag width u )
347 DUP ( flag width u u )
348 UWIDTH ( flag width u uwidth )
349 ROT ( flag u uwidth width )
350 SWAP - ( flag u width-uwidth )
355 IF ( was it negative? print the - character )
365 [CHAR] < EMIT DEPTH U. [CHAR] > EMIT SPACE
378 ( ? fetches the integer at an address and prints it. )
379 : ? ( addr -- ) @ . ;
381 ( c a b WITHIN returns true if a <= c and c < b )
398 ( STRINGS ---------------------------------------------------------------------- )
401 ( Block copy, however, is important and novel: )
402 : CMOVE ( src dest length -- )
408 -ROT OVER - ( length src (dest-src) )
409 -ROT DUP ROT + SWAP ( (dest-src) (src+length) src )
412 I @ ( (dest-src) i@ )
413 OVER I + ( (dest-src) i@ (dest-src+i) )
420 ( C, appends a byte to the current compiled word. )
426 : S" IMMEDIATE ( -- addr len )
427 STATE @ IF ( compiling? )
428 ['] LITSTRING , ( compile LITSTRING )
429 HERE @ ( save the address of the length word on the stack )
430 0 , ( dummy length - we don't know what it is yet )
433 KEY ( get next character of the string )
436 C, ( copy character )
438 DROP ( drop the double quote character at the end )
439 DUP ( get the saved address of the length word )
440 HERE @ SWAP - ( calculate the length )
441 1- ( subtract 1 (because we measured from the start of the length word) )
442 SWAP ! ( and back-fill the length location )
443 ELSE ( immediate mode )
444 HERE @ ( get the start address of the temporary space )
450 OVER C! ( save next character )
451 1+ ( increment address )
453 DROP ( drop the final " character )
454 HERE @ - ( calculate the length )
455 HERE @ ( push the start address )
460 : ." IMMEDIATE ( -- )
461 [COMPILE] S" ( read the string, and compile LITSTRING, etc. )
462 ['] TYPE , ( compile the final TYPE )
470 DROP ( drop the double quote character )
471 EXIT ( return from this function )
477 ( Converts address of counted string into address of
478 start of string and length of string. )
479 : COUNT ( addr1 -- addr2 n )
483 ( CONSTANTS AND VARIABLES ------------------------------------------------------ )
486 WORD HEADER ( make dictionary entry (the name follows CONSTANT) )
487 DOCOL , ( append DOCOL (the codeword field of this word) )
488 ['] LIT , ( append the codeword LIT )
489 , ( append the value on the top of the stack )
490 ['] EXIT , ( append the codeword EXIT )
494 HERE +! ( adds n to HERE, after this the old value of HERE is still on the stack )
499 1 CELLS ALLOT ( allocate 1 cell of memory, push the pointer to this memory )
504 WORD HEADER ( make the dictionary entry (the name follows VALUE) )
505 DOCOL , ( append DOCOL )
506 ['] LIT , ( append the codeword LIT )
507 , ( append the initial value )
508 ['] EXIT , ( append the codeword EXIT )
511 : TO IMMEDIATE ( n -- )
512 WORD ( get the name of the value )
513 FIND ( look it up in the dictionary )
514 >DFA ( get a pointer to the first data field (the 'LIT') )
515 1+ ( increment to point at the value )
516 STATE @ IF ( compiling? )
517 ['] LIT , ( compile LIT )
518 , ( compile the address of the value )
519 ['] ! , ( compile ! )
520 ELSE ( immediate mode )
521 ! ( update it straightaway )
525 ( x +TO VAL adds x to VAL )
527 WORD ( get the name of the value )
528 FIND ( look it up in the dictionary )
529 >DFA ( 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 ( Fill u ints, starting at a, with the value b )
552 ( PRINTING THE DICTIONARY ------------------------------------------------------ )
555 1+ ( skip over the link pointer )
556 DUP @ ( get the flags/length byte )
557 F_LENMASK AND ( mask out the flags - just want the length )
560 DUP 0> ( length > 0? )
562 SWAP 1+ ( addr len -- len addr+1 )
563 DUP @ ( len addr -- len addr char | get the next character)
564 DUP 32 >= OVER 127 <= AND IF
565 EMIT ( len addr char -- len addr | and print it)
571 SWAP 1- ( len addr -- addr len-1 | subtract one from length )
573 2DROP ( len addr -- )
577 1+ ( skip over the link pointer )
578 @ ( get the flags/length byte )
579 F_HIDDEN AND ( mask the F_HIDDEN flag and return it (as a truth value) )
582 1+ ( skip over the link pointer )
583 @ ( get the flags/length byte )
584 F_IMMED AND ( mask the F_IMMED flag and return it (as a truth value) )
588 LATEST @ ( start at LATEST dictionary entry )
590 ?DUP ( while link pointer is not null )
592 DUP ?HIDDEN NOT IF ( ignore hidden words )
593 DUP ID. ( but if not hidden, print the word )
596 @ ( dereference the link pointer - go to previous word )
602 ( FORGET ---------------------------------------------------------------------- )
605 WORD FIND ( find the word, gets the dictionary entry address )
606 DUP @ LATEST ! ( set LATEST to point to the previous word )
607 HERE ! ( and store HERE with the dictionary address )
610 ( DUMP ------------------------------------------------------------------------ )
615 ( DECOMPILER ------------------------------------------------------------------ )
618 LATEST @ ( start at LATEST dictionary entry )
620 ?DUP ( while link pointer is not null )
622 2DUP SWAP ( cfa curr curr cfa )
623 < IF ( current dictionary entry < cfa? )
624 NIP ( leave curr dictionary entry on the stack )
627 @ ( follow link pointer back )
629 DROP ( restore stack )
630 0 ( sorry, nothing found )
634 WORD 2DUP FIND ( find the dictionary entry to decompile )
637 ." Word '" TYPE ." ' not found in dictionary."
643 ( Now we search again, looking for the next word in the dictionary. This gives us
644 the length of the word that we will be decompiling. (Well, mostly it does). )
645 HERE @ ( address of the end of the last compiled word )
646 LATEST @ ( word last curr )
648 2 PICK ( word last curr word )
649 OVER ( word last curr word curr )
650 <> ( word last curr word<>curr? )
651 WHILE ( word last curr )
653 DUP @ ( word curr prev (which becomes: word last curr) )
656 DROP ( at this point, the stack is: start-of-word end-of-word )
657 SWAP ( end-of-word start-of-word )
662 [CHAR] : EMIT SPACE DUP ID. SPACE
663 DUP ?IMMEDIATE IF ." IMMEDIATE " THEN CR
666 \ Variable definition
667 ." Variable " DUP ID. CR
671 \ Constant definition
672 ." Constant " DUP ID. CR
677 ." Primitive or word with unrecognized codeword." CR
681 ( begin the definition with : NAME [IMMEDIATE] )
682 ( [CHAR] : EMIT SPACE DUP ID. SPACE
683 DUP ?IMMEDIATE IF ." IMMEDIATE " THEN CR 4 )
687 >DFA ( get the data address, ie. points after DOCOL | end-of-word start-of-data )
689 ( now we start decompiling until we hit the end of the word )
693 DUP @ ( end start codeword )
696 ['] LIT OF ( is it LIT ? )
697 1+ DUP @ ( get next word which is the integer constant )
700 ['] LITSTRING OF ( is it LITSTRING ? )
701 [CHAR] S EMIT [CHAR] " EMIT SPACE ( print S"<space> )
702 1+ DUP @ ( get the length word )
703 SWAP 1+ SWAP ( end start+1 length )
704 2DUP TYPE ( print the string )
705 [CHAR] " EMIT SPACE ( finish the string with a final quote )
706 + ( end start+1+len, aligned )
707 1- ( because we're about to add 4 below )
709 ['] 0BRANCH OF ( is it 0BRANCH ? )
711 1+ DUP @ ( print the offset )
715 ['] BRANCH OF ( is it BRANCH ? )
717 1+ DUP @ ( print the offset )
721 ['] ['] OF ( is it ['] ? )
723 1+ DUP @ ( get the next codeword )
724 CFA> ( and force it to be printed as a dictionary entry )
727 ['] EXIT OF ( is it EXIT? )
728 ( We expect the last word to be EXIT, and if it is then we don't print it
729 because EXIT is normally implied by ;. EXIT can also appear in the middle
730 of words, and then it needs to be printed. )
731 2DUP ( end start end start )
732 1+ ( end start end start+1 )
733 <> IF ( end start | we're not at the end )
738 DUP ( in the default case we always need to DUP before using )
739 CFA> ( look up the codeword to get the dictionary entry )
740 ID. SPACE ( and print it )
748 2DROP ( restore stack )
752 ( MEMORY ------------------------------------------------------------------ )
754 : UNUSED ( -- cells )