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