File:  [gforth] / gforth / search.fs
Revision 1.10: download - view: text, annotated - select for diffs
Sat May 22 20:03:53 1999 UTC (24 years, 10 months ago) by anton
Branches: MAIN
CVS tags: HEAD
documentation changes (wordlists)
added push-order, small fixes in search.fs

    1: \ search order wordset                                 14may93py
    2: 
    3: \ Copyright (C) 1995,1996,1997,1998 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., 675 Mass Ave, Cambridge, MA 02139, USA.
   20: 
   21: $10 constant maxvp
   22: Variable vp
   23:   0 A, 0 A,  0 A, 0 A,   0 A, 0 A,   0 A, 0 A, 
   24:   0 A, 0 A,  0 A, 0 A,   0 A, 0 A,   0 A, 0 A, 
   25: 
   26: : get-current  ( -- wid ) \ search
   27:   \G @var{wid} is the identifier of the current compilation word list.
   28:   current @ ;
   29: 
   30: : set-current  ( wid -- )  \ search
   31:   \G Set the compilation word list to the word list identified by @var{wid}.
   32:   current ! ;
   33: 
   34: \ : context ( -- addr )  vp dup @ cells + ;
   35: : vp! dup vp ! cells vp + to context ;
   36: : definitions  ( -- ) \ search
   37:   \G Make the compilation word list the same as the word list
   38:   \G that is currently at the top of the search order stack.
   39:   context @ current ! ;
   40: 
   41: \ wordlist Vocabulary also previous                    14may93py
   42: 
   43: Variable slowvoc   0 slowvoc !
   44: 
   45: \ Forth-wordlist AConstant Forth-wordlist
   46: 
   47: : mappedwordlist ( map-struct -- wid )	\ gforth
   48: \G Create a wordlist with a special map-structure.
   49:   here swap A, 0 A, voclink @ A, 0 A,
   50:   dup wordlist-link voclink !
   51:   dup initvoc ;
   52: 
   53: : wordlist  ( -- wid ) \ search
   54:   \G Create a new, empty word list represented by @var{wid}.
   55:   slowvoc @
   56:   IF    \ this is now f83search because hashing may be loaded already
   57: 	\ jaw
   58: 	f83search 
   59:   ELSE  Forth-wordlist wordlist-map @   THEN
   60:   mappedwordlist ;
   61: 
   62: : Vocabulary ( "name" -- ) \ gforth
   63:   \G Create a definition "name" and associate a new word list with it.
   64:   \G The run-time effect of "name" is to push the new word list's wid
   65:   \G onto the top of the search order stack.
   66:   Create wordlist drop  DOES> context ! ;
   67: 
   68: : check-maxvp ( n -- )
   69:     maxvp > -49 and throw ;
   70: 
   71: : push-order ( wid -- ) \ gforth
   72:     \g Push @var{wid} on the search order.
   73:     vp @ 1+ dup check-maxvp vp! context ! ;
   74: 
   75: : also  ( -- ) \ search ext
   76:   \G Perform a @code{DUP} on the search order stack. Usually used prior
   77:   \G to @code{Forth}, @code{definitions} etc.
   78:   context @ push-order ;
   79: 
   80: : previous ( -- ) \ search ext
   81:   \G Perform a @code{DROP} on the search order stack, thereby removing the wid at the
   82:   \G top of the (search order) stack from the search order.
   83:   vp @ 1- dup 0= -50 and throw vp! ;
   84: 
   85: \ vocabulary find                                      14may93py
   86: 
   87: : (vocfind)  ( addr count wid -- nfa|false )
   88:     \ !! generalize this to be independent of vp
   89:     drop vp dup @ 1- cells over +
   90:     DO  2dup I 2@ over <>
   91:         IF  (search-wordlist) dup
   92: 	    IF  nip nip  UNLOOP EXIT
   93: 	    THEN  drop
   94:         ELSE  drop 2drop  THEN
   95:     [ -1 cells ] Literal +LOOP
   96:     2drop false ;
   97: 
   98: 0 value locals-wordlist
   99: 
  100: : (localsvocfind)  ( addr count wid -- nfa|false )
  101:     \ !! use generalized (vocfind)
  102:     drop locals-wordlist
  103:     IF 2dup locals-wordlist (search-wordlist) dup
  104: 	IF nip nip
  105: 	    EXIT
  106: 	THEN drop
  107:     THEN
  108:     0 (vocfind) ;
  109: 
  110: \ In the kernel the dictionary search works on only one wordlist.
  111: \ The following stuff builds a thing that looks to the kernel like one
  112: \ wordlist, but when searched it searches the whole search order
  113: \  (including locals)
  114: 
  115: \ this is the wordlist-map of the dictionary
  116: Create vocsearch ( -- wordlist-map )
  117: ' (localsvocfind) A, ' (reveal) A,  ' drop A, ' drop A,
  118: 
  119: \ create dummy wordlist for kernel
  120: slowvoc on
  121: vocsearch mappedwordlist \ the wordlist structure ( -- wid )
  122: 
  123: \ we don't want the dummy wordlist in our linked list
  124: 0 Voclink !
  125: slowvoc off
  126: 
  127: \ Only root                                            14may93py
  128: 
  129: Vocabulary Forth ( -- ) \ thisone- search-ext
  130:   \G Push the @var{wid} associated with @code{forth-wordlist} onto the
  131:   \G search order stack.
  132: 
  133: Vocabulary Root ( -- ) \ gforth
  134:   \G Add the vocabulary @code{Root} to the search order stack.
  135:   \G This vocabulary makes up the minimum search order and
  136:   \G contains these words: @code{order} @code{set-order}
  137:   \G @code{forth-wordlist} @code{Forth} @code{words}
  138: 
  139: : Only ( -- ) \ search-ext
  140:   \G Set the search order to the implementation-defined minimum search
  141:   \G order (for Gforth, this is the word list @code{Root}).
  142:   1 vp! Root also ;
  143: 
  144: \ set initial search order                             14may93py
  145: 
  146: Forth-wordlist wordlist-id @ ' Forth >body wordlist-id !
  147: 
  148: 0 vp! also Root also definitions
  149: Only Forth also definitions
  150: lookup ! \ our dictionary search order becomes the law ( -- )
  151: 
  152: ' Forth >body to Forth-wordlist \ "forth definitions get-current" and "forth-wordlist" should produce the same wid
  153: 
  154: 
  155: \ get-order set-order                                  14may93py
  156: 
  157: : get-order  ( -- widn .. wid1 n ) \ search
  158:   \G Copy the search order stack to the data stack. The current search
  159:   \G order has @var{n} entries, of which @var{wid1} represents the word
  160:   \G list that is searched first (the word list at the top of the stack) and
  161:   \G @var{widn} represents the word order that is searched last.
  162:   vp @ 0 ?DO  vp cell+ I cells + @  LOOP  vp @ ;
  163: 
  164: : set-order  ( widn .. wid1 n -- ) \ thisone- search
  165:     \G If @var{n}=0, empty the search order.  If @var{n}=-1, set the
  166:     \G search order to the implementation-defined minimum search order
  167:     \G (for Gforth, this is the word list @code{Root}). Otherwise,
  168:     \G replace the existing search order with the @var{n} wid entries
  169:     \G such that @var{wid1} represents the word list that will be
  170:     \G searched first and @var{widn} represents the word list that will
  171:     \G be searched last.
  172:     dup -1 = IF
  173: 	drop only exit
  174:     THEN
  175:     dup check-maxvp
  176:     dup vp!
  177:     ?dup IF 1- FOR vp cell+ I cells + !  NEXT THEN ;
  178: 
  179: : seal ( -- ) \ gforth
  180:   \G Remove all word lists from the search order stack other than the word
  181:   \G list that is currently on the top of the search order stack.
  182:   context @ 1 set-order ;
  183: 
  184: : .voc
  185:     body> >head name>string type space ;
  186: 
  187: : order ( -- )  \  thisone- search-ext
  188:   \G Print the search order and the compilation word list.  The
  189:   \G word lists are printed in the order in which they are searched
  190:   \G (which is reversed with respect to the conventional way of
  191:   \G displaying stacks). The compilation word list is displayed last.
  192:   \ The standard requires that the word lists are printed in the order
  193:   \ in which they are searched. Therefore, the output is reversed
  194:   \ with respect to the conventional way of displaying stacks.
  195:     get-order 0
  196:     ?DO
  197: 	.voc
  198:     LOOP
  199:     4 spaces get-current .voc ;
  200: 
  201: : vocs ( -- ) \ gforth
  202:     \G List vocabularies and wordlists defined in the system.
  203:     voclink
  204:     BEGIN
  205: 	@ dup
  206:     WHILE
  207: 	dup 0 wordlist-link - .voc
  208:     REPEAT
  209:     drop ;
  210: 
  211: Root definitions
  212: 
  213: ' words Alias words  ( -- ) \ tools
  214: \G Display a list of all of the definitions in the word list at the top
  215: \G of the search order.
  216: ' Forth Alias Forth
  217: ' forth-wordlist alias forth-wordlist ( -- wid ) \ search
  218:   \G CONSTANT: @var{wid} identifies the word list that includes all of the standard words
  219:   \G provided by Gforth. When Gforth is invoked, this word list is the compilation word
  220:   \G list and is at the top of the word list stack.
  221: ' set-order alias set-order
  222: ' order alias order
  223: 
  224: Forth definitions
  225: 

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