Annotation of gforth/kernel/int.fs, revision 1.69
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 )
1.67 anton 100: dup lcount-mask u> -&19 and throw ;
1.1 pazsan 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.67 anton 233: wordlist-id @ (listlfind) ;
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.69 ! pazsan 260: \ The constants are defined as 32 bits, but then erased
! 261: \ and overwritten by the right ones
1.67 anton 262:
263: $80000000 constant alias-mask
1.69 ! pazsan 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]
1.67 anton 267: $40000000 constant immediate-mask
1.69 ! pazsan 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]
1.67 anton 271: $20000000 constant restrict-mask
1.69 ! pazsan 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]
1.67 anton 275: $1fffffff constant lcount-mask
1.69 ! pazsan 276: 1 bits/char 3 - lshift 1 -
! 277: -1 cells allot bigendian [IF] c, $FF 1 cells 1- times
! 278: [ELSE] $FF 1 cells 1- times c, [THEN]
1.1 pazsan 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: : compile-only-error ( ... -- )
287: -&14 throw ;
288:
289: : (cfa>int) ( cfa -- xt )
290: [ has? compiler [IF] ]
291: dup interpret/compile?
292: if
293: interpret/compile-int @
294: then
295: [ [THEN] ] ;
296:
1.67 anton 297: : (x>int) ( cfa w -- xt )
1.1 pazsan 298: \ get interpretation semantics of name
299: restrict-mask and
300: if
301: drop ['] compile-only-error
302: else
303: (cfa>int)
304: then ;
305:
306: : name>string ( nt -- addr count ) \ gforth head-to-string
1.40 crook 307: \g @i{addr count} is the name of the word represented by @i{nt}.
1.67 anton 308: cell+ dup cell+ swap @ lcount-mask and ;
1.1 pazsan 309:
310: : ((name>)) ( nfa -- cfa )
311: name>string + cfaligned ;
312:
1.67 anton 313: : (name>x) ( nfa -- cfa w )
314: \ cfa is an intermediate cfa and w is the flags cell of nfa
1.1 pazsan 315: dup ((name>))
1.67 anton 316: swap cell+ @ dup alias-mask and 0=
1.1 pazsan 317: IF
318: swap @ swap
319: THEN ;
320:
321: : name>int ( nt -- xt ) \ gforth
1.31 crook 322: \G @i{xt} represents the interpretation semantics of the word
323: \G @i{nt}. If @i{nt} has no interpretation semantics (i.e. is
324: \G @code{compile-only}), @i{xt} is the execution token for
325: \G @code{compile-only-error}, which performs @code{-14 throw}.
1.1 pazsan 326: (name>x) (x>int) ;
327:
328: : name?int ( nt -- xt ) \ gforth
1.31 crook 329: \G Like @code{name>int}, but perform @code{-14 throw} if @i{nt}
330: \G has no interpretation semantics.
1.1 pazsan 331: (name>x) restrict-mask and
332: if
333: compile-only-error \ does not return
334: then
335: (cfa>int) ;
336:
337: : (name>comp) ( nt -- w +-1 ) \ gforth
1.31 crook 338: \G @i{w xt} is the compilation token for the word @i{nt}.
1.1 pazsan 339: (name>x) >r
340: [ has? compiler [IF] ]
341: dup interpret/compile?
342: if
343: interpret/compile-comp @
344: then
345: [ [THEN] ]
346: r> immediate-mask and flag-sign
347: ;
348:
349: : (name>intn) ( nfa -- xt +-1 )
1.67 anton 350: (name>x) tuck (x>int) ( w xt )
1.1 pazsan 351: swap immediate-mask and flag-sign ;
352:
1.30 jwilke 353: const Create ??? 0 , 3 c, char ? c, char ? c, char ? c,
354: \ ??? is used by dovar:, must be created/:dovar
355:
356: [IFDEF] forthstart
357: \ if we have a forthstart we can define head? with it
358: \ otherwise leave out the head? check
359:
1.14 anton 360: : head? ( addr -- f )
361: \G heuristic check whether addr is a name token; may deliver false
362: \G positives; addr must be a valid address
363: \ we follow the link fields and check for plausibility; two
364: \ iterations should catch most false addresses: on the first
365: \ iteration, we may get an xt, on the second a code address (or
366: \ some code), which is typically not in the dictionary.
367: 2 0 do
1.41 anton 368: dup dup aligned <> if \ protect @ against unaligned accesses
369: drop false unloop exit
370: then
1.14 anton 371: dup @ dup
372: if ( addr addr1 )
373: dup rot forthstart within
374: if \ addr1 is outside forthstart..addr, not a head
375: drop false unloop exit
376: then ( addr1 )
377: else \ 0 in the link field, no further checks
378: 2drop true unloop exit
379: then
380: loop
381: \ in dubio pro:
382: drop true ;
383:
1.48 anton 384: : >head-noprim ( cfa -- nt ) \ gforth to-head-noprim
1.67 anton 385: \ also heuristic; finds only names with up to 32 chars
1.45 pazsan 386: $25 cell do ( cfa )
1.67 anton 387: dup i - dup @ [ alias-mask lcount-mask or ] literal and ( cfa len|alias )
388: swap + cell + cfaligned over alias-mask + =
1.14 anton 389: if ( cfa )
390: dup i - cell - dup head?
391: if
392: nip unloop exit
393: then
394: drop
395: then
396: cell +loop
397: drop ??? ( wouldn't 0 be better? ) ;
1.1 pazsan 398:
1.30 jwilke 399: [ELSE]
400:
1.48 anton 401: : >head-noprim ( cfa -- nt ) \ gforth to-head-noprim
1.45 pazsan 402: $25 cell do ( cfa )
1.67 anton 403: dup i - dup @ [ alias-mask lcount-mask or ] literal and ( cfa len|alias )
404: swap + cell + cfaligned over alias-mask + =
1.30 jwilke 405: if ( cfa ) i - cell - unloop exit
406: then
407: cell +loop
408: drop ??? ( wouldn't 0 be better? ) ;
409:
410: [THEN]
1.1 pazsan 411:
412: : body> 0 >body - ;
413:
1.31 crook 414: : (search-wordlist) ( addr count wid -- nt | false )
1.1 pazsan 415: dup wordlist-map @ find-method perform ;
416:
1.31 crook 417: : search-wordlist ( c-addr count wid -- 0 | xt +-1 ) \ search
1.53 anton 418: \G Search the word list identified by @i{wid} for the definition
419: \G named by the string at @i{c-addr count}. If the definition is
420: \G not found, return 0. If the definition is found return 1 (if
421: \G the definition is immediate) or -1 (if the definition is not
422: \G immediate) together with the @i{xt}. In Gforth, the @i{xt}
423: \G returned represents the interpretation semantics. ANS Forth
424: \G does not specify clearly what @i{xt} represents.
1.1 pazsan 425: (search-wordlist) dup if
426: (name>intn)
427: then ;
428:
1.31 crook 429: : find-name ( c-addr u -- nt | 0 ) \ gforth
430: \g Find the name @i{c-addr u} in the current search
431: \g order. Return its @i{nt}, if found, otherwise 0.
1.1 pazsan 432: lookup @ (search-wordlist) ;
433:
434: : sfind ( c-addr u -- 0 / xt +-1 ) \ gforth-obsolete
435: find-name dup
436: if ( nt )
437: state @
438: if
439: (name>comp)
440: else
441: (name>intn)
442: then
443: then ;
444:
1.31 crook 445: : find ( c-addr -- xt +-1 | c-addr 0 ) \ core,search
1.53 anton 446: \G Search all word lists in the current search order for the
447: \G definition named by the counted string at @i{c-addr}. If the
448: \G definition is not found, return 0. If the definition is found
449: \G return 1 (if the definition has non-default compilation
450: \G semantics) or -1 (if the definition has default compilation
451: \G semantics). The @i{xt} returned in interpret state represents
452: \G the interpretation semantics. The @i{xt} returned in compile
453: \G state represented either the compilation semantics (for
454: \G non-default compilation semantics) or the run-time semantics
455: \G that the compilation semantics would @code{compile,} (for
456: \G default compilation semantics). The ANS Forth standard does
457: \G not specify clearly what the returned @i{xt} represents (and
458: \G also talks about immediacy instead of non-default compilation
459: \G semantics), so this word is questionable in portable programs.
460: \G If non-portability is ok, @code{find-name} and friends are
461: \G better (@pxref{Name token}).
1.1 pazsan 462: dup count sfind dup
463: if
464: rot drop
465: then ;
466:
1.34 jwilke 467: \ ticks in interpreter
1.1 pazsan 468:
469: : (') ( "name" -- nt ) \ gforth
1.32 anton 470: name name-too-short?
1.28 anton 471: find-name dup 0=
1.1 pazsan 472: IF
1.42 anton 473: drop -&13 throw
1.1 pazsan 474: THEN ;
475:
476: : ' ( "name" -- xt ) \ core tick
1.31 crook 477: \g @i{xt} represents @i{name}'s interpretation
478: \g semantics. Perform @code{-14 throw} if the word has no
1.1 pazsan 479: \g interpretation semantics.
480: (') name?int ;
1.34 jwilke 481:
482: has? compiler 0= [IF] \ interpreter only version of IS and TO
483:
484: : IS ' >body ! ;
485: ' IS Alias TO
486:
487: [THEN]
1.1 pazsan 488:
489: \ \ the interpreter loop mar92py
490:
491: \ interpret 10mar92py
492:
1.37 anton 493: Defer parser ( c-addr u -- )
1.1 pazsan 494: Defer name ( -- c-addr count ) \ gforth
1.55 anton 495: \G Get the next word from the input buffer
1.1 pazsan 496: ' (name) IS name
497: Defer compiler-notfound ( c-addr count -- )
498: Defer interpreter-notfound ( c-addr count -- )
499:
500: : no.extensions ( addr u -- )
1.42 anton 501: 2drop -&13 throw ;
1.1 pazsan 502: ' no.extensions IS compiler-notfound
503: ' no.extensions IS interpreter-notfound
504:
1.66 anton 505: : interpret1 ( ... -- ... )
1.33 jwilke 506: [ has? backtrace [IF] ]
1.24 anton 507: rp@ backtrace-rp0 !
1.33 jwilke 508: [ [THEN] ]
1.1 pazsan 509: BEGIN
510: ?stack name dup
511: WHILE
512: parser
513: REPEAT
1.66 anton 514: 2drop ;
515:
516: : interpret ( ?? -- ?? ) \ gforth
517: \ interpret/compile the (rest of the) input buffer
518: [ has? backtrace [IF] ]
519: backtrace-rp0 @ >r
520: [ [THEN] ]
521: ['] interpret1 catch
1.65 anton 522: [ has? backtrace [IF] ]
523: r> backtrace-rp0 !
1.66 anton 524: [ [THEN] ]
525: throw ;
1.1 pazsan 526:
527: \ interpreter 30apr92py
528:
529: \ not the most efficient implementations of interpreter and compiler
1.33 jwilke 530: : interpreter ( c-addr u -- )
1.1 pazsan 531: 2dup find-name dup
532: if
533: nip nip name>int execute
534: else
535: drop
536: 2dup 2>r snumber?
537: IF
538: 2rdrop
539: ELSE
540: 2r> interpreter-notfound
541: THEN
542: then ;
543:
544: ' interpreter IS parser
545:
546: \ \ Query Evaluate 07apr93py
547:
548: has? file 0= [IF]
1.12 pazsan 549: : sourceline# ( -- n ) 1 ;
1.61 pazsan 550: [ELSE]
1.64 pazsan 551: has? new-input 0= [IF]
1.58 pazsan 552: Variable #fill-bytes
553: \G number of bytes read via (read-line) by the last refill
1.61 pazsan 554: [THEN]
1.64 pazsan 555: [THEN]
1.58 pazsan 556:
1.64 pazsan 557: has? new-input 0= [IF]
1.1 pazsan 558: : refill ( -- flag ) \ core-ext,block-ext,file-ext
1.29 crook 559: \G Attempt to fill the input buffer from the input source. When
560: \G the input source is the user input device, attempt to receive
561: \G input into the terminal input device. If successful, make the
562: \G result the input buffer, set @code{>IN} to 0 and return true;
563: \G otherwise return false. When the input source is a block, add 1
564: \G to the value of @code{BLK} to make the next block the input
565: \G source and current input buffer, and set @code{>IN} to 0;
566: \G return true if the new value of @code{BLK} is a valid block
567: \G number, false otherwise. When the input source is a text file,
568: \G attempt to read the next line from the file. If successful,
569: \G make the result the current input buffer, set @code{>IN} to 0
570: \G and return true; otherwise, return false. A successful result
571: \G includes receipt of a line containing 0 characters.
1.12 pazsan 572: [ has? file [IF] ]
573: blk @ IF 1 blk +! true 0 >in ! EXIT THEN
574: [ [THEN] ]
575: tib /line
576: [ has? file [IF] ]
577: loadfile @ ?dup
1.59 pazsan 578: IF (read-line) throw #fill-bytes !
1.12 pazsan 579: ELSE
580: [ [THEN] ]
581: sourceline# 0< IF 2drop false EXIT THEN
582: accept true
583: [ has? file [IF] ]
584: THEN
585: 1 loadline +!
586: [ [THEN] ]
587: swap #tib ! 0 >in ! ;
1.1 pazsan 588:
589: : query ( -- ) \ core-ext
1.29 crook 590: \G Make the user input device the input source. Receive input into
591: \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
592: \G superceeded by @code{accept}.
1.12 pazsan 593: [ has? file [IF] ]
594: blk off loadfile off
595: [ [THEN] ]
1.64 pazsan 596: refill drop ;
597: [THEN]
1.1 pazsan 598:
599: \ save-mem extend-mem
600:
601: has? os [IF]
602: : save-mem ( addr1 u -- addr2 u ) \ gforth
603: \g copy a memory block into a newly allocated region in the heap
604: swap >r
605: dup allocate throw
606: swap 2dup r> -rot move ;
607:
1.68 anton 608: : free-mem-var ( addr -- )
609: \ addr is the address of a 2variable containing address and size
610: \ of a memory range; frees memory and clears the 2variable.
611: dup 2@ drop dup
612: if ( addr mem-start )
613: free throw
614: 0 0 rot 2!
615: else
616: 2drop
617: then ;
618:
1.1 pazsan 619: : extend-mem ( addr1 u1 u -- addr addr2 u2 )
620: \ extend memory block allocated from the heap by u aus
621: \ the (possibly reallocated piece is addr2 u2, the extension is at addr
622: over >r + dup >r resize throw
623: r> over r> + -rot ;
624: [THEN]
625:
626: \ EVALUATE 17may93jaw
627:
1.64 pazsan 628: has? file 0= has? new-input 0= and [IF]
1.1 pazsan 629: : push-file ( -- ) r>
1.12 pazsan 630: tibstack @ >r >tib @ >r #tib @ >r
1.1 pazsan 631: >tib @ tibstack @ = IF r@ tibstack +! THEN
632: tibstack @ >tib ! >in @ >r >r ;
633:
634: : pop-file ( throw-code -- throw-code )
635: r>
1.12 pazsan 636: r> >in ! r> #tib ! r> >tib ! r> tibstack ! >r ;
1.1 pazsan 637: [THEN]
638:
1.64 pazsan 639: has? new-input 0= [IF]
1.29 crook 640: : evaluate ( c-addr u -- ) \ core,block
1.40 crook 641: \G Save the current input source specification. Store @code{-1} in
642: \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
643: \G @code{0} and make the string @i{c-addr u} the input source
644: \G and input buffer. Interpret. When the parse area is empty,
645: \G restore the input source specification.
1.64 pazsan 646: [ has? file [IF] ]
1.56 anton 647: loadfilename# @ >r
1.60 anton 648: 1 loadfilename# ! \ "*evaluated string*"
1.64 pazsan 649: [ [THEN] ]
1.40 crook 650: push-file #tib ! >tib !
1.29 crook 651: >in off
652: [ has? file [IF] ]
653: blk off loadfile off -1 loadline !
654: [ [THEN] ]
655: ['] interpret catch
1.56 anton 656: pop-file
1.64 pazsan 657: [ has? file [IF] ]
1.56 anton 658: r> loadfilename# !
1.64 pazsan 659: [ [THEN] ]
1.56 anton 660: throw ;
1.64 pazsan 661: [THEN]
1.1 pazsan 662:
663: \ \ Quit 13feb93py
664:
665: Defer 'quit
666:
667: Defer .status
668:
669: : prompt state @ IF ." compiled" EXIT THEN ." ok" ;
670:
1.39 anton 671: : (quit) ( -- )
672: \ exits only through THROW etc.
1.42 anton 673: \ sp0 @ cell - handler @ &12 + ! \ !! kludge: fix the stack pointer
1.39 anton 674: \ stored in the system's CATCH frame, so the stack depth will be 0
675: \ after the next THROW it catches (it may be off due to BOUNCEs or
676: \ because process-args left something on the stack)
677: BEGIN
1.64 pazsan 678: .status cr query interpret prompt
1.39 anton 679: AGAIN ;
1.1 pazsan 680:
681: ' (quit) IS 'quit
682:
683: \ \ DOERROR (DOERROR) 13jun93jaw
684:
685: 8 Constant max-errors
686: Variable error-stack 0 error-stack !
1.64 pazsan 687: max-errors has? file [IF] 6 [ELSE] 4 [THEN] * cells allot
1.1 pazsan 688: \ format of one cell:
689: \ source ( addr u )
690: \ >in
691: \ line-number
692: \ Loadfilename ( addr u )
693:
1.64 pazsan 694: : error> ( -- addr u >in line# [addr u] )
695: -1 error-stack +!
696: error-stack dup @
697: [ has? file [IF] 6 [ELSE] 4 [THEN] ] Literal * cells + cell+
698: [ has? file [IF] 6 [ELSE] 4 [THEN] ] Literal cells bounds DO
699: I @
700: cell +LOOP ;
701: : >error ( addr u >in line# [addr u] -- )
702: error-stack dup @ dup 1+
703: max-errors 1- min error-stack !
704: [ has? file [IF] 6 [ELSE] 4 [THEN] ] Literal * cells + cell+
705: [ has? file [IF] 6 [ELSE] 4 [THEN] 1- ] Literal cells bounds swap DO
706: I !
707: -1 cells +LOOP ;
708:
1.1 pazsan 709: : dec. ( n -- ) \ gforth
1.40 crook 710: \G Display @i{n} as a signed decimal number, followed by a space.
711: \ !! not used...
1.1 pazsan 712: base @ decimal swap . base ! ;
713:
1.23 jwilke 714: : dec.r ( u -- ) \ gforth
1.40 crook 715: \G Display @i{u} as a unsigned decimal number
1.23 jwilke 716: base @ decimal swap 0 .r base ! ;
717:
1.1 pazsan 718: : hex. ( u -- ) \ gforth
1.40 crook 719: \G Display @i{u} as an unsigned hex number, prefixed with a "$" and
1.17 crook 720: \G followed by a space.
1.40 crook 721: \ !! not used...
1.33 jwilke 722: [char] $ emit base @ swap hex u. base ! ;
1.1 pazsan 723:
724: : typewhite ( addr u -- ) \ gforth
1.40 crook 725: \G Like type, but white space is printed instead of the characters.
1.1 pazsan 726: bounds ?do
727: i c@ #tab = if \ check for tab
728: #tab
729: else
730: bl
731: then
732: emit
733: loop ;
734:
735: DEFER DOERROR
1.33 jwilke 736:
737: has? backtrace [IF]
1.15 anton 738: Defer dobacktrace ( -- )
739: ' noop IS dobacktrace
1.33 jwilke 740: [THEN]
1.1 pazsan 741:
1.23 jwilke 742: : .error-string ( throw-code -- )
743: dup -2 =
744: IF "error @ ?dup IF count type THEN drop
745: ELSE .error
746: THEN ;
747:
1.64 pazsan 748: : .error-frame ( throwcode addr1 u1 n1 n2 [addr2 u2] -- throwcode )
749: \ addr2 u2: filename of included file - optional
1.23 jwilke 750: \ n2: line number
751: \ n1: error position in input line
752: \ addr1 u1: input line
1.1 pazsan 753: cr error-stack @
754: IF
1.64 pazsan 755: [ has? file [IF] ]
756: ." in file included from "
757: type ." :"
758: [ [THEN] ]
759: dec.r drop 2drop
1.1 pazsan 760: ELSE
1.64 pazsan 761: [ has? file [IF] ]
762: type ." :"
763: [ [THEN] ]
764: dup >r dec.r ." : " 3 pick .error-string
1.57 anton 765: r> IF \ if line# non-zero, there is a line
766: cr dup 2over type cr drop
767: nip -trailing 1- ( line-start index2 )
768: 0 >r BEGIN
769: 2dup + c@ bl > WHILE
770: r> 1+ >r 1- dup 0< UNTIL THEN 1+
771: ( line-start index1 )
772: typewhite
773: r> 1 max 0 ?do \ we want at least one "^", even if the length is 0
774: [char] ^ emit
775: loop
776: ELSE
777: 2drop drop
778: THEN
1.23 jwilke 779: THEN ;
1.1 pazsan 780:
781: : (DoError) ( throw-code -- )
782: [ has? os [IF] ]
1.8 pazsan 783: >stderr
1.1 pazsan 784: [ [THEN] ]
1.64 pazsan 785: source >in @ sourceline# [ has? file [IF] ]
786: sourcefilename
787: [ [THEN] ] .error-frame
1.1 pazsan 788: error-stack @ 0 ?DO
1.64 pazsan 789: error>
1.1 pazsan 790: .error-frame
791: LOOP
1.33 jwilke 792: drop
793: [ has? backtrace [IF] ]
794: dobacktrace
795: [ [THEN] ]
1.8 pazsan 796: normal-dp dpp ! ;
1.1 pazsan 797:
798: ' (DoError) IS DoError
799:
800: : quit ( ?? -- ?? ) \ core
1.27 crook 801: \G Empty the return stack, make the user input device
802: \G the input source, enter interpret state and start
803: \G the text interpreter.
1.64 pazsan 804: rp0 @ rp! handler off clear-tibstack
805: [ has? new-input 0= [IF] ] >tib @ >r [ [THEN] ]
1.1 pazsan 806: BEGIN
807: [ has? compiler [IF] ]
808: postpone [
809: [ [THEN] ]
810: ['] 'quit CATCH dup
811: WHILE
1.22 anton 812: <# \ reset hold area, or we may get another error
1.64 pazsan 813: DoError
814: [ has? new-input [IF] ] clear-tibstack
815: [ [ELSE] ] r@ >tib ! r@ tibstack !
816: [ [THEN] ]
1.1 pazsan 817: REPEAT
1.64 pazsan 818: drop [ has? new-input [IF] ] clear-tibstack
819: [ [ELSE] ] r> >tib !
820: [ [THEN] ] ;
1.1 pazsan 821:
822: \ \ Cold Boot 13feb93py
823:
824: : (bootmessage)
825: ." GForth " version-string type
1.52 pazsan 826: ." , Copyright (C) 1995-2000 Free Software Foundation, Inc." cr
1.1 pazsan 827: ." GForth comes with ABSOLUTELY NO WARRANTY; for details type `license'"
828: [ has? os [IF] ]
829: cr ." Type `bye' to exit"
830: [ [THEN] ] ;
831:
832: defer bootmessage
833: defer process-args
834:
835: ' (bootmessage) IS bootmessage
836:
1.10 anton 837: Defer 'cold ( -- ) \ gforth tick-cold
1.1 pazsan 838: \ hook (deferred word) for things to do right before interpreting the
839: \ command-line arguments
840: ' noop IS 'cold
841:
842:
843: Variable init8
844:
845: : cold ( -- ) \ gforth
1.44 anton 846: [ has? backtrace [IF] ]
847: rp@ backtrace-rp0 !
848: [ [THEN] ]
1.1 pazsan 849: [ has? file [IF] ]
850: pathstring 2@ fpath only-path
851: init-included-files
852: [ [THEN] ]
853: 'cold
854: init8 chainperform
855: [ has? file [IF] ]
1.62 anton 856: loadfilename# off
1.8 pazsan 857: process-args
1.12 pazsan 858: loadline off
1.1 pazsan 859: [ [THEN] ]
860: bootmessage
1.12 pazsan 861: quit ;
1.1 pazsan 862:
1.64 pazsan 863: has? new-input 0= [IF]
1.5 anton 864: : clear-tibstack ( -- )
865: [ has? glocals [IF] ]
866: lp@ forthstart 7 cells + @ -
867: [ [ELSE] ]
868: [ has? os [IF] ]
1.8 pazsan 869: r0 @ forthstart 6 cells + @ -
1.5 anton 870: [ [ELSE] ]
1.16 pazsan 871: sp@ $10 cells +
1.5 anton 872: [ [THEN] ]
873: [ [THEN] ]
874: dup >tib ! tibstack ! #tib off >in off ;
1.64 pazsan 875: [THEN]
1.5 anton 876:
1.64 pazsan 877: : boot ( path n **argv argc -- )
1.1 pazsan 878: main-task up!
879: [ has? os [IF] ]
880: stdout TO outfile-id
1.7 pazsan 881: stdin TO infile-id
1.1 pazsan 882: \ !! [ [THEN] ]
883: \ !! [ has? file [IF] ]
884: argc ! argv ! pathstring 2!
885: [ [THEN] ]
886: sp@ sp0 !
1.64 pazsan 887: [ has? new-input [IF] ]
888: current-input off
889: [ [THEN] ]
1.5 anton 890: clear-tibstack
1.1 pazsan 891: rp@ rp0 !
892: [ has? floating [IF] ]
893: fp@ fp0 !
894: [ [THEN] ]
1.46 anton 895: handler off
1.8 pazsan 896: ['] cold catch DoError cr
1.1 pazsan 897: [ has? os [IF] ]
1.35 anton 898: 1 (bye) \ !! determin exit code from throw code?
1.1 pazsan 899: [ [THEN] ]
900: ;
901:
902: has? os [IF]
903: : bye ( -- ) \ tools-ext
904: [ has? file [IF] ]
905: script? 0= IF cr THEN
906: [ [ELSE] ]
907: cr
908: [ [THEN] ]
909: 0 (bye) ;
910: [THEN]
911:
912: \ **argv may be scanned by the C starter to get some important
913: \ information, as -display and -geometry for an X client FORTH
914: \ or space and stackspace overrides
915:
916: \ 0 arg contains, however, the name of the program.
917:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>