Fixed ROT/-ROT, added LEAVE? and LOOP+
[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
174 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
175 : SPACES        ( n -- )
176         BEGIN
177                 DUP 0>          ( while n > 0 )
178         WHILE
179                 SPACE           ( print a space )
180                 1-              ( until we count down to 0 )
181         REPEAT
182         DROP
183 ;
184
185 ( Standard words for manipulating BASE. )
186 : DECIMAL ( -- ) 10 BASE ! ;
187 : HEX ( -- ) 16 BASE ! ;
188
189 ( Compute absolute value. )
190 : ABS           ( n -- |n| )
191         dup 0< if
192                 negate
193         then
194 ;
195
196 : MAX           ( n m -- max )
197         2dup - 0< if
198                 swap drop
199         else
200                 drop
201         then
202 ;
203
204 : MIN           ( n m -- max )
205         2dup - 0> if
206                 swap drop
207         else
208                 drop
209         then
210 ;
211
212 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
213
214 ( This is the underlying recursive definition of U. )
215 : U.            ( u -- )
216         BASE @ /MOD     ( width rem quot )
217         ?DUP IF                 ( if quotient <> 0 then )
218                 RECURSE         ( print the quotient )
219         THEN
220
221         ( print the remainder )
222         DUP 10 < IF
223                 '0'             ( decimal digits 0..9 )
224         ELSE
225                 10 -            ( hex and beyond digits A..Z )
226                 'A'
227         THEN
228         +
229         EMIT
230 ;
231
232 ( This word returns the width (in characters) of an unsigned number in the current base )
233 : UWIDTH        ( u -- width )
234         BASE @ /        ( rem quot )
235         ?DUP IF         ( if quotient <> 0 then )
236                 RECURSE 1+      ( return 1+recursive call )
237         ELSE
238                 1               ( return 1 )
239         THEN
240 ;
241
242 : U.R           ( u width -- )
243         SWAP            ( width u )
244         DUP             ( width u u )
245         UWIDTH          ( width u uwidth )
246         ROT            ( u uwidth width )
247         SWAP -          ( u width-uwidth )
248         ( At this point if the requested width is narrower, we'll have a negative number on the stack.
249           Otherwise the number on the stack is the number of spaces to print.  But SPACES won't print
250           a negative number of spaces anyway, so it's now safe to call SPACES ... )
251         SPACES
252         ( ... and then call the underlying implementation of U. )
253         U.
254 ;
255
256 : .R            ( n width -- )
257         SWAP            ( width n )
258         DUP 0< IF
259                 NEGATE          ( width u )
260                 1               ( save a flag to remember that it was negative | width n 1 )
261                 -ROT             ( 1 width u )
262                 SWAP            ( 1 u width )
263                 1-              ( 1 u width-1 )
264         ELSE
265                 0               ( width u 0 )
266                 -ROT             ( 0 width u )
267                 SWAP            ( 0 u width )
268         THEN
269         SWAP            ( flag width u )
270         DUP             ( flag width u u )
271         UWIDTH          ( flag width u uwidth )
272         ROT            ( flag u uwidth width )
273         SWAP -          ( flag u width-uwidth )
274
275         SPACES          ( flag u )
276         SWAP            ( u flag )
277
278         IF                      ( was it negative? print the - character )
279                 '-' EMIT
280         THEN
281
282         U.
283 ;
284
285 : . 0 .R SPACE ;
286
287 : .S            ( -- )
288         '<' EMIT DEPTH U. '>' EMIT SPACE
289         PSP0 @ 1+
290         BEGIN
291                 DUP PSP@ 2 - <=
292         WHILE
293                 DUP @ .
294                 1+
295         REPEAT
296         DROP
297 ;
298
299 : U. U. SPACE ;
300
301 ( ? fetches the integer at an address and prints it. )
302 : ? ( addr -- ) @ . ;
303
304 ( c a b WITHIN returns true if a <= c and c < b )
305 : WITHIN
306         -ROT             ( b c a )
307         OVER            ( b c a c )
308         <= IF
309                 > IF            ( b c -- )
310                         TRUE
311                 ELSE
312                         FALSE
313                 THEN
314         ELSE
315                 2DROP           ( b c -- )
316                 FALSE
317         THEN
318 ;
319