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