Finished ROLL
[forth.jl.git] / src / lib.4th
1 : / /MOD SWAP DROP ;
2 : MOD /MOD DROP ;
3 : */ * / ;
4
5 : NEGATE 0 SWAP - ;
6
7 : TRUE -1 ;
8 : FALSE 0 ;
9 : NOT 0= ;
10
11 : CELLS ; \ Allow for slightly more portable code
12
13 : DEPTH PSP@ PSP0 @ - ;
14
15 : '\n' 10 ;
16 : BL 32 ;
17
18 : LITERAL IMMEDIATE ' LIT , , ;
19
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 ;
31
32 : CR '\n' emit ;
33 : SPACE BL emit ;
34
35 : [COMPILE] IMMEDIATE
36         WORD            \ get the next word
37         FIND            \ find it in the dictionary
38         >CFA            \ get its codeword
39         ,               \ and compile that
40 ;
41
42 : RECURSE IMMEDIATE
43         LATEST @        \ LATEST points to the word being compiled at the moment
44         >CFA            \ get the codeword
45         ,               \ compile it
46 ;
47
48 : DEBUGON TRUE DEBUG ! ;
49 : DEBUGOFF FALSE DEBUG ! ;
50
51 \ CONTROL STRUCTURES ----------------------------------------------------------------------
52
53 : IF IMMEDIATE
54         ' 0BRANCH ,     \ compile 0BRANCH
55         HERE @          \ save location of the offset on the stack
56         0 ,             \ compile a dummy offset
57 ;
58
59 : THEN IMMEDIATE
60         DUP
61         HERE @ SWAP -   \ calculate the offset from the address saved on the stack
62         SWAP !          \ store the offset in the back-filled location
63 ;
64
65 : ELSE IMMEDIATE
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
71         HERE @ SWAP -
72         SWAP !
73 ;
74
75 : BEGIN IMMEDIATE
76         HERE @          \ save location on the stack
77 ;
78
79 : UNTIL IMMEDIATE
80         ' 0BRANCH ,     \ compile 0BRANCH
81         HERE @ -        \ calculate the offset from the address saved on the stack
82         ,               \ compile the offset here
83 ;
84
85 : AGAIN IMMEDIATE
86         ' BRANCH ,      \ compile BRANCH
87         HERE @ -        \ calculate the offset back
88         ,               \ compile the offset here
89 ;
90
91 : WHILE IMMEDIATE
92         ' 0BRANCH ,     \ compile 0BRANCH
93         HERE @          \ save location of the offset2 on the stack
94         0 ,             \ compile a dummy offset2
95 ;
96
97 : REPEAT IMMEDIATE
98         ' BRANCH ,      \ compile BRANCH
99         SWAP            \ get the original offset (from BEGIN)
100         HERE @ - ,      \ and compile it after BRANCH
101         DUP
102         HERE @ SWAP -   \ calculate the offset2
103         SWAP !          \ and back-fill it in the original location
104 ;
105
106 : UNLESS IMMEDIATE
107         ' NOT ,         \ compile NOT (to reverse the test)
108         [COMPILE] IF    \ continue by calling the normal IF
109 ;
110
111 : DO IMMEDIATE
112         ' >R , ' >R ,
113         ' LIT , HERE @ 0 , ' >R ,
114         HERE @
115 ;
116
117 : I RSP@ 3 - @ ;
118
119 : ?LEAVE IMMEDIATE
120         ' 0BRANCH , 13 ,
121         ' R> , ' RDROP , ' RDROP ,
122         ' LIT ,  HERE @ 7 + , ' DUP , ' -ROT , ' - , ' SWAP , ' ! ,
123         ' BRANCH ,
124         0 ,
125 ;
126
127 : LEAVE IMMEDIATE
128         ' LIT , -1 ,
129         [COMPILE] ?LEAVE
130 ;
131
132 : +LOOP IMMEDIATE
133         ' R> , ' SWAP , ' R> , ' SWAP , ' R> , ' SWAP , ' + , ' 2DUP , ' - ,
134         ' SWAP , ' >R , ' SWAP , ' >R , ' SWAP , ' >R ,
135         ' 0<= , ' 0BRANCH ,
136         HERE @ - ,
137         ' RDROP , ' RDROP , ' RDROP ,
138         HERE @ SWAP !
139 ;
140
141 : LOOP IMMEDIATE
142         ' LIT , 1 ,
143         [COMPILE] +LOOP
144 ;
145
146 \ COMMENTS ----------------------------------------------------------------------
147
148 : ( IMMEDIATE
149         1               \ allowed nested parens by keeping track of depth
150         BEGIN
151                 KEY             \ read next character
152                 DUP '(' = IF    \ open paren?
153                         DROP            \ drop the open paren
154                         1+              \ depth increases
155                 ELSE
156                         ')' = IF        \ close paren?
157                                 1-              \ depth decreases
158                         THEN
159                 THEN
160         DUP 0= UNTIL            \ continue until we reach matching close paren, depth 0
161         DROP            \ drop the depth counter
162 ;
163
164 ( Some more complicated stack examples, showing the stack notation. )
165 : NIP ( x y -- y ) SWAP DROP ;
166 : TUCK ( x y -- y x y ) DUP -ROT ;
167 : PICK ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
168         1+              ( add one because of 'u' on the stack )
169         PSP@ SWAP -     ( add to the stack pointer )
170         @               ( and fetch )
171 ;
172
173 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
174 : SPACES        ( n -- )
175         0 DO
176             SPACE
177         LOOP
178 ;
179
180 ( Standard words for manipulating BASE. )
181 : DECIMAL ( -- ) 10 BASE ! ;
182 : HEX ( -- ) 16 BASE ! ;
183
184 ( Compute absolute value. )
185 : ABS           ( n -- |n| )
186         dup 0< if
187                 negate
188         then
189 ;
190
191 : MAX           ( n m -- max )
192         2dup - 0< if
193                 swap drop
194         else
195                 drop
196         then
197 ;
198
199 : MIN           ( n m -- max )
200         2dup - 0> if
201                 swap drop
202         else
203                 drop
204         then
205 ;
206
207 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
208
209 ( This is the underlying recursive definition of U. )
210 : U.            ( u -- )
211         BASE @ /MOD     ( width rem quot )
212         ?DUP IF                 ( if quotient <> 0 then )
213                 RECURSE         ( print the quotient )
214         THEN
215
216         ( print the remainder )
217         DUP 10 < IF
218                 '0'             ( decimal digits 0..9 )
219         ELSE
220                 10 -            ( hex and beyond digits A..Z )
221                 'A'
222         THEN
223         +
224         EMIT
225 ;
226
227 ( This word returns the width (in characters) of an unsigned number in the current base )
228 : UWIDTH        ( u -- width )
229         BASE @ /        ( rem quot )
230         ?DUP IF         ( if quotient <> 0 then )
231                 RECURSE 1+      ( return 1+recursive call )
232         ELSE
233                 1               ( return 1 )
234         THEN
235 ;
236
237 : U.R           ( u width -- )
238         SWAP            ( width u )
239         DUP             ( width u u )
240         UWIDTH          ( width u uwidth )
241         ROT            ( u uwidth width )
242         SWAP -          ( u width-uwidth )
243         ( At this point if the requested width is narrower, we'll have a negative number on the stack.
244           Otherwise the number on the stack is the number of spaces to print.  But SPACES won't print
245           a negative number of spaces anyway, so it's now safe to call SPACES ... )
246         SPACES
247         ( ... and then call the underlying implementation of U. )
248         U.
249 ;
250
251 : .R            ( n width -- )
252         SWAP            ( width n )
253         DUP 0< IF
254                 NEGATE          ( width u )
255                 1               ( save a flag to remember that it was negative | width n 1 )
256                 -ROT             ( 1 width u )
257                 SWAP            ( 1 u width )
258                 1-              ( 1 u width-1 )
259         ELSE
260                 0               ( width u 0 )
261                 -ROT             ( 0 width u )
262                 SWAP            ( 0 u width )
263         THEN
264         SWAP            ( flag width u )
265         DUP             ( flag width u u )
266         UWIDTH          ( flag width u uwidth )
267         ROT            ( flag u uwidth width )
268         SWAP -          ( flag u width-uwidth )
269
270         SPACES          ( flag u )
271         SWAP            ( u flag )
272
273         IF                      ( was it negative? print the - character )
274                 '-' EMIT
275         THEN
276
277         U.
278 ;
279
280 : . 0 .R SPACE ;
281
282 : .S            ( -- )
283         '<' EMIT DEPTH U. '>' EMIT SPACE
284         PSP0 @ 1+
285         BEGIN
286                 DUP PSP@ 2 - <=
287         WHILE
288                 DUP @ .
289                 1+
290         REPEAT
291         DROP
292 ;
293
294 : U. U. SPACE ;
295
296 ( ? fetches the integer at an address and prints it. )
297 : ? ( addr -- ) @ . ;
298
299 ( c a b WITHIN returns true if a <= c and c < b )
300 : WITHIN
301         -ROT             ( b c a )
302         OVER            ( b c a c )
303         <= IF
304                 > IF            ( b c -- )
305                         TRUE
306                 ELSE
307                         FALSE
308                 THEN
309         ELSE
310                 2DROP           ( b c -- )
311                 FALSE
312         THEN
313 ;
314
315 : ROLL ( x_u x_u-1... x_0 u -- x_u-1 ... x_0 x_u )
316         1+ DUP PICK SWAP    ( x_u x_u-1 ... x_0 x_u u+1 )
317         PSP@ 1- SWAP - PSP@ 2- SWAP
318         DO
319             i 1+ @ i !
320         LOOP
321         SWAP DROP
322 ;
323
324