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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help