File:
[gforth] /
gforth /
kernel /
int.fs
Revision
1.120:
download - view:
text,
annotated -
select for diffs
Tue Jun 28 06:45:08 2005 UTC (18 years, 9 months ago) by
anton
Branches:
MAIN
CVS tags:
HEAD
refactored text interpreter to make return stack words work within a line
replaced parser, compiler, interpreter, ...-notfound by words
with an appended "1"
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 parser1 ( c-addr u -- ... xt)
590: \ "... xt" is the action to be performed by the text-interpretation of c-addr u
591:
592: : parser ( c-addr u -- ... )
593: \ text-interpret the word/number c-addr u, possibly producing a number
594: parser1 execute ;
595:
596: Defer parse-name ( "name" -- c-addr u ) \ gforth
597: \G Get the next word from the input buffer
598: ' (name) IS parse-name
599:
600: ' parse-name alias parse-word ( -- c-addr u ) \ gforth-obsolete
601: \G old name for @code{parse-name}
602:
603: ' parse-name alias name ( -- c-addr u ) \ gforth-obsolete
604: \G old name for @code{parse-name}
605:
606: Defer compiler-notfound1 ( c-addr count -- ... xt )
607: Defer interpreter-notfound1 ( c-addr count -- ... xt )
608:
609: : no.extensions ( addr u -- )
610: 2drop -&13 throw ;
611: ' no.extensions IS compiler-notfound1
612: ' no.extensions IS interpreter-notfound1
613:
614: Defer before-word ( -- ) \ gforth
615: \ called before the text interpreter parses the next word
616: ' noop IS before-word
617:
618: : interpret1 ( ... -- ... )
619: [ has? backtrace [IF] ]
620: rp@ backtrace-rp0 !
621: [ [THEN] ]
622: BEGIN
623: ?stack before-word name dup
624: WHILE
625: parser1 execute
626: REPEAT
627: 2drop ;
628:
629: : interpret ( ?? -- ?? ) \ gforth
630: \ interpret/compile the (rest of the) input buffer
631: [ has? backtrace [IF] ]
632: backtrace-rp0 @ >r
633: [ [THEN] ]
634: ['] interpret1 catch
635: [ has? backtrace [IF] ]
636: r> backtrace-rp0 !
637: [ [THEN] ]
638: throw ;
639:
640: \ interpreter 30apr92py
641:
642: \ not the most efficient implementations of interpreter and compiler
643: : interpreter1 ( c-addr u -- ... xt )
644: 2dup find-name dup
645: if
646: nip nip name>int
647: else
648: drop
649: 2dup 2>r snumber?
650: IF
651: 2rdrop ['] noop
652: ELSE
653: 2r> interpreter-notfound1
654: THEN
655: then ;
656:
657: ' interpreter1 IS parser1
658:
659: \ \ Query Evaluate 07apr93py
660:
661: has? file 0= [IF]
662: : sourceline# ( -- n ) 1 ;
663: [ELSE]
664: has? new-input 0= [IF]
665: Variable #fill-bytes
666: \G number of bytes read via (read-line) by the last refill
667: [THEN]
668: [THEN]
669:
670: has? new-input 0= [IF]
671: : refill ( -- flag ) \ core-ext,block-ext,file-ext
672: \G Attempt to fill the input buffer from the input source. When
673: \G the input source is the user input device, attempt to receive
674: \G input into the terminal input device. If successful, make the
675: \G result the input buffer, set @code{>IN} to 0 and return true;
676: \G otherwise return false. When the input source is a block, add 1
677: \G to the value of @code{BLK} to make the next block the input
678: \G source and current input buffer, and set @code{>IN} to 0;
679: \G return true if the new value of @code{BLK} is a valid block
680: \G number, false otherwise. When the input source is a text file,
681: \G attempt to read the next line from the file. If successful,
682: \G make the result the current input buffer, set @code{>IN} to 0
683: \G and return true; otherwise, return false. A successful result
684: \G includes receipt of a line containing 0 characters.
685: [ has? file [IF] ]
686: blk @ IF 1 blk +! true 0 >in ! EXIT THEN
687: [ [THEN] ]
688: tib /line
689: [ has? file [IF] ]
690: loadfile @ ?dup
691: IF (read-line) throw #fill-bytes !
692: ELSE
693: [ [THEN] ]
694: sourceline# 0< IF 2drop false EXIT THEN
695: accept true
696: [ has? file [IF] ]
697: THEN
698: 1 loadline +!
699: [ [THEN] ]
700: swap #tib ! 0 >in ! ;
701:
702: : query ( -- ) \ core-ext
703: \G Make the user input device the input source. Receive input into
704: \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
705: \G superceeded by @code{accept}.
706: [ has? file [IF] ]
707: blk off loadfile off
708: [ [THEN] ]
709: refill drop ;
710: [THEN]
711:
712: \ save-mem extend-mem
713:
714: has? os [IF]
715: : save-mem ( addr1 u -- addr2 u ) \ gforth
716: \g copy a memory block into a newly allocated region in the heap
717: swap >r
718: dup allocate throw
719: swap 2dup r> -rot move ;
720:
721: : free-mem-var ( addr -- )
722: \ addr is the address of a 2variable containing address and size
723: \ of a memory range; frees memory and clears the 2variable.
724: dup 2@ drop dup
725: if ( addr mem-start )
726: free throw
727: 0 0 rot 2!
728: else
729: 2drop
730: then ;
731:
732: : extend-mem ( addr1 u1 u -- addr addr2 u2 )
733: \ extend memory block allocated from the heap by u aus
734: \ the (possibly reallocated) piece is addr2 u2, the extension is at addr
735: over >r + dup >r resize throw
736: r> over r> + -rot ;
737: [THEN]
738:
739: \ EVALUATE 17may93jaw
740:
741: has? file 0= has? new-input 0= and [IF]
742: : push-file ( -- ) r>
743: tibstack @ >r >tib @ >r #tib @ >r
744: >tib @ tibstack @ = IF r@ tibstack +! THEN
745: tibstack @ >tib ! >in @ >r >r ;
746:
747: : pop-file ( throw-code -- throw-code )
748: r>
749: r> >in ! r> #tib ! r> >tib ! r> tibstack ! >r ;
750: [THEN]
751:
752: has? new-input 0= [IF]
753: : evaluate ( c-addr u -- ) \ core,block
754: \G Save the current input source specification. Store @code{-1} in
755: \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
756: \G @code{0} and make the string @i{c-addr u} the input source
757: \G and input buffer. Interpret. When the parse area is empty,
758: \G restore the input source specification.
759: [ has? file [IF] ]
760: s" *evaluated string*" loadfilename>r
761: [ [THEN] ]
762: push-file #tib ! >tib !
763: >in off
764: [ has? file [IF] ]
765: blk off loadfile off -1 loadline !
766: [ [THEN] ]
767: ['] interpret catch
768: pop-file
769: [ has? file [IF] ]
770: r>loadfilename
771: [ [THEN] ]
772: throw ;
773: [THEN]
774:
775: \ \ Quit 13feb93py
776:
777: Defer 'quit
778:
779: Defer .status
780:
781: : prompt state @ IF ." compiled" EXIT THEN ." ok" ;
782:
783: : (quit) ( -- )
784: \ exits only through THROW etc.
785: BEGIN
786: .status
787: ['] cr catch if
788: >stderr cr ." Can't print to stdout, leaving" cr
789: \ if stderr does not work either, already DoError causes a hang
790: 2 (bye)
791: endif
792: query interpret prompt
793: AGAIN ;
794:
795: ' (quit) IS 'quit
796:
797: \ \ DOERROR (DOERROR) 13jun93jaw
798:
799: 8 Constant max-errors
800: 4 has? file 2 and + Constant /error
801: Variable error-stack 0 error-stack !
802: max-errors /error * cells allot
803: \ format of one cell:
804: \ source ( addr u )
805: \ >in
806: \ line-number
807: \ Loadfilename ( addr u )
808:
809: : error> ( -- addr u >in line# [addr u] )
810: -1 error-stack +!
811: error-stack dup @
812: /error * cells + cell+
813: /error cells bounds DO
814: I @
815: cell +LOOP ;
816: : >error ( addr u >in line# [addr u] -- )
817: error-stack dup @ dup 1+
818: max-errors 1- min error-stack !
819: /error * cells + cell+
820: /error 1- cells bounds swap DO
821: I !
822: -1 cells +LOOP ;
823:
824: : dec. ( n -- ) \ gforth
825: \G Display @i{n} as a signed decimal number, followed by a space.
826: \ !! not used...
827: base @ decimal swap . base ! ;
828:
829: : dec.r ( u n -- ) \ gforth
830: \G Display @i{u} as a unsigned decimal number in a field @i{n}
831: \G characters wide.
832: base @ >r decimal .r r> base ! ;
833:
834: : hex. ( u -- ) \ gforth
835: \G Display @i{u} as an unsigned hex number, prefixed with a "$" and
836: \G followed by a space.
837: \ !! not used...
838: [char] $ emit base @ swap hex u. base ! ;
839:
840: : -trailing ( c_addr u1 -- c_addr u2 ) \ string dash-trailing
841: \G Adjust the string specified by @i{c-addr, u1} to remove all
842: \G trailing spaces. @i{u2} is the length of the modified string.
843: BEGIN
844: dup
845: WHILE
846: 1- 2dup + c@ bl <>
847: UNTIL 1+ THEN ;
848:
849: DEFER DOERROR
850:
851: has? backtrace [IF]
852: Defer dobacktrace ( -- )
853: ' noop IS dobacktrace
854: [THEN]
855:
856: : .error-string ( throw-code -- )
857: dup -2 =
858: IF "error @ ?dup IF count type THEN drop
859: ELSE .error
860: THEN ;
861:
862: : umin ( u1 u2 -- u )
863: 2dup u>
864: if
865: swap
866: then
867: drop ;
868:
869: Defer mark-start
870: Defer mark-end
871:
872: :noname ." >>>" ; IS mark-start
873: :noname ." <<<" ; IS mark-end
874:
875: : .error-line ( addr1 u1 n1 -- )
876: \ print error ending at char n1 in line addr1 u1
877: \ should work with UTF-8 (whitespace check looks ok)
878: over umin \ protect against wrong n1
879: swap >r ( addr1 n1 R: u1 )
880: -trailing 1- \ last non-space
881: 0 >r BEGIN \ search for the first non-space
882: 2dup + c@ bl > WHILE
883: r> 1+ >r 1- dup 0< UNTIL THEN 1+
884: ( addr1 n2 r: u1 namelen )
885: 2dup type mark-start
886: r> -rot r> swap /string ( namelen addr2 u2 )
887: >r swap 2dup type mark-end ( addr2 namelen r: u2 )
888: r> swap /string type ;
889:
890: : .error-frame ( throwcode addr1 u1 n1 n2 [addr2 u2] -- throwcode )
891: \ addr2 u2: filename of included file - optional
892: \ n2: line number
893: \ n1: error position in input line
894: \ addr1 u1: input line
895: cr error-stack @
896: IF ( throwcode addr1 u1 n1 n2 [addr2 u2] )
897: [ has? file [IF] ] \ !! unbalanced stack effect
898: ." in file included from "
899: type ." :"
900: [ [THEN] ] ( throwcode addr1 u1 n1 n2 )
901: 0 dec.r drop 2drop
902: ELSE ( throwcode addr1 u1 n1 n2 [addr2 u2] )
903: [ has? file [IF] ]
904: type ." :"
905: [ [THEN] ] ( throwcode addr1 u1 n1 n2 )
906: dup 0 dec.r ." : " 4 pick .error-string
907: IF \ if line# non-zero, there is a line
908: cr .error-line
909: ELSE
910: 2drop drop
911: THEN
912: THEN ;
913:
914: : (DoError) ( throw-code -- )
915: [ has? os [IF] ]
916: >stderr
917: [ [THEN] ]
918: source >in @ sourceline# [ has? file [IF] ] \ !! unbalanced stack effect
919: sourcefilename
920: [ [THEN] ] .error-frame
921: error-stack @ 0 ?DO
922: error>
923: .error-frame
924: LOOP
925: drop
926: [ has? backtrace [IF] ]
927: dobacktrace
928: [ [THEN] ]
929: normal-dp dpp ! ;
930:
931: ' (DoError) IS DoError
932:
933: : quit ( ?? -- ?? ) \ core
934: \G Empty the return stack, make the user input device
935: \G the input source, enter interpret state and start
936: \G the text interpreter.
937: rp0 @ rp! handler off clear-tibstack
938: [ has? new-input 0= [IF] ] >tib @ >r [ [THEN] ]
939: BEGIN
940: [ has? compiler [IF] ]
941: [compile] [
942: [ [THEN] ]
943: \ stack depths may be arbitrary here
944: ['] 'quit CATCH dup
945: WHILE
946: <# \ reset hold area, or we may get another error
947: DoError
948: \ stack depths may be arbitrary still (or again), so clear them
949: clearstacks
950: [ has? new-input [IF] ] clear-tibstack
951: [ [ELSE] ] r@ >tib ! r@ tibstack !
952: [ [THEN] ]
953: REPEAT
954: drop [ has? new-input [IF] ] clear-tibstack
955: [ [ELSE] ] r> >tib !
956: [ [THEN] ] ;
957:
958: \ \ Cold Boot 13feb93py
959:
960: : (bootmessage)
961: ." Gforth " version-string type
962: ." , Copyright (C) 1995-2004 Free Software Foundation, Inc." cr
963: ." Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'"
964: [ has? os [IF] ]
965: cr ." Type `bye' to exit"
966: [ [THEN] ] ;
967:
968: defer bootmessage
969: defer process-args
970:
971: ' (bootmessage) IS bootmessage
972:
973: Defer 'cold ( -- ) \ gforth tick-cold
974: \ hook (deferred word) for things to do right before interpreting the
975: \ command-line arguments
976: ' noop IS 'cold
977:
978:
979: AVariable init8 NIL init8 !
980:
981: : cold ( -- ) \ gforth
982: [ has? backtrace [IF] ]
983: rp@ backtrace-rp0 !
984: [ [THEN] ]
985: [ has? file [IF] ]
986: os-cold
987: [ [THEN] ]
988: set-encoding-fixed-width
989: 'cold
990: init8 chainperform
991: [ has? file [IF] ]
992: s" *the terminal*" loadfilename 2!
993: process-args
994: loadline off
995: [ [THEN] ]
996: bootmessage
997: quit ;
998:
999: has? new-input 0= [IF]
1000: : clear-tibstack ( -- )
1001: [ has? glocals [IF] ]
1002: lp@ forthstart 7 cells + @ -
1003: [ [ELSE] ]
1004: [ has? os [IF] ]
1005: r0 @ forthstart 6 cells + @ -
1006: [ [ELSE] ]
1007: sp@ $10 cells +
1008: [ [THEN] ]
1009: [ [THEN] ]
1010: dup >tib ! tibstack ! #tib off >in off ;
1011: [THEN]
1012:
1013: : boot ( path n **argv argc -- )
1014: main-task up!
1015: [ has? os [IF] ]
1016: os-boot
1017: [ [THEN] ]
1018: sp@ sp0 !
1019: [ has? peephole [IF] ]
1020: \ only needed for greedy static superinstruction selection
1021: \ primtable prepare-peephole-table TO peeptable
1022: [ [THEN] ]
1023: [ has? new-input [IF] ]
1024: current-input off
1025: [ [THEN] ]
1026: clear-tibstack
1027: rp@ rp0 !
1028: [ has? floating [IF] ]
1029: fp@ fp0 !
1030: [ [THEN] ]
1031: handler off
1032: ['] cold catch dup -&2049 <> if \ broken pipe?
1033: DoError cr
1034: endif
1035: [ has? os [IF] ]
1036: 1 (bye) \ !! determin exit code from throw code?
1037: [ [THEN] ]
1038: ;
1039:
1040: has? os [IF]
1041: : bye ( -- ) \ tools-ext
1042: [ has? file [IF] ]
1043: script? 0= IF cr THEN
1044: [ [ELSE] ]
1045: cr
1046: [ [THEN] ]
1047: 0 (bye) ;
1048: [THEN]
1049:
1050: \ **argv may be scanned by the C starter to get some important
1051: \ information, as -display and -geometry for an X client FORTH
1052: \ or space and stackspace overrides
1053:
1054: \ 0 arg contains, however, the name of the program.
1055:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>