Annotation of gforth/kernel/input.fs, revision 1.15
1.1 pazsan 1: \ Input handling (object oriented) 22oct00py
2:
1.13 anton 3: \ Copyright (C) 2000,2003,2004,2005 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.15 ! anton 60: cell input-var input-start-parse ( -- addr ) \ gforth
! 61: \G @code{input-var} variable -- @i{a-addr} is the address of a
! 62: \G cell containing a pointer to the start of the last parsed
! 63: \G string (but after skipped characters, if any); this is set
! 64: \G automatically by @code{parse}, @code{parse-name} and
! 65: \G @code{word}, but not if you set @code{>in} yourself.
1.11 anton 66: cell input-var #tib ( -- addr ) \ core-ext number-t-i-b
1.1 pazsan 67: \G @code{input-var} variable -- @i{a-addr} is the address of a
68: \G cell containing the number of characters in the terminal input
69: \G buffer. OBSOLESCENT: @code{source} superceeds the function of
70: \G this word.
1.11 anton 71: cell input-var max#tib ( -- addr ) \ gforth max-number-t-i-b
1.1 pazsan 72: \G @code{input-var} variable -- This cell contains the maximum
73: \G size of the current tib.
1.11 anton 74: cell input-var old-input ( -- addr ) \ gforth
1.1 pazsan 75: \G @code{input-var} variable -- This cell contains the pointer to
76: \G the previous input buffer
1.11 anton 77: cell input-var loadline ( -- addr ) \ gforth
1.1 pazsan 78: \G @code{input-var} variable -- This cell contains the line that's
79: \G currently loaded from
80: has? file [IF]
1.11 anton 81: cell input-var loadfile ( -- addr ) \ gforth
1.1 pazsan 82: \G @code{input-var} variable -- This cell contains the file the
83: \G input buffer is associated with (0 if none)
1.11 anton 84: cell input-var blk ( -- addr ) \ block
1.1 pazsan 85: \G @code{input-var} variable -- This cell contains the current
86: \G block number
1.11 anton 87: cell input-var #fill-bytes ( -- addr ) \ gforth
1.1 pazsan 88: \G @code{input-var} variable -- number of bytes read via
89: \G (read-line) by the last refill
1.11 anton 90: 2 cells input-var loadfilename ( -- addr ) \ gforth
1.5 anton 91: \G @code{input-var} variable -- addr u describes name of currently
92: \G interpreted input (file name or somesuch)
1.1 pazsan 93: [THEN]
1.11 anton 94: 0 input-var tib ( -- addr ) \ core
1.1 pazsan 95:
96: Constant tib+
97:
1.15 ! anton 98: \ helper words
! 99:
! 100: : input-start-line ( -- )
! 101: >in off input-start-parse off ;
! 102:
! 103: : start-lexeme ( c-addr -- )
! 104: \ record that the current lexeme starts at c-addr
! 105: source drop - input-start-parse ! ;
! 106:
1.1 pazsan 107: \ terminal input implementation
108:
1.4 pazsan 109: :noname ( in 1 -- ) 1 <> -12 and throw >in ! ;
1.1 pazsan 110: \ restore-input
1.4 pazsan 111: :noname ( -- in 1 ) >in @ 1 ; \ save-input
1.2 pazsan 112: ' false \ source-id
1.4 pazsan 113: :noname ( -- flag ) [ has? file [IF] ]
1.3 pazsan 114: stdin file-eof? IF false EXIT THEN [ [THEN] ]
115: tib max#tib @ accept #tib !
1.15 ! anton 116: input-start-line true 1 loadline +! ; \ refill
1.4 pazsan 117: :noname ( -- addr u ) tib #tib @ ; \ source
1.1 pazsan 118:
119: | Create terminal-input A, A, A, A, A,
1.4 pazsan 120: :noname ( -- addr u ) tib @ #tib @ ; \ source
1.1 pazsan 121: | Create evaluate-input
1.2 pazsan 122: A, \ source
123: ' false A, \ refill
124: ' true A, \ source-id
125: terminal-input 3 cells + @ A, \ terminal::restore-input
126: terminal-input 4 cells + @ A, \ terminal::save-input
1.1 pazsan 127:
128: \ file input implementation
129:
130: has? file [IF]
1.11 anton 131: : read-line ( c_addr u1 wfileid -- u2 flag wior ) \ file
132: (read-line) nip ;
1.3 pazsan 133:
1.4 pazsan 134: :noname ( in line# udpos 4 -- ) 4 <> -12 and throw
1.1 pazsan 135: loadfile @ reposition-file throw
136: refill 0= -36 and throw \ should never throw
137: loadline ! >in ! ; \ restore-input
1.4 pazsan 138: :noname ( -- in line# udpos 4 ) >in @ sourceline#
1.1 pazsan 139: loadfile @ file-position throw #fill-bytes @ 0 d-
140: 4 ; \ save-input
1.4 pazsan 141: :noname ( -- file ) loadfile @ ; \ source-id
142: :noname ( -- flag )
1.15 ! anton 143: #tib off #fill-bytes off input-start-line
1.3 pazsan 144: BEGIN
145: tib max#tib @ #tib @ /string
146: loadfile @ (read-line) throw #fill-bytes +!
147: swap #tib +!
148: \ auto-expanding the tib
149: dup #tib @ #fill-bytes @ = and WHILE
150: drop max#tib @ 2* expand-tib
151: REPEAT
152: 1 loadline +! ;
1.1 pazsan 153: \ refill
154: terminal-input @ \ source -> terminal-input::source
155:
156: | Create file-input A, A, A, A, A,
157: [THEN]
158:
159: \ push-file, pop-file
160:
161: : new-tib ( method n -- ) \ gforth
162: \G Create a new entry of the tib stack, size @i{n}, method table
163: \G @i{method}.
164: dup >r tib+ + dup allocate throw tuck swap 0 fill
165: current-input @ swap current-input ! old-input ! r> max#tib !
166: current-input @ ! ;
1.3 pazsan 167: : expand-tib ( n -- )
168: dup tib+ + current-input @ swap resize throw current-input !
169: max#tib ! tib max#tib @ #tib @ /string 0 fill ;
1.1 pazsan 170: has? file [IF]
171: : push-file ( -- ) \ gforth
172: \G Create a new file input buffer
173: file-input def#tib new-tib ;
174: [THEN]
175: : pop-file ( throw-code -- throw-code ) \ gforth
176: \G pop and free the current top input buffer
177: dup IF
1.15 ! anton 178: input-error-data >error
1.1 pazsan 179: THEN
180: current-input @ old-input @ current-input ! free throw ;
181:
182: \ save-input, restore-input
183:
184: : save-input ( -- x1 .. xn n ) \ core-ext
185: \G The @i{n} entries @i{xn - x1} describe the current state of the
186: \G input source specification, in some platform-dependent way that can
187: \G be used by @code{restore-input}.
188: (save-input) current-input @ swap 1+ ;
189: : restore-input ( x1 .. xn n -- flag ) \ core-ext
190: \G Attempt to restore the input source specification to the state
191: \G described by the @i{n} entries @i{xn - x1}. @i{flag} is true if
192: \G the restore fails. In Gforth with the new input code, it fails
193: \G only with a flag that can be used to throw again; it is also
194: \G possible to save and restore between different active input
195: \G streams. Note that closing the input streams must happen in the
196: \G reverse order as they have been opened, but in between
197: \G everything is allowed.
198: current-input @ >r swap current-input ! 1- dup >r
199: ['] (restore-input) catch
200: dup IF r> 0 ?DO nip LOOP r> current-input ! EXIT THEN
201: rdrop rdrop ;
202:
203: \ create terminal input block
204:
205: : create-input ( -- )
206: \G create a new terminal input
207: terminal-input def#tib new-tib ;
1.5 anton 208: \ s" *the terminal*" loadfilename 2!
1.1 pazsan 209:
1.14 anton 210: : execute-parsing-wrapper ( ... addr1 u1 xt addr2 u2 -- ... ) \ gforth-internal
211: \ addr1 u1 is the string to be processed, xt is the word for
212: \ processing it, addr2 u2 is the name of the input source
213: rot >r 2>r evaluate-input cell new-tib 2r>
1.1 pazsan 214: [ has? file [IF] ]
1.14 anton 215: loadfilename 2!
216: [ [ELSE] ]
217: 2drop
1.1 pazsan 218: [ [THEN] ]
1.2 pazsan 219: -1 loadline ! #tib ! tib !
1.6 anton 220: r> catch pop-file throw ;
1.14 anton 221:
222: : execute-parsing ( ... addr u xt -- ... ) \ gforth
223: \G Make @i{addr u} the current input source, execute @i{xt @code{(
224: \G ... -- ... )}}, then restore the previous input source.
225: s" *evaluated string*" execute-parsing-wrapper ;
1.6 anton 226:
227: : evaluate ( ... addr u -- ... ) \ core,block
228: \G Save the current input source specification. Store @code{-1} in
229: \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
230: \G @code{0} and make the string @i{c-addr u} the input source and
231: \G input buffer. Interpret. When the parse area is empty, restore the
232: \G input source specification.
233: ['] interpret execute-parsing ;
1.1 pazsan 234:
235: \ clear tibstack
236:
237: : clear-tibstack ( -- ) \ gforth
238: \G clears the tibstack; if there is none, create the bottom entry:
239: \G the terminal input buffer.
240: current-input @ 0= IF create-input THEN
241: BEGIN old-input @ WHILE 0 pop-file drop REPEAT ;
242:
243: : query ( -- ) \ core-ext
244: \G Make the user input device the input source. Receive input into
245: \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
246: \G superceeded by @code{accept}.
1.12 anton 247: clear-tibstack refill 0= -39 and throw ;
1.1 pazsan 248:
249: \ load a file
250:
251: has? file [IF]
1.9 anton 252: defer line-end-hook ( -- ) \ gforth
253: \G called at every end-of-line when text-interpreting from a file
254: \ alternatively we could use a wrapper for REFILL
255: ' noop is line-end-hook
256:
1.1 pazsan 257: : read-loop ( i*x -- j*x ) \ gforth
258: \G refill and interpret a file until EOF
1.9 anton 259: BEGIN refill WHILE interpret line-end-hook REPEAT ;
1.1 pazsan 260:
1.7 anton 261: : execute-parsing-named-file ( i*x wfileid filename-addr filename-u xt -- j*x )
262: >r push-file \ dup 2* cells included-files 2@ drop + 2@ type
1.5 anton 263: loadfilename 2! loadfile !
1.7 anton 264: r> catch
1.1 pazsan 265: loadfile @ close-file swap 2dup or
266: pop-file drop throw throw ;
267:
1.11 anton 268: : execute-parsing-file ( i*x fileid xt -- j*x ) \ gforth
1.7 anton 269: \G Make @i{fileid} the current input source, execute @i{xt @code{( i*x
270: \G -- j*x )}}, then restore the previous input source.
271: s" *a file*" rot execute-parsing-named-file ;
272:
1.11 anton 273: : include-file ( i*x wfileid -- j*x ) \ file
1.1 pazsan 274: \G Interpret (process using the text interpreter) the contents of
275: \G the file @var{wfileid}.
1.7 anton 276: ['] read-loop execute-parsing-file ;
1.1 pazsan 277: [THEN]
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>