File:  [gforth] / gforth / kernel / int.fs
Revision 1.83: download - view: text, annotated - select for diffs
Wed Jan 8 09:53:07 2003 UTC (21 years, 2 months ago) by anton
Branches: MAIN
CVS tags: HEAD
moved >BODY from prim to kernel/int.fs

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>