File:  [gforth] / gforth / kernel / int.fs
Revision 1.142: download - view: text, annotated - select for diffs
Sun Mar 5 14:10:52 2006 UTC (18 years, 1 month ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Flash-enabled Gforth EC

    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 [ has? rom [IF] ] 0= [ [THEN] ]
  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 [ has? rom [IF] ] 0= [ [THEN] ]
  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 [ has? rom [IF] ] 0= [ [THEN] ] flag-sign
  442:     ;
  443: 
  444: : (name>intn) ( nfa -- xt +-1 )
  445:     (name>x) tuck (x>int) ( w xt )
  446:     swap immediate-mask and [ has? rom [IF] ] 0= [ [THEN] ] 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: has? flash [IF] ' flash! [ELSE] ' ! [THEN]
  541: alias code-address! ( c_addr xt -- ) \ gforth
  542: \G Create a code field with code address @i{c-addr} at @i{xt}.
  543: 
  544: : does-code! ( a_addr xt -- ) \ gforth
  545: \G Create a code field at @i{xt} for a child of a @code{DOES>}-word;
  546: \G @i{a-addr} is the start of the Forth code after @code{DOES>}.
  547:     [ has? flash [IF] ]
  548:     dodoes: over flash! cell+ flash!
  549:     [ [ELSE] ]
  550:     dodoes: over ! cell+ !
  551:     [ [THEN] ] ;
  552: 
  553: ' drop alias does-handler! ( a_addr -- ) \ gforth
  554: \G Create a @code{DOES>}-handler at address @i{a-addr}. Normally,
  555: \G @i{a-addr} points just behind a @code{DOES>}.
  556: 
  557: 2 cells constant /does-handler ( -- n ) \ gforth
  558: \G The size of a @code{DOES>}-handler (includes possible padding).
  559: 
  560: [THEN]	
  561: 
  562: : sfind ( c-addr u -- 0 / xt +-1  ) \ gforth-obsolete
  563:     find-name dup
  564:     if ( nt )
  565: 	state @
  566: 	if
  567: 	    (name>comp)
  568: 	else
  569: 	    (name>intn)
  570: 	then
  571:    then ;
  572: 
  573: : find ( c-addr -- xt +-1 | c-addr 0 ) \ core,search
  574:     \G Search all word lists in the current search order for the
  575:     \G definition named by the counted string at @i{c-addr}.  If the
  576:     \G definition is not found, return 0. If the definition is found
  577:     \G return 1 (if the definition has non-default compilation
  578:     \G semantics) or -1 (if the definition has default compilation
  579:     \G semantics).  The @i{xt} returned in interpret state represents
  580:     \G the interpretation semantics.  The @i{xt} returned in compile
  581:     \G state represented either the compilation semantics (for
  582:     \G non-default compilation semantics) or the run-time semantics
  583:     \G that the compilation semantics would @code{compile,} (for
  584:     \G default compilation semantics).  The ANS Forth standard does
  585:     \G not specify clearly what the returned @i{xt} represents (and
  586:     \G also talks about immediacy instead of non-default compilation
  587:     \G semantics), so this word is questionable in portable programs.
  588:     \G If non-portability is ok, @code{find-name} and friends are
  589:     \G better (@pxref{Name token}).
  590:     dup count sfind dup
  591:     if
  592: 	rot drop
  593:     then ;
  594: 
  595: \ ticks in interpreter
  596: 
  597: : (') ( "name" -- nt ) \ gforth
  598:     parse-name name-too-short?
  599:     find-name dup 0=
  600:     IF
  601: 	drop -&13 throw
  602:     THEN  ;
  603: 
  604: : '    ( "name" -- xt ) \ core	tick
  605:     \g @i{xt} represents @i{name}'s interpretation
  606:     \g semantics. Perform @code{-14 throw} if the word has no
  607:     \g interpretation semantics.
  608:     (') name?int ;
  609: 
  610: has? compiler 0= [IF]	\ interpreter only version of IS and TO
  611: 
  612: : IS ' >body ! ;
  613: ' IS Alias TO
  614: 
  615: [THEN]
  616: 
  617: \ \ the interpreter loop				  mar92py
  618: 
  619: \ interpret                                            10mar92py
  620: 
  621: Defer parser1 ( c-addr u -- ... xt)
  622: \ "... xt" is the action to be performed by the text-interpretation of c-addr u
  623: 
  624: : parser ( c-addr u -- ... )
  625: \ text-interpret the word/number c-addr u, possibly producing a number
  626:     parser1 execute ;
  627: 
  628: has? ec [IF]
  629:     ' (name) Alias parse-name
  630:     : no.extensions  2drop -&13 throw ;
  631:     ' no.extensions Alias compiler-notfound1
  632:     ' no.extensions Alias interpreter-notfound1
  633: [ELSE]    
  634: Defer parse-name ( "name" -- c-addr u ) \ gforth
  635: \G Get the next word from the input buffer
  636: ' (name) IS parse-name
  637: 
  638: ' parse-name alias parse-word ( -- c-addr u ) \ gforth-obsolete
  639: \G old name for @code{parse-name}
  640:     
  641: ' parse-name alias name ( -- c-addr u ) \ gforth-obsolete
  642: \G old name for @code{parse-name}
  643:     
  644: Defer compiler-notfound1 ( c-addr count -- ... xt )
  645: Defer interpreter-notfound1 ( c-addr count -- ... xt )
  646: 
  647: : no.extensions  ( addr u -- )
  648:     2drop -&13 throw ;
  649: ' no.extensions IS compiler-notfound1
  650: ' no.extensions IS interpreter-notfound1
  651: 
  652: Defer before-word ( -- ) \ gforth
  653: \ called before the text interpreter parses the next word
  654: ' noop IS before-word
  655: [THEN]
  656: 
  657: : interpret1 ( ... -- ... )
  658: [ has? backtrace [IF] ]
  659:     rp@ backtrace-rp0 !
  660: [ [THEN] ]
  661:     BEGIN
  662: 	?stack [ has? EC 0= [IF] ] before-word [ [THEN] ] parse-name dup
  663:     WHILE
  664: 	parser1 execute
  665:     REPEAT
  666:     2drop ;
  667:     
  668: : interpret ( ?? -- ?? ) \ gforth
  669:     \ interpret/compile the (rest of the) input buffer
  670: [ has? backtrace [IF] ]
  671:     backtrace-rp0 @ >r	
  672: [ [THEN] ]
  673:     ['] interpret1 catch
  674: [ has? backtrace [IF] ]
  675:     r> backtrace-rp0 !
  676:     [ [THEN] ]
  677:     throw ;
  678: 
  679: \ interpreter                                 	30apr92py
  680: 
  681: \ not the most efficient implementations of interpreter and compiler
  682: : interpreter1 ( c-addr u -- ... xt ) 
  683:     2dup find-name dup
  684:     if
  685: 	nip nip name>int
  686:     else
  687: 	drop
  688: 	2dup 2>r snumber?
  689: 	IF
  690: 	    2rdrop ['] noop
  691: 	ELSE
  692: 	    2r> interpreter-notfound1
  693: 	THEN
  694:     then ;
  695: 
  696: ' interpreter1  IS  parser1
  697: 
  698: \ \ Query Evaluate                                 	07apr93py
  699: 
  700: has? file 0= [IF]
  701: : sourceline# ( -- n )  1 ;
  702: [ELSE]
  703: has? new-input 0= [IF]
  704: Variable #fill-bytes
  705: \G number of bytes read via (read-line) by the last refill
  706: [THEN]
  707: [THEN]
  708: 
  709: has? new-input 0= [IF]
  710: : input-start-line ( -- )  >in off ;
  711: : refill ( -- flag ) \ core-ext,block-ext,file-ext
  712:     \G Attempt to fill the input buffer from the input source.  When
  713:     \G the input source is the user input device, attempt to receive
  714:     \G input into the terminal input device. If successful, make the
  715:     \G result the input buffer, set @code{>IN} to 0 and return true;
  716:     \G otherwise return false. When the input source is a block, add 1
  717:     \G to the value of @code{BLK} to make the next block the input
  718:     \G source and current input buffer, and set @code{>IN} to 0;
  719:     \G return true if the new value of @code{BLK} is a valid block
  720:     \G number, false otherwise. When the input source is a text file,
  721:     \G attempt to read the next line from the file. If successful,
  722:     \G make the result the current input buffer, set @code{>IN} to 0
  723:     \G and return true; otherwise, return false.  A successful result
  724:     \G includes receipt of a line containing 0 characters.
  725:     [ has? file [IF] ]
  726: 	blk @  IF  1 blk +!  true  EXIT  THEN
  727: 	[ [THEN] ]
  728:     tib /line
  729:     [ has? file [IF] ]
  730: 	loadfile @ ?dup
  731: 	IF    (read-line) throw #fill-bytes !
  732: 	ELSE
  733: 	    [ [THEN] ]
  734: 	sourceline# 0< IF 2drop false EXIT THEN
  735: 	accept true
  736: 	[ has? file [IF] ]
  737: 	THEN
  738: 	1 loadline +!
  739: 	[ [THEN] ]
  740:     swap #tib !
  741:     input-start-line ;
  742: 
  743: : query   ( -- ) \ core-ext
  744:     \G Make the user input device the input source. Receive input into
  745:     \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
  746:     \G superceeded by @code{accept}.
  747:     [ has? file [IF] ]
  748: 	blk off loadfile off
  749: 	[ [THEN] ]
  750:     refill drop ;
  751: [THEN]
  752: 
  753: \ save-mem extend-mem
  754: 
  755: has? os [IF]
  756: : save-mem	( addr1 u -- addr2 u ) \ gforth
  757:     \g copy a memory block into a newly allocated region in the heap
  758:     swap >r
  759:     dup allocate throw
  760:     swap 2dup r> -rot move ;
  761: 
  762: : free-mem-var ( addr -- )
  763:     \ addr is the address of a 2variable containing address and size
  764:     \ of a memory range; frees memory and clears the 2variable.
  765:     dup 2@ drop dup
  766:     if ( addr mem-start )
  767: 	free throw
  768: 	0 0 rot 2!
  769:     else
  770: 	2drop
  771:     then ;
  772: 
  773: : extend-mem	( addr1 u1 u -- addr addr2 u2 )
  774:     \ extend memory block allocated from the heap by u aus
  775:     \ the (possibly reallocated) piece is addr2 u2, the extension is at addr
  776:     over >r + dup >r resize throw
  777:     r> over r> + -rot ;
  778: [THEN]
  779: 
  780: \ EVALUATE                                              17may93jaw
  781: 
  782: has? file 0= has? new-input 0= and [IF]
  783: : push-file  ( -- )  r>
  784:   tibstack @ >r  >tib @ >r  #tib @ >r
  785:   >tib @ tibstack @ = IF  r@ tibstack +!  THEN
  786:   tibstack @ >tib ! >in @ >r  >r ;
  787: 
  788: : pop-file   ( throw-code -- throw-code )
  789:   r>
  790:   r> >in !  r> #tib !  r> >tib !  r> tibstack !  >r ;
  791: [THEN]
  792: 
  793: has? new-input 0= [IF]
  794: : evaluate ( c-addr u -- ) \ core,block
  795:     \G Save the current input source specification. Store @code{-1} in
  796:     \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
  797:     \G @code{0} and make the string @i{c-addr u} the input source
  798:     \G and input buffer. Interpret. When the parse area is empty,
  799:     \G restore the input source specification.
  800: [ has? file [IF] ]
  801:     s" *evaluated string*" loadfilename>r
  802: [ [THEN] ]
  803:     push-file #tib ! >tib !
  804:     input-start-line
  805:     [ has? file [IF] ]
  806: 	blk off loadfile off -1 loadline !
  807: 	[ [THEN] ]
  808:     ['] interpret catch
  809:     pop-file
  810: [ has? file [IF] ]
  811:     r>loadfilename
  812: [ [THEN] ]
  813:     throw ;
  814: [THEN]
  815: 
  816: \ \ Quit                                            	13feb93py
  817: 
  818: Defer 'quit
  819: 
  820: Defer .status
  821: 
  822: : prompt        state @ IF ."  compiled" EXIT THEN ."  ok" ;
  823: 
  824: : (quit) ( -- )
  825:     \ exits only through THROW etc.
  826:     BEGIN
  827: 	.status
  828: 	['] cr catch if
  829: 	    >stderr cr ." Can't print to stdout, leaving" cr
  830: 	    \ if stderr does not work either, already DoError causes a hang
  831: 	    2 (bye)
  832: 	endif
  833: 	refill  WHILE
  834: 	    interpret prompt
  835:     REPEAT
  836:     bye ;
  837: 
  838: ' (quit) IS 'quit
  839: 
  840: \ \ DOERROR (DOERROR)                        		13jun93jaw
  841: 
  842: has? ec 0= [IF]
  843: 8 Constant max-errors
  844: 5 has? file 2 and + Constant /error
  845: Variable error-stack  0 error-stack !
  846: max-errors /error * cells allot
  847: \ format of one cell:
  848: \ source ( c-addr u )
  849: \ last parsed lexeme ( c-addr u )
  850: \ line-number
  851: \ Loadfilename ( addr u )
  852: 
  853: : error> ( --  c-addr1 u1 c-addr2 u2 line# [addr u] )
  854:     -1 error-stack +!
  855:     error-stack dup @
  856:     /error * cells + cell+
  857:     /error cells bounds DO
  858:         I @
  859:     cell +LOOP ;
  860: 
  861: : >error ( c-addr1 u1 c-addr2 u2 line# [addr u] -- )
  862:     error-stack dup @ dup 1+
  863:     max-errors 1- min error-stack !
  864:     /error * cells + cell+
  865:     /error 1- cells bounds swap DO
  866:         I !
  867:     -1 cells +LOOP ;
  868: 
  869: : input-error-data ( -- c-addr1 u1 c-addr2 u2 line# [addr u] )
  870:     \ error data for the current input, to be used by >error or .error-frame
  871:     source input-lexeme 2@ sourceline#
  872:     [ has? file [IF] ] sourcefilename [ [THEN] ] ;
  873: 
  874: : dec. ( n -- ) \ gforth
  875:     \G Display @i{n} as a signed decimal number, followed by a space.
  876:     \ !! not used...
  877:     base @ decimal swap . base ! ;
  878: 
  879: : dec.r ( u n -- ) \ gforth
  880:     \G Display @i{u} as a unsigned decimal number in a field @i{n}
  881:     \G characters wide.
  882:     base @ >r decimal .r r> base ! ;
  883: 
  884: : hex. ( u -- ) \ gforth
  885:     \G Display @i{u} as an unsigned hex number, prefixed with a "$" and
  886:     \G followed by a space.
  887:     \ !! not used...
  888:     [char] $ emit base @ swap hex u. base ! ;
  889: 
  890: : -trailing  ( c_addr u1 -- c_addr u2 ) \ string dash-trailing
  891: \G Adjust the string specified by @i{c-addr, u1} to remove all
  892: \G trailing spaces. @i{u2} is the length of the modified string.
  893:     BEGIN
  894: 	dup
  895:     WHILE
  896: 	1- 2dup + c@ bl <>
  897:     UNTIL  1+  THEN ;
  898: 
  899: DEFER DOERROR
  900: 
  901: has? backtrace [IF]
  902: Defer dobacktrace ( -- )
  903: ' noop IS dobacktrace
  904: [THEN]
  905: 
  906: : .error-string ( throw-code -- )
  907:   dup -2 = 
  908:   IF 	"error @ ?dup IF count type  THEN drop
  909:   ELSE	.error
  910:   THEN ;
  911: 
  912: : umin ( u1 u2 -- u )
  913:     2dup u>
  914:     if
  915: 	swap
  916:     then
  917:     drop ;
  918: 
  919: Defer mark-start
  920: Defer mark-end
  921: 
  922: :noname ." >>>" ; IS mark-start
  923: :noname ." <<<" ; IS mark-end
  924: 
  925: : part-type ( addr1 u1 u -- addr2 u2 )
  926:     \ print first u characters of addr1 u1, addr2 u2 is the rest
  927:     over umin 2 pick over type /string ;
  928: 
  929: : .error-line ( c-addr1 u1 c-addr2 u2 -- )
  930:     \ print error in line c-addr1 u1, where the error-causing lexeme
  931:     \ is c-addr2 u2
  932:     >r 2 pick - part-type ( c-addr3 u3 R: u2 )
  933:     mark-start r> part-type mark-end ( c-addr4 u4 )
  934:     type ;
  935: 
  936: : .error-frame ( throwcode addr1 u1 addr2 u2 n2 [addr3 u3] -- throwcode )
  937:     \ addr3 u3: filename of included file - optional
  938:     \ n2:       line number
  939:     \ addr2 u2: parsed lexeme (should be marked as causing the error)
  940:     \ addr1 u1: input line
  941:     error-stack @
  942:     IF ( throwcode addr1 u1 n0 n1 n2 [addr2 u2] )
  943:         [ has? file [IF] ] \ !! unbalanced stack effect
  944: 	  over IF
  945: 	      cr ." in file included from "
  946: 	      type ." :"
  947: 	      0 dec.r  2drop 2drop
  948:           ELSE
  949:               2drop 2drop 2drop drop
  950:           THEN
  951:           [ [THEN] ] ( throwcode addr1 u1 n0 n1 n2 )
  952:     ELSE ( throwcode addr1 u1 n0 n1 n2 [addr2 u2] )
  953:         [ has? file [IF] ]
  954:             cr type ." :"
  955:             [ [THEN] ] ( throwcode addr1 u1 n0 n1 n2 )
  956:         dup 0 dec.r ." : " 5 pick .error-string
  957:         IF \ if line# non-zero, there is a line
  958:             cr .error-line
  959:         ELSE
  960:             2drop 2drop
  961:         THEN
  962:     THEN ;
  963: 
  964: : (DoError) ( throw-code -- )
  965:   [ has? os [IF] ]
  966:       >stderr
  967:   [ [THEN] ] 
  968:   input-error-data .error-frame
  969:   error-stack @ 0 ?DO
  970:     error>
  971:     .error-frame
  972:   LOOP
  973:   drop 
  974: [ has? backtrace [IF] ]
  975:   dobacktrace
  976: [ [THEN] ]
  977:   normal-dp dpp ! ;
  978: 
  979: ' (DoError) IS DoError
  980: 
  981: [ELSE]
  982:     : dec.  base @ >r decimal . r> base ! ;
  983:     : DoError ( throw-code -- ) ." Error# " dec. cr ;
  984: [THEN]
  985: 
  986: : quit ( ?? -- ?? ) \ core
  987:     \G Empty the return stack, make the user input device
  988:     \G the input source, enter interpret state and start
  989:     \G the text interpreter.
  990:     rp0 @ rp! handler off clear-tibstack
  991:     [ has? new-input 0= [IF] ] >tib @ >r [ [THEN] ]
  992:     BEGIN
  993: 	[ has? compiler [IF] ]
  994: 	    [compile] [
  995: 	[ [THEN] ]
  996: 	\ stack depths may be arbitrary here
  997: 	['] 'quit CATCH dup
  998:     WHILE
  999: 	    <# \ reset hold area, or we may get another error
 1000: 	    DoError
 1001: 	    \ stack depths may be arbitrary still (or again), so clear them
 1002: 	    clearstacks
 1003: 	    [ has? new-input [IF] ] clear-tibstack
 1004: 	    [ [ELSE] ] r@ >tib ! r@ tibstack !
 1005: 	    [ [THEN] ]
 1006:     REPEAT
 1007:     drop [ has? new-input [IF] ] clear-tibstack
 1008:     [ [ELSE] ] r> >tib !
 1009:     [ [THEN] ] ;
 1010: 
 1011: \ \ Cold Boot                                    	13feb93py
 1012: 
 1013: : (bootmessage)
 1014:     ." Gforth " version-string type 
 1015:     ." , Copyright (C) 1995-2006 Free Software Foundation, Inc." cr
 1016:     ." Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'"
 1017: [ has? os [IF] ]
 1018:      cr ." Type `bye' to exit"
 1019: [ [THEN] ] ;
 1020: 
 1021: defer bootmessage
 1022: has? file [IF]
 1023: defer process-args
 1024: [THEN]
 1025: 
 1026: ' (bootmessage) IS bootmessage
 1027: 
 1028: has? ec 0= [IF]
 1029: Defer 'cold ( -- ) \ gforth  tick-cold
 1030: \ hook (deferred word) for things to do right before interpreting the
 1031: \ command-line arguments
 1032: ' noop IS 'cold
 1033: [THEN]
 1034: 
 1035: AVariable init8 NIL init8 !
 1036: 
 1037: : cold ( -- ) \ gforth
 1038:     [ has? backtrace [IF] ]
 1039:     rp@ backtrace-rp0 !
 1040: [ [THEN] ]
 1041: [ has? file [IF] ]
 1042:     os-cold
 1043: [ [THEN] ]
 1044: [ has? ec 0= [IF] ]
 1045:     set-encoding-fixed-width
 1046:     'cold
 1047: [ [THEN] ]
 1048:     init8 chainperform
 1049: [ has? file [IF] ]
 1050:     process-args
 1051:     loadline off
 1052: [ [THEN] ]
 1053:     bootmessage
 1054:     quit ;
 1055: 
 1056: has? new-input 0= [IF]
 1057: : clear-tibstack ( -- )
 1058: [ has? glocals [IF] ]
 1059:     lp@ forthstart 7 cells + @ - 
 1060: [ [ELSE] ]
 1061:     [ has? os [IF] ]
 1062:     r0 @ forthstart 6 cells + @ -
 1063:     [ [ELSE] ]
 1064:     sp@ cell+
 1065:     [ [THEN] ]
 1066: [ [THEN] ]
 1067:     dup >tib ! tibstack ! #tib off
 1068:     input-start-line ;
 1069: [THEN]
 1070: 
 1071: : boot ( path n **argv argc -- )
 1072: [ has? no-userspace 0= [IF] ]
 1073:     main-task up!
 1074: [ [THEN] ]
 1075: [ has? os [IF] ]
 1076:     os-boot
 1077: [ [THEN] ]
 1078: [ has? rom [IF] ]
 1079:     ram-mirror ram-start ram-size cmove
 1080: [ [THEN] ]
 1081:     sp@ sp0 !
 1082: [ has? peephole [IF] ]
 1083:     \ only needed for greedy static superinstruction selection
 1084:     \ primtable prepare-peephole-table TO peeptable
 1085: [ [THEN] ]
 1086: [ has? new-input [IF] ]
 1087:     current-input off
 1088: [ [THEN] ]
 1089:     clear-tibstack
 1090:     0 0 includefilename 2!
 1091:     rp@ rp0 !
 1092: [ has? floating [IF] ]
 1093:     fp@ fp0 !
 1094: [ [THEN] ]
 1095:     handler off
 1096:     ['] cold catch dup -&2049 <> if \ broken pipe?
 1097: 	DoError cr
 1098:     endif
 1099: [ has? os [IF] ]
 1100:     1 (bye) \ !! determin exit code from throw code?
 1101: [ [THEN] ]
 1102: ;
 1103: 
 1104: has? os [IF]
 1105: : bye ( -- ) \ tools-ext
 1106: [ has? file [IF] ]
 1107:     script? 0= IF  cr  THEN
 1108: [ [ELSE] ]
 1109:     cr
 1110: [ [THEN] ]
 1111:     0 (bye) ;
 1112: [THEN]
 1113: 
 1114: \ **argv may be scanned by the C starter to get some important
 1115: \ information, as -display and -geometry for an X client FORTH
 1116: \ or space and stackspace overrides
 1117: 
 1118: \ 0 arg contains, however, the name of the program.
 1119: 

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