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