1 \ ** ficl/softwords/softcore.fr
2 \ ** FICL soft extensions
3 \ ** John Sadler (john_sadler@alum.mit.edu)
8 \ ** Ficl USER variables
9 \ ** See words.c for primitive def'n of USER
11 variable nUser 0 nUser !
13 nUser dup @ user 1 swap +! ;
18 \ EMPTY cleans the parameter stack
19 : empty ( xn..x1 -- ) depth 0 ?do drop loop ;
21 : cell- ( addr -- addr ) [ 1 cells ] literal - ;
22 : -rot ( a b c -- c a b ) 2 -roll ;
26 dup 0< if negate endif ;
27 decimal 32 constant bl
29 : space ( -- ) bl emit ;
31 : spaces ( n -- ) 0 ?do space loop ;
57 false invert constant true
61 : convert char+ 65535 >number drop ; \ cribbed from DPANS A.6.2.0970
62 : erase ( addr u -- ) 0 fill ;
64 : expect ( c-addr u1 -- ) accept span ! ;
65 \ see marker.fr for MARKER implementation
66 : nip ( y x -- x ) swap drop ;
67 : tuck ( y x -- x y x) swap over ;
68 : within ( test low high -- flag ) over - >r - r> u< ;
77 \ ** LOCAL EXT word set
78 \ #if FICL_WANT_LOCALS
79 : locals| ( name...name | -- )
82 dup 0= abort" where's the delimiter??"
87 repeat 2drop 0 0 (local)
90 : local ( name -- ) bl word count (local) ; immediate
92 : 2local ( name -- ) bl word count (2local) ; immediate
94 : end-locals ( -- ) 0 0 (local) ; immediate
98 \ ** TOOLS word set...
103 : i' ( R:w R:w2 -- R:w R:w2 w )
104 r> r> r> dup >r swap >r swap >r ;
106 : .4 ( addr -- addr' )
107 4 0 DO -1 /dump +! /dump @ 0<
108 IF 3 spaces ELSE dup c@ 0 <# # # #> type space THEN
113 ?DO I c@ dup 127 bl within
114 IF drop [char] . THEN emit
118 dup .4 space .4 ." - " .4 space .4 drop 16 /dump +! space .chars ;
120 : dump ( addr u -- ) \ tools dump
121 cr base @ >r hex \ save base on return stack
122 0 ?DO I' I - 16 min /dump !
123 dup 8 u.r ." : " dup .line cr 16 +
127 \ ** SEARCH+EXT words and ficl helpers
128 \ BRAND-WORDLIST is a helper for ficl-named-wordlist. Usage idiom:
129 \ wordlist dup create , brand-wordlist
130 \ gets the name of the word made by create and applies it to the wordlist...
131 : brand-wordlist ( wid -- ) last-word >name drop wid-set-name ;
133 : ficl-named-wordlist \ ( hash-size name -- ) run: ( -- wid )
134 ficl-wordlist dup create , brand-wordlist does> @ ;
139 \ FICL-SET-CURRENT sets the compile wordlist and pushes the previous value
140 : ficl-set-current ( wid -- old-wid )
141 get-current swap set-current ;
143 \ DO_VOCABULARY handles the DOES> part of a VOCABULARY
144 \ When executed, new voc replaces top of search stack
145 : do-vocabulary ( -- )
146 does> @ search> drop >search ;
148 : ficl-vocabulary ( nBuckets name -- )
149 ficl-named-wordlist do-vocabulary ;
151 : vocabulary ( name -- )
154 \ PREVIOUS drops the search order stack
155 : previous ( -- ) search> drop ;
157 \ HIDDEN vocabulary is a place to keep helper words from cluttering the namespace
160 \ <definitions to hide>
162 \ <words that use hidden defs>
163 \ previous ( pop HIDDEN off the search order )
165 1 ficl-named-wordlist hidden
166 : hide hidden dup >search ficl-set-current ;
168 \ ALSO dups the search stack...
170 search> dup >search >search ;
172 \ FORTH drops the top of the search stack and pushes FORTH-WORDLIST
175 forth-wordlist >search ;
177 \ ONLY sets the search order to a default state
181 \ ORDER displays the compile wid and the search order list
183 : list-wid ( wid -- )
184 dup wid-get-name ( wid c-addr u )
188 drop ." (unnamed wid) " x.
191 set-current \ stop hiding words
195 get-order 0 ?do 3 spaces list-wid loop cr
196 ." Compile: " get-current list-wid cr
199 : debug ' debug-xt ; immediate
200 : on-step ." S: " .s cr ;
204 : strdup ( c-addr length -- c-addr2 length2 ior )
205 0 locals| addr2 length c-addr | end-locals
209 c-addr addr2 length move
216 : strcat ( 2:a 2:b -- 2:new-a )
217 0 locals| b-length b-u b-addr a-u a-addr | end-locals
219 b-addr a-addr a-u + b-length move
220 a-addr a-u b-length +
223 : strcpy ( 2:a 2:b -- 2:new-a )
224 locals| b-u b-addr a-u a-addr | end-locals
225 a-addr 0 b-addr b-u strcat
229 dup 0x80 u< if emit exit then \ special case ASCII
232 2/ >r dup 0x3F and 0x80 or swap 6 rshift r>
233 repeat 0x7F xor 2* or
234 begin dup 0x80 u< 0= while emit repeat drop
237 previous \ lose hidden words from search order
239 \ ** E N D S O F T C O R E . F R