Added LOOP+ and expressed LOOP as a special case.
[forth.jl.git] / src / lib.4th
1 : / /MOD SWAP DROP ;
2 : MOD /MOD DROP ;
3 : */ * / ;
4
5 : '\n' 10 ;
6 : BL 32 ;
7
8 : CR '\n' emit ;
9 : SPACE BL emit ;
10
11 : NEGATE 0 swap - ;
12
13 : TRUE -1 ;
14 : FALSE 0 ;
15 : NOT 0= ;
16
17 : CELLS ; \ Allow for slightly more portable code
18
19 : DEPTH PSP@ PSP0 @ - ;
20
21 : LITERAL IMMEDIATE ' LIT , , ;
22
23 : ':' [ CHAR : ] LITERAL ;
24 : ';' [ CHAR ; ] LITERAL ;
25 : '(' [ CHAR ( ] LITERAL ;
26 : ')' [ CHAR ) ] LITERAL ;
27 : '<' [ CHAR < ] LITERAL ;
28 : '>' [ CHAR > ] LITERAL ;
29 : '"' [ CHAR " ] LITERAL ;
30 : 'A' [ CHAR A ] LITERAL ;
31 : '0' [ CHAR 0 ] LITERAL ;
32 : '-' [ CHAR - ] LITERAL ;
33 : '.' [ CHAR . ] LITERAL ;
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 -- m)
174         dup 0< if
175                 negate
176         then
177 ;
178
179 ( PRINTING NUMBERS ---------------------------------------------------------------------- )
180
181 ( This is the underlying recursive definition of U. )
182 : U.            ( u -- )
183         BASE @ /MOD     ( width rem quot )
184         ?DUP IF                 ( if quotient <> 0 then )
185                 RECURSE         ( print the quotient )
186         THEN
187
188         ( print the remainder )
189         DUP 10 < IF
190                 '0'             ( decimal digits 0..9 )
191         ELSE
192                 10 -            ( hex and beyond digits A..Z )
193                 'A'
194         THEN
195         +
196         EMIT
197 ;
198
199 ( This word returns the width (in characters) of an unsigned number in the current base )
200 : UWIDTH        ( u -- width )
201         BASE @ /        ( rem quot )
202         ?DUP IF         ( if quotient <> 0 then )
203                 RECURSE 1+      ( return 1+recursive call )
204         ELSE
205                 1               ( return 1 )
206         THEN
207 ;
208
209 : U.R           ( u width -- )
210         SWAP            ( width u )
211         DUP             ( width u u )
212         UWIDTH          ( width u uwidth )
213         -ROT            ( u uwidth width )
214         SWAP -          ( u width-uwidth )
215         ( At this point if the requested width is narrower, we'll have a negative number on the stack.
216           Otherwise the number on the stack is the number of spaces to print.  But SPACES won't print
217           a negative number of spaces anyway, so it's now safe to call SPACES ... )
218         SPACES
219         ( ... and then call the underlying implementation of U. )
220         U.
221 ;
222
223 : .R            ( n width -- )
224         SWAP            ( width n )
225         DUP 0< IF
226                 NEGATE          ( width u )
227                 1               ( save a flag to remember that it was negative | width n 1 )
228                 ROT             ( 1 width u )
229                 SWAP            ( 1 u width )
230                 1-              ( 1 u width-1 )
231         ELSE
232                 0               ( width u 0 )
233                 ROT             ( 0 width u )
234                 SWAP            ( 0 u width )
235         THEN
236         SWAP            ( flag width u )
237         DUP             ( flag width u u )
238         UWIDTH          ( flag width u uwidth )
239         -ROT            ( flag u uwidth width )
240         SWAP -          ( flag u width-uwidth )
241
242         SPACES          ( flag u )
243         SWAP            ( u flag )
244
245         IF                      ( was it negative? print the - character )
246                 '-' EMIT
247         THEN
248
249         U.
250 ;
251
252 : . 0 .R SPACE ;
253
254 : .S            ( -- )
255         '<' EMIT DEPTH U. '>' EMIT SPACE
256         PSP0 @ 1+
257         BEGIN
258                 DUP PSP@ 2 - <=
259         WHILE
260                 DUP @ .
261                 1+
262         REPEAT
263         DROP
264 ;
265
266 : U. U. SPACE ;
267
268 ( ? fetches the integer at an address and prints it. )
269 : ? ( addr -- ) @ . ;
270
271 ( c a b WITHIN returns true if a <= c and c < b )
272 : WITHIN
273         ROT             ( b c a )
274         OVER            ( b c a c )
275         <= IF
276                 > IF            ( b c -- )
277                         TRUE
278                 ELSE
279                         FALSE
280                 THEN
281         ELSE
282                 2DROP           ( b c -- )
283                 FALSE
284         THEN
285 ;
286