11 : CELLS ; \ Allow for slightly more portable code
13 : DEPTH PSP@ PSP0 @ - ;
18 : LITERAL IMMEDIATE ['] LIT , , ;
20 : ':' [ CHAR : ] LITERAL ;
21 : ';' [ CHAR ; ] LITERAL ;
22 : '(' [ CHAR ( ] LITERAL ;
23 : ')' [ CHAR ) ] LITERAL ;
24 : '<' [ CHAR < ] LITERAL ;
25 : '>' [ CHAR > ] LITERAL ;
26 : '"' [ CHAR " ] LITERAL ;
27 : 'A' [ CHAR A ] LITERAL ;
28 : '0' [ CHAR 0 ] LITERAL ;
29 : '-' [ CHAR - ] LITERAL ;
30 : '.' [ CHAR . ] LITERAL ;
36 WORD \ get the next word
37 FIND \ find it in the dictionary
38 >CFA \ get its codeword
43 LATEST @ \ LATEST points to the word being compiled at the moment
44 >CFA \ get the codeword
48 : DEBUGON TRUE DEBUG ! ;
49 : DEBUGOFF FALSE DEBUG ! ;
51 \ CONTROL STRUCTURES ----------------------------------------------------------------------
54 ['] 0BRANCH , \ compile 0BRANCH
55 HERE @ \ save location of the offset on the stack
56 0 , \ compile a dummy offset
61 HERE @ SWAP - \ calculate the offset from the address saved on the stack
62 SWAP ! \ store the offset in the back-filled location
66 ['] BRANCH , \ definite branch to just over the false-part
67 HERE @ \ save location of the offset on the stack
68 0 , \ compile a dummy offset
69 SWAP \ now back-fill the original (IF) offset
70 DUP \ same as for THEN word above
76 HERE @ \ save location on the stack
80 ['] 0BRANCH , \ compile 0BRANCH
81 HERE @ - \ calculate the offset from the address saved on the stack
82 , \ compile the offset here
86 ['] BRANCH , \ compile BRANCH
87 HERE @ - \ calculate the offset back
88 , \ compile the offset here
92 ['] 0BRANCH , \ compile 0BRANCH
93 HERE @ \ save location of the offset2 on the stack
94 0 , \ compile a dummy offset2
98 ['] BRANCH , \ compile BRANCH
99 SWAP \ get the original offset (from BEGIN)
100 HERE @ - , \ and compile it after BRANCH
102 HERE @ SWAP - \ calculate the offset2
103 SWAP ! \ and back-fill it in the original location
107 ['] NOT , \ compile NOT (to reverse the test)
108 [COMPILE] IF \ continue by calling the normal IF
112 ['] LIT , -1 , [COMPILE] IF
114 ['] LIT , HERE @ 0 , ['] >R ,
119 ['] 2DUP , ['] - , [COMPILE] IF
121 ['] LIT , HERE @ 0 , ['] >R ,
131 ['] R> , ['] RDROP , ['] RDROP ,
132 ['] LIT , HERE @ 7 + , ['] DUP , ['] -ROT , ['] - , ['] SWAP , ['] ! ,
143 ['] DUP , \ Store copy of increment
145 ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] + , ['] 2DUP , ['] - ,
146 ['] SWAP , ['] >R , ['] SWAP , ['] >R , ['] SWAP , ['] >R ,
148 \ Condition differently depending on sign of increment
149 ['] SWAP , ['] 0>= , [COMPILE] IF
155 \ Branch back to begining of loop kernel
156 ['] 0BRANCH , HERE @ - ,
159 ['] RDROP , ['] RDROP , ['] RDROP ,
161 \ Record address of loop end for any LEAVEs to use
165 ['] 2DROP , \ Clean up if loop was entirely skipped (?DO)
175 \ CASE ------------------------------------------------------------------------
178 0 \ push 0 to mark the bottom of the stack
182 ['] OVER , \ compile OVER
184 [COMPILE] IF \ compile IF
185 ['] DROP , \ compile DROP
189 [COMPILE] ELSE \ ENDOF is the same as ELSE
193 ['] DROP , \ compile DROP
195 \ keep compiling THEN until we get to our zero marker
204 \ COMMENTS ----------------------------------------------------------------------
207 1 \ allowed nested parens by keeping track of depth
209 KEY \ read next character
210 DUP '(' = IF \ open paren?
211 DROP \ drop the open paren
214 ')' = IF \ close paren?
218 DUP 0= UNTIL \ continue until we reach matching close paren, depth 0
219 DROP \ drop the depth counter
222 ( Some more complicated stack examples, showing the stack notation. )
223 : NIP ( x y -- y ) SWAP DROP ;
224 : TUCK ( x y -- y x y ) DUP -ROT ;
225 : PICK ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
226 1+ ( add one because of 'u' on the stack )
227 PSP@ SWAP - ( add to the stack pointer )
230 : ROLL ( x_u x_u-1... x_0 u -- x_u-1 ... x_0 x_u )
231 1+ DUP PICK SWAP ( x_u x_u-1 ... x_0 x_u u+1 )
232 PSP@ 1- SWAP - PSP@ 2- SWAP
239 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
248 ( Standard words for manipulating BASE. )
249 : DECIMAL ( -- ) 10 BASE ! ;
250 : HEX ( -- ) 16 BASE ! ;
252 ( Compute absolute value. )
275 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
277 ( This is the underlying recursive definition of U. )
279 BASE @ /MOD ( width rem quot )
280 ?DUP IF ( if quotient <> 0 then )
281 RECURSE ( print the quotient )
284 ( print the remainder )
286 '0' ( decimal digits 0..9 )
288 10 - ( hex and beyond digits A..Z )
295 ( This word returns the width (in characters) of an unsigned number in the current base )
296 : UWIDTH ( u -- width )
297 BASE @ / ( rem quot )
298 ?DUP IF ( if quotient <> 0 then )
299 RECURSE 1+ ( return 1+recursive call )
308 UWIDTH ( width u uwidth )
309 ROT ( u uwidth width )
310 SWAP - ( u width-uwidth )
311 ( At this point if the requested width is narrower, we'll have a negative number on the stack.
312 Otherwise the number on the stack is the number of spaces to print. But SPACES won't print
313 a negative number of spaces anyway, so it's now safe to call SPACES ... )
315 ( ... and then call the underlying implementation of U. )
323 1 ( save a flag to remember that it was negative | width n 1 )
332 SWAP ( flag width u )
333 DUP ( flag width u u )
334 UWIDTH ( flag width u uwidth )
335 ROT ( flag u uwidth width )
336 SWAP - ( flag u width-uwidth )
341 IF ( was it negative? print the - character )
351 '<' EMIT DEPTH U. '>' EMIT SPACE
364 ( ? fetches the integer at an address and prints it. )
365 : ? ( addr -- ) @ . ;
367 ( c a b WITHIN returns true if a <= c and c < b )
384 ( STRINGS ---------------------------------------------------------------------- )
386 ( Since the smallest unit of memory in our system is 64 bits and since strings
387 are stored as arrays of 64 bit integers, the character store/fetch words are
388 just aliases of the standard store/fetch words. )
392 ( Block copy, however, is important and novel: )
393 : CMOVE ( src dest length -- )
399 -ROT OVER - ( length src (dest-src) )
400 -ROT DUP ROT + SWAP ( (dest-src) (src+length) src )
403 I @ ( (dest-src) i@ )
404 OVER I + ( (dest-src) i@ (dest-src+i) )
411 ( C, appends a byte to the current compiled word. )
417 : S" IMMEDIATE ( -- addr len )
418 STATE @ IF ( compiling? )
419 ['] LITSTRING , ( compile LITSTRING )
420 HERE @ ( save the address of the length word on the stack )
421 0 , ( dummy length - we don't know what it is yet )
424 KEY ( get next character of the string )
427 C, ( copy character )
429 DROP ( drop the double quote character at the end )
430 DUP ( get the saved address of the length word )
431 HERE @ SWAP - ( calculate the length )
432 1- ( subtract 1 (because we measured from the start of the length word) )
433 SWAP ! ( and back-fill the length location )
434 ELSE ( immediate mode )
435 HERE @ ( get the start address of the temporary space )
441 OVER C! ( save next character )
442 1+ ( increment address )
444 DROP ( drop the final " character )
445 HERE @ - ( calculate the length )
446 HERE @ ( push the start address )
451 : ." IMMEDIATE ( -- )
452 STATE @ IF ( compiling? )
453 [COMPILE] S" ( read the string, and compile LITSTRING, etc. )
454 ['] TELL , ( compile the final TELL )
456 ( In immediate mode, just read characters and print them until we get
457 to the ending double quote. )
462 DROP ( drop the double quote character )
463 EXIT ( return from this function )
471 ( CONSTANTS AND VARIABLES ------------------------------------------------------ )
474 WORD HEADER ( make dictionary entry (the name follows CONSTANT) )
475 DOCOL , ( append DOCOL (the codeword field of this word) )
476 ['] LIT , ( append the codeword LIT )
477 , ( append the value on the top of the stack )
478 ['] EXIT , ( append the codeword EXIT )
481 : ALLOT ( n -- addr )
482 HERE @ SWAP ( here n )
483 HERE +! ( adds n to HERE, after this the old value of HERE is still on the stack )
487 1 CELLS ALLOT ( allocate 1 cell of memory, push the pointer to this memory )
488 WORD HEADER ( make the dictionary entry (the name follows VARIABLE) )
489 DOCOL , ( append DOCOL (the codeword field of this word) )
490 ['] LIT , ( append the codeword LIT )
491 , ( append the pointer to the new memory )
492 ['] EXIT , ( append the codeword EXIT )
497 WORD HEADER ( make the dictionary entry (the name follows VALUE) )
498 DOCOL , ( append DOCOL )
499 ['] LIT , ( append the codeword LIT )
500 , ( append the initial value )
501 ['] EXIT , ( append the codeword EXIT )
504 : TO IMMEDIATE ( n -- )
505 WORD ( get the name of the value )
506 FIND ( look it up in the dictionary )
507 >DFA ( get a pointer to the first data field (the 'LIT') )
508 1+ ( increment to point at the value )
509 STATE @ IF ( compiling? )
510 ['] LIT , ( compile LIT )
511 , ( compile the address of the value )
512 ['] ! , ( compile ! )
513 ELSE ( immediate mode )
514 ! ( update it straightaway )
518 ( x +TO VAL adds x to VAL )
520 WORD ( get the name of the value )
521 FIND ( look it up in the dictionary )
522 >DFA ( get a pointer to the first data field (the 'LIT') )
523 1+ ( increment to point at the value )
524 STATE @ IF ( compiling? )
525 ['] LIT , ( compile LIT )
526 , ( compile the address of the value )
527 ['] +! , ( compile +! )
528 ELSE ( immediate mode )
529 +! ( update it straightaway )
534 ( PRINTING THE DICTIONARY ------------------------------------------------------ )
537 1+ ( skip over the link pointer )
538 DUP @ ( get the flags/length byte )
539 F_LENMASK AND ( mask out the flags - just want the length )
542 DUP 0> ( length > 0? )
544 SWAP 1+ ( addr len -- len addr+1 )
545 DUP @ ( len addr -- len addr char | get the next character)
546 DUP 32 >= OVER 127 <= AND IF
547 EMIT ( len addr char -- len addr | and print it)
553 SWAP 1- ( len addr -- addr len-1 | subtract one from length )
555 2DROP ( len addr -- )
559 1+ ( skip over the link pointer )
560 @ ( get the flags/length byte )
561 F_HIDDEN AND ( mask the F_HIDDEN flag and return it (as a truth value) )
564 1+ ( skip over the link pointer )
565 @ ( get the flags/length byte )
566 F_IMMED AND ( mask the F_IMMED flag and return it (as a truth value) )
570 LATEST @ ( start at LATEST dictionary entry )
572 ?DUP ( while link pointer is not null )
574 DUP ?HIDDEN NOT IF ( ignore hidden words )
575 DUP ID. ( but if not hidden, print the word )
578 @ ( dereference the link pointer - go to previous word )
584 ( FORGET ---------------------------------------------------------------------- )
587 WORD FIND ( find the word, gets the dictionary entry address )
588 DUP @ LATEST ! ( set LATEST to point to the previous word )
589 HERE ! ( and store HERE with the dictionary address )
592 ( DUMP ------------------------------------------------------------------------ )
597 ( DECOMPILER ------------------------------------------------------------------ )
600 LATEST @ ( start at LATEST dictionary entry )
602 ?DUP ( while link pointer is not null )
604 2DUP SWAP ( cfa curr curr cfa )
605 < IF ( current dictionary entry < cfa? )
606 NIP ( leave curr dictionary entry on the stack )
609 @ ( follow link pointer back )
611 DROP ( restore stack )
612 0 ( sorry, nothing found )
616 WORD FIND ( find the dictionary entry to decompile )
618 ( Now we search again, looking for the next word in the dictionary. This gives us
619 the length of the word that we will be decompiling. (Well, mostly it does). )
620 HERE @ ( address of the end of the last compiled word )
621 LATEST @ ( word last curr )
623 2 PICK ( word last curr word )
624 OVER ( word last curr word curr )
625 <> ( word last curr word<>curr? )
626 WHILE ( word last curr )
628 DUP @ ( word curr prev (which becomes: word last curr) )
631 DROP ( at this point, the stack is: start-of-word end-of-word )
632 SWAP ( end-of-word start-of-word )
637 ':' EMIT SPACE DUP ID. SPACE
638 DUP ?IMMEDIATE IF ." IMMEDIATE " THEN CR
641 \ Variable definition
642 ." Variable " DUP ID. CR
646 \ Constant definition
647 ." Constant " DUP ID. CR
652 ." Primitive or word with unrecognized codeword." CR
656 ( begin the definition with : NAME [IMMEDIATE] )
657 ( ':' EMIT SPACE DUP ID. SPACE
658 DUP ?IMMEDIATE IF ." IMMEDIATE " THEN CR 4 )
662 >DFA ( get the data address, ie. points after DOCOL | end-of-word start-of-data )
664 ( now we start decompiling until we hit the end of the word )
668 DUP @ ( end start codeword )
671 ['] LIT OF ( is it LIT ? )
672 1+ DUP @ ( get next word which is the integer constant )
675 ['] LITSTRING OF ( is it LITSTRING ? )
676 [ CHAR S ] LITERAL EMIT '"' EMIT SPACE ( print S"<space> )
677 1+ DUP @ ( get the length word )
678 SWAP 1+ SWAP ( end start+1 length )
679 2DUP TELL ( print the string )
680 '"' EMIT SPACE ( finish the string with a final quote )
681 + ( end start+1+len, aligned )
682 1- ( because we're about to add 4 below )
684 ['] 0BRANCH OF ( is it 0BRANCH ? )
686 1+ DUP @ ( print the offset )
690 ['] BRANCH OF ( is it BRANCH ? )
692 1+ DUP @ ( print the offset )
696 ['] ['] OF ( is it ['] ? )
698 1+ DUP @ ( get the next codeword )
699 CFA> ( and force it to be printed as a dictionary entry )
702 ['] EXIT OF ( is it EXIT? )
703 ( We expect the last word to be EXIT, and if it is then we don't print it
704 because EXIT is normally implied by ;. EXIT can also appear in the middle
705 of words, and then it needs to be printed. )
706 2DUP ( end start end start )
707 1+ ( end start end start+1 )
708 <> IF ( end start | we're not at the end )
713 DUP ( in the default case we always need to DUP before using )
714 CFA> ( look up the codeword to get the dictionary entry )
715 ID. SPACE ( and print it )
723 2DROP ( restore stack )