[gforth] / gforth / kernel / int.fs  

gforth: gforth/kernel/int.fs


1 : pazsan 1.1 \ definitions needed for interpreter only
2 :    
3 : anton 1.115 \ Copyright (C) 1995-2000,2004 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 : anton 1.80 \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 : anton 1.80 over - dup r> IF 1+ THEN >in +! ;
85 : pazsan 1.1
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 : anton 1.110 const Create bases 0A , 10 , 2 , 0A ,
108 : anton 1.109 \ 10 16 2 10
109 : pazsan 1.1
110 : anton 1.18 \ !! protect BASE saving wrapper against exceptions
111 : pazsan 1.1 : getbase ( addr u -- addr' u' )
112 : anton 1.108 2dup s" 0x" string-prefix? >r
113 :     2dup s" 0X" string-prefix? r> or
114 : anton 1.117 base @ &34 < and if
115 : anton 1.108 hex 2 /string
116 :     endif
117 : anton 1.109 over c@ [char] # - dup 4 u<
118 : pazsan 1.1 IF
119 :     cells bases + @ base ! 1 /string
120 :     ELSE
121 :     drop
122 :     THEN ;
123 :    
124 : pazsan 1.20 : sign? ( addr u -- addr u flag )
125 : jwilke 1.33 over c@ [char] - = dup >r
126 : pazsan 1.1 IF
127 :     1 /string
128 :     THEN
129 : pazsan 1.20 r> ;
130 :    
131 : anton 1.109 : s'>unumber? ( addr u -- ud flag )
132 :     \ convert string "C" or "C'" to character code
133 :     dup 0= if
134 :     false exit
135 :     endif
136 : anton 1.116 x@+/string 0 s" '" 2rot string-prefix? ;
137 : anton 1.109
138 : pazsan 1.20 : s>unumber? ( addr u -- ud flag )
139 : anton 1.109 over c@ '' = if
140 :     1 /string s'>unumber? exit
141 :     endif
142 : pazsan 1.21 base @ >r dpl on getbase
143 : pazsan 1.20 0. 2swap
144 : anton 1.18 BEGIN ( d addr len )
145 : pazsan 1.1 dup >r >number dup
146 : anton 1.18 WHILE \ there are characters left
147 : pazsan 1.1 dup r> -
148 : anton 1.18 WHILE \ the last >number parsed something
149 :     dup 1- dpl ! over c@ [char] . =
150 :     WHILE \ the current char is '.'
151 : pazsan 1.1 1 /string
152 : anton 1.18 REPEAT THEN \ there are unparseable characters left
153 : pazsan 1.21 2drop false
154 : pazsan 1.20 ELSE
155 :     rdrop 2drop true
156 : pazsan 1.21 THEN
157 :     r> base ! ;
158 : pazsan 1.20
159 :     \ ouch, this is complicated; there must be a simpler way - anton
160 :     : s>number? ( addr len -- d f )
161 :     \ converts string addr len into d, flag indicates success
162 : pazsan 1.21 sign? >r
163 : pazsan 1.20 s>unumber?
164 :     0= IF
165 : pazsan 1.21 rdrop false
166 : anton 1.18 ELSE \ no characters left, all ok
167 : pazsan 1.20 r>
168 : pazsan 1.1 IF
169 :     dnegate
170 :     THEN
171 : anton 1.18 true
172 : pazsan 1.21 THEN ;
173 : pazsan 1.1
174 : anton 1.18 : s>number ( addr len -- d )
175 :     \ don't use this, there is no way to tell success
176 :     s>number? drop ;
177 :    
178 : pazsan 1.1 : snumber? ( c-addr u -- 0 / n -1 / d 0> )
179 : anton 1.18 s>number? 0=
180 : pazsan 1.1 IF
181 :     2drop false EXIT
182 :     THEN
183 : anton 1.18 dpl @ dup 0< IF
184 : pazsan 1.1 nip
185 : anton 1.18 ELSE
186 :     1+
187 : pazsan 1.1 THEN ;
188 :    
189 :     : number? ( string -- string 0 / n -1 / d 0> )
190 :     dup >r count snumber? dup if
191 :     rdrop
192 :     else
193 :     r> swap
194 :     then ;
195 :    
196 :     : number ( string -- d )
197 :     number? ?dup 0= abort" ?" 0<
198 :     IF
199 :     s>d
200 :     THEN ;
201 :    
202 :     \ \ Comments ( \ \G
203 :    
204 : crook 1.29 : ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ thisone- core,file paren
205 : crook 1.17 \G ** this will not get annotated. The alias in glocals.fs will instead **
206 : crook 1.29 \G It does not work to use "wordset-" prefix since this file is glossed
207 :     \G by cross.fs which doesn't have the same functionalty as makedoc.fs
208 : pazsan 1.1 [char] ) parse 2drop ; immediate
209 :    
210 : anton 1.51 : \ ( compilation 'ccc<newline>' -- ; run-time -- ) \ thisone- core-ext,block-ext backslash
211 : crook 1.29 \G ** this will not get annotated. The alias in glocals.fs will instead **
212 :     \G It does not work to use "wordset-" prefix since this file is glossed
213 :     \G by cross.fs which doesn't have the same functionalty as makedoc.fs
214 : pazsan 1.12 [ has? file [IF] ]
215 : pazsan 1.1 blk @
216 :     IF
217 :     >in @ c/l / 1+ c/l * >in !
218 :     EXIT
219 :     THEN
220 : pazsan 1.12 [ [THEN] ]
221 : pazsan 1.1 source >in ! drop ; immediate
222 :    
223 : anton 1.51 : \G ( compilation 'ccc<newline>' -- ; run-time -- ) \ gforth backslash-gee
224 : crook 1.19 \G Equivalent to @code{\} but used as a tag to annotate definition
225 :     \G comments into documentation.
226 : pazsan 1.1 POSTPONE \ ; immediate
227 :    
228 :     \ \ object oriented search list 17mar93py
229 :    
230 :     \ word list structure:
231 :    
232 :     struct
233 :     cell% field find-method \ xt: ( c_addr u wid -- nt )
234 :     cell% field reveal-method \ xt: ( nt wid -- ) \ used by dofield:, must be field
235 :     cell% field rehash-method \ xt: ( wid -- ) \ re-initializes a "search-data" (hashtables)
236 :     cell% field hash-method \ xt: ( wid -- ) \ initializes ""
237 :     \ \ !! what else
238 :     end-struct wordlist-map-struct
239 :    
240 :     struct
241 : pazsan 1.6 cell% field wordlist-map \ pointer to a wordlist-map-struct
242 : anton 1.13 cell% field wordlist-id \ linked list of words (for WORDS etc.)
243 : pazsan 1.1 cell% field wordlist-link \ link field to other wordlists
244 : anton 1.13 cell% field wordlist-extend \ wordlist extensions (eg bucket offset)
245 : pazsan 1.1 end-struct wordlist-struct
246 :    
247 : pazsan 1.103 has? f83headerstring [IF]
248 :     : f83find ( addr len wordlist -- nt / false )
249 :     wordlist-id @ (f83find) ;
250 :     [ELSE]
251 : pazsan 1.1 : f83find ( addr len wordlist -- nt / false )
252 : anton 1.67 wordlist-id @ (listlfind) ;
253 : pazsan 1.103 [THEN]
254 : pazsan 1.1
255 :     : initvoc ( wid -- )
256 :     dup wordlist-map @ hash-method perform ;
257 :    
258 :     \ Search list table: find reveal
259 :     Create f83search ( -- wordlist-map )
260 :     ' f83find A, ' drop A, ' drop A, ' drop A,
261 :    
262 : pazsan 1.6 here G f83search T A, NIL A, NIL A, NIL A,
263 : pazsan 1.1 AValue forth-wordlist \ variable, will be redefined by search.fs
264 :    
265 :     AVariable lookup forth-wordlist lookup !
266 :     \ !! last is user and lookup?! jaw
267 :     AVariable current ( -- addr ) \ gforth
268 : crook 1.43 \G @code{Variable} -- holds the @i{wid} of the compilation word list.
269 : pazsan 1.1 AVariable voclink forth-wordlist wordlist-link voclink !
270 : anton 1.38 \ lookup AValue context ( -- addr ) \ gforth
271 :     Defer context ( -- addr ) \ gforth
272 : crook 1.43 \G @code{context} @code{@@} is the @i{wid} of the word list at the
273 :     \G top of the search order.
274 : pazsan 1.1
275 : anton 1.38 ' lookup is context
276 : pazsan 1.1 forth-wordlist current !
277 :    
278 :     \ \ header, finding, ticks 17dec92py
279 :    
280 : pazsan 1.69 \ The constants are defined as 32 bits, but then erased
281 :     \ and overwritten by the right ones
282 : anton 1.67
283 : pazsan 1.103 has? f83headerstring [IF]
284 :     \ to save space, Gforth EC limits words to 31 characters
285 :     $80 constant alias-mask
286 :     $40 constant immediate-mask
287 :     $20 constant restrict-mask
288 :     $1f constant lcount-mask
289 :     [ELSE]
290 : anton 1.67 $80000000 constant alias-mask
291 : pazsan 1.69 1 bits/char 1 - lshift
292 :     -1 cells allot bigendian [IF] c, 0 1 cells 1- times
293 :     [ELSE] 0 1 cells 1- times c, [THEN]
294 : anton 1.67 $40000000 constant immediate-mask
295 : pazsan 1.69 1 bits/char 2 - lshift
296 :     -1 cells allot bigendian [IF] c, 0 1 cells 1- times
297 :     [ELSE] 0 1 cells 1- times c, [THEN]
298 : anton 1.67 $20000000 constant restrict-mask
299 : pazsan 1.69 1 bits/char 3 - lshift
300 :     -1 cells allot bigendian [IF] c, 0 1 cells 1- times
301 :     [ELSE] 0 1 cells 1- times c, [THEN]
302 : anton 1.67 $1fffffff constant lcount-mask
303 : pazsan 1.69 1 bits/char 3 - lshift 1 -
304 : pazsan 1.71 -1 cells allot bigendian [IF] c, -1 1 cells 1- times
305 :     [ELSE] -1 1 cells 1- times c, [THEN]
306 : pazsan 1.103 [THEN]
307 : pazsan 1.1
308 :     \ higher level parts of find
309 :    
310 :     : flag-sign ( f -- 1|-1 )
311 :     \ true becomes 1, false -1
312 :     0= 2* 1+ ;
313 :    
314 : anton 1.79 : ticking-compile-only-error ( ... -- )
315 :     -&2048 throw ;
316 : pazsan 1.1
317 : anton 1.93 : compile-only-error ( ... -- )
318 :     -&14 throw ;
319 :    
320 : pazsan 1.1 : (cfa>int) ( cfa -- xt )
321 :     [ has? compiler [IF] ]
322 :     dup interpret/compile?
323 :     if
324 :     interpret/compile-int @
325 :     then
326 :     [ [THEN] ] ;
327 :    
328 : anton 1.67 : (x>int) ( cfa w -- xt )
329 : pazsan 1.1 \ get interpretation semantics of name
330 :     restrict-mask and
331 :     if
332 : anton 1.93 drop ['] compile-only-error
333 : pazsan 1.1 else
334 :     (cfa>int)
335 :     then ;
336 :    
337 : pazsan 1.103 has? f83headerstring [IF]
338 :     : name>string ( nt -- addr count ) \ gforth head-to-string
339 :     \g @i{addr count} is the name of the word represented by @i{nt}.
340 :     cell+ count lcount-mask and ;
341 :    
342 :     : ((name>)) ( nfa -- cfa )
343 :     name>string + cfaligned ;
344 :    
345 :     : (name>x) ( nfa -- cfa w )
346 :     \ cfa is an intermediate cfa and w is the flags cell of nfa
347 :     dup ((name>))
348 :     swap cell+ c@ dup alias-mask and 0=
349 :     IF
350 :     swap @ swap
351 :     THEN ;
352 :     [ELSE]
353 : pazsan 1.1 : name>string ( nt -- addr count ) \ gforth head-to-string
354 : crook 1.40 \g @i{addr count} is the name of the word represented by @i{nt}.
355 : anton 1.67 cell+ dup cell+ swap @ lcount-mask and ;
356 : pazsan 1.1
357 :     : ((name>)) ( nfa -- cfa )
358 :     name>string + cfaligned ;
359 :    
360 : anton 1.67 : (name>x) ( nfa -- cfa w )
361 :     \ cfa is an intermediate cfa and w is the flags cell of nfa
362 : pazsan 1.1 dup ((name>))
363 : anton 1.67 swap cell+ @ dup alias-mask and 0=
364 : pazsan 1.1 IF
365 :     swap @ swap
366 :     THEN ;
367 : pazsan 1.103 [THEN]
368 : pazsan 1.1
369 :     : name>int ( nt -- xt ) \ gforth
370 : crook 1.31 \G @i{xt} represents the interpretation semantics of the word
371 :     \G @i{nt}. If @i{nt} has no interpretation semantics (i.e. is
372 :     \G @code{compile-only}), @i{xt} is the execution token for
373 : anton 1.79 \G @code{ticking-compile-only-error}, which performs @code{-2048 throw}.
374 : pazsan 1.1 (name>x) (x>int) ;
375 :    
376 :     : name?int ( nt -- xt ) \ gforth
377 : anton 1.79 \G Like @code{name>int}, but perform @code{-2048 throw} if @i{nt}
378 : crook 1.31 \G has no interpretation semantics.
379 : pazsan 1.1 (name>x) restrict-mask and
380 :     if
381 : anton 1.79 ticking-compile-only-error \ does not return
382 : pazsan 1.1 then
383 :     (cfa>int) ;
384 :    
385 :     : (name>comp) ( nt -- w +-1 ) \ gforth
386 : crook 1.31 \G @i{w xt} is the compilation token for the word @i{nt}.
387 : pazsan 1.1 (name>x) >r
388 :     [ has? compiler [IF] ]
389 :     dup interpret/compile?
390 :     if
391 :     interpret/compile-comp @
392 :     then
393 :     [ [THEN] ]
394 :     r> immediate-mask and flag-sign
395 :     ;
396 :    
397 :     : (name>intn) ( nfa -- xt +-1 )
398 : anton 1.67 (name>x) tuck (x>int) ( w xt )
399 : pazsan 1.1 swap immediate-mask and flag-sign ;
400 :    
401 : pazsan 1.72 const Create ??? 0 , 3 , char ? c, char ? c, char ? c,
402 : jwilke 1.30 \ ??? is used by dovar:, must be created/:dovar
403 :    
404 :     [IFDEF] forthstart
405 :     \ if we have a forthstart we can define head? with it
406 :     \ otherwise leave out the head? check
407 :    
408 : anton 1.14 : head? ( addr -- f )
409 : anton 1.82 \G heuristic check whether addr is a name token; may deliver false
410 :     \G positives; addr must be a valid address; returns 1 for
411 :     \G particularly unsafe positives
412 : anton 1.14 \ we follow the link fields and check for plausibility; two
413 :     \ iterations should catch most false addresses: on the first
414 :     \ iteration, we may get an xt, on the second a code address (or
415 :     \ some code), which is typically not in the dictionary.
416 : anton 1.82 \ we added a third iteration for working with code and ;code words.
417 :     3 0 do
418 : anton 1.41 dup dup aligned <> if \ protect @ against unaligned accesses
419 :     drop false unloop exit
420 :     then
421 : anton 1.14 dup @ dup
422 :     if ( addr addr1 )
423 :     dup rot forthstart within
424 :     if \ addr1 is outside forthstart..addr, not a head
425 :     drop false unloop exit
426 :     then ( addr1 )
427 :     else \ 0 in the link field, no further checks
428 : anton 1.81 2drop 1 unloop exit \ this is very unsure, so return 1
429 : anton 1.14 then
430 :     loop
431 :     \ in dubio pro:
432 :     drop true ;
433 :    
434 : anton 1.48 : >head-noprim ( cfa -- nt ) \ gforth to-head-noprim
435 : anton 1.97 \ also heuristic
436 :     dup forthstart - max-name-length @ float+ cell+ min cell max cell ?do ( cfa )
437 : pazsan 1.70 dup i - dup @ [ alias-mask lcount-mask or ] literal
438 :     [ 1 bits/char 3 - lshift 1 - 1 bits/char 1 - lshift or
439 : pazsan 1.71 -1 cells allot bigendian [IF] c, -1 1 cells 1- times
440 :     [ELSE] -1 1 cells 1- times c, [THEN] ]
441 : pazsan 1.70 and ( cfa len|alias )
442 : anton 1.97 swap + cell+ cfaligned over alias-mask + =
443 : anton 1.14 if ( cfa )
444 :     dup i - cell - dup head?
445 :     if
446 :     nip unloop exit
447 :     then
448 :     drop
449 :     then
450 :     cell +loop
451 :     drop ??? ( wouldn't 0 be better? ) ;
452 : pazsan 1.1
453 : jwilke 1.30 [ELSE]
454 :    
455 : anton 1.48 : >head-noprim ( cfa -- nt ) \ gforth to-head-noprim
456 : pazsan 1.45 $25 cell do ( cfa )
457 : pazsan 1.70 dup i - dup @ [ alias-mask lcount-mask or ] literal
458 :     [ 1 bits/char 3 - lshift 1 - 1 bits/char 1 - lshift or
459 : pazsan 1.71 -1 cells allot bigendian [IF] c, -1 1 cells 1- times
460 :     [ELSE] -1 1 cells 1- times c, [THEN] ]
461 : pazsan 1.70 and ( cfa len|alias )
462 : anton 1.67 swap + cell + cfaligned over alias-mask + =
463 : jwilke 1.30 if ( cfa ) i - cell - unloop exit
464 :     then
465 :     cell +loop
466 :     drop ??? ( wouldn't 0 be better? ) ;
467 :    
468 :     [THEN]
469 : pazsan 1.1
470 : anton 1.83 cell% 2* 0 0 field >body ( xt -- a_addr ) \ core
471 :     \G Get the address of the body of the word represented by @i{xt} (the
472 :     \G address of the word's data field).
473 :     drop drop
474 :    
475 :     cell% -2 * 0 0 field body> ( xt -- a_addr )
476 : anton 1.84 drop drop
477 :    
478 :     has? standardthreading has? compiler and [IF]
479 :    
480 :     ' @ alias >code-address ( xt -- c_addr ) \ gforth
481 :     \G @i{c-addr} is the code address of the word @i{xt}.
482 :    
483 :     : >does-code ( xt -- a_addr ) \ gforth
484 :     \G If @i{xt} is the execution token of a child of a @code{DOES>} word,
485 :     \G @i{a-addr} is the start of the Forth code after the @code{DOES>};
486 :     \G Otherwise @i{a-addr} is 0.
487 :     dup @ dodoes: = if
488 :     cell+ @
489 :     else
490 :     drop 0
491 :     endif ;
492 :    
493 : anton 1.85 ' ! alias code-address! ( c_addr xt -- ) \ gforth
494 :     \G Create a code field with code address @i{c-addr} at @i{xt}.
495 :    
496 :     : does-code! ( a_addr xt -- ) \ gforth
497 :     \G Create a code field at @i{xt} for a child of a @code{DOES>}-word;
498 :     \G @i{a-addr} is the start of the Forth code after @code{DOES>}.
499 :     dodoes: over ! cell+ ! ;
500 :    
501 : anton 1.86 ' drop alias does-handler! ( a_addr -- ) \ gforth
502 :     \G Create a @code{DOES>}-handler at address @i{a-addr}. Normally,
503 :     \G @i{a-addr} points just behind a @code{DOES>}.
504 :    
505 : anton 1.85 2 cells constant /does-handler ( -- n ) \ gforth
506 :     \G The size of a @code{DOES>}-handler (includes possible padding).
507 :    
508 : anton 1.84 [THEN]
509 : pazsan 1.1
510 : crook 1.31 : (search-wordlist) ( addr count wid -- nt | false )
511 : pazsan 1.1 dup wordlist-map @ find-method perform ;
512 :    
513 : crook 1.31 : search-wordlist ( c-addr count wid -- 0 | xt +-1 ) \ search
514 : anton 1.53 \G Search the word list identified by @i{wid} for the definition
515 :     \G named by the string at @i{c-addr count}. If the definition is
516 :     \G not found, return 0. If the definition is found return 1 (if
517 :     \G the definition is immediate) or -1 (if the definition is not
518 :     \G immediate) together with the @i{xt}. In Gforth, the @i{xt}
519 :     \G returned represents the interpretation semantics. ANS Forth
520 :     \G does not specify clearly what @i{xt} represents.
521 : pazsan 1.1 (search-wordlist) dup if
522 :     (name>intn)
523 :     then ;
524 :    
525 : crook 1.31 : find-name ( c-addr u -- nt | 0 ) \ gforth
526 :     \g Find the name @i{c-addr u} in the current search
527 :     \g order. Return its @i{nt}, if found, otherwise 0.
528 : pazsan 1.1 lookup @ (search-wordlist) ;
529 :    
530 :     : sfind ( c-addr u -- 0 / xt +-1 ) \ gforth-obsolete
531 :     find-name dup
532 :     if ( nt )
533 :     state @
534 :     if
535 :     (name>comp)
536 :     else
537 :     (name>intn)
538 :     then
539 :     then ;
540 :    
541 : crook 1.31 : find ( c-addr -- xt +-1 | c-addr 0 ) \ core,search
542 : anton 1.53 \G Search all word lists in the current search order for the
543 :     \G definition named by the counted string at @i{c-addr}. If the
544 :     \G definition is not found, return 0. If the definition is found
545 :     \G return 1 (if the definition has non-default compilation
546 :     \G semantics) or -1 (if the definition has default compilation
547 :     \G semantics). The @i{xt} returned in interpret state represents
548 :     \G the interpretation semantics. The @i{xt} returned in compile
549 :     \G state represented either the compilation semantics (for
550 :     \G non-default compilation semantics) or the run-time semantics
551 :     \G that the compilation semantics would @code{compile,} (for
552 :     \G default compilation semantics). The ANS Forth standard does
553 :     \G not specify clearly what the returned @i{xt} represents (and
554 :     \G also talks about immediacy instead of non-default compilation
555 :     \G semantics), so this word is questionable in portable programs.
556 :     \G If non-portability is ok, @code{find-name} and friends are
557 :     \G better (@pxref{Name token}).
558 : pazsan 1.1 dup count sfind dup
559 :     if
560 :     rot drop
561 :     then ;
562 :    
563 : jwilke 1.34 \ ticks in interpreter
564 : pazsan 1.1
565 :     : (') ( "name" -- nt ) \ gforth
566 : anton 1.32 name name-too-short?
567 : anton 1.28 find-name dup 0=
568 : pazsan 1.1 IF
569 : anton 1.42 drop -&13 throw
570 : pazsan 1.1 THEN ;
571 :    
572 :     : ' ( "name" -- xt ) \ core tick
573 : crook 1.31 \g @i{xt} represents @i{name}'s interpretation
574 :     \g semantics. Perform @code{-14 throw} if the word has no
575 : pazsan 1.1 \g interpretation semantics.
576 :     (') name?int ;
577 : jwilke 1.34
578 :     has? compiler 0= [IF] \ interpreter only version of IS and TO
579 :    
580 :     : IS ' >body ! ;
581 :     ' IS Alias TO
582 :    
583 :     [THEN]
584 : pazsan 1.1
585 :     \ \ the interpreter loop mar92py
586 :    
587 :     \ interpret 10mar92py
588 :    
589 : anton 1.37 Defer parser ( c-addr u -- )
590 : anton 1.100 Defer parse-word ( "name" -- c-addr u ) \ gforth
591 : anton 1.55 \G Get the next word from the input buffer
592 : anton 1.77 ' (name) IS parse-word
593 :    
594 :     ' parse-word alias name ( -- c-addr u ) \ gforth-obsolete
595 :     \G old name for @code{parse-word}
596 :    
597 : pazsan 1.1 Defer compiler-notfound ( c-addr count -- )
598 :     Defer interpreter-notfound ( c-addr count -- )
599 :    
600 :     : no.extensions ( addr u -- )
601 : anton 1.42 2drop -&13 throw ;
602 : pazsan 1.1 ' no.extensions IS compiler-notfound
603 :     ' no.extensions IS interpreter-notfound
604 :    
605 : anton 1.106 Defer before-word ( -- ) \ gforth
606 :     \ called before the text interpreter parses the next word
607 :     ' noop IS before-word
608 :    
609 : anton 1.66 : interpret1 ( ... -- ... )
610 : jwilke 1.33 [ has? backtrace [IF] ]
611 : anton 1.24 rp@ backtrace-rp0 !
612 : jwilke 1.33 [ [THEN] ]
613 : pazsan 1.1 BEGIN
614 : anton 1.106 ?stack before-word name dup
615 : pazsan 1.1 WHILE
616 :     parser
617 :     REPEAT
618 : anton 1.66 2drop ;
619 :    
620 :     : interpret ( ?? -- ?? ) \ gforth
621 :     \ interpret/compile the (rest of the) input buffer
622 :     [ has? backtrace [IF] ]
623 :     backtrace-rp0 @ >r
624 :     [ [THEN] ]
625 :     ['] interpret1 catch
626 : anton 1.65 [ has? backtrace [IF] ]
627 :     r> backtrace-rp0 !
628 : anton 1.66 [ [THEN] ]
629 :     throw ;
630 : pazsan 1.1
631 :     \ interpreter 30apr92py
632 :    
633 :     \ not the most efficient implementations of interpreter and compiler
634 : jwilke 1.33 : interpreter ( c-addr u -- )
635 : pazsan 1.1 2dup find-name dup
636 :     if
637 :     nip nip name>int execute
638 :     else
639 :     drop
640 :     2dup 2>r snumber?
641 :     IF
642 :     2rdrop
643 :     ELSE
644 :     2r> interpreter-notfound
645 :     THEN
646 :     then ;
647 :    
648 :     ' interpreter IS parser
649 :    
650 :     \ \ Query Evaluate 07apr93py
651 :    
652 :     has? file 0= [IF]
653 : pazsan 1.12 : sourceline# ( -- n ) 1 ;
654 : pazsan 1.61 [ELSE]
655 : pazsan 1.64 has? new-input 0= [IF]
656 : pazsan 1.58 Variable #fill-bytes
657 :     \G number of bytes read via (read-line) by the last refill
658 : pazsan 1.61 [THEN]
659 : pazsan 1.64 [THEN]
660 : pazsan 1.58
661 : pazsan 1.64 has? new-input 0= [IF]
662 : pazsan 1.1 : refill ( -- flag ) \ core-ext,block-ext,file-ext
663 : crook 1.29 \G Attempt to fill the input buffer from the input source. When
664 :     \G the input source is the user input device, attempt to receive
665 :     \G input into the terminal input device. If successful, make the
666 :     \G result the input buffer, set @code{>IN} to 0 and return true;
667 :     \G otherwise return false. When the input source is a block, add 1
668 :     \G to the value of @code{BLK} to make the next block the input
669 :     \G source and current input buffer, and set @code{>IN} to 0;
670 :     \G return true if the new value of @code{BLK} is a valid block
671 :     \G number, false otherwise. When the input source is a text file,
672 :     \G attempt to read the next line from the file. If successful,
673 :     \G make the result the current input buffer, set @code{>IN} to 0
674 :     \G and return true; otherwise, return false. A successful result
675 :     \G includes receipt of a line containing 0 characters.
676 : pazsan 1.12 [ has? file [IF] ]
677 :     blk @ IF 1 blk +! true 0 >in ! EXIT THEN
678 :     [ [THEN] ]
679 :     tib /line
680 :     [ has? file [IF] ]
681 :     loadfile @ ?dup
682 : pazsan 1.59 IF (read-line) throw #fill-bytes !
683 : pazsan 1.12 ELSE
684 :     [ [THEN] ]
685 :     sourceline# 0< IF 2drop false EXIT THEN
686 :     accept true
687 :     [ has? file [IF] ]
688 :     THEN
689 :     1 loadline +!
690 :     [ [THEN] ]
691 :     swap #tib ! 0 >in ! ;
692 : pazsan 1.1
693 :     : query ( -- ) \ core-ext
694 : crook 1.29 \G Make the user input device the input source. Receive input into
695 :     \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
696 :     \G superceeded by @code{accept}.
697 : pazsan 1.12 [ has? file [IF] ]
698 :     blk off loadfile off
699 :     [ [THEN] ]
700 : pazsan 1.64 refill drop ;
701 :     [THEN]
702 : pazsan 1.1
703 :     \ save-mem extend-mem
704 :    
705 :     has? os [IF]
706 :     : save-mem ( addr1 u -- addr2 u ) \ gforth
707 :     \g copy a memory block into a newly allocated region in the heap
708 :     swap >r
709 :     dup allocate throw
710 :     swap 2dup r> -rot move ;
711 :    
712 : anton 1.68 : free-mem-var ( addr -- )
713 :     \ addr is the address of a 2variable containing address and size
714 :     \ of a memory range; frees memory and clears the 2variable.
715 :     dup 2@ drop dup
716 :     if ( addr mem-start )
717 :     free throw
718 :     0 0 rot 2!
719 :     else
720 :     2drop
721 :     then ;
722 :    
723 : pazsan 1.1 : extend-mem ( addr1 u1 u -- addr addr2 u2 )
724 :     \ extend memory block allocated from the heap by u aus
725 : anton 1.105 \ the (possibly reallocated) piece is addr2 u2, the extension is at addr
726 : pazsan 1.1 over >r + dup >r resize throw
727 :     r> over r> + -rot ;
728 :     [THEN]
729 :    
730 :     \ EVALUATE 17may93jaw
731 :    
732 : pazsan 1.64 has? file 0= has? new-input 0= and [IF]
733 : pazsan 1.1 : push-file ( -- ) r>
734 : pazsan 1.12 tibstack @ >r >tib @ >r #tib @ >r
735 : pazsan 1.1 >tib @ tibstack @ = IF r@ tibstack +! THEN
736 :     tibstack @ >tib ! >in @ >r >r ;
737 :    
738 :     : pop-file ( throw-code -- throw-code )
739 :     r>
740 : pazsan 1.12 r> >in ! r> #tib ! r> >tib ! r> tibstack ! >r ;
741 : pazsan 1.1 [THEN]
742 :    
743 : pazsan 1.64 has? new-input 0= [IF]
744 : crook 1.29 : evaluate ( c-addr u -- ) \ core,block
745 : crook 1.40 \G Save the current input source specification. Store @code{-1} in
746 :     \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
747 :     \G @code{0} and make the string @i{c-addr u} the input source
748 :     \G and input buffer. Interpret. When the parse area is empty,
749 :     \G restore the input source specification.
750 : pazsan 1.64 [ has? file [IF] ]
751 : anton 1.92 s" *evaluated string*" loadfilename>r
752 : pazsan 1.64 [ [THEN] ]
753 : crook 1.40 push-file #tib ! >tib !
754 : crook 1.29 >in off
755 :     [ has? file [IF] ]
756 :     blk off loadfile off -1 loadline !
757 :     [ [THEN] ]
758 :     ['] interpret catch
759 : anton 1.56 pop-file
760 : pazsan 1.64 [ has? file [IF] ]
761 : anton 1.92 r>loadfilename
762 : pazsan 1.64 [ [THEN] ]
763 : anton 1.56 throw ;
764 : pazsan 1.64 [THEN]
765 : pazsan 1.1
766 :     \ \ Quit 13feb93py
767 :    
768 :     Defer 'quit
769 :    
770 :     Defer .status
771 :    
772 :     : prompt state @ IF ." compiled" EXIT THEN ." ok" ;
773 :    
774 : anton 1.39 : (quit) ( -- )
775 :     \ exits only through THROW etc.
776 :     BEGIN
777 : anton 1.98 .status
778 :     ['] cr catch if
779 : anton 1.99 >stderr cr ." Can't print to stdout, leaving" cr
780 : anton 1.98 \ if stderr does not work either, already DoError causes a hang
781 :     2 (bye)
782 :     endif
783 :     query interpret prompt
784 : anton 1.39 AGAIN ;
785 : pazsan 1.1
786 :     ' (quit) IS 'quit
787 :    
788 :     \ \ DOERROR (DOERROR) 13jun93jaw
789 :    
790 :     8 Constant max-errors
791 : pazsan 1.112 4 has? file 2 and + Constant /error
792 : pazsan 1.1 Variable error-stack 0 error-stack !
793 : pazsan 1.112 max-errors /error * cells allot
794 : pazsan 1.1 \ format of one cell:
795 :     \ source ( addr u )
796 :     \ >in
797 :     \ line-number
798 :     \ Loadfilename ( addr u )
799 :    
800 : pazsan 1.64 : error> ( -- addr u >in line# [addr u] )
801 :     -1 error-stack +!
802 :     error-stack dup @
803 : pazsan 1.112 /error * cells + cell+
804 :     /error cells bounds DO
805 : pazsan 1.64 I @
806 :     cell +LOOP ;
807 :     : >error ( addr u >in line# [addr u] -- )
808 :     error-stack dup @ dup 1+
809 :     max-errors 1- min error-stack !
810 : pazsan 1.112 /error * cells + cell+
811 :     /error 1- cells bounds swap DO
812 : pazsan 1.64 I !
813 :     -1 cells +LOOP ;
814 :    
815 : pazsan 1.1 : dec. ( n -- ) \ gforth
816 : crook 1.40 \G Display @i{n} as a signed decimal number, followed by a space.
817 :     \ !! not used...
818 : pazsan 1.1 base @ decimal swap . base ! ;
819 :    
820 : anton 1.111 : dec.r ( u n -- ) \ gforth
821 :     \G Display @i{u} as a unsigned decimal number in a field @i{n}
822 :     \G characters wide.
823 :     base @ >r decimal .r r> base ! ;
824 : jwilke 1.23
825 : pazsan 1.1 : hex. ( u -- ) \ gforth
826 : crook 1.40 \G Display @i{u} as an unsigned hex number, prefixed with a "$" and
827 : crook 1.17 \G followed by a space.
828 : crook 1.40 \ !! not used...
829 : jwilke 1.33 [char] $ emit base @ swap hex u. base ! ;
830 : pazsan 1.1
831 : anton 1.94 : -trailing ( c_addr u1 -- c_addr u2 ) \ string dash-trailing
832 :     \G Adjust the string specified by @i{c-addr, u1} to remove all
833 :     \G trailing spaces. @i{u2} is the length of the modified string.
834 :     BEGIN
835 : pazsan 1.102 dup
836 : anton 1.94 WHILE
837 : pazsan 1.102 1- 2dup + c@ bl <>
838 :     UNTIL 1+ THEN ;
839 : anton 1.94
840 : pazsan 1.1 DEFER DOERROR
841 : jwilke 1.33
842 :     has? backtrace [IF]
843 : anton 1.15 Defer dobacktrace ( -- )
844 :     ' noop IS dobacktrace
845 : jwilke 1.33 [THEN]
846 : pazsan 1.1
847 : jwilke 1.23 : .error-string ( throw-code -- )
848 :     dup -2 =
849 :     IF "error @ ?dup IF count type THEN drop
850 :     ELSE .error
851 :     THEN ;
852 :    
853 : anton 1.111 : umin ( u1 u2 -- u )
854 :     2dup u>
855 :     if
856 :     swap
857 :     then
858 :     drop ;
859 :    
860 : pazsan 1.112 Defer mark-start
861 :     Defer mark-end
862 :    
863 :     :noname ." >>>" ; IS mark-start
864 :     :noname ." <<<" ; IS mark-end
865 :    
866 : anton 1.111 : .error-line ( addr1 u1 n1 -- )
867 :     \ print error ending at char n1 in line addr1 u1
868 :     \ should work with UTF-8 (whitespace check looks ok)
869 :     over umin \ protect against wrong n1
870 :     swap >r ( addr1 n1 R: u1 )
871 :     -trailing 1- \ last non-space
872 :     0 >r BEGIN \ search for the first non-space
873 :     2dup + c@ bl > WHILE
874 :     r> 1+ >r 1- dup 0< UNTIL THEN 1+
875 :     ( addr1 n2 r: u1 namelen )
876 : pazsan 1.112 2dup type mark-start
877 : anton 1.111 r> -rot r> swap /string ( namelen addr2 u2 )
878 : pazsan 1.112 >r swap 2dup type mark-end ( addr2 namelen r: u2 )
879 : anton 1.111 r> swap /string type ;
880 :    
881 : pazsan 1.64 : .error-frame ( throwcode addr1 u1 n1 n2 [addr2 u2] -- throwcode )
882 :     \ addr2 u2: filename of included file - optional
883 : jwilke 1.23 \ n2: line number
884 :     \ n1: error position in input line
885 :     \ addr1 u1: input line
886 : pazsan 1.1 cr error-stack @
887 : anton 1.111 IF ( throwcode addr1 u1 n1 n2 [addr2 u2] )
888 :     [ has? file [IF] ] \ !! unbalanced stack effect
889 : pazsan 1.64 ." in file included from "
890 :     type ." :"
891 : anton 1.111 [ [THEN] ] ( throwcode addr1 u1 n1 n2 )
892 :     0 dec.r drop 2drop
893 :     ELSE ( throwcode addr1 u1 n1 n2 [addr2 u2] )
894 : pazsan 1.64 [ has? file [IF] ]
895 :     type ." :"
896 : anton 1.111 [ [THEN] ] ( throwcode addr1 u1 n1 n2 )
897 :     dup 0 dec.r ." : " 4 pick .error-string
898 :     IF \ if line# non-zero, there is a line
899 :     cr .error-line
900 : anton 1.57 ELSE
901 :     2drop drop
902 :     THEN
903 : jwilke 1.23 THEN ;
904 : pazsan 1.1
905 :     : (DoError) ( throw-code -- )
906 :     [ has? os [IF] ]
907 : pazsan 1.8 >stderr
908 : pazsan 1.1 [ [THEN] ]
909 : anton 1.111 source >in @ sourceline# [ has? file [IF] ] \ !! unbalanced stack effect
910 : pazsan 1.64 sourcefilename
911 :     [ [THEN] ] .error-frame
912 : pazsan 1.1 error-stack @ 0 ?DO
913 : pazsan 1.64 error>
914 : pazsan 1.1 .error-frame
915 :     LOOP
916 : jwilke 1.33 drop
917 :     [ has? backtrace [IF] ]
918 :     dobacktrace
919 :     [ [THEN] ]
920 : pazsan 1.8 normal-dp dpp ! ;
921 : pazsan 1.1
922 :     ' (DoError) IS DoError
923 :    
924 :     : quit ( ?? -- ?? ) \ core
925 : crook 1.27 \G Empty the return stack, make the user input device
926 :     \G the input source, enter interpret state and start
927 :     \G the text interpreter.
928 : pazsan 1.64 rp0 @ rp! handler off clear-tibstack
929 :     [ has? new-input 0= [IF] ] >tib @ >r [ [THEN] ]
930 : pazsan 1.1 BEGIN
931 :     [ has? compiler [IF] ]
932 : anton 1.104 [compile] [
933 : pazsan 1.1 [ [THEN] ]
934 : anton 1.104 \ stack depths may be arbitrary here
935 : pazsan 1.1 ['] 'quit CATCH dup
936 :     WHILE
937 : anton 1.104 <# \ reset hold area, or we may get another error
938 :     DoError
939 :     \ stack depths may be arbitrary still (or again), so clear them
940 :     clearstacks
941 :     [ has? new-input [IF] ] clear-tibstack
942 :     [ [ELSE] ] r@ >tib ! r@ tibstack !
943 :     [ [THEN] ]
944 : pazsan 1.1 REPEAT
945 : pazsan 1.64 drop [ has? new-input [IF] ] clear-tibstack
946 :     [ [ELSE] ] r> >tib !
947 :     [ [THEN] ] ;
948 : pazsan 1.1
949 :     \ \ Cold Boot 13feb93py
950 :    
951 :     : (bootmessage)
952 : anton 1.101 ." Gforth " version-string type
953 : anton 1.115 ." , Copyright (C) 1995-2003,2004 Free Software Foundation, Inc." cr
954 : anton 1.101 ." Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'"
955 : pazsan 1.1 [ has? os [IF] ]
956 :     cr ." Type `bye' to exit"
957 :     [ [THEN] ] ;
958 :    
959 :     defer bootmessage
960 :     defer process-args
961 :    
962 :     ' (bootmessage) IS bootmessage
963 :    
964 : anton 1.10 Defer 'cold ( -- ) \ gforth tick-cold
965 : pazsan 1.1 \ hook (deferred word) for things to do right before interpreting the
966 :     \ command-line arguments
967 :     ' noop IS 'cold
968 :    
969 :    
970 : jwilke 1.76 AVariable init8 NIL init8 !
971 : pazsan 1.1
972 :     : cold ( -- ) \ gforth
973 : anton 1.44 [ has? backtrace [IF] ]
974 :     rp@ backtrace-rp0 !
975 :     [ [THEN] ]
976 : pazsan 1.1 [ has? file [IF] ]
977 : pazsan 1.78 os-cold
978 : pazsan 1.1 [ [THEN] ]
979 : anton 1.116 set-encoding-fixed-width
980 : pazsan 1.1 'cold
981 :     init8 chainperform
982 :     [ has? file [IF] ]
983 : anton 1.91 s" *the terminal*" loadfilename 2!
984 : pazsan 1.8 process-args
985 : pazsan 1.12 loadline off
986 : pazsan 1.1 [ [THEN] ]
987 :     bootmessage
988 : pazsan 1.12 quit ;
989 : pazsan 1.1
990 : pazsan 1.64 has? new-input 0= [IF]
991 : anton 1.5 : clear-tibstack ( -- )
992 :     [ has? glocals [IF] ]
993 :     lp@ forthstart 7 cells + @ -
994 :     [ [ELSE] ]
995 :     [ has? os [IF] ]
996 : pazsan 1.8 r0 @ forthstart 6 cells + @ -
997 : anton 1.5 [ [ELSE] ]
998 : pazsan 1.16 sp@ $10 cells +
999 : anton 1.5 [ [THEN] ]
1000 :     [ [THEN] ]
1001 :     dup >tib ! tibstack ! #tib off >in off ;
1002 : pazsan 1.64 [THEN]
1003 : anton 1.5
1004 : pazsan 1.64 : boot ( path n **argv argc -- )
1005 : pazsan 1.1 main-task up!
1006 :     [ has? os [IF] ]
1007 : pazsan 1.78 os-boot
1008 : pazsan 1.1 [ [THEN] ]
1009 :     sp@ sp0 !
1010 : pazsan 1.74 [ has? peephole [IF] ]
1011 : anton 1.87 \ only needed for greedy static superinstruction selection
1012 :     \ primtable prepare-peephole-table TO peeptable
1013 : pazsan 1.74 [ [THEN] ]
1014 : pazsan 1.64 [ has? new-input [IF] ]
1015 :     current-input off
1016 :     [ [THEN] ]
1017 : anton 1.5 clear-tibstack
1018 : pazsan 1.1 rp@ rp0 !
1019 :     [ has? floating [IF] ]
1020 :     fp@ fp0 !
1021 :     [ [THEN] ]
1022 : anton 1.46 handler off
1023 : anton 1.98 ['] cold catch dup -&2049 <> if \ broken pipe?
1024 :     DoError cr
1025 :     endif
1026 : pazsan 1.1 [ has? os [IF] ]
1027 : anton 1.35 1 (bye) \ !! determin exit code from throw code?
1028 : pazsan 1.1 [ [THEN] ]
1029 :     ;
1030 :    
1031 :     has? os [IF]
1032 :     : bye ( -- ) \ tools-ext
1033 :     [ has? file [IF] ]
1034 :     script? 0= IF cr THEN
1035 :     [ [ELSE] ]
1036 :     cr
1037 :     [ [THEN] ]
1038 :     0 (bye) ;
1039 :     [THEN]
1040 :    
1041 :     \ **argv may be scanned by the C starter to get some important
1042 :     \ information, as -display and -geometry for an X client FORTH
1043 :     \ or space and stackspace overrides
1044 :    
1045 :     \ 0 arg contains, however, the name of the program.
1046 :    

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help