File:  [gforth] / gforth / kernel / int.fs
Revision 1.106: download - view: text, annotated - select for diffs
Mon Aug 23 14:03:53 2004 UTC (19 years, 8 months ago) by anton
Branches: MAIN
CVS tags: HEAD
added BEFORE-WORD and OTHER-CONTROL-FLOW hooks
changed profile.fs to use BEFORE-WORD hook and profile various control structs
added count-calls? stuff to profile.fs
minor changes

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

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