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