DO LOOP LEAVE working
[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         ' R> , ' RDROP , ' RDROP ,
121         ' LIT ,  HERE @ 7 + , ' DUP , ' ROT , ' - , ' SWAP , ' ! ,
122         ' BRANCH ,
123         0 ,
124 ;
125
126 : LOOP IMMEDIATE
127         ' R> , ' R> , ' R> , ' 1+ , ' 2DUP , ' - ,
128         ' SWAP , ' >R , ' SWAP , ' >R , ' SWAP , ' >R ,
129         ' 0<= , ' 0BRANCH ,
130         HERE @ - ,
131         ' RDROP , ' RDROP , ' RDROP ,
132         HERE @ SWAP !
133 ;
134
135 : lt 10 0 do leave loop ;
136
137
138 \ COMMENTS ----------------------------------------------------------------------
139
140 : ( IMMEDIATE
141         1               \ allowed nested parens by keeping track of depth
142         BEGIN
143                 KEY             \ read next character
144                 DUP '(' = IF    \ open paren?
145                         DROP            \ drop the open paren
146                         1+              \ depth increases
147                 ELSE
148                         ')' = IF        \ close paren?
149                                 1-              \ depth decreases
150                         THEN
151                 THEN
152         DUP 0= UNTIL            \ continue until we reach matching close paren, depth 0
153         DROP            \ drop the depth counter
154 ;
155
156 ( Some more complicated stack examples, showing the stack notation. )
157 : NIP ( x y -- y ) SWAP DROP ;
158 : TUCK ( x y -- y x y ) DUP ROT ;
159 : PICK ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
160         1+              ( add one because of 'u' on the stack )
161         PSP@ SWAP -     ( add to the stack pointer )
162         @               ( and fetch )
163 ;
164
165
166 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
167 : SPACES        ( n -- )
168         BEGIN
169                 DUP 0>          ( while n > 0 )
170         WHILE
171                 SPACE           ( print a space )
172                 1-              ( until we count down to 0 )
173         REPEAT
174         DROP
175 ;
176
177 ( Standard words for manipulating BASE. )
178 : DECIMAL ( -- ) 10 BASE ! ;
179 : HEX ( -- ) 16 BASE ! ;
180
181 ( Compute absolute value. )
182 : ABS           ( n -- |n| )
183         dup 0< if
184                 negate
185         then
186 ;
187
188 : MAX           ( n m -- max )
189         2dup - 0< if
190                 swap drop
191         else
192                 drop
193         then
194 ;
195
196 : MIN           ( n m -- max )
197         2dup - 0> if
198                 swap drop
199         else
200                 drop
201         then
202 ;
203
204 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
205
206 ( This is the underlying recursive definition of U. )
207 : U.            ( u -- )
208         BASE @ /MOD     ( width rem quot )
209         ?DUP IF                 ( if quotient <> 0 then )
210                 RECURSE         ( print the quotient )
211         THEN
212
213         ( print the remainder )
214         DUP 10 < IF
215                 '0'             ( decimal digits 0..9 )
216         ELSE
217                 10 -            ( hex and beyond digits A..Z )
218                 'A'
219         THEN
220         +
221         EMIT
222 ;
223
224 ( This word returns the width (in characters) of an unsigned number in the current base )
225 : UWIDTH        ( u -- width )
226         BASE @ /        ( rem quot )
227         ?DUP IF         ( if quotient <> 0 then )
228                 RECURSE 1+      ( return 1+recursive call )
229         ELSE
230                 1               ( return 1 )
231         THEN
232 ;
233
234 : U.R           ( u width -- )
235         SWAP            ( width u )
236         DUP             ( width u u )
237         UWIDTH          ( width u uwidth )
238         -ROT            ( u uwidth width )
239         SWAP -          ( u width-uwidth )
240         ( At this point if the requested width is narrower, we'll have a negative number on the stack.
241           Otherwise the number on the stack is the number of spaces to print.  But SPACES won't print
242           a negative number of spaces anyway, so it's now safe to call SPACES ... )
243         SPACES
244         ( ... and then call the underlying implementation of U. )
245         U.
246 ;
247
248 : .R            ( n width -- )
249         SWAP            ( width n )
250         DUP 0< IF
251                 NEGATE          ( width u )
252                 1               ( save a flag to remember that it was negative | width n 1 )
253                 ROT             ( 1 width u )
254                 SWAP            ( 1 u width )
255                 1-              ( 1 u width-1 )
256         ELSE
257                 0               ( width u 0 )
258                 ROT             ( 0 width u )
259                 SWAP            ( 0 u width )
260         THEN
261         SWAP            ( flag width u )
262         DUP             ( flag width u u )
263         UWIDTH          ( flag width u uwidth )
264         -ROT            ( flag u uwidth width )
265         SWAP -          ( flag u width-uwidth )
266
267         SPACES          ( flag u )
268         SWAP            ( u flag )
269
270         IF                      ( was it negative? print the - character )
271                 '-' EMIT
272         THEN
273
274         U.
275 ;
276
277 : . 0 .R SPACE ;
278
279 : .S            ( -- )
280         '<' EMIT DEPTH U. '>' EMIT SPACE
281         PSP0 @ 1+
282         BEGIN
283                 DUP PSP@ 2 - <=
284         WHILE
285                 DUP @ .
286                 1+
287         REPEAT
288         DROP
289 ;
290
291 : U. U. SPACE ;
292
293 ( ? fetches the integer at an address and prints it. )
294 : ? ( addr -- ) @ . ;
295
296 ( c a b WITHIN returns true if a <= c and c < b )
297 : WITHIN
298         ROT             ( b c a )
299         OVER            ( b c a c )
300         <= IF
301                 > IF            ( b c -- )
302                         TRUE
303                 ELSE
304                         FALSE
305                 THEN
306         ELSE
307                 2DROP           ( b c -- )
308                 FALSE
309         THEN
310 ;
311