[gforth] / gforth / kernel / int.fs  

gforth: gforth/kernel/int.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help