File:
[gforth] /
gforth /
blocks.fs
Revision
1.34:
download - view:
text,
annotated -
select for diffs
Sun Oct 29 20:27:02 2000 UTC (22 years, 4 months ago) by
pazsan
Branches:
MAIN
CVS tags:
HEAD
Added new input handling (OO approach). Only available if capability
new-input is true (so setting that to false in machpc.fs gets you the old
input handling back).
1: \ A less simple implementation of the blocks wordset.
2:
3: \ Copyright (C) 1995,1996,1997,1998,2000 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 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., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20:
21:
22: \ A more efficient implementation would use mmap on OSs that
23: \ provide it and many buffers on OSs that do not provide mmap.
24:
25: \ Now, the replacement algorithm is "direct mapped"; change to LRU
26: \ if too slow. Using more buffers helps, too.
27:
28: \ I think I avoid the assumption 1 char = 1 here, but I have not tested this
29:
30: \ 1024 constant chars/block \ mandated by the standard
31:
32: require struct.fs
33:
34: struct
35: cell% field buffer-block \ the block number
36: cell% field buffer-fid \ the block's fid
37: cell% field buffer-dirty \ the block dirty flag
38: char% chars/block * field block-buffer \ the data
39: cell% 0 * field next-buffer
40: end-struct buffer-struct
41:
42: Variable block-buffers
43: Variable last-block
44:
45: $20 Value buffers
46:
47: User block-fid
48: User block-offset ( -- addr ) \ gforth
49: \G User variable containing the number of the first block (default
50: \G since 0.5.0: 0). Block files created with Gforth versions before
51: \G 0.5.0 have the offset 1. If you use these files you can: @code{1
52: \G offset !}; or add 1 to every block number used; or prepend 1024
53: \G characters to the file.
54: 0 block-offset ! \ store 1 here fore 0.4.0 compatibility
55:
56: ' block-offset alias offset \ !! eliminate this?
57:
58: : block-cold ( -- )
59: block-fid off last-block off
60: buffer-struct buffers * %alloc dup block-buffers ! ( addr )
61: buffer-struct %size buffers * erase ;
62:
63: ' block-cold INIT8 chained
64:
65: block-cold
66:
67: Defer flush-blocks ( -- ) \ gforth
68:
69: : open-blocks ( c-addr u -- ) \ gforth
70: \g Use the file, whose name is given by @i{c-addr u}, as the blocks file.
71: 2dup open-fpath-file 0<>
72: if
73: r/w bin create-file throw
74: else
75: rot close-file throw 2dup file-status throw bin open-file throw
76: >r 2drop r>
77: then
78: block-fid @ IF flush-blocks block-fid @ close-file throw THEN
79: block-fid ! ;
80:
81: : use ( "file" -- ) \ gforth
82: \g Use @i{file} as the blocks file.
83: name open-blocks ;
84:
85: \ the file is opened as binary file, since it either will contain text
86: \ without newlines or binary data
87: : get-block-fid ( -- wfileid ) \ gforth
88: \G Return the file-id of the current blocks file. If no blocks
89: \G file has been opened, use @file{blocks.fb} as the default
90: \G blocks file.
91: block-fid @ 0=
92: if
93: s" blocks.fb" open-blocks
94: then
95: block-fid @ ;
96:
97: : block-position ( u -- ) \ block
98: \G Position the block file to the start of block @i{u}.
99: offset @ - chars/block chars um* get-block-fid reposition-file throw ;
100:
101: : update ( -- ) \ block
102: \G Mark the state of the current block buffer as assigned-dirty.
103: last-block @ ?dup IF buffer-dirty on THEN ;
104:
105: : save-buffer ( buffer -- ) \ gforth
106: >r
107: r@ buffer-dirty @ r@ buffer-block @ 0<> and
108: if
109: r@ buffer-block @ block-position
110: r@ block-buffer chars/block r@ buffer-fid @ write-file throw
111: r@ buffer-dirty off
112: endif
113: rdrop ;
114:
115: : empty-buffer ( buffer -- ) \ gforth
116: buffer-block off ;
117:
118: : save-buffers ( -- ) \ block
119: \G Transfer the contents of each @code{update}d block buffer to
120: \G mass storage, then mark all block buffers as assigned-clean.
121: block-buffers @
122: buffers 0 ?DO dup save-buffer next-buffer LOOP drop ;
123:
124: : empty-buffers ( -- ) \ block-ext
125: \G Mark all block buffers as unassigned; if any had been marked as
126: \G assigned-dirty (by @code{update}), the changes to those blocks
127: \G will be lost.
128: block-buffers @
129: buffers 0 ?DO dup empty-buffer next-buffer LOOP drop ;
130:
131: : flush ( -- ) \ block
132: \G Perform the functions of @code{save-buffers} then
133: \G @code{empty-buffers}.
134: save-buffers
135: empty-buffers ;
136:
137: ' flush IS flush-blocks
138:
139: : get-buffer ( u -- a-addr ) \ gforth
140: 0 buffers um/mod drop buffer-struct %size * block-buffers @ + ;
141:
142: : block ( u -- a-addr ) \ gforthman- block
143: \G If a block buffer is assigned for block @i{u}, return its
144: \G start address, @i{a-addr}. Otherwise, assign a block buffer
145: \G for block @i{u} (if the assigned block buffer has been
146: \G @code{update}d, transfer the contents to mass storage), read
147: \G the block into the block buffer and return its start address,
148: \G @i{a-addr}.
149: dup offset @ u< -35 and throw
150: dup get-buffer >r
151: dup r@ buffer-block @ <>
152: r@ buffer-fid @ block-fid @ <> or
153: if
154: r@ save-buffer
155: dup block-position
156: r@ block-buffer chars/block get-block-fid read-file throw
157: \ clear the rest of the buffer if the file is too short
158: r@ block-buffer over chars + chars/block rot chars - blank
159: r@ buffer-block !
160: get-block-fid r@ buffer-fid !
161: else
162: drop
163: then
164: r> dup last-block ! block-buffer ;
165:
166: : buffer ( u -- a-addr ) \ block
167: \G If a block buffer is assigned for block @i{u}, return its
168: \G start address, @i{a-addr}. Otherwise, assign a block buffer
169: \G for block @i{u} (if the assigned block buffer has been
170: \G @code{update}d, transfer the contents to mass storage) and
171: \G return its start address, @i{a-addr}. The subtle difference
172: \G between @code{buffer} and @code{block} mean that you should
173: \G only use @code{buffer} if you don't care about the previous
174: \G contents of block @i{u}. In Gforth, this simply calls
175: \G @code{block}.
176: \ reading in the block is unnecessary, but simpler
177: block ;
178:
179: User scr ( -- a-addr ) \ block-ext s-c-r
180: \G @code{User} variable -- @i{a-addr} is the address of a cell containing
181: \G the block number of the block most recently processed by
182: \G @code{list}.
183: 0 scr !
184:
185: \ nac31Mar1999 moved "scr @" to list to make the stack comment correct
186: : updated? ( n -- f ) \ gforth
187: \G Return true if @code{updated} has been used to mark block @i{n}
188: \G as assigned-dirty.
189: buffer
190: [ 0 buffer-dirty 0 block-buffer - ] Literal + @ ;
191:
192: : list ( u -- ) \ block-ext
193: \G Display block @i{u}. In Gforth, the block is displayed as 16
194: \G numbered lines, each of 64 characters.
195: \ calling block again and again looks inefficient but is necessary
196: \ in a multitasking environment
197: dup scr !
198: ." Screen " u.
199: scr @ updated? 0= IF ." not " THEN ." modified " cr
200: 16 0
201: ?do
202: i 2 .r space scr @ block i 64 * chars + 64 type cr
203: loop ;
204:
205: [IFDEF] current-input
206: :noname 2 <> -12 and throw >in ! blk ! ;
207: \ restore-input
208: :noname blk @ >in @ 2 ; \ save-input
209: :noname 2 ; \ source-id "*a block*"
210: :noname 1 blk +! 1 loadline +! true ; \ refill
211: :noname blk @ block chars/block ; \ source
212:
213: Create block-input A, A, A, A, A,
214:
215: : load ( i*x n -- j*x ) \ block
216: \G Save the current input source specification. Store @i{n} in
217: \G @code{BLK}, set @code{>IN} to 0 and interpret. When the parse
218: \G area is exhausted, restore the input source specification.
219: block-input 0 new-tib dup loadline ! blk ! 2 loadfilename# !
220: ['] interpret catch pop-file throw ;
221: [ELSE]
222: : (source) ( -- c-addr u )
223: blk @ ?dup
224: IF block chars/block
225: ELSE tib #tib @
226: THEN ;
227:
228: ' (source) IS source ( -- c-addr u ) \ core
229: \G @i{c-addr} is the address of the input buffer and @i{u} is the
230: \G number of characters in it.
231:
232: : load ( i*x n -- j*x ) \ block
233: \G Save the current input source specification. Store @i{n} in
234: \G @code{BLK}, set @code{>IN} to 0 and interpret. When the parse
235: \G area is exhausted, restore the input source specification.
236: loadfilename# @ >r
237: 2 loadfilename# ! \ "*a block*"
238: push-file
239: dup loadline ! blk ! >in off ['] interpret catch
240: pop-file
241: r> loadfilename# !
242: throw ;
243: [THEN]
244:
245: : thru ( i*x n1 n2 -- j*x ) \ block-ext
246: \G @code{load} the blocks @i{n1} through @i{n2} in sequence.
247: 1+ swap ?DO I load LOOP ;
248:
249: : +load ( i*x n -- j*x ) \ gforth
250: \G Used within a block to load the block specified as the
251: \G current block + @i{n}.
252: blk @ + load ;
253:
254: : +thru ( i*x n1 n2 -- j*x ) \ gforth
255: \G Used within a block to load the range of blocks specified as the
256: \G current block + @i{n1} thru the current block + @i{n2}.
257: 1+ swap ?DO I +load LOOP ;
258:
259: : --> ( -- ) \ gforthman- gforth chain
260: \G If this symbol is encountered whilst loading block @i{n},
261: \G discard the remainder of the block and load block @i{n+1}. Used
262: \G for chaining multiple blocks together as a single loadable
263: \G unit. Not recommended, because it destroys the independence of
264: \G loading. Use @code{thru} (which is standard) or @code{+thru}
265: \G instead.
266: refill drop ; immediate
267:
268: : block-included ( a-addr u -- ) \ gforth
269: \G Use within a block that is to be processed by @code{load}. Save
270: \G the current blocks file specification, open the blocks file
271: \G specified by @i{a-addr u} and @code{load} block 1 from that
272: \G file (which may in turn chain or load other blocks). Finally,
273: \G close the blocks file and restore the original blocks file.
274: block-fid @ >r block-fid off open-blocks
275: 1 load block-fid @ close-file throw flush
276: r> block-fid ! ;
277:
278: \ thrown out because it may provide unpleasant surprises - anton
279: \ : include ( "name" -- )
280: \ name 2dup dup 3 - /string s" .fb" compare
281: \ 0= IF block-included ELSE included THEN ;
282:
283: get-current environment-wordlist set-current
284: true constant block
285: true constant block-ext
286: set-current
287:
288: : bye ( -- ) \ tools-ext
289: \G Return control to the host operating system (if any).
290: ['] flush catch drop bye ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>