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
113 ' LIT , HERE @ 0 , ' >R ,
123 ' R> , ' RDROP , ' RDROP ,
124 ' LIT , HERE @ 7 + , ' DUP , ' -ROT , ' - , ' SWAP , ' ! ,
135 ' R> , ' SWAP , ' R> , ' SWAP , ' R> , ' SWAP , ' + , ' 2DUP , ' - ,
136 ' SWAP , ' >R , ' SWAP , ' >R , ' SWAP , ' >R ,
139 ' RDROP , ' RDROP , ' RDROP ,
148 \ COMMENTS ----------------------------------------------------------------------
151 1 \ allowed nested parens by keeping track of depth
153 KEY \ read next character
154 DUP '(' = IF \ open paren?
155 DROP \ drop the open paren
158 ')' = IF \ close paren?
162 DUP 0= UNTIL \ continue until we reach matching close paren, depth 0
163 DROP \ drop the depth counter
166 ( Some more complicated stack examples, showing the stack notation. )
167 : NIP ( x y -- y ) SWAP DROP ;
168 : TUCK ( x y -- y x y ) DUP -ROT ;
169 : PICK ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
170 1+ ( add one because of 'u' on the stack )
171 PSP@ SWAP - ( add to the stack pointer )
175 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
182 ( Standard words for manipulating BASE. )
183 : DECIMAL ( -- ) 10 BASE ! ;
184 : HEX ( -- ) 16 BASE ! ;
186 ( Compute absolute value. )
209 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
211 ( This is the underlying recursive definition of U. )
213 BASE @ /MOD ( width rem quot )
214 ?DUP IF ( if quotient <> 0 then )
215 RECURSE ( print the quotient )
218 ( print the remainder )
220 '0' ( decimal digits 0..9 )
222 10 - ( hex and beyond digits A..Z )
229 ( This word returns the width (in characters) of an unsigned number in the current base )
230 : UWIDTH ( u -- width )
231 BASE @ / ( rem quot )
232 ?DUP IF ( if quotient <> 0 then )
233 RECURSE 1+ ( return 1+recursive call )
242 UWIDTH ( width u uwidth )
243 ROT ( u uwidth width )
244 SWAP - ( u width-uwidth )
245 ( At this point if the requested width is narrower, we'll have a negative number on the stack.
246 Otherwise the number on the stack is the number of spaces to print. But SPACES won't print
247 a negative number of spaces anyway, so it's now safe to call SPACES ... )
249 ( ... and then call the underlying implementation of U. )
257 1 ( save a flag to remember that it was negative | width n 1 )
266 SWAP ( flag width u )
267 DUP ( flag width u u )
268 UWIDTH ( flag width u uwidth )
269 ROT ( flag u uwidth width )
270 SWAP - ( flag u width-uwidth )
275 IF ( was it negative? print the - character )
285 '<' EMIT DEPTH U. '>' EMIT SPACE
298 ( ? fetches the integer at an address and prints it. )
299 : ? ( addr -- ) @ . ;
301 ( c a b WITHIN returns true if a <= c and c < b )
317 : ROLL ( x_u x_u-1... x_0 u -- x_u-1 ... x_0 x_u )
318 1+ DUP PICK SWAP ( x_u x_u-1 ... x_0 x_u u+1 )
319 PSP@ 1- SWAP - PSP@ 2- SWAP
326 include ../examples/mandelbrot.4th