File:  [gforth] / gforth / search.fs
Revision 1.34: download - view: text, annotated - select for diffs
Sat Dec 31 15:29:25 2011 UTC (7 years, 9 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright years

    1: \ search order wordset                                 14may93py
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2005,2007,2011 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 3
   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, see http://www.gnu.org/licenses/.
   19: 
   20: require struct.fs
   21: 
   22: $10 Value maxvp		\ current size of search order stack
   23: $400 Value maxvp-limit	\ upper limit for resizing search order stack
   24: 0 AValue vp		\ will be initialized later (dynamic)
   25: \ the first cell at vp contains the search order depth, the others
   26: \ contain the wordlists, starting with the last-searched one.
   27: 
   28: : get-current  ( -- wid ) \ search
   29:   \G @i{wid} is the identifier of the current compilation word list.
   30:   current @ ;
   31: 
   32: : set-current  ( wid -- )  \ search
   33:   \G Set the compilation word list to the word list identified by @i{wid}.
   34:   current ! ;
   35: 
   36: :noname ( -- addr )
   37:     vp dup @ cells + ;
   38: is context
   39: 
   40: : vp! ( u -- )
   41:     vp ! ;
   42: : definitions  ( -- ) \ search
   43:   \G Set the compilation word list to be the same as the word list
   44:   \G that is currently at the top of the search order.
   45:   context @ current ! ;
   46: 
   47: \ wordlist Vocabulary also previous                    14may93py
   48: 
   49: Variable slowvoc   0 slowvoc !
   50: 
   51: \ Forth-wordlist AConstant Forth-wordlist
   52: 
   53: : mappedwordlist ( map-struct -- wid )	\ gforth
   54: \G Create a wordlist with a special map-structure.
   55:   align here swap A, 0 A, voclink @ A, 0 A,
   56:   dup wordlist-link voclink !
   57:   dup initvoc ;
   58: 
   59: : wordlist  ( -- wid ) \ search
   60:   \G Create a new, empty word list represented by @i{wid}.
   61:   slowvoc @
   62:   IF    \ this is now f83search because hashing may be loaded already
   63: 	\ jaw
   64: 	f83search 
   65:   ELSE  Forth-wordlist wordlist-map @   THEN
   66:   mappedwordlist ;
   67: 
   68: : Vocabulary ( "name" -- ) \ gforth
   69:   \G Create a definition "name" and associate a new word list with it.
   70:   \G The run-time effect of "name" is to replace the @i{wid} at the
   71:   \G top of the search order with the @i{wid} associated with the new
   72:   \G word list.
   73:   Create wordlist drop  DOES> context ! ;
   74: 
   75: : check-maxvp ( n -- )
   76:    dup maxvp-limit > -49 and throw
   77:    dup maxvp > IF
   78:       BEGIN  dup  maxvp 2* dup TO maxvp  <= UNTIL
   79:       vp  maxvp 1+ cells resize throw TO vp
   80:    THEN drop ;
   81: 
   82: : >order ( wid -- ) \ gforth to-order
   83:     \g Push @var{wid} on the search order.
   84:     vp @ 1+ dup check-maxvp vp! context ! ;
   85: 
   86: : also  ( -- ) \ search-ext
   87:   \G Like @code{DUP} for the search order. Usually used before a
   88:   \G vocabulary (e.g., @code{also Forth}); the combined effect is to push
   89:   \G the wordlist represented by the vocabulary on the search order.
   90:   context @ >order ;
   91: 
   92: : previous ( -- ) \ search-ext
   93:   \G Drop the wordlist at the top of the search order.
   94:   vp @ 1- dup 0= -50 and throw vp! ;
   95: 
   96: \ vocabulary find                                      14may93py
   97: 
   98: : (vocfind)  ( addr count wid -- nfa|false )
   99:     \ !! generalize this to be independent of vp
  100:     drop 0 vp @ -DO ( addr count ) \ note that the loop does not reach 0
  101:         2dup vp i cells + @ (search-wordlist) dup if ( addr count nt )
  102:             nip nip unloop exit then
  103:     drop 1 -loop
  104:     2drop false ;
  105: 
  106: 0 value locals-wordlist
  107: 
  108: : (localsvocfind)  ( addr count wid -- nfa|false )
  109:     \ !! use generalized (vocfind)
  110:     drop locals-wordlist
  111:     IF 2dup locals-wordlist (search-wordlist) dup
  112: 	IF nip nip
  113: 	    EXIT
  114: 	THEN drop
  115:     THEN
  116:     0 (vocfind) ;
  117: 
  118: \ In the kernel the dictionary search works on only one wordlist.
  119: \ The following stuff builds a thing that looks to the kernel like one
  120: \ wordlist, but when searched it searches the whole search order
  121: \  (including locals)
  122: 
  123: \ this is the wordlist-map of the dictionary
  124: Create vocsearch ( -- wordlist-map )
  125: ' (localsvocfind) A, ' (reveal) A,  ' drop A, ' drop A,
  126: 
  127: \ create dummy wordlist for kernel
  128: slowvoc on
  129: vocsearch mappedwordlist \ the wordlist structure ( -- wid )
  130: 
  131: \ we don't want the dummy wordlist in our linked list
  132: 0 Voclink !
  133: slowvoc off
  134: 
  135: \ Only root                                            14may93py
  136: 
  137: Vocabulary Forth ( -- ) \ search-ext
  138:   \G Replace the @i{wid} at the top of the search order with the
  139:   \G @i{wid} associated with the word list @code{forth-wordlist}.
  140: 
  141: 
  142: Vocabulary Root ( -- ) \ gforth
  143:   \G Add the root wordlist to the search order stack.  This vocabulary
  144:   \G makes up the minimum search order and contains only a
  145:   \G search-order words.
  146: 
  147: : Only ( -- ) \ search-ext
  148:   \G Set the search order to the implementation-defined minimum search
  149:   \G order (for Gforth, this is the word list @code{Root}).
  150:   1 vp! Root also ;
  151: 
  152: : update-image-order ( -- )
  153:     \ save search order here, let vp point there
  154:     here vp over vp @ 1+ cells
  155:     dup allot move
  156:     to vp ;
  157: 
  158: : init-vp  ( -- )
  159:     vp @ $10 max to maxvp
  160:     maxvp 1+ cells allocate throw
  161:     vp over vp @ 1+ cells move
  162:     TO vp ;
  163: 
  164: :noname
  165:    init-vp DEFERS 'cold ;
  166: IS 'cold
  167: 
  168: here 0 , to vp
  169: 
  170: init-vp Only Forth also definitions
  171: 
  172: \ set initial search order                             14may93py
  173: 
  174: Forth-wordlist wordlist-id @ ' Forth >body wordlist-id !
  175: 
  176: lookup ! \ our dictionary search order becomes the law ( -- )
  177: 
  178: ' Forth >body to Forth-wordlist \ "forth definitions get-current" and "forth-wordlist" should produce the same wid
  179: 
  180: 
  181: \ get-order set-order                                  14may93py
  182: 
  183: : get-order  ( -- widn .. wid1 n ) \ search
  184:   \G Copy the search order to the data stack. The current search order
  185:   \G has @i{n} entries, of which @i{wid1} represents the wordlist
  186:   \G that is searched first (the word list at the top of the search
  187:   \G order) and @i{widn} represents the wordlist that is searched
  188:   \G last.
  189:   vp @ 0 ?DO vp cell+ I cells + @ LOOP vp @ ;
  190: 
  191: : set-order  ( widn .. wid1 n -- ) \ search
  192:     \G If @var{n}=0, empty the search order.  If @var{n}=-1, set the
  193:     \G search order to the implementation-defined minimum search order
  194:     \G (for Gforth, this is the word list @code{Root}). Otherwise,
  195:     \G replace the existing search order with the @var{n} wid entries
  196:     \G such that @var{wid1} represents the word list that will be
  197:     \G searched first and @var{widn} represents the word list that will
  198:     \G be searched last.
  199:     dup -1 = IF
  200: 	drop only exit
  201:     THEN
  202:     dup check-maxvp
  203:     dup vp!
  204:     0 swap -DO ( wid1 ... widi )
  205:         vp i cells + ! \ note that the loop does not reach 0
  206:     1 -loop ;
  207: 
  208: : seal ( -- ) \ gforth
  209:   \G Remove all word lists from the search order stack other than the word
  210:   \G list that is currently on the top of the search order stack.
  211:   context @ 1 set-order ;
  212: 
  213: [IFUNDEF] .name
  214: : id. ( nt -- ) \ gforth  i-d-dot
  215:     \G Print the name of the word represented by @var{nt}.
  216:     \ this name comes from fig-Forth
  217:     name>string type space ;
  218: 
  219: ' id. alias .id ( nt -- ) \ F83  dot-i-d
  220: \G F83 name for @code{id.}.
  221: 
  222: ' id. alias .name ( nt -- ) \ gforth-obsolete  dot-name
  223: \G Gforth <=0.5.0 name for @code{id.}.
  224: 
  225: [THEN]
  226: 
  227: : .voc ( wid -- ) \ gforth  dot-voc
  228: \G print the name of the wordlist represented by @var{wid}.  Can
  229: \G only print names defined with @code{vocabulary} or
  230: \G @code{wordlist constant}, otherwise prints @samp{???}.
  231:     dup >r wordlist-struct %size + dup head? true = if ( wid nt )
  232: 	dup name>int dup >code-address docon: = swap >body @ r@ = and if
  233: 	    id. rdrop exit
  234: 	endif
  235:     endif
  236:     drop r> body> >head-noprim id. ;
  237: 
  238: : order ( -- )  \  search-ext
  239:   \G Print the search order and the compilation word list.  The
  240:   \G word lists are printed in the order in which they are searched
  241:   \G (which is reversed with respect to the conventional way of
  242:   \G displaying stacks). The compilation word list is displayed last.
  243:   \ The standard requires that the word lists are printed in the order
  244:   \ in which they are searched. Therefore, the output is reversed
  245:   \ with respect to the conventional way of displaying stacks.
  246:     get-order 0
  247:     ?DO
  248: 	.voc
  249:     LOOP
  250:     4 spaces get-current .voc ;
  251: 
  252: : vocs ( -- ) \ gforth
  253:     \G List vocabularies and wordlists defined in the system.
  254:     voclink
  255:     BEGIN
  256: 	@ dup
  257:     WHILE
  258: 	dup 0 wordlist-link - .voc
  259:     REPEAT
  260:     drop ;
  261: 
  262: Root definitions
  263: 
  264: ' words Alias words  ( -- ) \ tools
  265: \G Display a list of all of the definitions in the word list at the top
  266: \G of the search order.
  267: ' Forth Alias Forth \ alias- search-ext
  268: ' forth-wordlist alias forth-wordlist ( -- wid ) \ search
  269:   \G @code{Constant} -- @i{wid} identifies the word list that includes all of the standard words
  270:   \G provided by Gforth. When Gforth is invoked, this word list is the compilation word
  271:   \G list and is at the top of the search order.
  272: ' set-order alias set-order ( wid1 ... widu u -- ) \ alias- search
  273: ' order alias order ( -- ) \ alias- search-ext
  274: 
  275: Forth definitions
  276: 

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