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 , , ;
45 WORD \ get the next word
46 FIND \ find it in the dictionary
47 >CFA \ get its codeword
52 LATEST @ \ LATEST points to the word being compiled at the moment
53 >CFA \ get the codeword
57 : DEBUGON TRUE DEBUG ! ;
58 : DEBUGOFF FALSE DEBUG ! ;
60 \ CONTROL STRUCTURES ----------------------------------------------------------------------
63 ['] 0BRANCH , \ compile 0BRANCH
64 HERE @ \ save location of the offset on the stack
65 0 , \ compile a dummy offset
70 HERE @ SWAP - \ calculate the offset from the address saved on the stack
71 SWAP ! \ store the offset in the back-filled location
75 ['] BRANCH , \ definite branch to just over the false-part
76 HERE @ \ save location of the offset on the stack
77 0 , \ compile a dummy offset
78 SWAP \ now back-fill the original (IF) offset
79 DUP \ same as for THEN word above
85 HERE @ \ save location on the stack
89 ['] 0BRANCH , \ compile 0BRANCH
90 HERE @ - \ calculate the offset from the address saved on the stack
91 , \ compile the offset here
95 ['] BRANCH , \ compile BRANCH
96 HERE @ - \ calculate the offset back
97 , \ compile the offset here
101 ['] 0BRANCH , \ compile 0BRANCH
102 HERE @ \ save location of the offset2 on the stack
103 0 , \ compile a dummy offset2
107 ['] BRANCH , \ compile BRANCH
108 SWAP \ get the original offset (from BEGIN)
109 HERE @ - , \ and compile it after BRANCH
111 HERE @ SWAP - \ calculate the offset2
112 SWAP ! \ and back-fill it in the original location
116 ['] NOT , \ compile NOT (to reverse the test)
117 [COMPILE] IF \ continue by calling the normal IF
121 ['] LIT , -1 , [COMPILE] IF
123 ['] LIT , HERE @ 0 , ['] >R ,
128 ['] 2DUP , ['] - , [COMPILE] IF
130 ['] LIT , HERE @ 0 , ['] >R ,
140 ['] R> , ['] RDROP , ['] RDROP ,
141 ['] 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 KEY \ read next character
219 DUP [CHAR] ( = IF \ open paren?
220 DROP \ drop the open paren
223 [CHAR] ) = IF \ close paren?
227 DUP 0= UNTIL \ continue until we reach matching close paren, depth 0
228 DROP \ drop the depth counter
231 ( Some more complicated stack examples, showing the stack notation. )
232 : NIP ( x y -- y ) SWAP DROP ;
233 : TUCK ( x y -- y x y ) DUP -ROT ;
234 : PICK ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
235 1+ ( add one because of 'u' on the stack )
236 PSP@ SWAP - ( add to the stack pointer )
239 : ROLL ( x_u x_u-1... x_0 u -- x_u-1 ... x_0 x_u )
240 1+ DUP PICK SWAP ( x_u x_u-1 ... x_0 x_u u+1 )
241 PSP@ 1- SWAP - PSP@ 2- SWAP
248 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
257 ( Standard words for manipulating BASE. )
258 : DECIMAL ( -- ) 10 BASE ! ;
259 : HEX ( -- ) 16 BASE ! ;
261 ( Compute absolute value. )
284 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
286 ( This is the underlying recursive definition of U. )
288 BASE @ /MOD ( width rem quot )
289 ?DUP IF ( if quotient <> 0 then )
290 RECURSE ( print the quotient )
293 ( print the remainder )
295 [CHAR] 0 ( decimal digits 0..9 )
297 10 - ( hex and beyond digits A..Z )
304 ( This word returns the width (in characters) of an unsigned number in the current base )
305 : UWIDTH ( u -- width )
306 BASE @ / ( rem quot )
307 ?DUP IF ( if quotient <> 0 then )
308 RECURSE 1+ ( return 1+recursive call )
317 UWIDTH ( width u uwidth )
318 ROT ( u uwidth width )
319 SWAP - ( u width-uwidth )
320 ( At this point if the requested width is narrower, we'll have a negative number on the stack.
321 Otherwise the number on the stack is the number of spaces to print. But SPACES won't print
322 a negative number of spaces anyway, so it's now safe to call SPACES ... )
324 ( ... and then call the underlying implementation of U. )
332 1 ( save a flag to remember that it was negative | width n 1 )
341 SWAP ( flag width u )
342 DUP ( flag width u u )
343 UWIDTH ( flag width u uwidth )
344 ROT ( flag u uwidth width )
345 SWAP - ( flag u width-uwidth )
350 IF ( was it negative? print the - character )
360 [CHAR] < EMIT DEPTH U. [CHAR] > EMIT SPACE
373 ( ? fetches the integer at an address and prints it. )
374 : ? ( addr -- ) @ . ;
376 ( c a b WITHIN returns true if a <= c and c < b )
393 ( STRINGS ---------------------------------------------------------------------- )
396 ( Block copy, however, is important and novel: )
397 : CMOVE ( src dest length -- )
403 -ROT OVER - ( length src (dest-src) )
404 -ROT DUP ROT + SWAP ( (dest-src) (src+length) src )
407 I @ ( (dest-src) i@ )
408 OVER I + ( (dest-src) i@ (dest-src+i) )
415 ( C, appends a byte to the current compiled word. )
421 : S" IMMEDIATE ( -- addr len )
422 STATE @ IF ( compiling? )
423 ['] LITSTRING , ( compile LITSTRING )
424 HERE @ ( save the address of the length word on the stack )
425 0 , ( dummy length - we don't know what it is yet )
428 KEY ( get next character of the string )
431 C, ( copy character )
433 DROP ( drop the double quote character at the end )
434 DUP ( get the saved address of the length word )
435 HERE @ SWAP - ( calculate the length )
436 1- ( subtract 1 (because we measured from the start of the length word) )
437 SWAP ! ( and back-fill the length location )
438 ELSE ( immediate mode )
439 HERE @ ( get the start address of the temporary space )
445 OVER C! ( save next character )
446 1+ ( increment address )
448 DROP ( drop the final " character )
449 HERE @ - ( calculate the length )
450 HERE @ ( push the start address )
455 : ." IMMEDIATE ( -- )
456 [COMPILE] S" ( read the string, and compile LITSTRING, etc. )
457 ['] TELL , ( compile the final TELL )
465 DROP ( drop the double quote character )
466 EXIT ( return from this function )
473 ( CONSTANTS AND VARIABLES ------------------------------------------------------ )
476 WORD HEADER ( make dictionary entry (the name follows CONSTANT) )
477 DOCOL , ( append DOCOL (the codeword field of this word) )
478 ['] LIT , ( append the codeword LIT )
479 , ( append the value on the top of the stack )
480 ['] EXIT , ( append the codeword EXIT )
484 HERE +! ( adds n to HERE, after this the old value of HERE is still on the stack )
489 1 CELLS ALLOT ( allocate 1 cell of memory, push the pointer to this memory )
494 WORD HEADER ( make the dictionary entry (the name follows VALUE) )
495 DOCOL , ( append DOCOL )
496 ['] LIT , ( append the codeword LIT )
497 , ( append the initial value )
498 ['] EXIT , ( append the codeword EXIT )
501 : TO IMMEDIATE ( n -- )
502 WORD ( get the name of the value )
503 FIND ( look it up in the dictionary )
504 >DFA ( get a pointer to the first data field (the 'LIT') )
505 1+ ( increment to point at the value )
506 STATE @ IF ( compiling? )
507 ['] LIT , ( compile LIT )
508 , ( compile the address of the value )
509 ['] ! , ( compile ! )
510 ELSE ( immediate mode )
511 ! ( update it straightaway )
515 ( x +TO VAL adds x to VAL )
517 WORD ( get the name of the value )
518 FIND ( look it up in the dictionary )
519 >DFA ( get a pointer to the first data field (the 'LIT') )
520 1+ ( increment to point at the value )
521 STATE @ IF ( compiling? )
522 ['] LIT , ( compile LIT )
523 , ( compile the address of the value )
524 ['] +! , ( compile +! )
525 ELSE ( immediate mode )
526 +! ( update it straightaway )
530 ( Fill u ints, starting at a, with the value b )
542 ( PRINTING THE DICTIONARY ------------------------------------------------------ )
545 1+ ( skip over the link pointer )
546 DUP @ ( get the flags/length byte )
547 F_LENMASK AND ( mask out the flags - just want the length )
550 DUP 0> ( length > 0? )
552 SWAP 1+ ( addr len -- len addr+1 )
553 DUP @ ( len addr -- len addr char | get the next character)
554 DUP 32 >= OVER 127 <= AND IF
555 EMIT ( len addr char -- len addr | and print it)
561 SWAP 1- ( len addr -- addr len-1 | subtract one from length )
563 2DROP ( len addr -- )
567 1+ ( skip over the link pointer )
568 @ ( get the flags/length byte )
569 F_HIDDEN AND ( mask the F_HIDDEN flag and return it (as a truth value) )
572 1+ ( skip over the link pointer )
573 @ ( get the flags/length byte )
574 F_IMMED AND ( mask the F_IMMED flag and return it (as a truth value) )
578 LATEST @ ( start at LATEST dictionary entry )
580 ?DUP ( while link pointer is not null )
582 DUP ?HIDDEN NOT IF ( ignore hidden words )
583 DUP ID. ( but if not hidden, print the word )
586 @ ( dereference the link pointer - go to previous word )
592 ( FORGET ---------------------------------------------------------------------- )
595 WORD FIND ( find the word, gets the dictionary entry address )
596 DUP @ LATEST ! ( set LATEST to point to the previous word )
597 HERE ! ( and store HERE with the dictionary address )
600 ( DUMP ------------------------------------------------------------------------ )
605 ( DECOMPILER ------------------------------------------------------------------ )
608 LATEST @ ( start at LATEST dictionary entry )
610 ?DUP ( while link pointer is not null )
612 2DUP SWAP ( cfa curr curr cfa )
613 < IF ( current dictionary entry < cfa? )
614 NIP ( leave curr dictionary entry on the stack )
617 @ ( follow link pointer back )
619 DROP ( restore stack )
620 0 ( sorry, nothing found )
624 WORD FIND ( find the dictionary entry to decompile )
626 ( Now we search again, looking for the next word in the dictionary. This gives us
627 the length of the word that we will be decompiling. (Well, mostly it does). )
628 HERE @ ( address of the end of the last compiled word )
629 LATEST @ ( word last curr )
631 2 PICK ( word last curr word )
632 OVER ( word last curr word curr )
633 <> ( word last curr word<>curr? )
634 WHILE ( word last curr )
636 DUP @ ( word curr prev (which becomes: word last curr) )
639 DROP ( at this point, the stack is: start-of-word end-of-word )
640 SWAP ( end-of-word start-of-word )
645 [CHAR] : EMIT SPACE DUP ID. SPACE
646 DUP ?IMMEDIATE IF ." IMMEDIATE " THEN CR
649 \ Variable definition
650 ." Variable " DUP ID. CR
654 \ Constant definition
655 ." Constant " DUP ID. CR
660 ." Primitive or word with unrecognized codeword." CR
664 ( begin the definition with : NAME [IMMEDIATE] )
665 ( [CHAR] : EMIT SPACE DUP ID. SPACE
666 DUP ?IMMEDIATE IF ." IMMEDIATE " THEN CR 4 )
670 >DFA ( get the data address, ie. points after DOCOL | end-of-word start-of-data )
672 ( now we start decompiling until we hit the end of the word )
676 DUP @ ( end start codeword )
679 ['] LIT OF ( is it LIT ? )
680 1+ DUP @ ( get next word which is the integer constant )
683 ['] LITSTRING OF ( is it LITSTRING ? )
684 [CHAR] S EMIT [CHAR] " EMIT SPACE ( print S"<space> )
685 1+ DUP @ ( get the length word )
686 SWAP 1+ SWAP ( end start+1 length )
687 2DUP TELL ( print the string )
688 [CHAR] " EMIT SPACE ( finish the string with a final quote )
689 + ( end start+1+len, aligned )
690 1- ( because we're about to add 4 below )
692 ['] 0BRANCH OF ( is it 0BRANCH ? )
694 1+ DUP @ ( print the offset )
698 ['] BRANCH OF ( is it BRANCH ? )
700 1+ DUP @ ( print the offset )
704 ['] ['] OF ( is it ['] ? )
706 1+ DUP @ ( get the next codeword )
707 CFA> ( and force it to be printed as a dictionary entry )
710 ['] EXIT OF ( is it EXIT? )
711 ( We expect the last word to be EXIT, and if it is then we don't print it
712 because EXIT is normally implied by ;. EXIT can also appear in the middle
713 of words, and then it needs to be printed. )
714 2DUP ( end start end start )
715 1+ ( end start end start+1 )
716 <> IF ( end start | we're not at the end )
721 DUP ( in the default case we always need to DUP before using )
722 CFA> ( look up the codeword to get the dictionary entry )
723 ID. SPACE ( and print it )
731 2DROP ( restore stack )
735 ( MEMORY ------------------------------------------------------------------ )
737 : UNUSED ( -- cells )