Annotation of gforth/kernel/int.fs, revision 1.65
1.1 pazsan 1: \ definitions needed for interpreter only
2:
1.52 pazsan 3: \ Copyright (C) 1995-2000 Free Software Foundation, Inc.
1.11 anton 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
1.63 anton 19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.11 anton 20:
1.1 pazsan 21: \ \ Revision-Log
22:
23: \ put in seperate file 14sep97jaw
24:
25: \ \ input stream primitives 23feb93py
26:
1.33 jwilke 27: require ./basics.fs \ bounds decimal hex ...
28: require ./io.fs \ type ...
29: require ./nio.fs \ . <# ...
30: require ./errore.fs \ .error ...
1.50 anton 31: require kernel/version.fs \ version-string
1.33 jwilke 32: require ./../chains.fs
33:
1.64 pazsan 34: has? new-input 0= [IF]
1.43 crook 35: : tib ( -- c-addr ) \ core-ext t-i-b
1.40 crook 36: \G @i{c-addr} is the address of the Terminal Input Buffer.
1.29 crook 37: \G OBSOLESCENT: @code{source} superceeds the function of this word.
1.1 pazsan 38: >tib @ ;
39:
1.29 crook 40: Defer source ( -- c-addr u ) \ core
1.1 pazsan 41: \ used by dodefer:, must be defer
1.40 crook 42: \G @i{c-addr} is the address of the input buffer and @i{u} is the
1.29 crook 43: \G number of characters in it.
1.1 pazsan 44:
1.29 crook 45: : (source) ( -- c-addr u )
1.1 pazsan 46: tib #tib @ ;
47: ' (source) IS source
1.64 pazsan 48: [THEN]
1.1 pazsan 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:
1.29 crook 58: : sword ( char -- addr len ) \ gforth s-word
1.40 crook 59: \G Parses like @code{word}, but the output is like @code{parse} output.
1.47 anton 60: \G @xref{core-idef}.
1.3 anton 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).
1.1 pazsan 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:
1.29 crook 68: : word ( char "<chars>ccc<char>-- c-addr ) \ core
1.40 crook 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
1.29 crook 71: \G transient region containing the parsed string in
1.40 crook 72: \G counted-string format. If the parse area was empty or
1.29 crook 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
1.40 crook 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.
1.29 crook 83: >r source >in @ over min /string over swap r> scan >r
1.1 pazsan 84: over - dup r> IF 1+ THEN >in +! ;
85:
86: \ name 13feb93py
87:
88: [IFUNDEF] (name) \ name might be a primitive
89:
1.40 crook 90: : (name) ( -- c-addr count ) \ gforth
1.1 pazsan 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 $1F 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:
1.18 anton 110: \ !! protect BASE saving wrapper against exceptions
1.1 pazsan 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:
1.20 pazsan 119: : sign? ( addr u -- addr u flag )
1.33 jwilke 120: over c@ [char] - = dup >r
1.1 pazsan 121: IF
122: 1 /string
123: THEN
1.20 pazsan 124: r> ;
125:
126: : s>unumber? ( addr u -- ud flag )
1.21 pazsan 127: base @ >r dpl on getbase
1.20 pazsan 128: 0. 2swap
1.18 anton 129: BEGIN ( d addr len )
1.1 pazsan 130: dup >r >number dup
1.18 anton 131: WHILE \ there are characters left
1.1 pazsan 132: dup r> -
1.18 anton 133: WHILE \ the last >number parsed something
134: dup 1- dpl ! over c@ [char] . =
135: WHILE \ the current char is '.'
1.1 pazsan 136: 1 /string
1.18 anton 137: REPEAT THEN \ there are unparseable characters left
1.21 pazsan 138: 2drop false
1.20 pazsan 139: ELSE
140: rdrop 2drop true
1.21 pazsan 141: THEN
142: r> base ! ;
1.20 pazsan 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
1.21 pazsan 147: sign? >r
1.20 pazsan 148: s>unumber?
149: 0= IF
1.21 pazsan 150: rdrop false
1.18 anton 151: ELSE \ no characters left, all ok
1.20 pazsan 152: r>
1.1 pazsan 153: IF
154: dnegate
155: THEN
1.18 anton 156: true
1.21 pazsan 157: THEN ;
1.1 pazsan 158:
1.18 anton 159: : s>number ( addr len -- d )
160: \ don't use this, there is no way to tell success
161: s>number? drop ;
162:
1.1 pazsan 163: : snumber? ( c-addr u -- 0 / n -1 / d 0> )
1.18 anton 164: s>number? 0=
1.1 pazsan 165: IF
166: 2drop false EXIT
167: THEN
1.18 anton 168: dpl @ dup 0< IF
1.1 pazsan 169: nip
1.18 anton 170: ELSE
171: 1+
1.1 pazsan 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:
1.29 crook 189: : ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ thisone- core,file paren
1.17 crook 190: \G ** this will not get annotated. The alias in glocals.fs will instead **
1.29 crook 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
1.1 pazsan 193: [char] ) parse 2drop ; immediate
194:
1.51 anton 195: : \ ( compilation 'ccc<newline>' -- ; run-time -- ) \ thisone- core-ext,block-ext backslash
1.29 crook 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
1.12 pazsan 199: [ has? file [IF] ]
1.1 pazsan 200: blk @
201: IF
202: >in @ c/l / 1+ c/l * >in !
203: EXIT
204: THEN
1.12 pazsan 205: [ [THEN] ]
1.1 pazsan 206: source >in ! drop ; immediate
207:
1.51 anton 208: : \G ( compilation 'ccc<newline>' -- ; run-time -- ) \ gforth backslash-gee
1.19 crook 209: \G Equivalent to @code{\} but used as a tag to annotate definition
210: \G comments into documentation.
1.1 pazsan 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
1.6 pazsan 226: cell% field wordlist-map \ pointer to a wordlist-map-struct
1.13 anton 227: cell% field wordlist-id \ linked list of words (for WORDS etc.)
1.1 pazsan 228: cell% field wordlist-link \ link field to other wordlists
1.13 anton 229: cell% field wordlist-extend \ wordlist extensions (eg bucket offset)
1.1 pazsan 230: end-struct wordlist-struct
231:
232: : f83find ( addr len wordlist -- nt / false )
1.6 pazsan 233: wordlist-id @ (f83find) ;
1.1 pazsan 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:
1.6 pazsan 242: here G f83search T A, NIL A, NIL A, NIL A,
1.1 pazsan 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
1.43 crook 248: \G @code{Variable} -- holds the @i{wid} of the compilation word list.
1.1 pazsan 249: AVariable voclink forth-wordlist wordlist-link voclink !
1.38 anton 250: \ lookup AValue context ( -- addr ) \ gforth
251: Defer context ( -- addr ) \ gforth
1.43 crook 252: \G @code{context} @code{@@} is the @i{wid} of the word list at the
253: \G top of the search order.
1.1 pazsan 254:
1.38 anton 255: ' lookup is context
1.1 pazsan 256: forth-wordlist current !
257:
258: \ \ header, finding, ticks 17dec92py
259:
1.33 jwilke 260: hex
261: 80 constant alias-mask \ set when the word is not an alias!
262: 40 constant immediate-mask
263: 20 constant restrict-mask
1.1 pazsan 264:
265: \ higher level parts of find
266:
267: : flag-sign ( f -- 1|-1 )
268: \ true becomes 1, false -1
269: 0= 2* 1+ ;
270:
271: : compile-only-error ( ... -- )
272: -&14 throw ;
273:
274: : (cfa>int) ( cfa -- xt )
275: [ has? compiler [IF] ]
276: dup interpret/compile?
277: if
278: interpret/compile-int @
279: then
280: [ [THEN] ] ;
281:
282: : (x>int) ( cfa b -- xt )
283: \ get interpretation semantics of name
284: restrict-mask and
285: if
286: drop ['] compile-only-error
287: else
288: (cfa>int)
289: then ;
290:
291: : name>string ( nt -- addr count ) \ gforth head-to-string
1.40 crook 292: \g @i{addr count} is the name of the word represented by @i{nt}.
1.1 pazsan 293: cell+ count $1F and ;
294:
295: : ((name>)) ( nfa -- cfa )
296: name>string + cfaligned ;
297:
298: : (name>x) ( nfa -- cfa b )
299: \ cfa is an intermediate cfa and b is the flags byte of nfa
300: dup ((name>))
301: swap cell+ c@ dup alias-mask and 0=
302: IF
303: swap @ swap
304: THEN ;
305:
306: : name>int ( nt -- xt ) \ gforth
1.31 crook 307: \G @i{xt} represents the interpretation semantics of the word
308: \G @i{nt}. If @i{nt} has no interpretation semantics (i.e. is
309: \G @code{compile-only}), @i{xt} is the execution token for
310: \G @code{compile-only-error}, which performs @code{-14 throw}.
1.1 pazsan 311: (name>x) (x>int) ;
312:
313: : name?int ( nt -- xt ) \ gforth
1.31 crook 314: \G Like @code{name>int}, but perform @code{-14 throw} if @i{nt}
315: \G has no interpretation semantics.
1.1 pazsan 316: (name>x) restrict-mask and
317: if
318: compile-only-error \ does not return
319: then
320: (cfa>int) ;
321:
322: : (name>comp) ( nt -- w +-1 ) \ gforth
1.31 crook 323: \G @i{w xt} is the compilation token for the word @i{nt}.
1.1 pazsan 324: (name>x) >r
325: [ has? compiler [IF] ]
326: dup interpret/compile?
327: if
328: interpret/compile-comp @
329: then
330: [ [THEN] ]
331: r> immediate-mask and flag-sign
332: ;
333:
334: : (name>intn) ( nfa -- xt +-1 )
335: (name>x) tuck (x>int) ( b xt )
336: swap immediate-mask and flag-sign ;
337:
1.30 jwilke 338: const Create ??? 0 , 3 c, char ? c, char ? c, char ? c,
339: \ ??? is used by dovar:, must be created/:dovar
340:
341: [IFDEF] forthstart
342: \ if we have a forthstart we can define head? with it
343: \ otherwise leave out the head? check
344:
1.14 anton 345: : head? ( addr -- f )
346: \G heuristic check whether addr is a name token; may deliver false
347: \G positives; addr must be a valid address
348: \ we follow the link fields and check for plausibility; two
349: \ iterations should catch most false addresses: on the first
350: \ iteration, we may get an xt, on the second a code address (or
351: \ some code), which is typically not in the dictionary.
352: 2 0 do
1.41 anton 353: dup dup aligned <> if \ protect @ against unaligned accesses
354: drop false unloop exit
355: then
1.14 anton 356: dup @ dup
357: if ( addr addr1 )
358: dup rot forthstart within
359: if \ addr1 is outside forthstart..addr, not a head
360: drop false unloop exit
361: then ( addr1 )
362: else \ 0 in the link field, no further checks
363: 2drop true unloop exit
364: then
365: loop
366: \ in dubio pro:
367: drop true ;
368:
1.48 anton 369: : >head-noprim ( cfa -- nt ) \ gforth to-head-noprim
1.45 pazsan 370: $25 cell do ( cfa )
1.14 anton 371: dup i - count $9F and + cfaligned over alias-mask + =
372: if ( cfa )
373: dup i - cell - dup head?
374: if
375: nip unloop exit
376: then
377: drop
378: then
379: cell +loop
380: drop ??? ( wouldn't 0 be better? ) ;
1.1 pazsan 381:
1.30 jwilke 382: [ELSE]
383:
1.48 anton 384: : >head-noprim ( cfa -- nt ) \ gforth to-head-noprim
1.45 pazsan 385: $25 cell do ( cfa )
1.30 jwilke 386: dup i - count $9F and + cfaligned over alias-mask + =
387: if ( cfa ) i - cell - unloop exit
388: then
389: cell +loop
390: drop ??? ( wouldn't 0 be better? ) ;
391:
392: [THEN]
1.1 pazsan 393:
394: : body> 0 >body - ;
395:
1.31 crook 396: : (search-wordlist) ( addr count wid -- nt | false )
1.1 pazsan 397: dup wordlist-map @ find-method perform ;
398:
1.31 crook 399: : search-wordlist ( c-addr count wid -- 0 | xt +-1 ) \ search
1.53 anton 400: \G Search the word list identified by @i{wid} for the definition
401: \G named by the string at @i{c-addr count}. If the definition is
402: \G not found, return 0. If the definition is found return 1 (if
403: \G the definition is immediate) or -1 (if the definition is not
404: \G immediate) together with the @i{xt}. In Gforth, the @i{xt}
405: \G returned represents the interpretation semantics. ANS Forth
406: \G does not specify clearly what @i{xt} represents.
1.1 pazsan 407: (search-wordlist) dup if
408: (name>intn)
409: then ;
410:
1.31 crook 411: : find-name ( c-addr u -- nt | 0 ) \ gforth
412: \g Find the name @i{c-addr u} in the current search
413: \g order. Return its @i{nt}, if found, otherwise 0.
1.1 pazsan 414: lookup @ (search-wordlist) ;
415:
416: : sfind ( c-addr u -- 0 / xt +-1 ) \ gforth-obsolete
417: find-name dup
418: if ( nt )
419: state @
420: if
421: (name>comp)
422: else
423: (name>intn)
424: then
425: then ;
426:
1.31 crook 427: : find ( c-addr -- xt +-1 | c-addr 0 ) \ core,search
1.53 anton 428: \G Search all word lists in the current search order for the
429: \G definition named by the counted string at @i{c-addr}. If the
430: \G definition is not found, return 0. If the definition is found
431: \G return 1 (if the definition has non-default compilation
432: \G semantics) or -1 (if the definition has default compilation
433: \G semantics). The @i{xt} returned in interpret state represents
434: \G the interpretation semantics. The @i{xt} returned in compile
435: \G state represented either the compilation semantics (for
436: \G non-default compilation semantics) or the run-time semantics
437: \G that the compilation semantics would @code{compile,} (for
438: \G default compilation semantics). The ANS Forth standard does
439: \G not specify clearly what the returned @i{xt} represents (and
440: \G also talks about immediacy instead of non-default compilation
441: \G semantics), so this word is questionable in portable programs.
442: \G If non-portability is ok, @code{find-name} and friends are
443: \G better (@pxref{Name token}).
1.1 pazsan 444: dup count sfind dup
445: if
446: rot drop
447: then ;
448:
1.34 jwilke 449: \ ticks in interpreter
1.1 pazsan 450:
451: : (') ( "name" -- nt ) \ gforth
1.32 anton 452: name name-too-short?
1.28 anton 453: find-name dup 0=
1.1 pazsan 454: IF
1.42 anton 455: drop -&13 throw
1.1 pazsan 456: THEN ;
457:
458: : ' ( "name" -- xt ) \ core tick
1.31 crook 459: \g @i{xt} represents @i{name}'s interpretation
460: \g semantics. Perform @code{-14 throw} if the word has no
1.1 pazsan 461: \g interpretation semantics.
462: (') name?int ;
1.34 jwilke 463:
464: has? compiler 0= [IF] \ interpreter only version of IS and TO
465:
466: : IS ' >body ! ;
467: ' IS Alias TO
468:
469: [THEN]
1.1 pazsan 470:
471: \ \ the interpreter loop mar92py
472:
473: \ interpret 10mar92py
474:
1.37 anton 475: Defer parser ( c-addr u -- )
1.1 pazsan 476: Defer name ( -- c-addr count ) \ gforth
1.55 anton 477: \G Get the next word from the input buffer
1.1 pazsan 478: ' (name) IS name
479: Defer compiler-notfound ( c-addr count -- )
480: Defer interpreter-notfound ( c-addr count -- )
481:
482: : no.extensions ( addr u -- )
1.42 anton 483: 2drop -&13 throw ;
1.1 pazsan 484: ' no.extensions IS compiler-notfound
485: ' no.extensions IS interpreter-notfound
486:
487: : interpret ( ?? -- ?? ) \ gforth
488: \ interpret/compile the (rest of the) input buffer
1.33 jwilke 489: [ has? backtrace [IF] ]
1.65 ! anton 490: backtrace-rp0 @ >r
1.24 anton 491: rp@ backtrace-rp0 !
1.33 jwilke 492: [ [THEN] ]
1.1 pazsan 493: BEGIN
494: ?stack name dup
495: WHILE
496: parser
497: REPEAT
1.65 ! anton 498: 2drop
! 499: [ has? backtrace [IF] ]
! 500: r> backtrace-rp0 !
! 501: [ [THEN] ]
! 502: ;
1.1 pazsan 503:
504: \ interpreter 30apr92py
505:
506: \ not the most efficient implementations of interpreter and compiler
1.33 jwilke 507: : interpreter ( c-addr u -- )
1.1 pazsan 508: 2dup find-name dup
509: if
510: nip nip name>int execute
511: else
512: drop
513: 2dup 2>r snumber?
514: IF
515: 2rdrop
516: ELSE
517: 2r> interpreter-notfound
518: THEN
519: then ;
520:
521: ' interpreter IS parser
522:
523: \ \ Query Evaluate 07apr93py
524:
525: has? file 0= [IF]
1.12 pazsan 526: : sourceline# ( -- n ) 1 ;
1.61 pazsan 527: [ELSE]
1.64 pazsan 528: has? new-input 0= [IF]
1.58 pazsan 529: Variable #fill-bytes
530: \G number of bytes read via (read-line) by the last refill
1.61 pazsan 531: [THEN]
1.64 pazsan 532: [THEN]
1.58 pazsan 533:
1.64 pazsan 534: has? new-input 0= [IF]
1.1 pazsan 535: : refill ( -- flag ) \ core-ext,block-ext,file-ext
1.29 crook 536: \G Attempt to fill the input buffer from the input source. When
537: \G the input source is the user input device, attempt to receive
538: \G input into the terminal input device. If successful, make the
539: \G result the input buffer, set @code{>IN} to 0 and return true;
540: \G otherwise return false. When the input source is a block, add 1
541: \G to the value of @code{BLK} to make the next block the input
542: \G source and current input buffer, and set @code{>IN} to 0;
543: \G return true if the new value of @code{BLK} is a valid block
544: \G number, false otherwise. When the input source is a text file,
545: \G attempt to read the next line from the file. If successful,
546: \G make the result the current input buffer, set @code{>IN} to 0
547: \G and return true; otherwise, return false. A successful result
548: \G includes receipt of a line containing 0 characters.
1.12 pazsan 549: [ has? file [IF] ]
550: blk @ IF 1 blk +! true 0 >in ! EXIT THEN
551: [ [THEN] ]
552: tib /line
553: [ has? file [IF] ]
554: loadfile @ ?dup
1.59 pazsan 555: IF (read-line) throw #fill-bytes !
1.12 pazsan 556: ELSE
557: [ [THEN] ]
558: sourceline# 0< IF 2drop false EXIT THEN
559: accept true
560: [ has? file [IF] ]
561: THEN
562: 1 loadline +!
563: [ [THEN] ]
564: swap #tib ! 0 >in ! ;
1.1 pazsan 565:
566: : query ( -- ) \ core-ext
1.29 crook 567: \G Make the user input device the input source. Receive input into
568: \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
569: \G superceeded by @code{accept}.
1.12 pazsan 570: [ has? file [IF] ]
571: blk off loadfile off
572: [ [THEN] ]
1.64 pazsan 573: refill drop ;
574: [THEN]
1.1 pazsan 575:
576: \ save-mem extend-mem
577:
578: has? os [IF]
579: : save-mem ( addr1 u -- addr2 u ) \ gforth
580: \g copy a memory block into a newly allocated region in the heap
581: swap >r
582: dup allocate throw
583: swap 2dup r> -rot move ;
584:
585: : extend-mem ( addr1 u1 u -- addr addr2 u2 )
586: \ extend memory block allocated from the heap by u aus
587: \ the (possibly reallocated piece is addr2 u2, the extension is at addr
588: over >r + dup >r resize throw
589: r> over r> + -rot ;
590: [THEN]
591:
592: \ EVALUATE 17may93jaw
593:
1.64 pazsan 594: has? file 0= has? new-input 0= and [IF]
1.1 pazsan 595: : push-file ( -- ) r>
1.12 pazsan 596: tibstack @ >r >tib @ >r #tib @ >r
1.1 pazsan 597: >tib @ tibstack @ = IF r@ tibstack +! THEN
598: tibstack @ >tib ! >in @ >r >r ;
599:
600: : pop-file ( throw-code -- throw-code )
601: r>
1.12 pazsan 602: r> >in ! r> #tib ! r> >tib ! r> tibstack ! >r ;
1.1 pazsan 603: [THEN]
604:
1.64 pazsan 605: has? new-input 0= [IF]
1.29 crook 606: : evaluate ( c-addr u -- ) \ core,block
1.40 crook 607: \G Save the current input source specification. Store @code{-1} in
608: \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
609: \G @code{0} and make the string @i{c-addr u} the input source
610: \G and input buffer. Interpret. When the parse area is empty,
611: \G restore the input source specification.
1.64 pazsan 612: [ has? file [IF] ]
1.56 anton 613: loadfilename# @ >r
1.60 anton 614: 1 loadfilename# ! \ "*evaluated string*"
1.64 pazsan 615: [ [THEN] ]
1.40 crook 616: push-file #tib ! >tib !
1.29 crook 617: >in off
618: [ has? file [IF] ]
619: blk off loadfile off -1 loadline !
620: [ [THEN] ]
621: ['] interpret catch
1.56 anton 622: pop-file
1.64 pazsan 623: [ has? file [IF] ]
1.56 anton 624: r> loadfilename# !
1.64 pazsan 625: [ [THEN] ]
1.56 anton 626: throw ;
1.64 pazsan 627: [THEN]
1.1 pazsan 628:
629: \ \ Quit 13feb93py
630:
631: Defer 'quit
632:
633: Defer .status
634:
635: : prompt state @ IF ." compiled" EXIT THEN ." ok" ;
636:
1.39 anton 637: : (quit) ( -- )
638: \ exits only through THROW etc.
1.42 anton 639: \ sp0 @ cell - handler @ &12 + ! \ !! kludge: fix the stack pointer
1.39 anton 640: \ stored in the system's CATCH frame, so the stack depth will be 0
641: \ after the next THROW it catches (it may be off due to BOUNCEs or
642: \ because process-args left something on the stack)
643: BEGIN
1.64 pazsan 644: .status cr query interpret prompt
1.39 anton 645: AGAIN ;
1.1 pazsan 646:
647: ' (quit) IS 'quit
648:
649: \ \ DOERROR (DOERROR) 13jun93jaw
650:
651: 8 Constant max-errors
652: Variable error-stack 0 error-stack !
1.64 pazsan 653: max-errors has? file [IF] 6 [ELSE] 4 [THEN] * cells allot
1.1 pazsan 654: \ format of one cell:
655: \ source ( addr u )
656: \ >in
657: \ line-number
658: \ Loadfilename ( addr u )
659:
1.64 pazsan 660: : error> ( -- addr u >in line# [addr u] )
661: -1 error-stack +!
662: error-stack dup @
663: [ has? file [IF] 6 [ELSE] 4 [THEN] ] Literal * cells + cell+
664: [ has? file [IF] 6 [ELSE] 4 [THEN] ] Literal cells bounds DO
665: I @
666: cell +LOOP ;
667: : >error ( addr u >in line# [addr u] -- )
668: error-stack dup @ dup 1+
669: max-errors 1- min error-stack !
670: [ has? file [IF] 6 [ELSE] 4 [THEN] ] Literal * cells + cell+
671: [ has? file [IF] 6 [ELSE] 4 [THEN] 1- ] Literal cells bounds swap DO
672: I !
673: -1 cells +LOOP ;
674:
1.1 pazsan 675: : dec. ( n -- ) \ gforth
1.40 crook 676: \G Display @i{n} as a signed decimal number, followed by a space.
677: \ !! not used...
1.1 pazsan 678: base @ decimal swap . base ! ;
679:
1.23 jwilke 680: : dec.r ( u -- ) \ gforth
1.40 crook 681: \G Display @i{u} as a unsigned decimal number
1.23 jwilke 682: base @ decimal swap 0 .r base ! ;
683:
1.1 pazsan 684: : hex. ( u -- ) \ gforth
1.40 crook 685: \G Display @i{u} as an unsigned hex number, prefixed with a "$" and
1.17 crook 686: \G followed by a space.
1.40 crook 687: \ !! not used...
1.33 jwilke 688: [char] $ emit base @ swap hex u. base ! ;
1.1 pazsan 689:
690: : typewhite ( addr u -- ) \ gforth
1.40 crook 691: \G Like type, but white space is printed instead of the characters.
1.1 pazsan 692: bounds ?do
693: i c@ #tab = if \ check for tab
694: #tab
695: else
696: bl
697: then
698: emit
699: loop ;
700:
701: DEFER DOERROR
1.33 jwilke 702:
703: has? backtrace [IF]
1.15 anton 704: Defer dobacktrace ( -- )
705: ' noop IS dobacktrace
1.33 jwilke 706: [THEN]
1.1 pazsan 707:
1.23 jwilke 708: : .error-string ( throw-code -- )
709: dup -2 =
710: IF "error @ ?dup IF count type THEN drop
711: ELSE .error
712: THEN ;
713:
1.64 pazsan 714: : .error-frame ( throwcode addr1 u1 n1 n2 [addr2 u2] -- throwcode )
715: \ addr2 u2: filename of included file - optional
1.23 jwilke 716: \ n2: line number
717: \ n1: error position in input line
718: \ addr1 u1: input line
1.1 pazsan 719: cr error-stack @
720: IF
1.64 pazsan 721: [ has? file [IF] ]
722: ." in file included from "
723: type ." :"
724: [ [THEN] ]
725: dec.r drop 2drop
1.1 pazsan 726: ELSE
1.64 pazsan 727: [ has? file [IF] ]
728: type ." :"
729: [ [THEN] ]
730: dup >r dec.r ." : " 3 pick .error-string
1.57 anton 731: r> IF \ if line# non-zero, there is a line
732: cr dup 2over type cr drop
733: nip -trailing 1- ( line-start index2 )
734: 0 >r BEGIN
735: 2dup + c@ bl > WHILE
736: r> 1+ >r 1- dup 0< UNTIL THEN 1+
737: ( line-start index1 )
738: typewhite
739: r> 1 max 0 ?do \ we want at least one "^", even if the length is 0
740: [char] ^ emit
741: loop
742: ELSE
743: 2drop drop
744: THEN
1.23 jwilke 745: THEN ;
1.1 pazsan 746:
747: : (DoError) ( throw-code -- )
748: [ has? os [IF] ]
1.8 pazsan 749: >stderr
1.1 pazsan 750: [ [THEN] ]
1.64 pazsan 751: source >in @ sourceline# [ has? file [IF] ]
752: sourcefilename
753: [ [THEN] ] .error-frame
1.1 pazsan 754: error-stack @ 0 ?DO
1.64 pazsan 755: error>
1.1 pazsan 756: .error-frame
757: LOOP
1.33 jwilke 758: drop
759: [ has? backtrace [IF] ]
760: dobacktrace
761: [ [THEN] ]
1.8 pazsan 762: normal-dp dpp ! ;
1.1 pazsan 763:
764: ' (DoError) IS DoError
765:
766: : quit ( ?? -- ?? ) \ core
1.27 crook 767: \G Empty the return stack, make the user input device
768: \G the input source, enter interpret state and start
769: \G the text interpreter.
1.64 pazsan 770: rp0 @ rp! handler off clear-tibstack
771: [ has? new-input 0= [IF] ] >tib @ >r [ [THEN] ]
1.1 pazsan 772: BEGIN
773: [ has? compiler [IF] ]
774: postpone [
775: [ [THEN] ]
776: ['] 'quit CATCH dup
777: WHILE
1.22 anton 778: <# \ reset hold area, or we may get another error
1.64 pazsan 779: DoError
780: [ has? new-input [IF] ] clear-tibstack
781: [ [ELSE] ] r@ >tib ! r@ tibstack !
782: [ [THEN] ]
1.1 pazsan 783: REPEAT
1.64 pazsan 784: drop [ has? new-input [IF] ] clear-tibstack
785: [ [ELSE] ] r> >tib !
786: [ [THEN] ] ;
1.1 pazsan 787:
788: \ \ Cold Boot 13feb93py
789:
790: : (bootmessage)
791: ." GForth " version-string type
1.52 pazsan 792: ." , Copyright (C) 1995-2000 Free Software Foundation, Inc." cr
1.1 pazsan 793: ." GForth comes with ABSOLUTELY NO WARRANTY; for details type `license'"
794: [ has? os [IF] ]
795: cr ." Type `bye' to exit"
796: [ [THEN] ] ;
797:
798: defer bootmessage
799: defer process-args
800:
801: ' (bootmessage) IS bootmessage
802:
1.10 anton 803: Defer 'cold ( -- ) \ gforth tick-cold
1.1 pazsan 804: \ hook (deferred word) for things to do right before interpreting the
805: \ command-line arguments
806: ' noop IS 'cold
807:
808:
809: Variable init8
810:
811: : cold ( -- ) \ gforth
1.44 anton 812: [ has? backtrace [IF] ]
813: rp@ backtrace-rp0 !
814: [ [THEN] ]
1.1 pazsan 815: [ has? file [IF] ]
816: pathstring 2@ fpath only-path
817: init-included-files
818: [ [THEN] ]
819: 'cold
820: init8 chainperform
821: [ has? file [IF] ]
1.62 anton 822: loadfilename# off
1.8 pazsan 823: process-args
1.12 pazsan 824: loadline off
1.1 pazsan 825: [ [THEN] ]
826: bootmessage
1.12 pazsan 827: quit ;
1.1 pazsan 828:
1.64 pazsan 829: has? new-input 0= [IF]
1.5 anton 830: : clear-tibstack ( -- )
831: [ has? glocals [IF] ]
832: lp@ forthstart 7 cells + @ -
833: [ [ELSE] ]
834: [ has? os [IF] ]
1.8 pazsan 835: r0 @ forthstart 6 cells + @ -
1.5 anton 836: [ [ELSE] ]
1.16 pazsan 837: sp@ $10 cells +
1.5 anton 838: [ [THEN] ]
839: [ [THEN] ]
840: dup >tib ! tibstack ! #tib off >in off ;
1.64 pazsan 841: [THEN]
1.5 anton 842:
1.64 pazsan 843: : boot ( path n **argv argc -- )
1.1 pazsan 844: main-task up!
845: [ has? os [IF] ]
846: stdout TO outfile-id
1.7 pazsan 847: stdin TO infile-id
1.1 pazsan 848: \ !! [ [THEN] ]
849: \ !! [ has? file [IF] ]
850: argc ! argv ! pathstring 2!
851: [ [THEN] ]
852: sp@ sp0 !
1.64 pazsan 853: [ has? new-input [IF] ]
854: current-input off
855: [ [THEN] ]
1.5 anton 856: clear-tibstack
1.1 pazsan 857: rp@ rp0 !
858: [ has? floating [IF] ]
859: fp@ fp0 !
860: [ [THEN] ]
1.46 anton 861: handler off
1.8 pazsan 862: ['] cold catch DoError cr
1.1 pazsan 863: [ has? os [IF] ]
1.35 anton 864: 1 (bye) \ !! determin exit code from throw code?
1.1 pazsan 865: [ [THEN] ]
866: ;
867:
868: has? os [IF]
869: : bye ( -- ) \ tools-ext
870: [ has? file [IF] ]
871: script? 0= IF cr THEN
872: [ [ELSE] ]
873: cr
874: [ [THEN] ]
875: 0 (bye) ;
876: [THEN]
877:
878: \ **argv may be scanned by the C starter to get some important
879: \ information, as -display and -geometry for an X client FORTH
880: \ or space and stackspace overrides
881:
882: \ 0 arg contains, however, the name of the program.
883:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>