[gforth] / gforth / kernel / int.fs  

gforth: gforth/kernel/int.fs


1 : pazsan 1.1 \ definitions needed for interpreter only
2 :    
3 : pazsan 1.52 \ Copyright (C) 1995-2000 Free Software Foundation, Inc.
4 : anton 1.11
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 : anton 1.63 \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20 : anton 1.11
21 : pazsan 1.1 \ \ Revision-Log
22 :    
23 :     \ put in seperate file 14sep97jaw
24 :    
25 :     \ \ input stream primitives 23feb93py
26 :    
27 : jwilke 1.33 require ./basics.fs \ bounds decimal hex ...
28 :     require ./io.fs \ type ...
29 :     require ./nio.fs \ . <# ...
30 :     require ./errore.fs \ .error ...
31 : anton 1.50 require kernel/version.fs \ version-string
32 : jwilke 1.33 require ./../chains.fs
33 :    
34 : pazsan 1.64 has? new-input 0= [IF]
35 : crook 1.43 : tib ( -- c-addr ) \ core-ext t-i-b
36 : crook 1.40 \G @i{c-addr} is the address of the Terminal Input Buffer.
37 : crook 1.29 \G OBSOLESCENT: @code{source} superceeds the function of this word.
38 : pazsan 1.1 >tib @ ;
39 :    
40 : crook 1.29 Defer source ( -- c-addr u ) \ core
41 : pazsan 1.1 \ used by dodefer:, must be defer
42 : crook 1.40 \G @i{c-addr} is the address of the input buffer and @i{u} is the
43 : crook 1.29 \G number of characters in it.
44 : pazsan 1.1
45 : crook 1.29 : (source) ( -- c-addr u )
46 : pazsan 1.1 tib #tib @ ;
47 :     ' (source) IS source
48 : pazsan 1.64 [THEN]
49 : pazsan 1.1
50 :     : (word) ( addr1 n1 char -- addr2 n2 )
51 :     dup >r skip 2dup r> scan nip - ;
52 :    
53 :     \ (word) should fold white spaces
54 :     \ this is what (parse-white) does
55 :    
56 :     \ word parse 23feb93py
57 :    
58 : crook 1.29 : sword ( char -- addr len ) \ gforth s-word
59 : crook 1.40 \G Parses like @code{word}, but the output is like @code{parse} output.
60 : anton 1.47 \G @xref{core-idef}.
61 : anton 1.3 \ this word was called PARSE-WORD until 0.3.0, but Open Firmware and
62 :     \ dpANS6 A.6.2.2008 have a word with that name that behaves
63 :     \ differently (like NAME).
64 : pazsan 1.1 source 2dup >r >r >in @ over min /string
65 :     rot dup bl = IF drop (parse-white) ELSE (word) THEN
66 :     2dup + r> - 1+ r> min >in ! ;
67 :    
68 : crook 1.29 : word ( char "<chars>ccc<char>-- c-addr ) \ core
69 : crook 1.40 \G Skip leading delimiters. Parse @i{ccc}, delimited by
70 :     \G @i{char}, in the parse area. @i{c-addr} is the address of a
71 : crook 1.29 \G transient region containing the parsed string in
72 : crook 1.40 \G counted-string format. If the parse area was empty or
73 : crook 1.29 \G contained no characters other than delimiters, the resulting
74 :     \G string has zero length. A program may replace characters within
75 :     \G the counted string. OBSOLESCENT: the counted string has a
76 :     \G trailing space that is not included in its length.
77 :     sword here place bl here count + c! here ;
78 :    
79 :     : parse ( char "ccc<char>" -- c-addr u ) \ core-ext
80 : crook 1.40 \G Parse @i{ccc}, delimited by @i{char}, in the parse
81 :     \G area. @i{c-addr u} specifies the parsed string within the
82 :     \G parse area. If the parse area was empty, @i{u} is 0.
83 : crook 1.29 >r source >in @ over min /string over swap r> scan >r
84 : pazsan 1.1 over - dup r> IF 1+ THEN >in +! ;
85 :    
86 :     \ name 13feb93py
87 :    
88 :     [IFUNDEF] (name) \ name might be a primitive
89 :    
90 : crook 1.40 : (name) ( -- c-addr count ) \ gforth
91 : pazsan 1.1 source 2dup >r >r >in @ /string (parse-white)
92 :     2dup + r> - 1+ r> min >in ! ;
93 :     \ name count ;
94 :     [THEN]
95 :    
96 :     : name-too-short? ( c-addr u -- c-addr u )
97 :     dup 0= -&16 and throw ;
98 :    
99 :     : name-too-long? ( c-addr u -- c-addr u )
100 : anton 1.67 dup lcount-mask u> -&19 and throw ;
101 : pazsan 1.1
102 :     \ \ Number parsing 23feb93py
103 :    
104 :     \ number? number 23feb93py
105 :    
106 :     hex
107 :     const Create bases 10 , 2 , A , 100 ,
108 :     \ 16 2 10 character
109 :    
110 : anton 1.18 \ !! protect BASE saving wrapper against exceptions
111 : pazsan 1.1 : getbase ( addr u -- addr' u' )
112 :     over c@ [char] $ - dup 4 u<
113 :     IF
114 :     cells bases + @ base ! 1 /string
115 :     ELSE
116 :     drop
117 :     THEN ;
118 :    
119 : pazsan 1.20 : sign? ( addr u -- addr u flag )
120 : jwilke 1.33 over c@ [char] - = dup >r
121 : pazsan 1.1 IF
122 :     1 /string
123 :     THEN
124 : pazsan 1.20 r> ;
125 :    
126 :     : s>unumber? ( addr u -- ud flag )
127 : pazsan 1.21 base @ >r dpl on getbase
128 : pazsan 1.20 0. 2swap
129 : anton 1.18 BEGIN ( d addr len )
130 : pazsan 1.1 dup >r >number dup
131 : anton 1.18 WHILE \ there are characters left
132 : pazsan 1.1 dup r> -
133 : anton 1.18 WHILE \ the last >number parsed something
134 :     dup 1- dpl ! over c@ [char] . =
135 :     WHILE \ the current char is '.'
136 : pazsan 1.1 1 /string
137 : anton 1.18 REPEAT THEN \ there are unparseable characters left
138 : pazsan 1.21 2drop false
139 : pazsan 1.20 ELSE
140 :     rdrop 2drop true
141 : pazsan 1.21 THEN
142 :     r> base ! ;
143 : pazsan 1.20
144 :     \ ouch, this is complicated; there must be a simpler way - anton
145 :     : s>number? ( addr len -- d f )
146 :     \ converts string addr len into d, flag indicates success
147 : pazsan 1.21 sign? >r
148 : pazsan 1.20 s>unumber?
149 :     0= IF
150 : pazsan 1.21 rdrop false
151 : anton 1.18 ELSE \ no characters left, all ok
152 : pazsan 1.20 r>
153 : pazsan 1.1 IF
154 :     dnegate
155 :     THEN
156 : anton 1.18 true
157 : pazsan 1.21 THEN ;
158 : pazsan 1.1
159 : anton 1.18 : s>number ( addr len -- d )
160 :     \ don't use this, there is no way to tell success
161 :     s>number? drop ;
162 :    
163 : pazsan 1.1 : snumber? ( c-addr u -- 0 / n -1 / d 0> )
164 : anton 1.18 s>number? 0=
165 : pazsan 1.1 IF
166 :     2drop false EXIT
167 :     THEN
168 : anton 1.18 dpl @ dup 0< IF
169 : pazsan 1.1 nip
170 : anton 1.18 ELSE
171 :     1+
172 : pazsan 1.1 THEN ;
173 :    
174 :     : number? ( string -- string 0 / n -1 / d 0> )
175 :     dup >r count snumber? dup if
176 :     rdrop
177 :     else
178 :     r> swap
179 :     then ;
180 :    
181 :     : number ( string -- d )
182 :     number? ?dup 0= abort" ?" 0<
183 :     IF
184 :     s>d
185 :     THEN ;
186 :    
187 :     \ \ Comments ( \ \G
188 :    
189 : crook 1.29 : ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ thisone- core,file paren
190 : crook 1.17 \G ** this will not get annotated. The alias in glocals.fs will instead **
191 : crook 1.29 \G It does not work to use "wordset-" prefix since this file is glossed
192 :     \G by cross.fs which doesn't have the same functionalty as makedoc.fs
193 : pazsan 1.1 [char] ) parse 2drop ; immediate
194 :    
195 : anton 1.51 : \ ( compilation 'ccc<newline>' -- ; run-time -- ) \ thisone- core-ext,block-ext backslash
196 : crook 1.29 \G ** this will not get annotated. The alias in glocals.fs will instead **
197 :     \G It does not work to use "wordset-" prefix since this file is glossed
198 :     \G by cross.fs which doesn't have the same functionalty as makedoc.fs
199 : pazsan 1.12 [ has? file [IF] ]
200 : pazsan 1.1 blk @
201 :     IF
202 :     >in @ c/l / 1+ c/l * >in !
203 :     EXIT
204 :     THEN
205 : pazsan 1.12 [ [THEN] ]
206 : pazsan 1.1 source >in ! drop ; immediate
207 :    
208 : anton 1.51 : \G ( compilation 'ccc<newline>' -- ; run-time -- ) \ gforth backslash-gee
209 : crook 1.19 \G Equivalent to @code{\} but used as a tag to annotate definition
210 :     \G comments into documentation.
211 : pazsan 1.1 POSTPONE \ ; immediate
212 :    
213 :     \ \ object oriented search list 17mar93py
214 :    
215 :     \ word list structure:
216 :    
217 :     struct
218 :     cell% field find-method \ xt: ( c_addr u wid -- nt )
219 :     cell% field reveal-method \ xt: ( nt wid -- ) \ used by dofield:, must be field
220 :     cell% field rehash-method \ xt: ( wid -- ) \ re-initializes a "search-data" (hashtables)
221 :     cell% field hash-method \ xt: ( wid -- ) \ initializes ""
222 :     \ \ !! what else
223 :     end-struct wordlist-map-struct
224 :    
225 :     struct
226 : pazsan 1.6 cell% field wordlist-map \ pointer to a wordlist-map-struct
227 : anton 1.13 cell% field wordlist-id \ linked list of words (for WORDS etc.)
228 : pazsan 1.1 cell% field wordlist-link \ link field to other wordlists
229 : anton 1.13 cell% field wordlist-extend \ wordlist extensions (eg bucket offset)
230 : pazsan 1.1 end-struct wordlist-struct
231 :    
232 :     : f83find ( addr len wordlist -- nt / false )
233 : anton 1.67 wordlist-id @ (listlfind) ;
234 : pazsan 1.1
235 :     : initvoc ( wid -- )
236 :     dup wordlist-map @ hash-method perform ;
237 :    
238 :     \ Search list table: find reveal
239 :     Create f83search ( -- wordlist-map )
240 :     ' f83find A, ' drop A, ' drop A, ' drop A,
241 :    
242 : pazsan 1.6 here G f83search T A, NIL A, NIL A, NIL A,
243 : pazsan 1.1 AValue forth-wordlist \ variable, will be redefined by search.fs
244 :    
245 :     AVariable lookup forth-wordlist lookup !
246 :     \ !! last is user and lookup?! jaw
247 :     AVariable current ( -- addr ) \ gforth
248 : crook 1.43 \G @code{Variable} -- holds the @i{wid} of the compilation word list.
249 : pazsan 1.1 AVariable voclink forth-wordlist wordlist-link voclink !
250 : anton 1.38 \ lookup AValue context ( -- addr ) \ gforth
251 :     Defer context ( -- addr ) \ gforth
252 : crook 1.43 \G @code{context} @code{@@} is the @i{wid} of the word list at the
253 :     \G top of the search order.
254 : pazsan 1.1
255 : anton 1.38 ' lookup is context
256 : pazsan 1.1 forth-wordlist current !
257 :    
258 :     \ \ header, finding, ticks 17dec92py
259 :    
260 : anton 1.67
261 :     \ !! these should be done using the target's operations and cell size
262 :     \ 0 invert 1 rshift invert ( u ) \ top bit set
263 :     \ constant alias-mask \ set when the word is not an alias!
264 :     \ alias-mask 1 rshift constant immediate-mask
265 :     \ alias-mask 2 rshift constant restrict-mask
266 :     \ 0 invert 3 rshift constant lcount-mask
267 :    
268 :     \ as an intermediate step, I define them correctly for 32-bit machines:
269 :    
270 :     $80000000 constant alias-mask
271 :     $40000000 constant immediate-mask
272 :     $20000000 constant restrict-mask
273 :     $1fffffff constant lcount-mask
274 : pazsan 1.1
275 :     \ higher level parts of find
276 :    
277 :     : flag-sign ( f -- 1|-1 )
278 :     \ true becomes 1, false -1
279 :     0= 2* 1+ ;
280 :    
281 :     : compile-only-error ( ... -- )
282 :     -&14 throw ;
283 :    
284 :     : (cfa>int) ( cfa -- xt )
285 :     [ has? compiler [IF] ]
286 :     dup interpret/compile?
287 :     if
288 :     interpret/compile-int @
289 :     then
290 :     [ [THEN] ] ;
291 :    
292 : anton 1.67 : (x>int) ( cfa w -- xt )
293 : pazsan 1.1 \ get interpretation semantics of name
294 :     restrict-mask and
295 :     if
296 :     drop ['] compile-only-error
297 :     else
298 :     (cfa>int)
299 :     then ;
300 :    
301 :     : name>string ( nt -- addr count ) \ gforth head-to-string
302 : crook 1.40 \g @i{addr count} is the name of the word represented by @i{nt}.
303 : anton 1.67 cell+ dup cell+ swap @ lcount-mask and ;
304 : pazsan 1.1
305 :     : ((name>)) ( nfa -- cfa )
306 :     name>string + cfaligned ;
307 :    
308 : anton 1.67 : (name>x) ( nfa -- cfa w )
309 :     \ cfa is an intermediate cfa and w is the flags cell of nfa
310 : pazsan 1.1 dup ((name>))
311 : anton 1.67 swap cell+ @ dup alias-mask and 0=
312 : pazsan 1.1 IF
313 :     swap @ swap
314 :     THEN ;
315 :    
316 :     : name>int ( nt -- xt ) \ gforth
317 : crook 1.31 \G @i{xt} represents the interpretation semantics of the word
318 :     \G @i{nt}. If @i{nt} has no interpretation semantics (i.e. is
319 :     \G @code{compile-only}), @i{xt} is the execution token for
320 :     \G @code{compile-only-error}, which performs @code{-14 throw}.
321 : pazsan 1.1 (name>x) (x>int) ;
322 :    
323 :     : name?int ( nt -- xt ) \ gforth
324 : crook 1.31 \G Like @code{name>int}, but perform @code{-14 throw} if @i{nt}
325 :     \G has no interpretation semantics.
326 : pazsan 1.1 (name>x) restrict-mask and
327 :     if
328 :     compile-only-error \ does not return
329 :     then
330 :     (cfa>int) ;
331 :    
332 :     : (name>comp) ( nt -- w +-1 ) \ gforth
333 : crook 1.31 \G @i{w xt} is the compilation token for the word @i{nt}.
334 : pazsan 1.1 (name>x) >r
335 :     [ has? compiler [IF] ]
336 :     dup interpret/compile?
337 :     if
338 :     interpret/compile-comp @
339 :     then
340 :     [ [THEN] ]
341 :     r> immediate-mask and flag-sign
342 :     ;
343 :    
344 :     : (name>intn) ( nfa -- xt +-1 )
345 : anton 1.67 (name>x) tuck (x>int) ( w xt )
346 : pazsan 1.1 swap immediate-mask and flag-sign ;
347 :    
348 : jwilke 1.30 const Create ??? 0 , 3 c, char ? c, char ? c, char ? c,
349 :     \ ??? is used by dovar:, must be created/:dovar
350 :    
351 :     [IFDEF] forthstart
352 :     \ if we have a forthstart we can define head? with it
353 :     \ otherwise leave out the head? check
354 :    
355 : anton 1.14 : head? ( addr -- f )
356 :     \G heuristic check whether addr is a name token; may deliver false
357 :     \G positives; addr must be a valid address
358 :     \ we follow the link fields and check for plausibility; two
359 :     \ iterations should catch most false addresses: on the first
360 :     \ iteration, we may get an xt, on the second a code address (or
361 :     \ some code), which is typically not in the dictionary.
362 :     2 0 do
363 : anton 1.41 dup dup aligned <> if \ protect @ against unaligned accesses
364 :     drop false unloop exit
365 :     then
366 : anton 1.14 dup @ dup
367 :     if ( addr addr1 )
368 :     dup rot forthstart within
369 :     if \ addr1 is outside forthstart..addr, not a head
370 :     drop false unloop exit
371 :     then ( addr1 )
372 :     else \ 0 in the link field, no further checks
373 :     2drop true unloop exit
374 :     then
375 :     loop
376 :     \ in dubio pro:
377 :     drop true ;
378 :    
379 : anton 1.48 : >head-noprim ( cfa -- nt ) \ gforth to-head-noprim
380 : anton 1.67 \ also heuristic; finds only names with up to 32 chars
381 : pazsan 1.45 $25 cell do ( cfa )
382 : anton 1.67 dup i - dup @ [ alias-mask lcount-mask or ] literal and ( cfa len|alias )
383 :     swap + cell + cfaligned over alias-mask + =
384 : anton 1.14 if ( cfa )
385 :     dup i - cell - dup head?
386 :     if
387 :     nip unloop exit
388 :     then
389 :     drop
390 :     then
391 :     cell +loop
392 :     drop ??? ( wouldn't 0 be better? ) ;
393 : pazsan 1.1
394 : jwilke 1.30 [ELSE]
395 :    
396 : anton 1.48 : >head-noprim ( cfa -- nt ) \ gforth to-head-noprim
397 : pazsan 1.45 $25 cell do ( cfa )
398 : anton 1.67 dup i - dup @ [ alias-mask lcount-mask or ] literal and ( cfa len|alias )
399 :     swap + cell + cfaligned over alias-mask + =
400 : jwilke 1.30 if ( cfa ) i - cell - unloop exit
401 :     then
402 :     cell +loop
403 :     drop ??? ( wouldn't 0 be better? ) ;
404 :    
405 :     [THEN]
406 : pazsan 1.1
407 :     : body> 0 >body - ;
408 :    
409 : crook 1.31 : (search-wordlist) ( addr count wid -- nt | false )
410 : pazsan 1.1 dup wordlist-map @ find-method perform ;
411 :    
412 : crook 1.31 : search-wordlist ( c-addr count wid -- 0 | xt +-1 ) \ search
413 : anton 1.53 \G Search the word list identified by @i{wid} for the definition
414 :     \G named by the string at @i{c-addr count}. If the definition is
415 :     \G not found, return 0. If the definition is found return 1 (if
416 :     \G the definition is immediate) or -1 (if the definition is not
417 :     \G immediate) together with the @i{xt}. In Gforth, the @i{xt}
418 :     \G returned represents the interpretation semantics. ANS Forth
419 :     \G does not specify clearly what @i{xt} represents.
420 : pazsan 1.1 (search-wordlist) dup if
421 :     (name>intn)
422 :     then ;
423 :    
424 : crook 1.31 : find-name ( c-addr u -- nt | 0 ) \ gforth
425 :     \g Find the name @i{c-addr u} in the current search
426 :     \g order. Return its @i{nt}, if found, otherwise 0.
427 : pazsan 1.1 lookup @ (search-wordlist) ;
428 :    
429 :     : sfind ( c-addr u -- 0 / xt +-1 ) \ gforth-obsolete
430 :     find-name dup
431 :     if ( nt )
432 :     state @
433 :     if
434 :     (name>comp)
435 :     else
436 :     (name>intn)
437 :     then
438 :     then ;
439 :    
440 : crook 1.31 : find ( c-addr -- xt +-1 | c-addr 0 ) \ core,search
441 : anton 1.53 \G Search all word lists in the current search order for the
442 :     \G definition named by the counted string at @i{c-addr}. If the
443 :     \G definition is not found, return 0. If the definition is found
444 :     \G return 1 (if the definition has non-default compilation
445 :     \G semantics) or -1 (if the definition has default compilation
446 :     \G semantics). The @i{xt} returned in interpret state represents
447 :     \G the interpretation semantics. The @i{xt} returned in compile
448 :     \G state represented either the compilation semantics (for
449 :     \G non-default compilation semantics) or the run-time semantics
450 :     \G that the compilation semantics would @code{compile,} (for
451 :     \G default compilation semantics). The ANS Forth standard does
452 :     \G not specify clearly what the returned @i{xt} represents (and
453 :     \G also talks about immediacy instead of non-default compilation
454 :     \G semantics), so this word is questionable in portable programs.
455 :     \G If non-portability is ok, @code{find-name} and friends are
456 :     \G better (@pxref{Name token}).
457 : pazsan 1.1 dup count sfind dup
458 :     if
459 :     rot drop
460 :     then ;
461 :    
462 : jwilke 1.34 \ ticks in interpreter
463 : pazsan 1.1
464 :     : (') ( "name" -- nt ) \ gforth
465 : anton 1.32 name name-too-short?
466 : anton 1.28 find-name dup 0=
467 : pazsan 1.1 IF
468 : anton 1.42 drop -&13 throw
469 : pazsan 1.1 THEN ;
470 :    
471 :     : ' ( "name" -- xt ) \ core tick
472 : crook 1.31 \g @i{xt} represents @i{name}'s interpretation
473 :     \g semantics. Perform @code{-14 throw} if the word has no
474 : pazsan 1.1 \g interpretation semantics.
475 :     (') name?int ;
476 : jwilke 1.34
477 :     has? compiler 0= [IF] \ interpreter only version of IS and TO
478 :    
479 :     : IS ' >body ! ;
480 :     ' IS Alias TO
481 :    
482 :     [THEN]
483 : pazsan 1.1
484 :     \ \ the interpreter loop mar92py
485 :    
486 :     \ interpret 10mar92py
487 :    
488 : anton 1.37 Defer parser ( c-addr u -- )
489 : pazsan 1.1 Defer name ( -- c-addr count ) \ gforth
490 : anton 1.55 \G Get the next word from the input buffer
491 : pazsan 1.1 ' (name) IS name
492 :     Defer compiler-notfound ( c-addr count -- )
493 :     Defer interpreter-notfound ( c-addr count -- )
494 :    
495 :     : no.extensions ( addr u -- )
496 : anton 1.42 2drop -&13 throw ;
497 : pazsan 1.1 ' no.extensions IS compiler-notfound
498 :     ' no.extensions IS interpreter-notfound
499 :    
500 : anton 1.66 : interpret1 ( ... -- ... )
501 : jwilke 1.33 [ has? backtrace [IF] ]
502 : anton 1.24 rp@ backtrace-rp0 !
503 : jwilke 1.33 [ [THEN] ]
504 : pazsan 1.1 BEGIN
505 :     ?stack name dup
506 :     WHILE
507 :     parser
508 :     REPEAT
509 : anton 1.66 2drop ;
510 :    
511 :     : interpret ( ?? -- ?? ) \ gforth
512 :     \ interpret/compile the (rest of the) input buffer
513 :     [ has? backtrace [IF] ]
514 :     backtrace-rp0 @ >r
515 :     [ [THEN] ]
516 :     ['] interpret1 catch
517 : anton 1.65 [ has? backtrace [IF] ]
518 :     r> backtrace-rp0 !
519 : anton 1.66 [ [THEN] ]
520 :     throw ;
521 : pazsan 1.1
522 :     \ interpreter 30apr92py
523 :    
524 :     \ not the most efficient implementations of interpreter and compiler
525 : jwilke 1.33 : interpreter ( c-addr u -- )
526 : pazsan 1.1 2dup find-name dup
527 :     if
528 :     nip nip name>int execute
529 :     else
530 :     drop
531 :     2dup 2>r snumber?
532 :     IF
533 :     2rdrop
534 :     ELSE
535 :     2r> interpreter-notfound
536 :     THEN
537 :     then ;
538 :    
539 :     ' interpreter IS parser
540 :    
541 :     \ \ Query Evaluate 07apr93py
542 :    
543 :     has? file 0= [IF]
544 : pazsan 1.12 : sourceline# ( -- n ) 1 ;
545 : pazsan 1.61 [ELSE]
546 : pazsan 1.64 has? new-input 0= [IF]
547 : pazsan 1.58 Variable #fill-bytes
548 :     \G number of bytes read via (read-line) by the last refill
549 : pazsan 1.61 [THEN]
550 : pazsan 1.64 [THEN]
551 : pazsan 1.58
552 : pazsan 1.64 has? new-input 0= [IF]
553 : pazsan 1.1 : refill ( -- flag ) \ core-ext,block-ext,file-ext
554 : crook 1.29 \G Attempt to fill the input buffer from the input source. When
555 :     \G the input source is the user input device, attempt to receive
556 :     \G input into the terminal input device. If successful, make the
557 :     \G result the input buffer, set @code{>IN} to 0 and return true;
558 :     \G otherwise return false. When the input source is a block, add 1
559 :     \G to the value of @code{BLK} to make the next block the input
560 :     \G source and current input buffer, and set @code{>IN} to 0;
561 :     \G return true if the new value of @code{BLK} is a valid block
562 :     \G number, false otherwise. When the input source is a text file,
563 :     \G attempt to read the next line from the file. If successful,
564 :     \G make the result the current input buffer, set @code{>IN} to 0
565 :     \G and return true; otherwise, return false. A successful result
566 :     \G includes receipt of a line containing 0 characters.
567 : pazsan 1.12 [ has? file [IF] ]
568 :     blk @ IF 1 blk +! true 0 >in ! EXIT THEN
569 :     [ [THEN] ]
570 :     tib /line
571 :     [ has? file [IF] ]
572 :     loadfile @ ?dup
573 : pazsan 1.59 IF (read-line) throw #fill-bytes !
574 : pazsan 1.12 ELSE
575 :     [ [THEN] ]
576 :     sourceline# 0< IF 2drop false EXIT THEN
577 :     accept true
578 :     [ has? file [IF] ]
579 :     THEN
580 :     1 loadline +!
581 :     [ [THEN] ]
582 :     swap #tib ! 0 >in ! ;
583 : pazsan 1.1
584 :     : query ( -- ) \ core-ext
585 : crook 1.29 \G Make the user input device the input source. Receive input into
586 :     \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
587 :     \G superceeded by @code{accept}.
588 : pazsan 1.12 [ has? file [IF] ]
589 :     blk off loadfile off
590 :     [ [THEN] ]
591 : pazsan 1.64 refill drop ;
592 :     [THEN]
593 : pazsan 1.1
594 :     \ save-mem extend-mem
595 :    
596 :     has? os [IF]
597 :     : save-mem ( addr1 u -- addr2 u ) \ gforth
598 :     \g copy a memory block into a newly allocated region in the heap
599 :     swap >r
600 :     dup allocate throw
601 :     swap 2dup r> -rot move ;
602 :    
603 :     : extend-mem ( addr1 u1 u -- addr addr2 u2 )
604 :     \ extend memory block allocated from the heap by u aus
605 :     \ the (possibly reallocated piece is addr2 u2, the extension is at addr
606 :     over >r + dup >r resize throw
607 :     r> over r> + -rot ;
608 :     [THEN]
609 :    
610 :     \ EVALUATE 17may93jaw
611 :    
612 : pazsan 1.64 has? file 0= has? new-input 0= and [IF]
613 : pazsan 1.1 : push-file ( -- ) r>
614 : pazsan 1.12 tibstack @ >r >tib @ >r #tib @ >r
615 : pazsan 1.1 >tib @ tibstack @ = IF r@ tibstack +! THEN
616 :     tibstack @ >tib ! >in @ >r >r ;
617 :    
618 :     : pop-file ( throw-code -- throw-code )
619 :     r>
620 : pazsan 1.12 r> >in ! r> #tib ! r> >tib ! r> tibstack ! >r ;
621 : pazsan 1.1 [THEN]
622 :    
623 : pazsan 1.64 has? new-input 0= [IF]
624 : crook 1.29 : evaluate ( c-addr u -- ) \ core,block
625 : crook 1.40 \G Save the current input source specification. Store @code{-1} in
626 :     \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
627 :     \G @code{0} and make the string @i{c-addr u} the input source
628 :     \G and input buffer. Interpret. When the parse area is empty,
629 :     \G restore the input source specification.
630 : pazsan 1.64 [ has? file [IF] ]
631 : anton 1.56 loadfilename# @ >r
632 : anton 1.60 1 loadfilename# ! \ "*evaluated string*"
633 : pazsan 1.64 [ [THEN] ]
634 : crook 1.40 push-file #tib ! >tib !
635 : crook 1.29 >in off
636 :     [ has? file [IF] ]
637 :     blk off loadfile off -1 loadline !
638 :     [ [THEN] ]
639 :     ['] interpret catch
640 : anton 1.56 pop-file
641 : pazsan 1.64 [ has? file [IF] ]
642 : anton 1.56 r> loadfilename# !
643 : pazsan 1.64 [ [THEN] ]
644 : anton 1.56 throw ;
645 : pazsan 1.64 [THEN]
646 : pazsan 1.1
647 :     \ \ Quit 13feb93py
648 :    
649 :     Defer 'quit
650 :    
651 :     Defer .status
652 :    
653 :     : prompt state @ IF ." compiled" EXIT THEN ." ok" ;
654 :    
655 : anton 1.39 : (quit) ( -- )
656 :     \ exits only through THROW etc.
657 : anton 1.42 \ sp0 @ cell - handler @ &12 + ! \ !! kludge: fix the stack pointer
658 : anton 1.39 \ stored in the system's CATCH frame, so the stack depth will be 0
659 :     \ after the next THROW it catches (it may be off due to BOUNCEs or
660 :     \ because process-args left something on the stack)
661 :     BEGIN
662 : pazsan 1.64 .status cr query interpret prompt
663 : anton 1.39 AGAIN ;
664 : pazsan 1.1
665 :     ' (quit) IS 'quit
666 :    
667 :     \ \ DOERROR (DOERROR) 13jun93jaw
668 :    
669 :     8 Constant max-errors
670 :     Variable error-stack 0 error-stack !
671 : pazsan 1.64 max-errors has? file [IF] 6 [ELSE] 4 [THEN] * cells allot
672 : pazsan 1.1 \ format of one cell:
673 :     \ source ( addr u )
674 :     \ >in
675 :     \ line-number
676 :     \ Loadfilename ( addr u )
677 :    
678 : pazsan 1.64 : error> ( -- addr u >in line# [addr u] )
679 :     -1 error-stack +!
680 :     error-stack dup @
681 :     [ has? file [IF] 6 [ELSE] 4 [THEN] ] Literal * cells + cell+
682 :     [ has? file [IF] 6 [ELSE] 4 [THEN] ] Literal cells bounds DO
683 :     I @
684 :     cell +LOOP ;
685 :     : >error ( addr u >in line# [addr u] -- )
686 :     error-stack dup @ dup 1+
687 :     max-errors 1- min error-stack !
688 :     [ has? file [IF] 6 [ELSE] 4 [THEN] ] Literal * cells + cell+
689 :     [ has? file [IF] 6 [ELSE] 4 [THEN] 1- ] Literal cells bounds swap DO
690 :     I !
691 :     -1 cells +LOOP ;
692 :    
693 : pazsan 1.1 : dec. ( n -- ) \ gforth
694 : crook 1.40 \G Display @i{n} as a signed decimal number, followed by a space.
695 :     \ !! not used...
696 : pazsan 1.1 base @ decimal swap . base ! ;
697 :    
698 : jwilke 1.23 : dec.r ( u -- ) \ gforth
699 : crook 1.40 \G Display @i{u} as a unsigned decimal number
700 : jwilke 1.23 base @ decimal swap 0 .r base ! ;
701 :    
702 : pazsan 1.1 : hex. ( u -- ) \ gforth
703 : crook 1.40 \G Display @i{u} as an unsigned hex number, prefixed with a "$" and
704 : crook 1.17 \G followed by a space.
705 : crook 1.40 \ !! not used...
706 : jwilke 1.33 [char] $ emit base @ swap hex u. base ! ;
707 : pazsan 1.1
708 :     : typewhite ( addr u -- ) \ gforth
709 : crook 1.40 \G Like type, but white space is printed instead of the characters.
710 : pazsan 1.1 bounds ?do
711 :     i c@ #tab = if \ check for tab
712 :     #tab
713 :     else
714 :     bl
715 :     then
716 :     emit
717 :     loop ;
718 :    
719 :     DEFER DOERROR
720 : jwilke 1.33
721 :     has? backtrace [IF]
722 : anton 1.15 Defer dobacktrace ( -- )
723 :     ' noop IS dobacktrace
724 : jwilke 1.33 [THEN]
725 : pazsan 1.1
726 : jwilke 1.23 : .error-string ( throw-code -- )
727 :     dup -2 =
728 :     IF "error @ ?dup IF count type THEN drop
729 :     ELSE .error
730 :     THEN ;
731 :    
732 : pazsan 1.64 : .error-frame ( throwcode addr1 u1 n1 n2 [addr2 u2] -- throwcode )
733 :     \ addr2 u2: filename of included file - optional
734 : jwilke 1.23 \ n2: line number
735 :     \ n1: error position in input line
736 :     \ addr1 u1: input line
737 : pazsan 1.1 cr error-stack @
738 :     IF
739 : pazsan 1.64 [ has? file [IF] ]
740 :     ." in file included from "
741 :     type ." :"
742 :     [ [THEN] ]
743 :     dec.r drop 2drop
744 : pazsan 1.1 ELSE
745 : pazsan 1.64 [ has? file [IF] ]
746 :     type ." :"
747 :     [ [THEN] ]
748 :     dup >r dec.r ." : " 3 pick .error-string
749 : anton 1.57 r> IF \ if line# non-zero, there is a line
750 :     cr dup 2over type cr drop
751 :     nip -trailing 1- ( line-start index2 )
752 :     0 >r BEGIN
753 :     2dup + c@ bl > WHILE
754 :     r> 1+ >r 1- dup 0< UNTIL THEN 1+
755 :     ( line-start index1 )
756 :     typewhite
757 :     r> 1 max 0 ?do \ we want at least one "^", even if the length is 0
758 :     [char] ^ emit
759 :     loop
760 :     ELSE
761 :     2drop drop
762 :     THEN
763 : jwilke 1.23 THEN ;
764 : pazsan 1.1
765 :     : (DoError) ( throw-code -- )
766 :     [ has? os [IF] ]
767 : pazsan 1.8 >stderr
768 : pazsan 1.1 [ [THEN] ]
769 : pazsan 1.64 source >in @ sourceline# [ has? file [IF] ]
770 :     sourcefilename
771 :     [ [THEN] ] .error-frame
772 : pazsan 1.1 error-stack @ 0 ?DO
773 : pazsan 1.64 error>
774 : pazsan 1.1 .error-frame
775 :     LOOP
776 : jwilke 1.33 drop
777 :     [ has? backtrace [IF] ]
778 :     dobacktrace
779 :     [ [THEN] ]
780 : pazsan 1.8 normal-dp dpp ! ;
781 : pazsan 1.1
782 :     ' (DoError) IS DoError
783 :    
784 :     : quit ( ?? -- ?? ) \ core
785 : crook 1.27 \G Empty the return stack, make the user input device
786 :     \G the input source, enter interpret state and start
787 :     \G the text interpreter.
788 : pazsan 1.64 rp0 @ rp! handler off clear-tibstack
789 :     [ has? new-input 0= [IF] ] >tib @ >r [ [THEN] ]
790 : pazsan 1.1 BEGIN
791 :     [ has? compiler [IF] ]
792 :     postpone [
793 :     [ [THEN] ]
794 :     ['] 'quit CATCH dup
795 :     WHILE
796 : anton 1.22 <# \ reset hold area, or we may get another error
797 : pazsan 1.64 DoError
798 :     [ has? new-input [IF] ] clear-tibstack
799 :     [ [ELSE] ] r@ >tib ! r@ tibstack !
800 :     [ [THEN] ]
801 : pazsan 1.1 REPEAT
802 : pazsan 1.64 drop [ has? new-input [IF] ] clear-tibstack
803 :     [ [ELSE] ] r> >tib !
804 :     [ [THEN] ] ;
805 : pazsan 1.1
806 :     \ \ Cold Boot 13feb93py
807 :    
808 :     : (bootmessage)
809 :     ." GForth " version-string type
810 : pazsan 1.52 ." , Copyright (C) 1995-2000 Free Software Foundation, Inc." cr
811 : pazsan 1.1 ." GForth comes with ABSOLUTELY NO WARRANTY; for details type `license'"
812 :     [ has? os [IF] ]
813 :     cr ." Type `bye' to exit"
814 :     [ [THEN] ] ;
815 :    
816 :     defer bootmessage
817 :     defer process-args
818 :    
819 :     ' (bootmessage) IS bootmessage
820 :    
821 : anton 1.10 Defer 'cold ( -- ) \ gforth tick-cold
822 : pazsan 1.1 \ hook (deferred word) for things to do right before interpreting the
823 :     \ command-line arguments
824 :     ' noop IS 'cold
825 :    
826 :    
827 :     Variable init8
828 :    
829 :     : cold ( -- ) \ gforth
830 : anton 1.44 [ has? backtrace [IF] ]
831 :     rp@ backtrace-rp0 !
832 :     [ [THEN] ]
833 : pazsan 1.1 [ has? file [IF] ]
834 :     pathstring 2@ fpath only-path
835 :     init-included-files
836 :     [ [THEN] ]
837 :     'cold
838 :     init8 chainperform
839 :     [ has? file [IF] ]
840 : anton 1.62 loadfilename# off
841 : pazsan 1.8 process-args
842 : pazsan 1.12 loadline off
843 : pazsan 1.1 [ [THEN] ]
844 :     bootmessage
845 : pazsan 1.12 quit ;
846 : pazsan 1.1
847 : pazsan 1.64 has? new-input 0= [IF]
848 : anton 1.5 : clear-tibstack ( -- )
849 :     [ has? glocals [IF] ]
850 :     lp@ forthstart 7 cells + @ -
851 :     [ [ELSE] ]
852 :     [ has? os [IF] ]
853 : pazsan 1.8 r0 @ forthstart 6 cells + @ -
854 : anton 1.5 [ [ELSE] ]
855 : pazsan 1.16 sp@ $10 cells +
856 : anton 1.5 [ [THEN] ]
857 :     [ [THEN] ]
858 :     dup >tib ! tibstack ! #tib off >in off ;
859 : pazsan 1.64 [THEN]
860 : anton 1.5
861 : pazsan 1.64 : boot ( path n **argv argc -- )
862 : pazsan 1.1 main-task up!
863 :     [ has? os [IF] ]
864 :     stdout TO outfile-id
865 : pazsan 1.7 stdin TO infile-id
866 : pazsan 1.1 \ !! [ [THEN] ]
867 :     \ !! [ has? file [IF] ]
868 :     argc ! argv ! pathstring 2!
869 :     [ [THEN] ]
870 :     sp@ sp0 !
871 : pazsan 1.64 [ has? new-input [IF] ]
872 :     current-input off
873 :     [ [THEN] ]
874 : anton 1.5 clear-tibstack
875 : pazsan 1.1 rp@ rp0 !
876 :     [ has? floating [IF] ]
877 :     fp@ fp0 !
878 :     [ [THEN] ]
879 : anton 1.46 handler off
880 : pazsan 1.8 ['] cold catch DoError cr
881 : pazsan 1.1 [ has? os [IF] ]
882 : anton 1.35 1 (bye) \ !! determin exit code from throw code?
883 : pazsan 1.1 [ [THEN] ]
884 :     ;
885 :    
886 :     has? os [IF]
887 :     : bye ( -- ) \ tools-ext
888 :     [ has? file [IF] ]
889 :     script? 0= IF cr THEN
890 :     [ [ELSE] ]
891 :     cr
892 :     [ [THEN] ]
893 :     0 (bye) ;
894 :     [THEN]
895 :    
896 :     \ **argv may be scanned by the C starter to get some important
897 :     \ information, as -display and -geometry for an X client FORTH
898 :     \ or space and stackspace overrides
899 :    
900 :     \ 0 arg contains, however, the name of the program.
901 :    

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help