1: \ EXTEND.FS CORE-EXT Word not fully tested! 12may93jaw
2:
3: \ Copyright (C) 1995,1998,2000,2003,2005,2007,2009,2010 Free Software Foundation, Inc.
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
9: \ as published by the Free Software Foundation, either version 3
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, see http://www.gnu.org/licenses/.
19:
20:
21: \ May be cross-compiled
22:
23: decimal
24:
25: \ .( 12may93jaw
26:
27: : .( ( compilation&interpretation "ccc<paren>" -- ) \ core-ext dot-paren
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.
32: [char] ) parse type ; immediate
33:
34: \ VALUE 2>R 2R> 2R@ 17may93jaw
35:
36: \ !! 2value
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: ' drop alias d>s ( d -- n ) \ double d_to_s
46:
47: : m*/ ( d1 n2 u3 -- dquot ) \ double m-star-slash
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.
50: >r s>d >r abs -rot
51: 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: -rot r> IF IF 1. d+ THEN dnegate ELSE drop THEN
55: [else]
56: nip swap r> IF dnegate THEN
57: [then] ;
58:
59: \ CASE OF ENDOF ENDCASE 17may93jaw
60:
61: \ just as described in dpANS5
62:
63: 0 CONSTANT case ( compilation -- case-sys ; run-time -- ) \ core-ext
64: immediate
65:
66: : of ( compilation -- of-sys ; run-time x1 x2 -- |x1 ) \ core-ext
67: \ !! the implementation does not match the stack effect
68: 1+ >r
69: postpone over postpone = postpone if postpone drop
70: r> ; immediate
71:
72: : endof ( compilation case-sys1 of-sys -- case-sys2 ; run-time -- ) \ core-ext end-of
73: >r postpone else r> ; immediate
74:
75: : endcase ( compilation case-sys -- ; run-time x -- ) \ core-ext end-case
76: postpone drop
77: 0 ?do postpone then loop ; immediate
78:
79: \ C" 17may93jaw
80:
81: : C" ( compilation "ccc<quote>" -- ; run-time -- c-addr ) \ core-ext c-quote
82: \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: \G semantics are undefined.
86: [char] " parse postpone CLiteral ; immediate restrict
87:
88: \ [COMPILE] 17may93jaw
89:
90: : [compile] ( compilation "name" -- ; run-time ? -- ? ) \ core-ext bracket-compile
91: 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:
98: \ CONVERT 17may93jaw
99:
100: : convert ( ud1 c-addr1 -- ud2 c-addr2 ) \ core-ext-obsolescent
101: \G Obsolescent: superseded by @code{>number}.
102: char+ true >number drop ;
103:
104: \ ERASE 17may93jaw
105:
106: : erase ( addr u -- ) \ core-ext
107: \G Clear all bits in @i{u} aus starting at @i{addr}.
108: \ !! dependence on "1 chars 1 ="
109: ( 0 1 chars um/mod nip ) 0 fill ;
110: : blank ( c-addr u -- ) \ string
111: \G Store the space character into @i{u} chars starting at @i{c-addr}.
112: bl fill ;
113:
114: \ SEARCH 02sep94py
115:
116: : search ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 flag ) \ string
117: \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: \ 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: 2dup 2r@ string-prefix? if
127: 2swap 2drop 2r> 2drop true exit
128: endif
129: 1 /string
130: repeat
131: 2drop 2r> 2drop false ;
132:
133: \ SOURCE-ID SAVE-INPUT RESTORE-INPUT 11jun93jaw
134:
135: [IFUNDEF] source-id
136: : source-id ( -- 0 | -1 | fileid ) \ core-ext,file source-i-d
137: \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: \G a @i{fileid} (the input source is the file specified by
140: \G @i{fileid}).
141: loadfile @ dup 0= IF drop sourceline# 0 min THEN ;
142:
143: : save-input ( -- xn .. x1 n ) \ core-ext
144: \G The @i{n} entries @i{xn - x1} describe the current state of the
145: \G input source specification, in some platform-dependent way that can
146: \G be used by @code{restore-input}.
147: >in @
148: loadfile @
149: if
150: loadfile @ file-position throw
151: [IFDEF] #fill-bytes #fill-bytes @ [ELSE] #tib @ 1+ [THEN] 0 d-
152: else
153: blk @
154: linestart @
155: then
156: sourceline#
157: >tib @
158: source-id
159: 6 ;
160:
161: : restore-input ( xn .. x1 n -- flag ) \ core-ext
162: \G Attempt to restore the input source specification to the state
163: \G described by the @i{n} entries @i{xn - x1}. @i{flag} is
164: \G true if the restore fails. In Gforth it fails pretty often
165: \G (and sometimes with a @code{throw}).
166: 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: refill 0= -36 and throw \ should never throw
174: 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: [THEN]
186: \ This things we don't need, but for being complete... jaw
187:
188: \ EXPECT SPAN 17may93jaw
189:
190: variable span ( -- c-addr ) \ core-ext-obsolescent
191: \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:
194: : expect ( c-addr +n -- ) \ core-ext-obsolescent
195: \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: \G displayed. Input terminates when the <return> key is pressed or
198: \G @i{+n} characters have been received. The normal Gforth line
199: \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: everyline
203: 0 rot over
204: BEGIN ( maxlen span c-addr pos1 )
205: xkey decode ( maxlen span c-addr pos2 flag )
206: >r 2over = r> or
207: UNTIL
208: 2 pick swap /string type
209: nip span ! ;
210:
211: \ marker 18dec94py
212:
213: \ Marker creates a mark that is removed (including everything
214: \ defined afterwards) when executing the mark.
215:
216: : included-files-mark ( -- u )
217: included-files @ ;
218:
219: \ hmm, most of the saving appears to be pretty unnecessary: we could
220: \ derive the wordlists and the words that have to be kept from the
221: \ saved value of dp value. - anton
222:
223: : marker, ( -- mark )
224: here
225: included-files-mark ,
226: dup A, \ here
227: voclink @ A, \ vocabulary list start
228: \ for all wordlists, remember wordlist-id (the linked list)
229: voclink
230: BEGIN
231: @ dup
232: WHILE
233: dup 0 wordlist-link - wordlist-id @ A,
234: REPEAT
235: drop
236: \ remember udp
237: udp @ ,
238: \ remember dyncode-ptr
239: here ['] noop , compile-prim1 finish-code ;
240:
241: : marker! ( mark -- )
242: \ reset included files count; resize will happen on next add-included-file
243: included-files @ over @ min included-files ! cell+
244: \ rest of marker!
245: dup @ swap cell+ ( here rest-of-marker )
246: dup @ voclink ! cell+
247: \ restore wordlists to former words
248: voclink
249: BEGIN
250: @ dup
251: WHILE
252: over @ over 0 wordlist-link - wordlist-id !
253: swap cell+ swap
254: REPEAT
255: drop
256: \ rehash wordlists to remove forgotten words
257: \ why don't we do this in a single step? - anton
258: voclink
259: BEGIN
260: @ dup
261: WHILE
262: dup 0 wordlist-link - rehash
263: REPEAT
264: drop
265: \ restore udp and dp
266: [IFDEF] forget-dyncode
267: dup cell+ @ forget-dyncode drop
268: [THEN]
269: @ udp ! dp !
270: \ clean up vocabulary stack
271: 0 vp @ 0
272: ?DO
273: vp cell+ I cells + @ dup here >
274: IF drop ELSE swap 1+ THEN
275: LOOP
276: dup 0= or set-order \ -1 set-order if order is empty
277: get-current here > IF
278: forth-wordlist set-current
279: THEN ;
280:
281: : marker ( "<spaces> name" -- ) \ core-ext
282: \G Create a definition, @i{name} (called a @i{mark}) whose
283: \G execution semantics are to remove itself and everything
284: \G defined after it.
285: marker, Create A,
286: DOES> ( -- )
287: @ marker! ;
288:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>