Annotation of gforth/kernel/input.fs, revision 1.12
1.1 pazsan 1: \ Input handling (object oriented) 22oct00py
2:
1.10 anton 3: \ Copyright (C) 2000,2003,2004 Free Software Foundation, Inc.
1.1 pazsan 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
1.4 pazsan 29: input-method source ( -- addr u ) \ core-ext,file source
30: \G Return address @i{addr} and length @i{u} of the current input
31: \G buffer
1.1 pazsan 32: input-method refill ( -- flag ) \ core-ext,block-ext,file-ext
33: \G Attempt to fill the input buffer from the input source. When
34: \G the input source is the user input device, attempt to receive
35: \G input into the terminal input device. If successful, make the
36: \G result the input buffer, set @code{>IN} to 0 and return true;
37: \G otherwise return false. When the input source is a block, add 1
38: \G to the value of @code{BLK} to make the next block the input
39: \G source and current input buffer, and set @code{>IN} to 0;
40: \G return true if the new value of @code{BLK} is a valid block
41: \G number, false otherwise. When the input source is a text file,
42: \G attempt to read the next line from the file. If successful,
43: \G make the result the current input buffer, set @code{>IN} to 0
44: \G and return true; otherwise, return false. A successful result
45: \G includes receipt of a line containing 0 characters.
46: input-method source-id ( -- 0 | -1 | fileid ) \ core-ext,file source-i-d
47: \G Return 0 (the input source is the user input device), -1 (the
48: \G input source is a string being processed by @code{evaluate}) or
49: \G a @i{fileid} (the input source is the file specified by
50: \G @i{fileid}).
51: | input-method (save-input) ( -- x1 .. xn n ) \ gforth
52: | input-method (restore-input) ( x1 .. xn n -- ) \ gforth
53: drop
54:
55: cell \ the first cell points to the method table
1.11 anton 56: cell input-var >in ( -- addr ) \ core to-in
1.1 pazsan 57: \G @code{input-var} variable -- @i{a-addr} is the address of a
58: \G cell containing the char offset from the start of the input
59: \G buffer to the start of the parse area.
1.11 anton 60: cell input-var #tib ( -- addr ) \ core-ext number-t-i-b
1.1 pazsan 61: \G @code{input-var} variable -- @i{a-addr} is the address of a
62: \G cell containing the number of characters in the terminal input
63: \G buffer. OBSOLESCENT: @code{source} superceeds the function of
64: \G this word.
1.11 anton 65: cell input-var max#tib ( -- addr ) \ gforth max-number-t-i-b
1.1 pazsan 66: \G @code{input-var} variable -- This cell contains the maximum
67: \G size of the current tib.
1.11 anton 68: cell input-var old-input ( -- addr ) \ gforth
1.1 pazsan 69: \G @code{input-var} variable -- This cell contains the pointer to
70: \G the previous input buffer
1.11 anton 71: cell input-var loadline ( -- addr ) \ gforth
1.1 pazsan 72: \G @code{input-var} variable -- This cell contains the line that's
73: \G currently loaded from
74: has? file [IF]
1.11 anton 75: cell input-var loadfile ( -- addr ) \ gforth
1.1 pazsan 76: \G @code{input-var} variable -- This cell contains the file the
77: \G input buffer is associated with (0 if none)
1.11 anton 78: cell input-var blk ( -- addr ) \ block
1.1 pazsan 79: \G @code{input-var} variable -- This cell contains the current
80: \G block number
1.11 anton 81: cell input-var #fill-bytes ( -- addr ) \ gforth
1.1 pazsan 82: \G @code{input-var} variable -- number of bytes read via
83: \G (read-line) by the last refill
1.11 anton 84: 2 cells input-var loadfilename ( -- addr ) \ gforth
1.5 anton 85: \G @code{input-var} variable -- addr u describes name of currently
86: \G interpreted input (file name or somesuch)
1.1 pazsan 87: [THEN]
1.11 anton 88: 0 input-var tib ( -- addr ) \ core
1.1 pazsan 89:
90: Constant tib+
91:
92: \ terminal input implementation
93:
1.4 pazsan 94: :noname ( in 1 -- ) 1 <> -12 and throw >in ! ;
1.1 pazsan 95: \ restore-input
1.4 pazsan 96: :noname ( -- in 1 ) >in @ 1 ; \ save-input
1.2 pazsan 97: ' false \ source-id
1.4 pazsan 98: :noname ( -- flag ) [ has? file [IF] ]
1.3 pazsan 99: stdin file-eof? IF false EXIT THEN [ [THEN] ]
100: tib max#tib @ accept #tib !
1.1 pazsan 101: >in off true 1 loadline +! ; \ refill
1.4 pazsan 102: :noname ( -- addr u ) tib #tib @ ; \ source
1.1 pazsan 103:
104: | Create terminal-input A, A, A, A, A,
1.4 pazsan 105: :noname ( -- addr u ) tib @ #tib @ ; \ source
1.1 pazsan 106: | Create evaluate-input
1.2 pazsan 107: A, \ source
108: ' false A, \ refill
109: ' true A, \ source-id
110: terminal-input 3 cells + @ A, \ terminal::restore-input
111: terminal-input 4 cells + @ A, \ terminal::save-input
1.1 pazsan 112:
113: \ file input implementation
114:
115: has? file [IF]
1.11 anton 116: : read-line ( c_addr u1 wfileid -- u2 flag wior ) \ file
117: (read-line) nip ;
1.3 pazsan 118:
1.4 pazsan 119: :noname ( in line# udpos 4 -- ) 4 <> -12 and throw
1.1 pazsan 120: loadfile @ reposition-file throw
121: refill 0= -36 and throw \ should never throw
122: loadline ! >in ! ; \ restore-input
1.4 pazsan 123: :noname ( -- in line# udpos 4 ) >in @ sourceline#
1.1 pazsan 124: loadfile @ file-position throw #fill-bytes @ 0 d-
125: 4 ; \ save-input
1.4 pazsan 126: :noname ( -- file ) loadfile @ ; \ source-id
127: :noname ( -- flag )
128: #tib off #fill-bytes off >in off
1.3 pazsan 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 +! ;
1.1 pazsan 138: \ 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 @ ! ;
1.3 pazsan 152: : expand-tib ( n -- )
153: dup tib+ + current-input @ swap resize throw current-input !
154: max#tib ! tib max#tib @ #tib @ /string 0 fill ;
1.1 pazsan 155: 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 ;
1.5 anton 195: \ s" *the terminal*" loadfilename 2!
1.1 pazsan 196:
1.11 anton 197: : execute-parsing ( ... addr u xt -- ... ) \ gforth
1.6 anton 198: \G Make @i{addr u} the current input source, execute @i{xt @code{(
199: \G ... -- ... )}}, then restore the previous input source.
200: >r evaluate-input cell new-tib
1.1 pazsan 201: [ has? file [IF] ]
1.5 anton 202: s" *evaluated string*" loadfilename 2!
1.1 pazsan 203: [ [THEN] ]
1.2 pazsan 204: -1 loadline ! #tib ! tib !
1.6 anton 205: r> catch pop-file throw ;
206:
207: : evaluate ( ... addr u -- ... ) \ core,block
208: \G Save the current input source specification. Store @code{-1} in
209: \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
210: \G @code{0} and make the string @i{c-addr u} the input source and
211: \G input buffer. Interpret. When the parse area is empty, restore the
212: \G input source specification.
213: ['] interpret execute-parsing ;
1.1 pazsan 214:
215: \ clear tibstack
216:
217: : clear-tibstack ( -- ) \ gforth
218: \G clears the tibstack; if there is none, create the bottom entry:
219: \G the terminal input buffer.
220: current-input @ 0= IF create-input THEN
221: BEGIN old-input @ WHILE 0 pop-file drop REPEAT ;
222:
223: : query ( -- ) \ core-ext
224: \G Make the user input device the input source. Receive input into
225: \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
226: \G superceeded by @code{accept}.
1.12 ! anton 227: clear-tibstack refill 0= -39 and throw ;
1.1 pazsan 228:
229: \ load a file
230:
231: has? file [IF]
1.9 anton 232: defer line-end-hook ( -- ) \ gforth
233: \G called at every end-of-line when text-interpreting from a file
234: \ alternatively we could use a wrapper for REFILL
235: ' noop is line-end-hook
236:
1.1 pazsan 237: : read-loop ( i*x -- j*x ) \ gforth
238: \G refill and interpret a file until EOF
1.9 anton 239: BEGIN refill WHILE interpret line-end-hook REPEAT ;
1.1 pazsan 240:
1.7 anton 241: : execute-parsing-named-file ( i*x wfileid filename-addr filename-u xt -- j*x )
242: >r push-file \ dup 2* cells included-files 2@ drop + 2@ type
1.5 anton 243: loadfilename 2! loadfile !
1.7 anton 244: r> catch
1.1 pazsan 245: loadfile @ close-file swap 2dup or
246: pop-file drop throw throw ;
247:
1.11 anton 248: : execute-parsing-file ( i*x fileid xt -- j*x ) \ gforth
1.7 anton 249: \G Make @i{fileid} the current input source, execute @i{xt @code{( i*x
250: \G -- j*x )}}, then restore the previous input source.
251: s" *a file*" rot execute-parsing-named-file ;
252:
1.11 anton 253: : include-file ( i*x wfileid -- j*x ) \ file
1.1 pazsan 254: \G Interpret (process using the text interpreter) the contents of
255: \G the file @var{wfileid}.
1.7 anton 256: ['] read-loop execute-parsing-file ;
1.1 pazsan 257: [THEN]
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>