[gforth] / gforth / extend.fs  

gforth: gforth/extend.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help