[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 :     \G Parse a string ccc delimited by a ) (right parenthesis). Display
30 :     \G the string. This is often used to display progress information
31 :     \G during compilation. See examples below.
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.32 \G Compile appropriate code such that, at run-time, cell pair w1, w2 are
40 :     \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 :     \G Compilation: parse a string ccc delimited by a " (double quote). At
80 :     \G run-time, return c-addr which specifies the counted string ccc.
81 :     \G Interpretation semantics are undefined.
82 : anton 1.11 [char] " parse postpone CLiteral ; immediate restrict
83 : anton 1.1
84 :     \ [COMPILE] 17may93jaw
85 :    
86 : anton 1.11 : [compile] ( compilation "name" -- ; run-time ? -- ? ) \ core-ext bracket-compile
87 : pazsan 1.24 comp' drop compile, ; immediate
88 : anton 1.1
89 :     \ CONVERT 17may93jaw
90 :    
91 : anton 1.11 : convert ( ud1 c-addr1 -- ud2 c-addr2 ) \ core-ext
92 : anton 1.26 \G obsolescent; superseded by @code{>number}.
93 :     char+ true >number drop ;
94 : anton 1.1
95 :     \ ERASE 17may93jaw
96 :    
97 : anton 1.11 : erase ( addr len -- ) \ core-ext
98 : crook 1.32 \G If len>0, clear all bits in each location of a memory region
99 :     \G of len address units starting at address addr.
100 : anton 1.11 \ !! dependence on "1 chars 1 ="
101 :     ( 0 1 chars um/mod nip ) 0 fill ;
102 :     : blank ( addr len -- ) \ string
103 : crook 1.32 \G If len>0, store the character value for a space in each
104 :     \G location of a memory region
105 :     \G of len character units starting at address addr.
106 : anton 1.11 bl fill ;
107 : anton 1.1
108 : pazsan 1.7 \ SEARCH 02sep94py
109 :    
110 : anton 1.28 : search ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 flag ) \ string
111 : crook 1.32 \G Search the string specified by c-addr1, u1 for the string
112 :     \G speficied by c-addr2, u2. If flag is true: match was found
113 :     \G at c-addr3 with u3 characters remaining. If flag is false:
114 :     \G no match was found; c-addr3, u3 are equal to c-addr1, u1.
115 : anton 1.28 \ not very efficient; but if we want efficiency, we'll do it as primitive
116 :     2>r 2dup
117 :     begin
118 :     dup r@ >=
119 :     while
120 :     over 2r@ swap -text 0= if
121 :     2swap 2drop 2r> 2drop true exit
122 :     endif
123 :     1 /string
124 :     repeat
125 :     2drop 2r> 2drop false ;
126 : pazsan 1.7
127 : anton 1.1 \ SOURCE-ID SAVE-INPUT RESTORE-INPUT 11jun93jaw
128 :    
129 : anton 1.22 : source-id ( -- 0 | -1 | fileid ) \ core-ext,file source-i-d
130 : anton 1.13 loadfile @ dup 0= IF drop sourceline# 0 min THEN ;
131 : anton 1.1
132 : anton 1.11 : save-input ( -- x1 .. xn n ) \ core-ext
133 : anton 1.14 >in @
134 :     loadfile @
135 :     if
136 :     loadfile @ file-position throw
137 :     else
138 :     blk @
139 :     linestart @
140 :     then
141 :     sourceline#
142 :     >tib @
143 :     source-id
144 :     6 ;
145 : anton 1.1
146 : anton 1.11 : restore-input ( x1 .. xn n -- flag ) \ core-ext
147 : anton 1.14 6 <> -12 and throw
148 :     source-id <> -12 and throw
149 :     >tib !
150 :     >r ( line# )
151 :     loadfile @ 0<>
152 :     if
153 :     loadfile @ reposition-file throw
154 :     else
155 :     linestart !
156 :     blk !
157 :     sourceline# r@ <> blk @ 0= and loadfile @ 0= and
158 :     if
159 :     drop rdrop true EXIT
160 :     then
161 :     then
162 :     r> loadline !
163 :     >in !
164 :     false ;
165 :    
166 : anton 1.1 \ This things we don't need, but for being complete... jaw
167 :    
168 :     \ EXPECT SPAN 17may93jaw
169 :    
170 : anton 1.11 variable span ( -- a-addr ) \ core-ext
171 :     \ obsolescent
172 : anton 1.1
173 : anton 1.11 : expect ( c-addr +len -- ) \ core-ext
174 :     \ obsolescent; use accept
175 : anton 1.9 0 rot over
176 :     BEGIN ( maxlen span c-addr pos1 )
177 :     key decode ( maxlen span c-addr pos2 flag )
178 :     >r 2over = r> or
179 :     UNTIL
180 : pazsan 1.17 2 pick swap /string type
181 :     nip span ! ;
182 : jwilke 1.27
183 :     \ marker 18dec94py
184 :    
185 :     \ Marker creates a mark that is removed (including everything
186 :     \ defined afterwards) when executing the mark.
187 :    
188 : anton 1.31 : included-files-mark ( -- u )
189 :     included-files 2@ nip
190 :     blk @ 0=
191 :     if \ not input from blocks
192 :     source-id 1 -1 within
193 :     if \ input from file
194 :     1- \ do not include the last file (hopefully this is the
195 :     \ currently included file)
196 :     then
197 :     then ;
198 :    
199 :     \ hmm, most of the saving appears to be pretty unnecessary: we could
200 :     \ derive the wordlists and the words that have to be kept from the
201 :     \ saved value of dp value. - anton
202 :    
203 :     : marker, ( -- mark )
204 :     here
205 :     included-files-mark ,
206 :     dup A, \ here
207 :     voclink @ A, \ vocabulary list start
208 :     \ for all wordlists, remember wordlist-id (the linked list)
209 :     voclink
210 :     BEGIN
211 :     @ dup
212 :     WHILE
213 :     dup 0 wordlist-link - wordlist-id @ A,
214 :     REPEAT
215 :     drop
216 :     \ remember udp
217 :     udp @ , ;
218 : jwilke 1.27
219 :     : marker! ( mark -- )
220 : anton 1.31 \ reset included files count; resize will happen on next add-included-file
221 :     included-files 2@ drop over @ included-files 2! cell+
222 :     \ rest of marker!
223 :     dup @ swap cell+ ( here rest-of-marker )
224 : jwilke 1.27 dup @ voclink ! cell+
225 : anton 1.31 \ restore wordlists to former words
226 : jwilke 1.27 voclink
227 :     BEGIN
228 :     @ dup
229 :     WHILE
230 : anton 1.31 over @ over 0 wordlist-link - wordlist-id !
231 : jwilke 1.27 swap cell+ swap
232 :     REPEAT
233 : anton 1.31 drop
234 :     \ rehash wordlists to remove forgotten words
235 :     \ why don't we do this in a single step? - anton
236 :     voclink
237 : jwilke 1.27 BEGIN
238 :     @ dup
239 :     WHILE
240 :     dup 0 wordlist-link - rehash
241 :     REPEAT
242 :     drop
243 : anton 1.31 \ restore udp and dp
244 : pazsan 1.29 @ udp ! dp !
245 :     \ clean up vocabulary stack
246 :     0 vp @ 0
247 :     ?DO
248 :     vp cell+ I cells + @ dup here >
249 :     IF drop ELSE swap 1+ THEN
250 :     LOOP
251 :     dup 0= or set-order \ -1 set-order if order is empty
252 :     get-current here > IF
253 :     forth-wordlist set-current
254 :     THEN ;
255 : jwilke 1.27
256 : crook 1.32 : marker ( "<spaces> name" -- ) \ core-ext
257 :     \G Create a definition, @var{name} (called a @var{mark}) whose
258 :     \G execution semantics are to remove itself and everything
259 :     \G defined after it.
260 : jwilke 1.27 marker, Create A,
261 :     DOES> ( -- )
262 :     @ marker! ;
263 : anton 1.1

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help