17 : CELLS ; \ Allow for slightly more portable code
19 : DEPTH PSP@ PSP0 @ - ;
21 : LITERAL IMMEDIATE ' LIT , , ;
23 : ':' [ CHAR : ] LITERAL ;
24 : ';' [ CHAR ; ] LITERAL ;
25 : '(' [ CHAR ( ] LITERAL ;
26 : ')' [ CHAR ) ] LITERAL ;
27 : '<' [ CHAR < ] LITERAL ;
28 : '>' [ CHAR > ] LITERAL ;
29 : '"' [ CHAR " ] LITERAL ;
30 : 'A' [ CHAR A ] LITERAL ;
31 : '0' [ CHAR 0 ] LITERAL ;
32 : '-' [ CHAR - ] LITERAL ;
33 : '.' [ 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 \ CONTROL STRUCTURES ----------------------------------------------------------------------
51 ' 0BRANCH , \ compile 0BRANCH
52 HERE @ \ save location of the offset on the stack
53 0 , \ compile a dummy offset
58 HERE @ SWAP - \ calculate the offset from the address saved on the stack
59 SWAP ! \ store the offset in the back-filled location
63 ' BRANCH , \ definite branch to just over the false-part
64 HERE @ \ save location of the offset on the stack
65 0 , \ compile a dummy offset
66 SWAP \ now back-fill the original (IF) offset
67 DUP \ same as for THEN word above
73 HERE @ \ save location on the stack
77 ' 0BRANCH , \ compile 0BRANCH
78 HERE @ - \ calculate the offset from the address saved on the stack
79 , \ compile the offset here
83 ' BRANCH , \ compile BRANCH
84 HERE @ - \ calculate the offset back
85 , \ compile the offset here
89 ' 0BRANCH , \ compile 0BRANCH
90 HERE @ \ save location of the offset2 on the stack
91 0 , \ compile a dummy offset2
95 ' BRANCH , \ compile BRANCH
96 SWAP \ get the original offset (from BEGIN)
97 HERE @ - , \ and compile it after BRANCH
99 HERE @ SWAP - \ calculate the offset2
100 SWAP ! \ and back-fill it in the original location
104 ' NOT , \ compile NOT (to reverse the test)
105 [COMPILE] IF \ continue by calling the normal IF
116 ' R> , ' R> , ' 1+ , ' 2DUP , ' - ,
117 ' SWAP , ' >R , ' SWAP , ' >R ,
124 \ COMMENTS ----------------------------------------------------------------------
127 1 \ allowed nested parens by keeping track of depth
129 KEY \ read next character
130 DUP '(' = IF \ open paren?
131 DROP \ drop the open paren
134 ')' = IF \ close paren?
138 DUP 0= UNTIL \ continue until we reach matching close paren, depth 0
139 DROP \ drop the depth counter
142 ( Some more complicated stack examples, showing the stack notation. )
143 : NIP ( x y -- y ) SWAP DROP ;
144 : TUCK ( x y -- y x y ) DUP ROT ;
145 : PICK ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
146 1+ ( add one because of 'u' on the stack )
147 PSP@ SWAP - ( add to the stack pointer )
152 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
155 DUP 0> ( while n > 0 )
157 SPACE ( print a space )
158 1- ( until we count down to 0 )
163 ( Standard words for manipulating BASE. )
164 : DECIMAL ( -- ) 10 BASE ! ;
165 : HEX ( -- ) 16 BASE ! ;
167 ( Compute absolute value. )
174 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
176 ( This is the underlying recursive definition of U. )
178 BASE @ /MOD ( width rem quot )
179 ?DUP IF ( if quotient <> 0 then )
180 RECURSE ( print the quotient )
183 ( print the remainder )
185 '0' ( decimal digits 0..9 )
187 10 - ( hex and beyond digits A..Z )
194 ( This word returns the width (in characters) of an unsigned number in the current base )
195 : UWIDTH ( u -- width )
196 BASE @ / ( rem quot )
197 ?DUP IF ( if quotient <> 0 then )
198 RECURSE 1+ ( return 1+recursive call )
207 UWIDTH ( width u uwidth )
208 -ROT ( u uwidth width )
209 SWAP - ( u width-uwidth )
210 ( At this point if the requested width is narrower, we'll have a negative number on the stack.
211 Otherwise the number on the stack is the number of spaces to print. But SPACES won't print
212 a negative number of spaces anyway, so it's now safe to call SPACES ... )
214 ( ... and then call the underlying implementation of U. )
222 1 ( save a flag to remember that it was negative | width n 1 )
231 SWAP ( flag width u )
232 DUP ( flag width u u )
233 UWIDTH ( flag width u uwidth )
234 -ROT ( flag u uwidth width )
235 SWAP - ( flag u width-uwidth )
240 IF ( was it negative? print the - character )
250 '<' EMIT DEPTH U. '>' EMIT SPACE
263 ( ? fetches the integer at an address and prints it. )
264 : ? ( addr -- ) @ . ;
266 ( c a b WITHIN returns true if a <= c and c < b )