1 \ ** ficl/softwords/softcore.fr
2 \ ** FICL soft extensions
3 \ ** John Sadler (john_sadler@alum.mit.edu)
7 \ ** Ficl USER variables
8 \ ** See words.c for primitive def'n of USER
10 variable nUser 0 nUser !
12 nUser dup @ user 1 swap +! ;
17 \ EMPTY cleans the parameter stack
18 : empty ( xn..x1 -- ) depth 0 ?do drop loop ;
20 : cell- ( addr -- addr ) [ 1 cells ] literal - ;
21 : -rot ( a b c -- c a b ) 2 -roll ;
25 dup 0< if negate endif ;
26 decimal 32 constant bl
28 : space ( -- ) bl emit ;
30 : spaces ( n -- ) 0 ?do space loop ;
56 false invert constant true
60 : convert char+ 65535 >number drop ; \ cribbed from DPANS A.6.2.0970
61 : erase ( addr u -- ) 0 fill ;
63 : expect ( c-addr u1 -- ) accept span ! ;
64 \ see marker.fr for MARKER implementation
65 : nip ( y x -- x ) swap drop ;
66 : tuck ( y x -- x y x) swap over ;
67 : within ( test low high -- flag ) over - >r - r> u< ;
76 \ ** LOCAL EXT word set
77 \ #if FICL_WANT_LOCALS
78 : locals| ( name...name | -- )
81 dup 0= abort" where's the delimiter??"
86 repeat 2drop 0 0 (local)
89 : local ( name -- ) bl word count (local) ; immediate
91 : 2local ( name -- ) bl word count (2local) ; immediate
93 : end-locals ( -- ) 0 0 (local) ; immediate
97 \ ** TOOLS word set...
102 : i' ( R:w R:w2 -- R:w R:w2 w )
103 r> r> r> dup >r swap >r swap >r ;
105 : .4 ( addr -- addr' )
106 4 0 DO -1 /dump +! /dump @ 0<
107 IF 3 spaces ELSE dup c@ 0 <# # # #> type space THEN
112 ?DO I c@ dup 127 bl within
113 IF drop [char] . THEN emit
117 dup .4 space .4 ." - " .4 space .4 drop 16 /dump +! space .chars ;
119 : dump ( addr u -- ) \ tools dump
120 cr base @ >r hex \ save base on return stack
121 0 ?DO I' I - 16 min /dump !
122 dup 8 u.r ." : " dup .line cr 16 +
126 \ ** SEARCH+EXT words and ficl helpers
127 \ BRAND-WORDLIST is a helper for ficl-named-wordlist. Usage idiom:
128 \ wordlist dup create , brand-wordlist
129 \ gets the name of the word made by create and applies it to the wordlist...
130 : brand-wordlist ( wid -- ) last-word >name drop wid-set-name ;
132 : ficl-named-wordlist \ ( hash-size name -- ) run: ( -- wid )
133 ficl-wordlist dup create , brand-wordlist does> @ ;
138 \ FICL-SET-CURRENT sets the compile wordlist and pushes the previous value
139 : ficl-set-current ( wid -- old-wid )
140 get-current swap set-current ;
142 \ DO_VOCABULARY handles the DOES> part of a VOCABULARY
143 \ When executed, new voc replaces top of search stack
144 : do-vocabulary ( -- )
145 does> @ search> drop >search ;
147 : ficl-vocabulary ( nBuckets name -- )
148 ficl-named-wordlist do-vocabulary ;
150 : vocabulary ( name -- )
153 \ PREVIOUS drops the search order stack
154 : previous ( -- ) search> drop ;
156 \ HIDDEN vocabulary is a place to keep helper words from cluttering the namespace
159 \ <definitions to hide>
161 \ <words that use hidden defs>
162 \ previous ( pop HIDDEN off the search order )
164 1 ficl-named-wordlist hidden
165 : hide hidden dup >search ficl-set-current ;
167 \ ALSO dups the search stack...
169 search> dup >search >search ;
171 \ FORTH drops the top of the search stack and pushes FORTH-WORDLIST
174 forth-wordlist >search ;
176 \ ONLY sets the search order to a default state
180 \ ORDER displays the compile wid and the search order list
182 : list-wid ( wid -- )
183 dup wid-get-name ( wid c-addr u )
187 drop ." (unnamed wid) " x.
190 set-current \ stop hiding words
194 get-order 0 ?do 3 spaces list-wid loop cr
195 ." Compile: " get-current list-wid cr
198 : debug ' debug-xt ; immediate
199 : on-step ." S: " .s cr ;
203 : strdup ( c-addr length -- c-addr2 length2 ior )
204 0 locals| addr2 length c-addr | end-locals
208 c-addr addr2 length move
215 : strcat ( 2:a 2:b -- 2:new-a )
216 0 locals| b-length b-u b-addr a-u a-addr | end-locals
218 b-addr a-addr a-u + b-length move
219 a-addr a-u b-length +
222 : strcpy ( 2:a 2:b -- 2:new-a )
223 locals| b-u b-addr a-u a-addr | end-locals
224 a-addr 0 b-addr b-u strcat
228 dup 0x80 u< if emit exit then \ special case ASCII
231 2/ >r dup 0x3F and 0x80 or swap 6 rshift r>
232 repeat 0x7F xor 2* or
233 begin dup 0x80 u< 0= while emit repeat drop
236 previous \ lose hidden words from search order
238 \ ** E N D S O F T C O R E . F R