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