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