| 1 : |
anton
|
1.1
|
\ EXTEND.FS CORE-EXT Word not fully tested! 12may93jaw |
| 2 : |
|
|
|
| 3 : |
anton
|
1.44
|
\ Copyright (C) 1995,1998,2000 Free Software Foundation, Inc. |
| 4 : |
anton
|
1.12
|
|
| 5 : |
|
|
\ This file is part of Gforth. |
| 6 : |
|
|
|
| 7 : |
|
|
\ Gforth is free software; you can redistribute it and/or |
| 8 : |
|
|
\ modify it under the terms of the GNU General Public License |
| 9 : |
|
|
\ as published by the Free Software Foundation; either version 2 |
| 10 : |
|
|
\ of the License, or (at your option) any later version. |
| 11 : |
|
|
|
| 12 : |
|
|
\ This program is distributed in the hope that it will be useful, |
| 13 : |
|
|
\ but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 : |
|
|
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 : |
|
|
\ GNU General Public License for more details. |
| 16 : |
|
|
|
| 17 : |
|
|
\ You should have received a copy of the GNU General Public License |
| 18 : |
|
|
\ along with this program; if not, write to the Free Software |
| 19 : |
anton
|
1.45
|
\ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
| 20 : |
anton
|
1.12
|
|
| 21 : |
|
|
|
| 22 : |
anton
|
1.1
|
\ May be cross-compiled |
| 23 : |
|
|
|
| 24 : |
|
|
decimal |
| 25 : |
|
|
|
| 26 : |
|
|
\ .( 12may93jaw |
| 27 : |
|
|
|
| 28 : |
anton
|
1.41
|
: .( ( compilation,interpretation "ccc<paren>" -- ) \ core-ext dot-paren |
| 29 : |
|
|
\G Compilation and interpretation semantics: Parse a string @i{ccc} |
| 30 : |
|
|
\G delimited by a @code{)} (right parenthesis). Display the |
| 31 : |
|
|
\G string. This is often used to display progress information during |
| 32 : |
|
|
\G compilation; see examples below. |
| 33 : |
crook
|
1.32
|
[char] ) parse type ; immediate |
| 34 : |
anton
|
1.1
|
|
| 35 : |
|
|
\ VALUE 2>R 2R> 2R@ 17may93jaw |
| 36 : |
|
|
|
| 37 : |
anton
|
1.3
|
\ !! 2value |
| 38 : |
anton
|
1.1
|
|
| 39 : |
anton
|
1.11
|
: 2Literal ( compilation w1 w2 -- ; run-time -- w1 w2 ) \ double two-literal |
| 40 : |
crook
|
1.36
|
\G Compile appropriate code such that, at run-time, cell pair @i{w1, w2} are |
| 41 : |
crook
|
1.32
|
\G placed on the stack. Interpretation semantics are undefined. |
| 42 : |
anton
|
1.11
|
swap postpone Literal postpone Literal ; immediate restrict |
| 43 : |
pazsan
|
1.2
|
|
| 44 : |
anton
|
1.16
|
' drop alias d>s ( d -- n ) \ double d_to_s |
| 45 : |
|
|
|
| 46 : |
anton
|
1.39
|
: m*/ ( d1 n2 u3 -- dquot ) \ double m-star-slash |
| 47 : |
anton
|
1.40
|
\G dquot=(d1*n2)/u3, with the intermediate result being triple-precision. |
| 48 : |
|
|
\G In ANS Forth u3 can only be a positive signed number. |
| 49 : |
anton
|
1.11
|
>r s>d >r abs -rot |
| 50 : |
|
|
s>d r> xor r> swap >r >r dabs rot tuck um* 2swap um* |
| 51 : |
|
|
swap >r 0 d+ r> -rot r@ um/mod -rot r> um/mod nip swap |
| 52 : |
|
|
r> IF dnegate THEN ; |
| 53 : |
pazsan
|
1.4
|
|
| 54 : |
anton
|
1.1
|
\ CASE OF ENDOF ENDCASE 17may93jaw |
| 55 : |
|
|
|
| 56 : |
|
|
\ just as described in dpANS5 |
| 57 : |
|
|
|
| 58 : |
anton
|
1.11
|
0 CONSTANT case ( compilation -- case-sys ; run-time -- ) \ core-ext |
| 59 : |
|
|
immediate |
| 60 : |
anton
|
1.1
|
|
| 61 : |
anton
|
1.11
|
: of ( compilation -- of-sys ; run-time x1 x2 -- |x1 ) \ core-ext |
| 62 : |
|
|
\ !! the implementation does not match the stack effect |
| 63 : |
|
|
1+ >r |
| 64 : |
|
|
postpone over postpone = postpone if postpone drop |
| 65 : |
|
|
r> ; immediate |
| 66 : |
|
|
|
| 67 : |
|
|
: endof ( compilation case-sys1 of-sys -- case-sys2 ; run-time -- ) \ core-ext end-of |
| 68 : |
|
|
>r postpone else r> ; immediate |
| 69 : |
|
|
|
| 70 : |
|
|
: endcase ( compilation case-sys -- ; run-time x -- ) \ core-ext end-case |
| 71 : |
|
|
postpone drop |
| 72 : |
|
|
0 ?do postpone then loop ; immediate |
| 73 : |
anton
|
1.1
|
|
| 74 : |
|
|
\ C" 17may93jaw |
| 75 : |
|
|
|
| 76 : |
|
|
: (c") "lit ; |
| 77 : |
|
|
|
| 78 : |
anton
|
1.11
|
: CLiteral |
| 79 : |
|
|
postpone (c") here over char+ allot place align ; immediate restrict |
| 80 : |
|
|
|
| 81 : |
crook
|
1.32
|
: C" ( compilation "ccc<quote>" -- ; run-time -- c-addr ) \ core-ext c-quote |
| 82 : |
crook
|
1.36
|
\G Compilation: parse a string @i{ccc} delimited by a @code{"} |
| 83 : |
|
|
\G (double quote). At run-time, return @i{c-addr} which |
| 84 : |
|
|
\G specifies the counted string @i{ccc}. Interpretation |
| 85 : |
crook
|
1.33
|
\G semantics are undefined. |
| 86 : |
anton
|
1.11
|
[char] " parse postpone CLiteral ; immediate restrict |
| 87 : |
anton
|
1.1
|
|
| 88 : |
|
|
\ [COMPILE] 17may93jaw |
| 89 : |
|
|
|
| 90 : |
anton
|
1.11
|
: [compile] ( compilation "name" -- ; run-time ? -- ? ) \ core-ext bracket-compile |
| 91 : |
anton
|
1.37
|
comp' drop |
| 92 : |
|
|
dup [ comp' exit drop ] literal = if |
| 93 : |
|
|
execute \ EXIT has default compilation semantics, perform them |
| 94 : |
|
|
else |
| 95 : |
|
|
compile, |
| 96 : |
|
|
then ; immediate |
| 97 : |
anton
|
1.1
|
|
| 98 : |
|
|
\ CONVERT 17may93jaw |
| 99 : |
|
|
|
| 100 : |
anton
|
1.11
|
: convert ( ud1 c-addr1 -- ud2 c-addr2 ) \ core-ext |
| 101 : |
crook
|
1.34
|
\G OBSOLESCENT: superseded by @code{>number}. |
| 102 : |
anton
|
1.26
|
char+ true >number drop ; |
| 103 : |
anton
|
1.1
|
|
| 104 : |
|
|
\ ERASE 17may93jaw |
| 105 : |
|
|
|
| 106 : |
anton
|
1.40
|
: erase ( addr u -- ) \ core-ext |
| 107 : |
|
|
\G Clear all bits in @i{u} aus starting at @i{addr}. |
| 108 : |
anton
|
1.11
|
\ !! dependence on "1 chars 1 =" |
| 109 : |
|
|
( 0 1 chars um/mod nip ) 0 fill ; |
| 110 : |
crook
|
1.34
|
: blank ( c-addr u -- ) \ string |
| 111 : |
anton
|
1.40
|
\G Store the space character into @i{u} chars starting at @i{c-addr}. |
| 112 : |
anton
|
1.11
|
bl fill ; |
| 113 : |
anton
|
1.1
|
|
| 114 : |
pazsan
|
1.7
|
\ SEARCH 02sep94py |
| 115 : |
|
|
|
| 116 : |
anton
|
1.28
|
: search ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 flag ) \ string |
| 117 : |
crook
|
1.36
|
\G Search the string specified by @i{c-addr1, u1} for the string |
| 118 : |
|
|
\G specified by @i{c-addr2, u2}. If @i{flag} is true: match was found |
| 119 : |
|
|
\G at @i{c-addr3} with @i{u3} characters remaining. If @i{flag} is false: |
| 120 : |
|
|
\G no match was found; @i{c-addr3, u3} are equal to @i{c-addr1, u1}. |
| 121 : |
anton
|
1.28
|
\ not very efficient; but if we want efficiency, we'll do it as primitive |
| 122 : |
|
|
2>r 2dup |
| 123 : |
|
|
begin |
| 124 : |
|
|
dup r@ >= |
| 125 : |
|
|
while |
| 126 : |
|
|
over 2r@ swap -text 0= if |
| 127 : |
|
|
2swap 2drop 2r> 2drop true exit |
| 128 : |
|
|
endif |
| 129 : |
|
|
1 /string |
| 130 : |
|
|
repeat |
| 131 : |
|
|
2drop 2r> 2drop false ; |
| 132 : |
pazsan
|
1.7
|
|
| 133 : |
anton
|
1.1
|
\ SOURCE-ID SAVE-INPUT RESTORE-INPUT 11jun93jaw |
| 134 : |
|
|
|
| 135 : |
pazsan
|
1.46
|
[IFUNDEF] source-id |
| 136 : |
anton
|
1.22
|
: source-id ( -- 0 | -1 | fileid ) \ core-ext,file source-i-d |
| 137 : |
crook
|
1.34
|
\G Return 0 (the input source is the user input device), -1 (the |
| 138 : |
|
|
\G input source is a string being processed by @code{evaluate}) or |
| 139 : |
crook
|
1.36
|
\G a @i{fileid} (the input source is the file specified by |
| 140 : |
|
|
\G @i{fileid}). |
| 141 : |
crook
|
1.34
|
loadfile @ dup 0= IF drop sourceline# 0 min THEN ; |
| 142 : |
|
|
|
| 143 : |
|
|
: save-input ( -- xn .. x1 n ) \ core-ext |
| 144 : |
crook
|
1.36
|
\G The @i{n} entries @i{xn - x1} describe the current state of the |
| 145 : |
crook
|
1.34
|
\G input source specification, in some platform-dependent way that can |
| 146 : |
|
|
\G be used by @code{restore-input}. |
| 147 : |
anton
|
1.14
|
>in @ |
| 148 : |
|
|
loadfile @ |
| 149 : |
|
|
if |
| 150 : |
pazsan
|
1.43
|
loadfile @ file-position throw |
| 151 : |
|
|
[IFDEF] #fill-bytes #fill-bytes @ [ELSE] #tib @ 1+ [THEN] 0 d- |
| 152 : |
anton
|
1.14
|
else |
| 153 : |
|
|
blk @ |
| 154 : |
|
|
linestart @ |
| 155 : |
|
|
then |
| 156 : |
|
|
sourceline# |
| 157 : |
|
|
>tib @ |
| 158 : |
|
|
source-id |
| 159 : |
|
|
6 ; |
| 160 : |
anton
|
1.1
|
|
| 161 : |
crook
|
1.34
|
: restore-input ( xn .. x1 n -- flag ) \ core-ext |
| 162 : |
|
|
\G Attempt to restore the input source specification to the state |
| 163 : |
crook
|
1.36
|
\G described by the @i{n} entries @i{xn - x1}. @i{flag} is |
| 164 : |
anton
|
1.35
|
\G true if the restore fails. In Gforth it fails pretty often |
| 165 : |
|
|
\G (and sometimes with a @code{throw}). |
| 166 : |
anton
|
1.14
|
6 <> -12 and throw |
| 167 : |
|
|
source-id <> -12 and throw |
| 168 : |
|
|
>tib ! |
| 169 : |
|
|
>r ( line# ) |
| 170 : |
|
|
loadfile @ 0<> |
| 171 : |
|
|
if |
| 172 : |
|
|
loadfile @ reposition-file throw |
| 173 : |
anton
|
1.38
|
refill 0= -36 and throw \ should never throw |
| 174 : |
anton
|
1.14
|
else |
| 175 : |
|
|
linestart ! |
| 176 : |
|
|
blk ! |
| 177 : |
|
|
sourceline# r@ <> blk @ 0= and loadfile @ 0= and |
| 178 : |
|
|
if |
| 179 : |
|
|
drop rdrop true EXIT |
| 180 : |
|
|
then |
| 181 : |
|
|
then |
| 182 : |
|
|
r> loadline ! |
| 183 : |
|
|
>in ! |
| 184 : |
|
|
false ; |
| 185 : |
pazsan
|
1.46
|
[THEN] |
| 186 : |
anton
|
1.1
|
\ This things we don't need, but for being complete... jaw |
| 187 : |
|
|
|
| 188 : |
|
|
\ EXPECT SPAN 17may93jaw |
| 189 : |
|
|
|
| 190 : |
crook
|
1.34
|
variable span ( -- c-addr ) \ core-ext |
| 191 : |
crook
|
1.36
|
\G @code{Variable} -- @i{c-addr} is the address of a cell that stores the |
| 192 : |
|
|
\G length of the last string received by @code{expect}. OBSOLESCENT. |
| 193 : |
crook
|
1.34
|
|
| 194 : |
|
|
: expect ( c-addr +n -- ) \ core-ext |
| 195 : |
crook
|
1.36
|
\G Receive a string of at most @i{+n} characters, and store it |
| 196 : |
|
|
\G in memory starting at @i{c-addr}. The string is |
| 197 : |
crook
|
1.34
|
\G displayed. Input terminates when the <return> key is pressed or |
| 198 : |
crook
|
1.36
|
\G @i{+n} characters have been received. The normal Gforth line |
| 199 : |
crook
|
1.34
|
\G editing capabilites are available. The length of the string is |
| 200 : |
|
|
\G stored in @code{span}; it does not include the <return> |
| 201 : |
|
|
\G character. OBSOLESCENT: superceeded by @code{accept}. |
| 202 : |
anton
|
1.9
|
0 rot over |
| 203 : |
|
|
BEGIN ( maxlen span c-addr pos1 ) |
| 204 : |
|
|
key decode ( maxlen span c-addr pos2 flag ) |
| 205 : |
|
|
>r 2over = r> or |
| 206 : |
|
|
UNTIL |
| 207 : |
pazsan
|
1.17
|
2 pick swap /string type |
| 208 : |
|
|
nip span ! ; |
| 209 : |
jwilke
|
1.27
|
|
| 210 : |
|
|
\ marker 18dec94py |
| 211 : |
|
|
|
| 212 : |
|
|
\ Marker creates a mark that is removed (including everything |
| 213 : |
|
|
\ defined afterwards) when executing the mark. |
| 214 : |
|
|
|
| 215 : |
anton
|
1.31
|
: included-files-mark ( -- u ) |
| 216 : |
|
|
included-files 2@ nip |
| 217 : |
|
|
blk @ 0= |
| 218 : |
|
|
if \ not input from blocks |
| 219 : |
|
|
source-id 1 -1 within |
| 220 : |
|
|
if \ input from file |
| 221 : |
|
|
1- \ do not include the last file (hopefully this is the |
| 222 : |
|
|
\ currently included file) |
| 223 : |
|
|
then |
| 224 : |
|
|
then ; |
| 225 : |
|
|
|
| 226 : |
|
|
\ hmm, most of the saving appears to be pretty unnecessary: we could |
| 227 : |
|
|
\ derive the wordlists and the words that have to be kept from the |
| 228 : |
|
|
\ saved value of dp value. - anton |
| 229 : |
|
|
|
| 230 : |
|
|
: marker, ( -- mark ) |
| 231 : |
|
|
here |
| 232 : |
|
|
included-files-mark , |
| 233 : |
|
|
dup A, \ here |
| 234 : |
|
|
voclink @ A, \ vocabulary list start |
| 235 : |
|
|
\ for all wordlists, remember wordlist-id (the linked list) |
| 236 : |
|
|
voclink |
| 237 : |
|
|
BEGIN |
| 238 : |
|
|
@ dup |
| 239 : |
|
|
WHILE |
| 240 : |
|
|
dup 0 wordlist-link - wordlist-id @ A, |
| 241 : |
|
|
REPEAT |
| 242 : |
|
|
drop |
| 243 : |
|
|
\ remember udp |
| 244 : |
|
|
udp @ , ; |
| 245 : |
jwilke
|
1.27
|
|
| 246 : |
|
|
: marker! ( mark -- ) |
| 247 : |
anton
|
1.31
|
\ reset included files count; resize will happen on next add-included-file |
| 248 : |
|
|
included-files 2@ drop over @ included-files 2! cell+ |
| 249 : |
|
|
\ rest of marker! |
| 250 : |
|
|
dup @ swap cell+ ( here rest-of-marker ) |
| 251 : |
jwilke
|
1.27
|
dup @ voclink ! cell+ |
| 252 : |
anton
|
1.31
|
\ restore wordlists to former words |
| 253 : |
jwilke
|
1.27
|
voclink |
| 254 : |
|
|
BEGIN |
| 255 : |
|
|
@ dup |
| 256 : |
|
|
WHILE |
| 257 : |
anton
|
1.31
|
over @ over 0 wordlist-link - wordlist-id ! |
| 258 : |
jwilke
|
1.27
|
swap cell+ swap |
| 259 : |
|
|
REPEAT |
| 260 : |
anton
|
1.31
|
drop |
| 261 : |
|
|
\ rehash wordlists to remove forgotten words |
| 262 : |
|
|
\ why don't we do this in a single step? - anton |
| 263 : |
|
|
voclink |
| 264 : |
jwilke
|
1.27
|
BEGIN |
| 265 : |
|
|
@ dup |
| 266 : |
|
|
WHILE |
| 267 : |
|
|
dup 0 wordlist-link - rehash |
| 268 : |
|
|
REPEAT |
| 269 : |
|
|
drop |
| 270 : |
anton
|
1.31
|
\ restore udp and dp |
| 271 : |
pazsan
|
1.29
|
@ udp ! dp ! |
| 272 : |
|
|
\ clean up vocabulary stack |
| 273 : |
|
|
0 vp @ 0 |
| 274 : |
|
|
?DO |
| 275 : |
|
|
vp cell+ I cells + @ dup here > |
| 276 : |
|
|
IF drop ELSE swap 1+ THEN |
| 277 : |
|
|
LOOP |
| 278 : |
|
|
dup 0= or set-order \ -1 set-order if order is empty |
| 279 : |
|
|
get-current here > IF |
| 280 : |
|
|
forth-wordlist set-current |
| 281 : |
|
|
THEN ; |
| 282 : |
jwilke
|
1.27
|
|
| 283 : |
crook
|
1.32
|
: marker ( "<spaces> name" -- ) \ core-ext |
| 284 : |
crook
|
1.36
|
\G Create a definition, @i{name} (called a @i{mark}) whose |
| 285 : |
crook
|
1.32
|
\G execution semantics are to remove itself and everything |
| 286 : |
|
|
\G defined after it. |
| 287 : |
jwilke
|
1.27
|
marker, Create A, |
| 288 : |
|
|
DOES> ( -- ) |
| 289 : |
|
|
@ marker! ; |
| 290 : |
anton
|
1.1
|
|