extend.fs 9.2 KB
Newer Older
anton's avatar
anton committed
1 2
\ EXTEND.FS    CORE-EXT Word not fully tested!         12may93jaw

Anton Ertl's avatar
Anton Ertl committed
3
\ Copyright (C) 1995,1998,2000,2003,2005,2007,2009,2010,2011,2013,2014,2016,2017,2018 Free Software Foundation, Inc.
anton's avatar
anton committed
4 5 6 7 8

\ This file is part of Gforth.

\ Gforth is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public License
9
\ as published by the Free Software Foundation, either version 3
anton's avatar
anton committed
10 11 12 13 14 15 16 17
\ of the License, or (at your option) any later version.

\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
\ GNU General Public License for more details.

\ You should have received a copy of the GNU General Public License
18
\ along with this program. If not, see http://www.gnu.org/licenses/.
anton's avatar
anton committed
19 20


anton's avatar
anton committed
21 22 23 24 25 26
\ May be cross-compiled

decimal

\ .(                                                    12may93jaw

27
: .(   ( compilation&interpretation "ccc<paren>" -- ) \ core-ext dot-paren
anton's avatar
anton committed
28 29 30 31
  \G Compilation and interpretation semantics: Parse a string @i{ccc}
  \G delimited by a @code{)} (right parenthesis). Display the
  \G string. This is often used to display progress information during
  \G compilation; see examples below.
32
  ')' parse type ; immediate
anton's avatar
anton committed
33 34 35

\ VALUE 2>R 2R> 2R@                                     17may93jaw

anton's avatar
anton committed
36
\ !! 2value
anton's avatar
anton committed
37

38
[ifundef] 2literal
39
: 2Literal ( compilation w1 w2 -- ; run-time  -- w1 w2 ) \ double two-literal
40
    \G Compile appropriate code such that, at run-time, cell pair @i{w1, w2} are
41
    \G placed on the stack. Interpretation semantics are undefined.
42
    swap postpone Literal  postpone Literal ; immediate restrict
43
[then]
44

45 46
' drop alias d>s ( d -- n ) \ double		d_to_s

47
: m*/ ( d1 n2 u3 -- dquot ) \ double m-star-slash
anton's avatar
anton committed
48 49
    \G dquot=(d1*n2)/u3, with the intermediate result being triple-precision.
    \G In ANS Forth u3 can only be a positive signed number.
50 51
    >r s>d >r abs -rot
    s>d r> xor r> swap >r >r dabs rot tuck um* 2swap um*
52
    swap >r 0 d+ r> -rot r@ um/mod -rot r> um/mod
53
    [ 1 -3 mod 0< ] [if]
54 55 56 57
        -rot r> IF IF 1. d+ THEN dnegate ELSE drop THEN
    [else]
        nip swap r> IF dnegate THEN
    [then] ;
pazsan's avatar
pazsan committed
58

anton's avatar
anton committed
59 60
\ C"                                                    17may93jaw

61
: C" ( compilation "ccc<quote>" -- ; run-time  -- c-addr ) \ core-ext c-quote
62 63 64
    \G Compilation: parse a string @i{ccc} delimited by a @code{"}
    \G (double quote). At run-time, return @i{c-addr} which
    \G specifies the counted string @i{ccc}.  Interpretation
crook's avatar
crook committed
65
    \G semantics are undefined.
66
    '"' parse postpone CLiteral ; immediate restrict
anton's avatar
anton committed
67 68 69

\ [COMPILE]                                             17may93jaw

70
: [compile] ( compilation "name" -- ; run-time ? -- ? ) \ core-ext bracket-compile
71 72 73 74 75 76
    comp' drop
    dup [ comp' exit drop ] literal = if
	execute \ EXIT has default compilation semantics, perform them
    else
	compile,
    then ; immediate
anton's avatar
anton committed
77 78 79

\ CONVERT                                               17may93jaw

80 81
: convert ( ud1 c-addr1 -- ud2 c-addr2 ) \ core-ext-obsolescent
    \G Obsolescent: superseded by @code{>number}.
anton's avatar
anton committed
82
    char+ true >number drop ;
anton's avatar
anton committed
83 84 85

\ ERASE                                                 17may93jaw

anton's avatar
anton committed
86 87
: erase ( addr u -- ) \ core-ext
    \G Clear all bits in @i{u} aus starting at @i{addr}.
88 89
    \ !! dependence on "1 chars 1 ="
    ( 0 1 chars um/mod nip )  0 fill ;
90
: blank ( c-addr u -- ) \ string
anton's avatar
anton committed
91
    \G Store the space character into @i{u} chars starting at @i{c-addr}.
92
    bl fill ;
anton's avatar
anton committed
93

94 95
\ SEARCH                                                02sep94py

96
: search ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 flag ) \ string
97 98 99 100
    \G Search the string specified by @i{c-addr1, u1} for the string
    \G specified by @i{c-addr2, u2}. If @i{flag} is true: match was found
    \G at @i{c-addr3} with @i{u3} characters remaining. If @i{flag} is false:
    \G no match was found; @i{c-addr3, u3} are equal to @i{c-addr1, u1}.
101 102 103 104 105
    \ not very efficient; but if we want efficiency, we'll do it as primitive
    2>r 2dup
    begin
	dup r@ >=
    while
106
	2dup 2r@ string-prefix? if
107 108 109 110 111
	    2swap 2drop 2r> 2drop true exit
	endif
	1 /string
    repeat
    2drop 2r> 2drop false ;
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
: capsstring-prefix? ( c-addr1 u1 c-addr2 u2 -- f ) \ gforth
    \G Is @var{c-addr2 u2} a case-oblivious prefix of @var{c-addr1 u1}?
    tuck 2>r umin 2r> capscompare 0= ;

: capssearch ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 flag ) \ gforth
    \G Like \code{search}, but case-insensitive for ASCII characters:
    \G Search for c-addr2 u2 in c-addr1 u1; flag is true if found.
    2>r 2dup begin ( c-addr1 u1 c-addr4 u4 )
        dup r@ u>= while
	    2dup 2r@ capsstring-prefix? if
		2swap 2drop 2r> 2drop true exit then
	    1 /string
    repeat
    2drop 2r> 2drop false ;

anton's avatar
anton committed
128 129
\ SOURCE-ID SAVE-INPUT RESTORE-INPUT                    11jun93jaw

130
[IFUNDEF] source-id
anton's avatar
anton committed
131
: source-id ( -- 0 | -1 | fileid ) \ core-ext,file source-i-d
132 133
    \G Return 0 (the input source is the user input device), -1 (the
    \G input source is a string being processed by @code{evaluate}) or
134 135
    \G a @i{fileid} (the input source is the file specified by
    \G @i{fileid}).
136 137 138
    loadfile @ dup 0= IF  drop sourceline# 0 min  THEN ;

: save-input ( -- xn .. x1 n ) \ core-ext
139
    \G The @i{n} entries @i{xn - x1} describe the current state of the
140 141
    \G input source specification, in some platform-dependent way that can
    \G be used by @code{restore-input}.
142 143 144
    >in @
    loadfile @
    if
pazsan's avatar
pazsan committed
145 146
	loadfile @ file-position throw
	[IFDEF] #fill-bytes #fill-bytes @ [ELSE] #tib @ 1+ [THEN] 0 d-
147 148 149 150 151 152 153 154
    else
	blk @
	linestart @
    then
    sourceline#
    >tib @
    source-id
    6 ;
anton's avatar
anton committed
155

156 157
: restore-input ( xn .. x1 n -- flag ) \ core-ext
    \G Attempt to restore the input source specification to the state
158
    \G described by the @i{n} entries @i{xn - x1}. @i{flag} is
anton's avatar
anton committed
159 160
    \G true if the restore fails.  In Gforth it fails pretty often
    \G (and sometimes with a @code{throw}).
161 162 163 164 165 166 167
    6 <> -12 and throw
    source-id <> -12 and throw
    >tib !
    >r ( line# )
    loadfile @ 0<>
    if
	loadfile @ reposition-file throw
168
	refill 0= -36 and throw \ should never throw
169 170 171 172 173 174 175 176 177 178 179
    else
	linestart !
	blk !
	sourceline# r@ <> blk @ 0= and loadfile @ 0= and
	if
	    drop rdrop true EXIT
	then
    then
    r> loadline !
    >in !
    false ;
180
[THEN]
anton's avatar
anton committed
181 182 183 184
\ This things we don't need, but for being complete... jaw

\ EXPECT SPAN                                           17may93jaw

185
variable span ( -- c-addr ) \ core-ext-obsolescent
186 187
\G @code{Variable} -- @i{c-addr} is the address of a cell that stores the
\G length of the last string received by @code{expect}. OBSOLESCENT.
188

189
: expect ( c-addr +n -- ) \ core-ext-obsolescent
190 191
    \G Receive a string of at most @i{+n} characters, and store it
    \G in memory starting at @i{c-addr}. The string is
192
    \G displayed. Input terminates when the <return> key is pressed or
193
    \G @i{+n} characters have been received. The normal Gforth line
194 195 196
    \G editing capabilites are available. The length of the string is
    \G stored in @code{span}; it does not include the <return>
    \G character. OBSOLESCENT: superceeded by @code{accept}.
pazsan's avatar
pazsan committed
197
    everyline
198 199
    0 rot over
    BEGIN ( maxlen span c-addr pos1 )
pazsan's avatar
pazsan committed
200
	xkey decode ( maxlen span c-addr pos2 flag )
201 202
	>r 2over = r> or
    UNTIL
203 204
    2 pick swap /string type
    nip span ! ;
anton's avatar
anton committed
205

jwilke's avatar
jwilke committed
206 207 208 209 210
\ marker                                               18dec94py

\ Marker creates a mark that is removed (including everything 
\ defined afterwards) when executing the mark.

anton's avatar
anton committed
211
: included-files-mark ( -- u )
212
    included-files $@len ;
anton's avatar
anton committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

\ hmm, most of the saving appears to be pretty unnecessary: we could
\ derive the wordlists and the words that have to be kept from the
\ saved value of dp value. - anton

: marker, ( -- mark )
    here
    included-files-mark ,
    dup A, \ here
    voclink @ A, \ vocabulary list start
    \ for all wordlists, remember wordlist-id (the linked list)
    voclink
    BEGIN
	@ dup
    WHILE
	dup 0 wordlist-link - wordlist-id @ A,
    REPEAT
    drop
    \ remember udp
232
    udp @ ,
233 234
    \ remember vtable-list
    vtable-list @ ,
235 236
    \ remember dyncode-ptr
    here ['] noop , compile-prim1 finish-code ;
jwilke's avatar
jwilke committed
237 238

: marker! ( mark -- )
anton's avatar
anton committed
239
    \ reset included files count; resize will happen on next add-included-file
240 241
    dup @ dup >r included-files $@ r> /string bounds +DO  I $off  cell +LOOP
    included-files $!len cell+
anton's avatar
anton committed
242 243
    \ rest of marker!
    dup @ swap cell+ ( here rest-of-marker )
jwilke's avatar
jwilke committed
244
    dup @ voclink ! cell+
anton's avatar
anton committed
245
    \ restore wordlists to former words
jwilke's avatar
jwilke committed
246 247 248 249
    voclink
    BEGIN
	@ dup 
    WHILE
anton's avatar
anton committed
250
	over @ over 0 wordlist-link - wordlist-id !
jwilke's avatar
jwilke committed
251 252
	swap cell+ swap
    REPEAT
anton's avatar
anton committed
253 254 255 256
    drop
    \ rehash wordlists to remove forgotten words
    \ why don't we do this in a single step? - anton
    voclink
jwilke's avatar
jwilke committed
257 258 259 260 261 262
    BEGIN
	@ dup
    WHILE
	dup 0 wordlist-link - rehash
    REPEAT
    drop
anton's avatar
anton committed
263
    \ restore udp and dp
pazsan's avatar
pazsan committed
264
[IFDEF] forget-dyncode
anton's avatar
anton committed
265
    dup cell+ @ forget-dyncode drop
pazsan's avatar
pazsan committed
266
[THEN]
267
    dup @ udp !  cell+ @ vtable-list !  dp !
268
    \ clean up vocabulary stack
269 270
    0 vocstack $@ bounds
    U+DO
Bernd Paysan's avatar
Bernd Paysan committed
271
	I @ dup here u>
272
	IF  drop  ELSE  swap 1+  THEN
273
    cell +LOOP
274 275 276 277
    dup 0= or set-order \ -1 set-order if order is empty
    get-current here > IF
	forth-wordlist set-current
    THEN ;
jwilke's avatar
jwilke committed
278

279
: marker ( "<spaces> name" -- ) \ core-ext
280
    \G Create a definition, @i{name} (called a @i{mark}) whose
281 282
    \G execution semantics are to remove itself and everything 
    \G defined after it.
jwilke's avatar
jwilke committed
283 284 285 286
    marker, Create A,
DOES> ( -- )
    @ marker! ;