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