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