[gforth] / gforth / kernel / input.fs  

gforth: gforth/kernel/input.fs


1 : pazsan 1.1 \ Input handling (object oriented) 22oct00py
2 :    
3 :     \ Copyright (C) 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 :     \ input handling structure:
22 :    
23 :     | : input-method ( m "name" -- m' ) Create dup , cell+
24 :     DOES> ( ... -- ... ) @ current-input @ @ + perform ;
25 :     | : input-var ( v size "name" -- v' ) Create over , +
26 :     DOES> ( -- addr ) @ current-input @ + ;
27 :    
28 :     0
29 :     input-method source ( -- 0 | -1 | fileid ) \ core-ext,file source-i-d
30 :     \G Return 0 (the input source is the user input device), -1 (the
31 :     \G input source is a string being processed by @code{evaluate}) or
32 :     \G a @i{fileid} (the input source is the file specified by
33 :     \G @i{fileid}).
34 :     input-method refill ( -- flag ) \ core-ext,block-ext,file-ext
35 :     \G Attempt to fill the input buffer from the input source. When
36 :     \G the input source is the user input device, attempt to receive
37 :     \G input into the terminal input device. If successful, make the
38 :     \G result the input buffer, set @code{>IN} to 0 and return true;
39 :     \G otherwise return false. When the input source is a block, add 1
40 :     \G to the value of @code{BLK} to make the next block the input
41 :     \G source and current input buffer, and set @code{>IN} to 0;
42 :     \G return true if the new value of @code{BLK} is a valid block
43 :     \G number, false otherwise. When the input source is a text file,
44 :     \G attempt to read the next line from the file. If successful,
45 :     \G make the result the current input buffer, set @code{>IN} to 0
46 :     \G and return true; otherwise, return false. A successful result
47 :     \G includes receipt of a line containing 0 characters.
48 :     input-method source-id ( -- 0 | -1 | fileid ) \ core-ext,file source-i-d
49 :     \G Return 0 (the input source is the user input device), -1 (the
50 :     \G input source is a string being processed by @code{evaluate}) or
51 :     \G a @i{fileid} (the input source is the file specified by
52 :     \G @i{fileid}).
53 :     | input-method (save-input) ( -- x1 .. xn n ) \ gforth
54 :     | input-method (restore-input) ( x1 .. xn n -- ) \ gforth
55 :     drop
56 :    
57 :     cell \ the first cell points to the method table
58 :     cell input-var >in \ core to-in
59 :     \G @code{input-var} variable -- @i{a-addr} is the address of a
60 :     \G cell containing the char offset from the start of the input
61 :     \G buffer to the start of the parse area.
62 :     cell input-var #tib \ core-ext number-t-i-b
63 :     \G @code{input-var} variable -- @i{a-addr} is the address of a
64 :     \G cell containing the number of characters in the terminal input
65 :     \G buffer. OBSOLESCENT: @code{source} superceeds the function of
66 :     \G this word.
67 :     cell input-var max#tib \ gforth max-number-t-i-b
68 :     \G @code{input-var} variable -- This cell contains the maximum
69 :     \G size of the current tib.
70 :     cell input-var old-input \ gforth
71 :     \G @code{input-var} variable -- This cell contains the pointer to
72 :     \G the previous input buffer
73 :     cell input-var loadline \ gforth
74 :     \G @code{input-var} variable -- This cell contains the line that's
75 :     \G currently loaded from
76 :     has? file [IF]
77 :     cell input-var loadfile \ gforth
78 :     \G @code{input-var} variable -- This cell contains the file the
79 :     \G input buffer is associated with (0 if none)
80 :     cell input-var blk \ block
81 :     \G @code{input-var} variable -- This cell contains the current
82 :     \G block number
83 :     cell input-var #fill-bytes \ gforth
84 :     \G @code{input-var} variable -- number of bytes read via
85 :     \G (read-line) by the last refill
86 :     cell input-var loadfilename# \ gforth
87 :     \G @code{input-var} variable -- This cell contains the index into
88 :     \G the file name array and allows to identify the loaded file.
89 :     [THEN]
90 :     0 input-var tib
91 :    
92 :     Constant tib+
93 :    
94 :     \ terminal input implementation
95 :    
96 :     :noname 1 <> -12 and throw >in ! ;
97 :     \ restore-input
98 :     :noname >in @ 1 ; \ save-input
99 : pazsan 1.2 ' false \ source-id
100 : pazsan 1.3 :noname [ has? file [IF] ]
101 :     stdin file-eof? IF false EXIT THEN [ [THEN] ]
102 :     tib max#tib @ accept #tib !
103 : pazsan 1.1 >in off true 1 loadline +! ; \ refill
104 :     :noname tib #tib @ ; \ source
105 :    
106 :     | Create terminal-input A, A, A, A, A,
107 : pazsan 1.2 :noname tib @ #tib @ ; \ source
108 : pazsan 1.1 | Create evaluate-input
109 : pazsan 1.2 A, \ source
110 :     ' false A, \ refill
111 :     ' true A, \ source-id
112 :     terminal-input 3 cells + @ A, \ terminal::restore-input
113 :     terminal-input 4 cells + @ A, \ terminal::save-input
114 : pazsan 1.1
115 :     \ file input implementation
116 :    
117 :     has? file [IF]
118 : pazsan 1.3 : read-line ( c_addr u1 wfileid -- u2 flag wior ) (read-line) nip ;
119 :    
120 : pazsan 1.1 :noname 4 <> -12 and throw
121 :     loadfile @ reposition-file throw
122 :     refill 0= -36 and throw \ should never throw
123 :     loadline ! >in ! ; \ restore-input
124 :     :noname >in @ sourceline#
125 :     loadfile @ file-position throw #fill-bytes @ 0 d-
126 :     4 ; \ save-input
127 :     :noname loadfile @ ; \ source-id
128 : pazsan 1.3 :noname #tib off #fill-bytes off >in off
129 :     BEGIN
130 :     tib max#tib @ #tib @ /string
131 :     loadfile @ (read-line) throw #fill-bytes +!
132 :     swap #tib +!
133 :     \ auto-expanding the tib
134 :     dup #tib @ #fill-bytes @ = and WHILE
135 :     drop max#tib @ 2* expand-tib
136 :     REPEAT
137 :     1 loadline +! ;
138 : pazsan 1.1 \ refill
139 :     terminal-input @ \ source -> terminal-input::source
140 :    
141 :     | Create file-input A, A, A, A, A,
142 :     [THEN]
143 :    
144 :     \ push-file, pop-file
145 :    
146 :     : new-tib ( method n -- ) \ gforth
147 :     \G Create a new entry of the tib stack, size @i{n}, method table
148 :     \G @i{method}.
149 :     dup >r tib+ + dup allocate throw tuck swap 0 fill
150 :     current-input @ swap current-input ! old-input ! r> max#tib !
151 :     current-input @ ! ;
152 : pazsan 1.3 : expand-tib ( n -- )
153 :     dup tib+ + current-input @ swap resize throw current-input !
154 :     max#tib ! tib max#tib @ #tib @ /string 0 fill ;
155 : pazsan 1.1 has? file [IF]
156 :     : push-file ( -- ) \ gforth
157 :     \G Create a new file input buffer
158 :     file-input def#tib new-tib ;
159 :     [THEN]
160 :     : pop-file ( throw-code -- throw-code ) \ gforth
161 :     \G pop and free the current top input buffer
162 :     dup IF
163 :     source >in @ sourceline#
164 :     [ has? file [IF] ] sourcefilename [ [THEN] ]
165 :     >error
166 :     THEN
167 :     current-input @ old-input @ current-input ! free throw ;
168 :    
169 :     \ save-input, restore-input
170 :    
171 :     : save-input ( -- x1 .. xn n ) \ core-ext
172 :     \G The @i{n} entries @i{xn - x1} describe the current state of the
173 :     \G input source specification, in some platform-dependent way that can
174 :     \G be used by @code{restore-input}.
175 :     (save-input) current-input @ swap 1+ ;
176 :     : restore-input ( x1 .. xn n -- flag ) \ core-ext
177 :     \G Attempt to restore the input source specification to the state
178 :     \G described by the @i{n} entries @i{xn - x1}. @i{flag} is true if
179 :     \G the restore fails. In Gforth with the new input code, it fails
180 :     \G only with a flag that can be used to throw again; it is also
181 :     \G possible to save and restore between different active input
182 :     \G streams. Note that closing the input streams must happen in the
183 :     \G reverse order as they have been opened, but in between
184 :     \G everything is allowed.
185 :     current-input @ >r swap current-input ! 1- dup >r
186 :     ['] (restore-input) catch
187 :     dup IF r> 0 ?DO nip LOOP r> current-input ! EXIT THEN
188 :     rdrop rdrop ;
189 :    
190 :     \ create terminal input block
191 :    
192 :     : create-input ( -- )
193 :     \G create a new terminal input
194 :     terminal-input def#tib new-tib ;
195 :     ( loadfilename# off ) \ "*the terminal*"
196 :    
197 :     : evaluate ( addr u -- ) \ core,block
198 :     \G Save the current input source specification. Store @code{-1} in
199 :     \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
200 :     \G @code{0} and make the string @i{c-addr u} the input source
201 :     \G and input buffer. Interpret. When the parse area is empty,
202 :     \G restore the input source specification.
203 : pazsan 1.2 evaluate-input cell new-tib
204 : pazsan 1.1 [ has? file [IF] ]
205 :     1 loadfilename# ! \ "*evaluated string*"
206 :     [ [THEN] ]
207 : pazsan 1.2 -1 loadline ! #tib ! tib !
208 : pazsan 1.1 ['] interpret catch pop-file throw ;
209 :    
210 :     \ clear tibstack
211 :    
212 :     : clear-tibstack ( -- ) \ gforth
213 :     \G clears the tibstack; if there is none, create the bottom entry:
214 :     \G the terminal input buffer.
215 :     current-input @ 0= IF create-input THEN
216 :     BEGIN old-input @ WHILE 0 pop-file drop REPEAT ;
217 :    
218 :     : query ( -- ) \ core-ext
219 :     \G Make the user input device the input source. Receive input into
220 :     \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
221 :     \G superceeded by @code{accept}.
222 :     clear-tibstack refill drop ;
223 :    
224 :     \ load a file
225 :    
226 :     has? file [IF]
227 :     : read-loop ( i*x -- j*x ) \ gforth
228 :     \G refill and interpret a file until EOF
229 :     BEGIN refill WHILE interpret REPEAT ;
230 :    
231 :     : include-file2 ( i*x wfileid loadfilename# -- j*x )
232 :     push-file \ dup 2* cells included-files 2@ drop + 2@ type
233 :     loadfilename# ! loadfile !
234 :     ['] read-loop catch
235 :     loadfile @ close-file swap 2dup or
236 :     pop-file drop throw throw ;
237 :    
238 :     : include-file ( i*x wfileid -- j*x )
239 :     \G Interpret (process using the text interpreter) the contents of
240 :     \G the file @var{wfileid}.
241 :     3 include-file2 ;
242 :     [THEN]

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help