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