File:  [gforth] / gforth / kernel / int.fs
Revision 1.140: download - view: text, annotated - select for diffs
Sun Feb 26 20:16:10 2006 UTC (18 years, 2 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Enable data memory

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

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