1 ' 1+ , : / /MOD SWAP DROP ;
16 : CELLS ; \ Allow for slightly more portable code
18 : DEPTH PSP@ PSP0 @ - ;
20 : LITERAL IMMEDIATE ' LIT , , ;
22 : ':' [ CHAR : ] LITERAL ;
23 : ';' [ CHAR ; ] LITERAL ;
24 : '(' [ CHAR ( ] LITERAL ;
25 : ')' [ CHAR ) ] LITERAL ;
26 : '<' [ CHAR < ] LITERAL ;
27 : '>' [ CHAR > ] LITERAL ;
28 : '"' [ CHAR " ] LITERAL ;
29 : 'A' [ CHAR A ] LITERAL ;
30 : '0' [ CHAR 0 ] LITERAL ;
31 : '-' [ CHAR - ] LITERAL ;
32 : '.' [ CHAR . ] LITERAL ;
35 WORD \ get the next word
36 FIND \ find it in the dictionary
37 >CFA \ get its codeword
42 LATEST @ \ LATEST points to the word being compiled at the moment
43 >CFA \ get the codeword
47 \ CONTROL STRUCTURES ----------------------------------------------------------------------
50 ' 0BRANCH , \ compile 0BRANCH
51 HERE @ \ save location of the offset on the stack
52 0 , \ compile a dummy offset
57 HERE @ SWAP - \ calculate the offset from the address saved on the stack
58 SWAP ! \ store the offset in the back-filled location
62 ' BRANCH , \ definite branch to just over the false-part
63 HERE @ \ save location of the offset on the stack
64 0 , \ compile a dummy offset
65 SWAP \ now back-fill the original (IF) offset
66 DUP \ same as for THEN word above
72 HERE @ \ save location on the stack
76 ' 0BRANCH , \ compile 0BRANCH
77 HERE @ - \ calculate the offset from the address saved on the stack
78 , \ compile the offset here
82 ' BRANCH , \ compile BRANCH
83 HERE @ - \ calculate the offset back
84 , \ compile the offset here
88 ' 0BRANCH , \ compile 0BRANCH
89 HERE @ \ save location of the offset2 on the stack
90 0 , \ compile a dummy offset2
94 ' BRANCH , \ compile BRANCH
95 SWAP \ get the original offset (from BEGIN)
96 HERE @ - , \ and compile it after BRANCH
98 HERE @ SWAP - \ calculate the offset2
99 SWAP ! \ and back-fill it in the original location
103 ' NOT , \ compile NOT (to reverse the test)
104 [COMPILE] IF \ continue by calling the normal IF
113 ' R> , ' R> , ' 1+ , ' 2DUP , ' - ,
114 ' SWAP , ' >R , ' SWAP , ' >R ,
121 \ COMMENTS ----------------------------------------------------------------------
124 1 \ allowed nested parens by keeping track of depth
126 KEY \ read next character
127 DUP '(' = IF \ open paren?
128 DROP \ drop the open paren
131 ')' = IF \ close paren?
135 DUP 0= UNTIL \ continue until we reach matching close paren, depth 0
136 DROP \ drop the depth counter
139 ( Some more complicated stack examples, showing the stack notation. )
140 : NIP ( x y -- y ) SWAP DROP ;
141 : TUCK ( x y -- y x y ) DUP ROT ;
142 : PICK ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
143 1+ ( add one because of 'u' on the stack )
144 PSP@ SWAP - ( add to the stack pointer )
148 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
151 DUP 0> ( while n > 0 )
153 SPACE ( print a space )
154 1- ( until we count down to 0 )
159 ( Standard words for manipulating BASE. )
160 : DECIMAL ( -- ) 10 BASE ! ;
161 : HEX ( -- ) 16 BASE ! ;
163 ( Compute absolute value. )
170 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
172 ( This is the underlying recursive definition of U. )
174 BASE @ /MOD ( width rem quot )
175 ?DUP IF ( if quotient <> 0 then )
176 RECURSE ( print the quotient )
179 ( print the remainder )
181 '0' ( decimal digits 0..9 )
183 10 - ( hex and beyond digits A..Z )
190 ( This word returns the width (in characters) of an unsigned number in the current base )
191 : UWIDTH ( u -- width )
192 BASE @ / ( rem quot )
193 ?DUP IF ( if quotient <> 0 then )
194 RECURSE 1+ ( return 1+recursive call )
203 UWIDTH ( width u uwidth )
204 -ROT ( u uwidth width )
205 SWAP - ( u width-uwidth )
206 ( At this point if the requested width is narrower, we'll have a negative number on the stack.
207 Otherwise the number on the stack is the number of spaces to print. But SPACES won't print
208 a negative number of spaces anyway, so it's now safe to call SPACES ... )
210 ( ... and then call the underlying implementation of U. )
218 1 ( save a flag to remember that it was negative | width n 1 )
227 SWAP ( flag width u )
228 DUP ( flag width u u )
229 UWIDTH ( flag width u uwidth )
230 -ROT ( flag u uwidth width )
231 SWAP - ( flag u width-uwidth )
236 IF ( was it negative? print the - character )
248 '<' EMIT DEPTH U. '>' EMIT SPACE
259 ( ? fetches the integer at an address and prints it. )
260 : ? ( addr -- ) @ . ;
262 ( c a b WITHIN returns true if a <= c and c < b )