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 , , ;
46 BL WORD \ get the next word
47 FIND \ find it in the dictionary
48 >CFA \ get its codeword
53 LATEST @ \ LATEST points to the word being compiled at the moment
54 >CFA \ get the codeword
58 \ CONTROL STRUCTURES ----------------------------------------------------------------------
61 ['] 0BRANCH , \ compile 0BRANCH
62 HERE @ \ save location of the offset on the stack
63 0 , \ compile a dummy offset
68 HERE @ SWAP - \ calculate the offset from the address saved on the stack
69 SWAP ! \ store the offset in the back-filled location
73 ['] BRANCH , \ definite branch to just over the false-part
74 HERE @ \ save location of the offset on the stack
75 0 , \ compile a dummy offset
76 SWAP \ now back-fill the original (IF) offset
77 DUP \ same as for THEN word above
83 HERE @ \ save location on the stack
87 ['] 0BRANCH , \ compile 0BRANCH
88 HERE @ - \ calculate the offset from the address saved on the stack
89 , \ compile the offset here
93 ['] BRANCH , \ compile BRANCH
94 HERE @ - \ calculate the offset back
95 , \ compile the offset here
99 ['] 0BRANCH , \ compile 0BRANCH
100 HERE @ \ save location of the offset2 on the stack
101 0 , \ compile a dummy offset2
105 ['] BRANCH , \ compile BRANCH
106 SWAP \ get the original offset (from BEGIN)
107 HERE @ - , \ and compile it after BRANCH
109 HERE @ SWAP - \ calculate the offset2
110 SWAP ! \ and back-fill it in the original location
114 ['] NOT , \ compile NOT (to reverse the test)
115 [COMPILE] IF \ continue by calling the normal IF
119 ['] LIT , -1 , [COMPILE] IF
121 ['] LIT , HERE @ 0 , ['] >R ,
126 ['] 2DUP , ['] - , [COMPILE] IF
128 ['] LIT , HERE @ 0 , ['] >R ,
138 ['] R> , ['] RDROP , ['] RDROP ,
139 ['] LIT , HERE @ 7 + , ['] DUP , ['] -ROT , ['] - , ['] SWAP , ['] ! ,
150 ['] DUP , \ Store copy of increment
152 ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] + , ['] 2DUP , ['] - ,
153 ['] SWAP , ['] >R , ['] SWAP , ['] >R , ['] SWAP , ['] >R ,
155 \ Condition differently depending on sign of increment
156 ['] SWAP , ['] 0>= , [COMPILE] IF
162 \ Branch back to begining of loop kernel
163 ['] 0BRANCH , HERE @ - ,
166 ['] RDROP , ['] RDROP , ['] RDROP ,
168 \ Record address of loop end for any LEAVEs to use
172 ['] 2DROP , \ Clean up if loop was entirely skipped (?DO)
182 \ CASE ------------------------------------------------------------------------
185 0 \ push 0 to mark the bottom of the stack
189 ['] OVER , \ compile OVER
191 [COMPILE] IF \ compile IF
192 ['] DROP , \ compile DROP
196 [COMPILE] ELSE \ ENDOF is the same as ELSE
200 ['] DROP , \ compile DROP
202 \ keep compiling THEN until we get to our zero marker
211 \ COMMENTS ----------------------------------------------------------------------
214 1 \ allowed nested parens by keeping track of depth
216 >IN @ #TIB @ >= IF \ End of TIB?
217 QUERY \ Get next line
220 TIB >IN @ + @ 1 >IN +!
221 DUP [CHAR] ( = IF \ open paren?
222 DROP \ drop the open paren
225 [CHAR] ) = IF \ close paren?
229 DUP 0= UNTIL \ continue until we reach matching close paren, depth 0
230 DROP \ drop the depth counter
233 ( Some more complicated stack examples, showing the stack notation. )
234 : NIP ( x y -- y ) SWAP DROP ;
235 : TUCK ( x y -- y x y ) DUP -ROT ;
236 : PICK ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
237 1+ ( add one because of 'u' on the stack )
238 PSP@ SWAP - ( add to the stack pointer )
241 : ROLL ( x_u x_u-1... x_0 u -- x_u-1 ... x_0 x_u )
242 1+ DUP PICK SWAP ( x_u x_u-1 ... x_0 x_u u+1 )
243 PSP@ 1- SWAP - PSP@ 2- SWAP
250 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
259 ( Standard words for manipulating BASE. )
260 : DECIMAL ( -- ) 10 BASE ! ;
261 : HEX ( -- ) 16 BASE ! ;
263 ( Compute absolute value. )
286 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
288 ( This is the underlying recursive definition of U. )
290 BASE @ /MOD ( width rem quot )
291 ?DUP IF ( if quotient <> 0 then )
292 RECURSE ( print the quotient )
295 ( print the remainder )
297 [CHAR] 0 ( decimal digits 0..9 )
299 10 - ( hex and beyond digits A..Z )
306 ( This word returns the width (in characters) of an unsigned number in the current base )
307 : UWIDTH ( u -- width )
308 BASE @ / ( rem quot )
309 ?DUP IF ( if quotient <> 0 then )
310 RECURSE 1+ ( return 1+recursive call )
319 UWIDTH ( width u uwidth )
320 ROT ( u uwidth width )
321 SWAP - ( u width-uwidth )
322 ( At this point if the requested width is narrower, we'll have a negative number on the stack.
323 Otherwise the number on the stack is the number of spaces to print. But SPACES won't print
324 a negative number of spaces anyway, so it's now safe to call SPACES ... )
326 ( ... and then call the underlying implementation of U. )
334 1 ( save a flag to remember that it was negative | width n 1 )
343 SWAP ( flag width u )
344 DUP ( flag width u u )
345 UWIDTH ( flag width u uwidth )
346 ROT ( flag u uwidth width )
347 SWAP - ( flag u width-uwidth )
352 IF ( was it negative? print the - character )
362 [CHAR] < EMIT DEPTH U. [CHAR] > EMIT SPACE
375 ( ? fetches the integer at an address and prints it. )
376 : ? ( addr -- ) @ . ;
378 ( c a b WITHIN returns true if a <= c and c < b )
395 ( STRINGS ---------------------------------------------------------------------- )
398 ( Block copy, however, is important and novel: )
399 : CMOVE ( src dest length -- )
405 -ROT OVER - ( length src (dest-src) )
406 -ROT DUP ROT + SWAP ( (dest-src) (src+length) src )
409 I @ ( (dest-src) i@ )
410 OVER I + ( (dest-src) i@ (dest-src+i) )
417 ( C, appends a byte to the current compiled word. )
423 : S" IMMEDIATE ( -- addr len )
424 STATE @ IF ( compiling? )
425 ['] LITSTRING , ( compile LITSTRING )
426 HERE @ ( save the address of the length word on the stack )
427 0 , ( dummy length - we don't know what it is yet )
430 KEY ( get next character of the string )
433 C, ( copy character )
435 DROP ( drop the double quote character at the end )
436 DUP ( get the saved address of the length word )
437 HERE @ SWAP - ( calculate the length )
438 1- ( subtract 1 (because we measured from the start of the length word) )
439 SWAP ! ( and back-fill the length location )
440 ELSE ( immediate mode )
441 HERE @ ( get the start address of the temporary space )
447 OVER C! ( save next character )
448 1+ ( increment address )
450 DROP ( drop the final " character )
451 HERE @ - ( calculate the length )
452 HERE @ ( push the start address )
457 : ." IMMEDIATE ( -- )
458 [COMPILE] S" ( read the string, and compile LITSTRING, etc. )
459 ['] TYPE , ( compile the final TYPE )
467 DROP ( drop the double quote character )
468 EXIT ( return from this function )
474 ( Converts address of counted string into address of
475 start of string and length of string. )
476 : COUNT ( addr1 -- addr2 n )
480 ( CONSTANTS AND VARIABLES ------------------------------------------------------ )
483 BL WORD HEADER ( make dictionary entry (the name follows CONSTANT) )
484 DOCOL , ( append DOCOL (the codeword field of this word) )
485 ['] LIT , ( append the codeword LIT )
486 , ( append the value on the top of the stack )
487 ['] EXIT , ( append the codeword EXIT )
491 HERE +! ( adds n to HERE, after this the old value of HERE is still on the stack )
496 1 CELLS ALLOT ( allocate 1 cell of memory, push the pointer to this memory )
501 BL WORD HEADER ( make the dictionary entry (the name follows VALUE) )
502 DOCOL , ( append DOCOL )
503 ['] LIT , ( append the codeword LIT )
504 , ( append the initial value )
505 ['] EXIT , ( append the codeword EXIT )
508 : TO IMMEDIATE ( n -- )
509 BL WORD ( get the name of the value )
510 FIND ( look it up in the dictionary )
511 >PFA ( get a pointer to the first data field (the 'LIT') )
512 1+ ( increment to point at the value )
513 STATE @ IF ( compiling? )
514 ['] LIT , ( compile LIT )
515 , ( compile the address of the value )
516 ['] ! , ( compile ! )
517 ELSE ( immediate mode )
518 ! ( update it straightaway )
522 ( x +TO VAL adds x to VAL )
524 BL WORD ( get the name of the value )
525 FIND ( look it up in the dictionary )
526 >PFA ( get a pointer to the first data field (the 'LIT') )
527 1+ ( increment to point at the value )
528 STATE @ IF ( compiling? )
529 ['] LIT , ( compile LIT )
530 , ( compile the address of the value )
531 ['] +! , ( compile +! )
532 ELSE ( immediate mode )
533 +! ( update it straightaway )
537 ( Fill u ints, starting at a, with the value b )
549 ( PRINTING THE DICTIONARY ------------------------------------------------------ )
552 1+ ( skip over the link pointer )
553 DUP @ ( get the flags/length byte )
554 F_LENMASK AND ( mask out the flags - just want the length )
557 DUP 0> ( length > 0? )
559 SWAP 1+ ( addr len -- len addr+1 )
560 DUP @ ( len addr -- len addr char | get the next character)
561 DUP 32 >= OVER 127 <= AND IF
562 EMIT ( len addr char -- len addr | and print it)
568 SWAP 1- ( len addr -- addr len-1 | subtract one from length )
570 2DROP ( len addr -- )
574 1+ ( skip over the link pointer )
575 @ ( get the flags/length byte )
576 F_HIDDEN AND ( mask the F_HIDDEN flag and return it (as a truth value) )
579 1+ ( skip over the link pointer )
580 @ ( get the flags/length byte )
581 F_IMMED AND ( mask the F_IMMED flag and return it (as a truth value) )
585 LATEST @ ( start at LATEST dictionary entry )
587 ?DUP ( while link pointer is not null )
589 DUP ?HIDDEN NOT IF ( ignore hidden words )
590 DUP ID. ( but if not hidden, print the word )
593 @ ( dereference the link pointer - go to previous word )
599 ( FORGET ---------------------------------------------------------------------- )
602 BL WORD FIND ( find the word, gets the dictionary entry address )
603 DUP @ LATEST ! ( set LATEST to point to the previous word )
604 HERE ! ( and store HERE with the dictionary address )
607 ( DUMP ------------------------------------------------------------------------ )
612 ( DECOMPILER ------------------------------------------------------------------ )
615 LATEST @ ( start at LATEST dictionary entry )
617 ?DUP ( while link pointer is not null )
619 2DUP SWAP ( cfa curr curr cfa )
620 < IF ( current dictionary entry < cfa? )
621 NIP ( leave curr dictionary entry on the stack )
624 @ ( follow link pointer back )
626 DROP ( restore stack )
627 0 ( sorry, nothing found )
631 BL WORD 2DUP FIND ( find the dictionary entry to decompile )
634 ." Word '" TYPE ." ' not found in dictionary."
640 ( Now we search again, looking for the next word in the dictionary. This gives us
641 the length of the word that we will be decompiling. (Well, mostly it does). )
642 HERE @ ( address of the end of the last compiled word )
643 LATEST @ ( word last curr )
645 2 PICK ( word last curr word )
646 OVER ( word last curr word curr )
647 <> ( word last curr word<>curr? )
648 WHILE ( word last curr )
650 DUP @ ( word curr prev (which becomes: word last curr) )
653 DROP ( at this point, the stack is: start-of-word end-of-word )
654 SWAP ( end-of-word start-of-word )
659 [CHAR] : EMIT SPACE DUP ID. SPACE
660 DUP ?IMMEDIATE IF ." IMMEDIATE " THEN CR
663 \ Variable definition
664 ." Variable " DUP ID. CR
668 \ Constant definition
669 ." Constant " DUP ID. CR
674 ." Primitive or word with unrecognized codeword." CR
678 ( begin the definition with : NAME [IMMEDIATE] )
679 ( [CHAR] : EMIT SPACE DUP ID. SPACE
680 DUP ?IMMEDIATE IF ." IMMEDIATE " THEN CR 4 )
684 >PFA ( get the data address, ie. points after DOCOL | end-of-word start-of-data )
686 ( now we start decompiling until we hit the end of the word )
690 DUP @ ( end start codeword )
693 ['] LIT OF ( is it LIT ? )
694 1+ DUP @ ( get next word which is the integer constant )
697 ['] LITSTRING OF ( is it LITSTRING ? )
698 [CHAR] S EMIT [CHAR] " EMIT SPACE ( print S"<space> )
699 1+ DUP @ ( get the length word )
700 SWAP 1+ SWAP ( end start+1 length )
701 2DUP TYPE ( print the string )
702 [CHAR] " EMIT SPACE ( finish the string with a final quote )
703 + ( end start+1+len, aligned )
704 1- ( because we're about to add 4 below )
706 ['] 0BRANCH OF ( is it 0BRANCH ? )
708 1+ DUP @ ( print the offset )
712 ['] BRANCH OF ( is it BRANCH ? )
714 1+ DUP @ ( print the offset )
718 ['] ['] OF ( is it ['] ? )
720 1+ DUP @ ( get the next codeword )
721 CFA> ( and force it to be printed as a dictionary entry )
724 ['] EXIT OF ( is it EXIT? )
725 ( We expect the last word to be EXIT, and if it is then we don't print it
726 because EXIT is normally implied by ;. EXIT can also appear in the middle
727 of words, and then it needs to be printed. )
728 2DUP ( end start end start )
729 1+ ( end start end start+1 )
730 <> IF ( end start | we're not at the end )
735 DUP ( in the default case we always need to DUP before using )
736 CFA> ( look up the codeword to get the dictionary entry )
737 ID. SPACE ( and print it )
745 2DROP ( restore stack )
749 ( MEMORY ------------------------------------------------------------------ )
751 : UNUSED ( -- cells )