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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help