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