[gforth] / gforth / kernel / int.fs  

gforth: gforth/kernel/int.fs


1 : pazsan 1.1 \ definitions needed for interpreter only
2 :    
3 : pazsan 1.89 \ Copyright (C) 1995-2003 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 :     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 : pazsan 1.69 \ The constants are defined as 32 bits, but then erased
261 :     \ and overwritten by the right ones
262 : anton 1.67
263 :     $80000000 constant alias-mask
264 : pazsan 1.69 1 bits/char 1 - lshift
265 :     -1 cells allot bigendian [IF] c, 0 1 cells 1- times
266 :     [ELSE] 0 1 cells 1- times c, [THEN]
267 : anton 1.67 $40000000 constant immediate-mask
268 : pazsan 1.69 1 bits/char 2 - lshift
269 :     -1 cells allot bigendian [IF] c, 0 1 cells 1- times
270 :     [ELSE] 0 1 cells 1- times c, [THEN]
271 : anton 1.67 $20000000 constant restrict-mask
272 : pazsan 1.69 1 bits/char 3 - lshift
273 :     -1 cells allot bigendian [IF] c, 0 1 cells 1- times
274 :     [ELSE] 0 1 cells 1- times c, [THEN]
275 : anton 1.67 $1fffffff constant lcount-mask
276 : pazsan 1.69 1 bits/char 3 - lshift 1 -
277 : pazsan 1.71 -1 cells allot bigendian [IF] c, -1 1 cells 1- times
278 :     [ELSE] -1 1 cells 1- times c, [THEN]
279 : pazsan 1.1
280 :     \ higher level parts of find
281 :    
282 :     : flag-sign ( f -- 1|-1 )
283 :     \ true becomes 1, false -1
284 :     0= 2* 1+ ;
285 :    
286 : anton 1.79 : ticking-compile-only-error ( ... -- )
287 :     -&2048 throw ;
288 : pazsan 1.1
289 :     : (cfa>int) ( cfa -- xt )
290 :     [ has? compiler [IF] ]
291 :     dup interpret/compile?
292 :     if
293 :     interpret/compile-int @
294 :     then
295 :     [ [THEN] ] ;
296 :    
297 : anton 1.67 : (x>int) ( cfa w -- xt )
298 : pazsan 1.1 \ get interpretation semantics of name
299 :     restrict-mask and
300 :     if
301 : anton 1.79 drop ['] ticking-compile-only-error
302 : pazsan 1.1 else
303 :     (cfa>int)
304 :     then ;
305 :    
306 :     : name>string ( nt -- addr count ) \ gforth head-to-string
307 : crook 1.40 \g @i{addr count} is the name of the word represented by @i{nt}.
308 : anton 1.67 cell+ dup cell+ swap @ lcount-mask and ;
309 : pazsan 1.1
310 :     : ((name>)) ( nfa -- cfa )
311 :     name>string + cfaligned ;
312 :    
313 : anton 1.67 : (name>x) ( nfa -- cfa w )
314 :     \ cfa is an intermediate cfa and w is the flags cell of nfa
315 : pazsan 1.1 dup ((name>))
316 : anton 1.67 swap cell+ @ dup alias-mask and 0=
317 : pazsan 1.1 IF
318 :     swap @ swap
319 :     THEN ;
320 :    
321 :     : name>int ( nt -- xt ) \ gforth
322 : crook 1.31 \G @i{xt} represents the interpretation semantics of the word
323 :     \G @i{nt}. If @i{nt} has no interpretation semantics (i.e. is
324 :     \G @code{compile-only}), @i{xt} is the execution token for
325 : anton 1.79 \G @code{ticking-compile-only-error}, which performs @code{-2048 throw}.
326 : pazsan 1.1 (name>x) (x>int) ;
327 :    
328 :     : name?int ( nt -- xt ) \ gforth
329 : anton 1.79 \G Like @code{name>int}, but perform @code{-2048 throw} if @i{nt}
330 : crook 1.31 \G has no interpretation semantics.
331 : pazsan 1.1 (name>x) restrict-mask and
332 :     if
333 : anton 1.79 ticking-compile-only-error \ does not return
334 : pazsan 1.1 then
335 :     (cfa>int) ;
336 :    
337 :     : (name>comp) ( nt -- w +-1 ) \ gforth
338 : crook 1.31 \G @i{w xt} is the compilation token for the word @i{nt}.
339 : pazsan 1.1 (name>x) >r
340 :     [ has? compiler [IF] ]
341 :     dup interpret/compile?
342 :     if
343 :     interpret/compile-comp @
344 :     then
345 :     [ [THEN] ]
346 :     r> immediate-mask and flag-sign
347 :     ;
348 :    
349 :     : (name>intn) ( nfa -- xt +-1 )
350 : anton 1.67 (name>x) tuck (x>int) ( w xt )
351 : pazsan 1.1 swap immediate-mask and flag-sign ;
352 :    
353 : pazsan 1.72 const Create ??? 0 , 3 , char ? c, char ? c, char ? c,
354 : jwilke 1.30 \ ??? is used by dovar:, must be created/:dovar
355 :    
356 :     [IFDEF] forthstart
357 :     \ if we have a forthstart we can define head? with it
358 :     \ otherwise leave out the head? check
359 :    
360 : anton 1.14 : head? ( addr -- f )
361 : anton 1.82 \G heuristic check whether addr is a name token; may deliver false
362 :     \G positives; addr must be a valid address; returns 1 for
363 :     \G particularly unsafe positives
364 : anton 1.14 \ we follow the link fields and check for plausibility; two
365 :     \ iterations should catch most false addresses: on the first
366 :     \ iteration, we may get an xt, on the second a code address (or
367 :     \ some code), which is typically not in the dictionary.
368 : anton 1.82 \ we added a third iteration for working with code and ;code words.
369 :     3 0 do
370 : anton 1.41 dup dup aligned <> if \ protect @ against unaligned accesses
371 :     drop false unloop exit
372 :     then
373 : anton 1.14 dup @ dup
374 :     if ( addr addr1 )
375 :     dup rot forthstart within
376 :     if \ addr1 is outside forthstart..addr, not a head
377 :     drop false unloop exit
378 :     then ( addr1 )
379 :     else \ 0 in the link field, no further checks
380 : anton 1.81 2drop 1 unloop exit \ this is very unsure, so return 1
381 : anton 1.14 then
382 :     loop
383 :     \ in dubio pro:
384 :     drop true ;
385 :    
386 : anton 1.48 : >head-noprim ( cfa -- nt ) \ gforth to-head-noprim
387 : anton 1.67 \ also heuristic; finds only names with up to 32 chars
388 : pazsan 1.45 $25 cell do ( cfa )
389 : pazsan 1.70 dup i - dup @ [ alias-mask lcount-mask or ] literal
390 :     [ 1 bits/char 3 - lshift 1 - 1 bits/char 1 - lshift or
391 : pazsan 1.71 -1 cells allot bigendian [IF] c, -1 1 cells 1- times
392 :     [ELSE] -1 1 cells 1- times c, [THEN] ]
393 : pazsan 1.70 and ( cfa len|alias )
394 : anton 1.67 swap + cell + cfaligned over alias-mask + =
395 : anton 1.14 if ( cfa )
396 :     dup i - cell - dup head?
397 :     if
398 :     nip unloop exit
399 :     then
400 :     drop
401 :     then
402 :     cell +loop
403 :     drop ??? ( wouldn't 0 be better? ) ;
404 : pazsan 1.1
405 : jwilke 1.30 [ELSE]
406 :    
407 : anton 1.48 : >head-noprim ( cfa -- nt ) \ gforth to-head-noprim
408 : pazsan 1.45 $25 cell do ( cfa )
409 : pazsan 1.70 dup i - dup @ [ alias-mask lcount-mask or ] literal
410 :     [ 1 bits/char 3 - lshift 1 - 1 bits/char 1 - lshift or
411 : pazsan 1.71 -1 cells allot bigendian [IF] c, -1 1 cells 1- times
412 :     [ELSE] -1 1 cells 1- times c, [THEN] ]
413 : pazsan 1.70 and ( cfa len|alias )
414 : anton 1.67 swap + cell + cfaligned over alias-mask + =
415 : jwilke 1.30 if ( cfa ) i - cell - unloop exit
416 :     then
417 :     cell +loop
418 :     drop ??? ( wouldn't 0 be better? ) ;
419 :    
420 :     [THEN]
421 : pazsan 1.1
422 : anton 1.83 cell% 2* 0 0 field >body ( xt -- a_addr ) \ core
423 :     \G Get the address of the body of the word represented by @i{xt} (the
424 :     \G address of the word's data field).
425 :     drop drop
426 :    
427 :     cell% -2 * 0 0 field body> ( xt -- a_addr )
428 : anton 1.84 drop drop
429 :    
430 :     has? standardthreading has? compiler and [IF]
431 :    
432 :     ' @ alias >code-address ( xt -- c_addr ) \ gforth
433 :     \G @i{c-addr} is the code address of the word @i{xt}.
434 :    
435 :     : >does-code ( xt -- a_addr ) \ gforth
436 :     \G If @i{xt} is the execution token of a child of a @code{DOES>} word,
437 :     \G @i{a-addr} is the start of the Forth code after the @code{DOES>};
438 :     \G Otherwise @i{a-addr} is 0.
439 :     dup @ dodoes: = if
440 :     cell+ @
441 :     else
442 :     drop 0
443 :     endif ;
444 :    
445 : anton 1.85 ' ! alias code-address! ( c_addr xt -- ) \ gforth
446 :     \G Create a code field with code address @i{c-addr} at @i{xt}.
447 :    
448 :     : does-code! ( a_addr xt -- ) \ gforth
449 :     \G Create a code field at @i{xt} for a child of a @code{DOES>}-word;
450 :     \G @i{a-addr} is the start of the Forth code after @code{DOES>}.
451 :     dodoes: over ! cell+ ! ;
452 :    
453 : anton 1.86 ' drop alias does-handler! ( a_addr -- ) \ gforth
454 :     \G Create a @code{DOES>}-handler at address @i{a-addr}. Normally,
455 :     \G @i{a-addr} points just behind a @code{DOES>}.
456 :    
457 : anton 1.85 2 cells constant /does-handler ( -- n ) \ gforth
458 :     \G The size of a @code{DOES>}-handler (includes possible padding).
459 :    
460 : anton 1.84 [THEN]
461 : pazsan 1.1
462 : crook 1.31 : (search-wordlist) ( addr count wid -- nt | false )
463 : pazsan 1.1 dup wordlist-map @ find-method perform ;
464 :    
465 : crook 1.31 : search-wordlist ( c-addr count wid -- 0 | xt +-1 ) \ search
466 : anton 1.53 \G Search the word list identified by @i{wid} for the definition
467 :     \G named by the string at @i{c-addr count}. If the definition is
468 :     \G not found, return 0. If the definition is found return 1 (if
469 :     \G the definition is immediate) or -1 (if the definition is not
470 :     \G immediate) together with the @i{xt}. In Gforth, the @i{xt}
471 :     \G returned represents the interpretation semantics. ANS Forth
472 :     \G does not specify clearly what @i{xt} represents.
473 : pazsan 1.1 (search-wordlist) dup if
474 :     (name>intn)
475 :     then ;
476 :    
477 : crook 1.31 : find-name ( c-addr u -- nt | 0 ) \ gforth
478 :     \g Find the name @i{c-addr u} in the current search
479 :     \g order. Return its @i{nt}, if found, otherwise 0.
480 : pazsan 1.1 lookup @ (search-wordlist) ;
481 :    
482 :     : sfind ( c-addr u -- 0 / xt +-1 ) \ gforth-obsolete
483 :     find-name dup
484 :     if ( nt )
485 :     state @
486 :     if
487 :     (name>comp)
488 :     else
489 :     (name>intn)
490 :     then
491 :     then ;
492 :    
493 : crook 1.31 : find ( c-addr -- xt +-1 | c-addr 0 ) \ core,search
494 : anton 1.53 \G Search all word lists in the current search order for the
495 :     \G definition named by the counted string at @i{c-addr}. If the
496 :     \G definition is not found, return 0. If the definition is found
497 :     \G return 1 (if the definition has non-default compilation
498 :     \G semantics) or -1 (if the definition has default compilation
499 :     \G semantics). The @i{xt} returned in interpret state represents
500 :     \G the interpretation semantics. The @i{xt} returned in compile
501 :     \G state represented either the compilation semantics (for
502 :     \G non-default compilation semantics) or the run-time semantics
503 :     \G that the compilation semantics would @code{compile,} (for
504 :     \G default compilation semantics). The ANS Forth standard does
505 :     \G not specify clearly what the returned @i{xt} represents (and
506 :     \G also talks about immediacy instead of non-default compilation
507 :     \G semantics), so this word is questionable in portable programs.
508 :     \G If non-portability is ok, @code{find-name} and friends are
509 :     \G better (@pxref{Name token}).
510 : pazsan 1.1 dup count sfind dup
511 :     if
512 :     rot drop
513 :     then ;
514 :    
515 : jwilke 1.34 \ ticks in interpreter
516 : pazsan 1.1
517 :     : (') ( "name" -- nt ) \ gforth
518 : anton 1.32 name name-too-short?
519 : anton 1.28 find-name dup 0=
520 : pazsan 1.1 IF
521 : anton 1.42 drop -&13 throw
522 : pazsan 1.1 THEN ;
523 :    
524 :     : ' ( "name" -- xt ) \ core tick
525 : crook 1.31 \g @i{xt} represents @i{name}'s interpretation
526 :     \g semantics. Perform @code{-14 throw} if the word has no
527 : pazsan 1.1 \g interpretation semantics.
528 :     (') name?int ;
529 : jwilke 1.34
530 :     has? compiler 0= [IF] \ interpreter only version of IS and TO
531 :    
532 :     : IS ' >body ! ;
533 :     ' IS Alias TO
534 :    
535 :     [THEN]
536 : pazsan 1.1
537 :     \ \ the interpreter loop mar92py
538 :    
539 :     \ interpret 10mar92py
540 :    
541 : anton 1.37 Defer parser ( c-addr u -- )
542 : anton 1.77 Defer parse-word ( -- c-addr count ) \ gforth
543 : anton 1.55 \G Get the next word from the input buffer
544 : anton 1.77 ' (name) IS parse-word
545 :    
546 :     ' parse-word alias name ( -- c-addr u ) \ gforth-obsolete
547 :     \G old name for @code{parse-word}
548 :    
549 : pazsan 1.1 Defer compiler-notfound ( c-addr count -- )
550 :     Defer interpreter-notfound ( c-addr count -- )
551 :    
552 :     : no.extensions ( addr u -- )
553 : anton 1.42 2drop -&13 throw ;
554 : pazsan 1.1 ' no.extensions IS compiler-notfound
555 :     ' no.extensions IS interpreter-notfound
556 :    
557 : anton 1.66 : interpret1 ( ... -- ... )
558 : jwilke 1.33 [ has? backtrace [IF] ]
559 : anton 1.24 rp@ backtrace-rp0 !
560 : jwilke 1.33 [ [THEN] ]
561 : pazsan 1.1 BEGIN
562 :     ?stack name dup
563 :     WHILE
564 :     parser
565 :     REPEAT
566 : anton 1.66 2drop ;
567 :    
568 :     : interpret ( ?? -- ?? ) \ gforth
569 :     \ interpret/compile the (rest of the) input buffer
570 :     [ has? backtrace [IF] ]
571 :     backtrace-rp0 @ >r
572 :     [ [THEN] ]
573 :     ['] interpret1 catch
574 : anton 1.65 [ has? backtrace [IF] ]
575 :     r> backtrace-rp0 !
576 : anton 1.66 [ [THEN] ]
577 :     throw ;
578 : pazsan 1.1
579 :     \ interpreter 30apr92py
580 :    
581 :     \ not the most efficient implementations of interpreter and compiler
582 : jwilke 1.33 : interpreter ( c-addr u -- )
583 : pazsan 1.1 2dup find-name dup
584 :     if
585 :     nip nip name>int execute
586 :     else
587 :     drop
588 :     2dup 2>r snumber?
589 :     IF
590 :     2rdrop
591 :     ELSE
592 :     2r> interpreter-notfound
593 :     THEN
594 :     then ;
595 :    
596 :     ' interpreter IS parser
597 :    
598 :     \ \ Query Evaluate 07apr93py
599 :    
600 :     has? file 0= [IF]
601 : pazsan 1.12 : sourceline# ( -- n ) 1 ;
602 : pazsan 1.61 [ELSE]
603 : pazsan 1.64 has? new-input 0= [IF]
604 : pazsan 1.58 Variable #fill-bytes
605 :     \G number of bytes read via (read-line) by the last refill
606 : pazsan 1.61 [THEN]
607 : pazsan 1.64 [THEN]
608 : pazsan 1.58
609 : pazsan 1.64 has? new-input 0= [IF]
610 : pazsan 1.1 : refill ( -- flag ) \ core-ext,block-ext,file-ext
611 : crook 1.29 \G Attempt to fill the input buffer from the input source. When
612 :     \G the input source is the user input device, attempt to receive
613 :     \G input into the terminal input device. If successful, make the
614 :     \G result the input buffer, set @code{>IN} to 0 and return true;
615 :     \G otherwise return false. When the input source is a block, add 1
616 :     \G to the value of @code{BLK} to make the next block the input
617 :     \G source and current input buffer, and set @code{>IN} to 0;
618 :     \G return true if the new value of @code{BLK} is a valid block
619 :     \G number, false otherwise. When the input source is a text file,
620 :     \G attempt to read the next line from the file. If successful,
621 :     \G make the result the current input buffer, set @code{>IN} to 0
622 :     \G and return true; otherwise, return false. A successful result
623 :     \G includes receipt of a line containing 0 characters.
624 : pazsan 1.12 [ has? file [IF] ]
625 :     blk @ IF 1 blk +! true 0 >in ! EXIT THEN
626 :     [ [THEN] ]
627 :     tib /line
628 :     [ has? file [IF] ]
629 :     loadfile @ ?dup
630 : pazsan 1.59 IF (read-line) throw #fill-bytes !
631 : pazsan 1.12 ELSE
632 :     [ [THEN] ]
633 :     sourceline# 0< IF 2drop false EXIT THEN
634 :     accept true
635 :     [ has? file [IF] ]
636 :     THEN
637 :     1 loadline +!
638 :     [ [THEN] ]
639 :     swap #tib ! 0 >in ! ;
640 : pazsan 1.1
641 :     : query ( -- ) \ core-ext
642 : crook 1.29 \G Make the user input device the input source. Receive input into
643 :     \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
644 :     \G superceeded by @code{accept}.
645 : pazsan 1.12 [ has? file [IF] ]
646 :     blk off loadfile off
647 :     [ [THEN] ]
648 : pazsan 1.64 refill drop ;
649 :     [THEN]
650 : pazsan 1.1
651 :     \ save-mem extend-mem
652 :    
653 :     has? os [IF]
654 :     : save-mem ( addr1 u -- addr2 u ) \ gforth
655 :     \g copy a memory block into a newly allocated region in the heap
656 :     swap >r
657 :     dup allocate throw
658 :     swap 2dup r> -rot move ;
659 :    
660 : anton 1.68 : free-mem-var ( addr -- )
661 :     \ addr is the address of a 2variable containing address and size
662 :     \ of a memory range; frees memory and clears the 2variable.
663 :     dup 2@ drop dup
664 :     if ( addr mem-start )
665 :     free throw
666 :     0 0 rot 2!
667 :     else
668 :     2drop
669 :     then ;
670 :    
671 : pazsan 1.1 : extend-mem ( addr1 u1 u -- addr addr2 u2 )
672 :     \ extend memory block allocated from the heap by u aus
673 :     \ the (possibly reallocated piece is addr2 u2, the extension is at addr
674 :     over >r + dup >r resize throw
675 :     r> over r> + -rot ;
676 :     [THEN]
677 :    
678 :     \ EVALUATE 17may93jaw
679 :    
680 : pazsan 1.64 has? file 0= has? new-input 0= and [IF]
681 : pazsan 1.1 : push-file ( -- ) r>
682 : pazsan 1.12 tibstack @ >r >tib @ >r #tib @ >r
683 : pazsan 1.1 >tib @ tibstack @ = IF r@ tibstack +! THEN
684 :     tibstack @ >tib ! >in @ >r >r ;
685 :    
686 :     : pop-file ( throw-code -- throw-code )
687 :     r>
688 : pazsan 1.12 r> >in ! r> #tib ! r> >tib ! r> tibstack ! >r ;
689 : pazsan 1.1 [THEN]
690 :    
691 : pazsan 1.64 has? new-input 0= [IF]
692 : crook 1.29 : evaluate ( c-addr u -- ) \ core,block
693 : crook 1.40 \G Save the current input source specification. Store @code{-1} in
694 :     \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
695 :     \G @code{0} and make the string @i{c-addr u} the input source
696 :     \G and input buffer. Interpret. When the parse area is empty,
697 :     \G restore the input source specification.
698 : pazsan 1.64 [ has? file [IF] ]
699 : anton 1.56 loadfilename# @ >r
700 : anton 1.60 1 loadfilename# ! \ "*evaluated string*"
701 : pazsan 1.64 [ [THEN] ]
702 : crook 1.40 push-file #tib ! >tib !
703 : crook 1.29 >in off
704 :     [ has? file [IF] ]
705 :     blk off loadfile off -1 loadline !
706 :     [ [THEN] ]
707 :     ['] interpret catch
708 : anton 1.56 pop-file
709 : pazsan 1.64 [ has? file [IF] ]
710 : anton 1.56 r> loadfilename# !
711 : pazsan 1.64 [ [THEN] ]
712 : anton 1.56 throw ;
713 : pazsan 1.64 [THEN]
714 : pazsan 1.1
715 :     \ \ Quit 13feb93py
716 :    
717 :     Defer 'quit
718 :    
719 :     Defer .status
720 :    
721 :     : prompt state @ IF ." compiled" EXIT THEN ." ok" ;
722 :    
723 : anton 1.39 : (quit) ( -- )
724 :     \ exits only through THROW etc.
725 : anton 1.42 \ sp0 @ cell - handler @ &12 + ! \ !! kludge: fix the stack pointer
726 : anton 1.39 \ stored in the system's CATCH frame, so the stack depth will be 0
727 :     \ after the next THROW it catches (it may be off due to BOUNCEs or
728 :     \ because process-args left something on the stack)
729 :     BEGIN
730 : pazsan 1.64 .status cr query interpret prompt
731 : anton 1.39 AGAIN ;
732 : pazsan 1.1
733 :     ' (quit) IS 'quit
734 :    
735 :     \ \ DOERROR (DOERROR) 13jun93jaw
736 :    
737 :     8 Constant max-errors
738 :     Variable error-stack 0 error-stack !
739 : pazsan 1.64 max-errors has? file [IF] 6 [ELSE] 4 [THEN] * cells allot
740 : pazsan 1.1 \ format of one cell:
741 :     \ source ( addr u )
742 :     \ >in
743 :     \ line-number
744 :     \ Loadfilename ( addr u )
745 :    
746 : pazsan 1.64 : error> ( -- addr u >in line# [addr u] )
747 :     -1 error-stack +!
748 :     error-stack dup @
749 :     [ has? file [IF] 6 [ELSE] 4 [THEN] ] Literal * cells + cell+
750 :     [ has? file [IF] 6 [ELSE] 4 [THEN] ] Literal cells bounds DO
751 :     I @
752 :     cell +LOOP ;
753 :     : >error ( addr u >in line# [addr u] -- )
754 :     error-stack dup @ dup 1+
755 :     max-errors 1- min error-stack !
756 :     [ has? file [IF] 6 [ELSE] 4 [THEN] ] Literal * cells + cell+
757 :     [ has? file [IF] 6 [ELSE] 4 [THEN] 1- ] Literal cells bounds swap DO
758 :     I !
759 :     -1 cells +LOOP ;
760 :    
761 : pazsan 1.1 : dec. ( n -- ) \ gforth
762 : crook 1.40 \G Display @i{n} as a signed decimal number, followed by a space.
763 :     \ !! not used...
764 : pazsan 1.1 base @ decimal swap . base ! ;
765 :    
766 : jwilke 1.23 : dec.r ( u -- ) \ gforth
767 : crook 1.40 \G Display @i{u} as a unsigned decimal number
768 : jwilke 1.23 base @ decimal swap 0 .r base ! ;
769 :    
770 : pazsan 1.1 : hex. ( u -- ) \ gforth
771 : crook 1.40 \G Display @i{u} as an unsigned hex number, prefixed with a "$" and
772 : crook 1.17 \G followed by a space.
773 : crook 1.40 \ !! not used...
774 : jwilke 1.33 [char] $ emit base @ swap hex u. base ! ;
775 : pazsan 1.1
776 :     : typewhite ( addr u -- ) \ gforth
777 : crook 1.40 \G Like type, but white space is printed instead of the characters.
778 : pazsan 1.1 bounds ?do
779 :     i c@ #tab = if \ check for tab
780 :     #tab
781 :     else
782 :     bl
783 :     then
784 :     emit
785 :     loop ;
786 :    
787 :     DEFER DOERROR
788 : jwilke 1.33
789 :     has? backtrace [IF]
790 : anton 1.15 Defer dobacktrace ( -- )
791 :     ' noop IS dobacktrace
792 : jwilke 1.33 [THEN]
793 : pazsan 1.1
794 : jwilke 1.23 : .error-string ( throw-code -- )
795 :     dup -2 =
796 :     IF "error @ ?dup IF count type THEN drop
797 :     ELSE .error
798 :     THEN ;
799 :    
800 : pazsan 1.64 : .error-frame ( throwcode addr1 u1 n1 n2 [addr2 u2] -- throwcode )
801 :     \ addr2 u2: filename of included file - optional
802 : jwilke 1.23 \ n2: line number
803 :     \ n1: error position in input line
804 :     \ addr1 u1: input line
805 : pazsan 1.1 cr error-stack @
806 :     IF
807 : pazsan 1.64 [ has? file [IF] ]
808 :     ." in file included from "
809 :     type ." :"
810 :     [ [THEN] ]
811 :     dec.r drop 2drop
812 : pazsan 1.1 ELSE
813 : pazsan 1.64 [ has? file [IF] ]
814 :     type ." :"
815 :     [ [THEN] ]
816 :     dup >r dec.r ." : " 3 pick .error-string
817 : anton 1.57 r> IF \ if line# non-zero, there is a line
818 :     cr dup 2over type cr drop
819 :     nip -trailing 1- ( line-start index2 )
820 :     0 >r BEGIN
821 :     2dup + c@ bl > WHILE
822 :     r> 1+ >r 1- dup 0< UNTIL THEN 1+
823 :     ( line-start index1 )
824 :     typewhite
825 :     r> 1 max 0 ?do \ we want at least one "^", even if the length is 0
826 :     [char] ^ emit
827 :     loop
828 :     ELSE
829 :     2drop drop
830 :     THEN
831 : jwilke 1.23 THEN ;
832 : pazsan 1.1
833 :     : (DoError) ( throw-code -- )
834 :     [ has? os [IF] ]
835 : pazsan 1.8 >stderr
836 : pazsan 1.1 [ [THEN] ]
837 : pazsan 1.64 source >in @ sourceline# [ has? file [IF] ]
838 :     sourcefilename
839 :     [ [THEN] ] .error-frame
840 : pazsan 1.1 error-stack @ 0 ?DO
841 : pazsan 1.64 error>
842 : pazsan 1.1 .error-frame
843 :     LOOP
844 : jwilke 1.33 drop
845 :     [ has? backtrace [IF] ]
846 :     dobacktrace
847 :     [ [THEN] ]
848 : pazsan 1.8 normal-dp dpp ! ;
849 : pazsan 1.1
850 :     ' (DoError) IS DoError
851 :    
852 :     : quit ( ?? -- ?? ) \ core
853 : crook 1.27 \G Empty the return stack, make the user input device
854 :     \G the input source, enter interpret state and start
855 :     \G the text interpreter.
856 : pazsan 1.64 rp0 @ rp! handler off clear-tibstack
857 :     [ has? new-input 0= [IF] ] >tib @ >r [ [THEN] ]
858 : pazsan 1.1 BEGIN
859 :     [ has? compiler [IF] ]
860 : jwilke 1.76 [compile] [
861 : pazsan 1.1 [ [THEN] ]
862 :     ['] 'quit CATCH dup
863 :     WHILE
864 : anton 1.22 <# \ reset hold area, or we may get another error
865 : pazsan 1.64 DoError
866 :     [ has? new-input [IF] ] clear-tibstack
867 :     [ [ELSE] ] r@ >tib ! r@ tibstack !
868 :     [ [THEN] ]
869 : pazsan 1.1 REPEAT
870 : pazsan 1.64 drop [ has? new-input [IF] ] clear-tibstack
871 :     [ [ELSE] ] r> >tib !
872 :     [ [THEN] ] ;
873 : pazsan 1.1
874 :     \ \ Cold Boot 13feb93py
875 :    
876 :     : (bootmessage)
877 :     ." GForth " version-string type
878 : pazsan 1.88 ." , Copyright (C) 1995-2003 Free Software Foundation, Inc." cr
879 : pazsan 1.1 ." GForth comes with ABSOLUTELY NO WARRANTY; for details type `license'"
880 :     [ has? os [IF] ]
881 :     cr ." Type `bye' to exit"
882 :     [ [THEN] ] ;
883 :    
884 :     defer bootmessage
885 :     defer process-args
886 :    
887 :     ' (bootmessage) IS bootmessage
888 :    
889 : anton 1.10 Defer 'cold ( -- ) \ gforth tick-cold
890 : pazsan 1.1 \ hook (deferred word) for things to do right before interpreting the
891 :     \ command-line arguments
892 :     ' noop IS 'cold
893 :    
894 :    
895 : jwilke 1.76 AVariable init8 NIL init8 !
896 : pazsan 1.1
897 :     : cold ( -- ) \ gforth
898 : anton 1.44 [ has? backtrace [IF] ]
899 :     rp@ backtrace-rp0 !
900 :     [ [THEN] ]
901 : pazsan 1.1 [ has? file [IF] ]
902 : pazsan 1.78 os-cold
903 : pazsan 1.1 [ [THEN] ]
904 :     'cold
905 :     init8 chainperform
906 :     [ has? file [IF] ]
907 : anton 1.62 loadfilename# off
908 : pazsan 1.8 process-args
909 : pazsan 1.12 loadline off
910 : pazsan 1.1 [ [THEN] ]
911 :     bootmessage
912 : pazsan 1.12 quit ;
913 : pazsan 1.1
914 : pazsan 1.64 has? new-input 0= [IF]
915 : anton 1.5 : clear-tibstack ( -- )
916 :     [ has? glocals [IF] ]
917 :     lp@ forthstart 7 cells + @ -
918 :     [ [ELSE] ]
919 :     [ has? os [IF] ]
920 : pazsan 1.8 r0 @ forthstart 6 cells + @ -
921 : anton 1.5 [ [ELSE] ]
922 : pazsan 1.16 sp@ $10 cells +
923 : anton 1.5 [ [THEN] ]
924 :     [ [THEN] ]
925 :     dup >tib ! tibstack ! #tib off >in off ;
926 : pazsan 1.64 [THEN]
927 : anton 1.5
928 : pazsan 1.64 : boot ( path n **argv argc -- )
929 : pazsan 1.1 main-task up!
930 :     [ has? os [IF] ]
931 : pazsan 1.78 os-boot
932 : pazsan 1.1 [ [THEN] ]
933 :     sp@ sp0 !
934 : pazsan 1.74 [ has? peephole [IF] ]
935 : anton 1.87 \ only needed for greedy static superinstruction selection
936 :     \ primtable prepare-peephole-table TO peeptable
937 : pazsan 1.74 [ [THEN] ]
938 : pazsan 1.64 [ has? new-input [IF] ]
939 :     current-input off
940 :     [ [THEN] ]
941 : anton 1.5 clear-tibstack
942 : pazsan 1.1 rp@ rp0 !
943 :     [ has? floating [IF] ]
944 :     fp@ fp0 !
945 :     [ [THEN] ]
946 : anton 1.46 handler off
947 : pazsan 1.8 ['] cold catch DoError cr
948 : pazsan 1.1 [ has? os [IF] ]
949 : anton 1.35 1 (bye) \ !! determin exit code from throw code?
950 : pazsan 1.1 [ [THEN] ]
951 :     ;
952 :    
953 :     has? os [IF]
954 :     : bye ( -- ) \ tools-ext
955 :     [ has? file [IF] ]
956 :     script? 0= IF cr THEN
957 :     [ [ELSE] ]
958 :     cr
959 :     [ [THEN] ]
960 :     0 (bye) ;
961 :     [THEN]
962 :    
963 :     \ **argv may be scanned by the C starter to get some important
964 :     \ information, as -display and -geometry for an X client FORTH
965 :     \ or space and stackspace overrides
966 :    
967 :     \ 0 arg contains, however, the name of the program.
968 :    

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help