File:  [gforth] / gforth / search.fs
Revision 1.11: download - view: text, annotated - select for diffs
Sat Oct 30 15:48:16 1999 UTC (24 years, 5 months ago) by anton
Branches: MAIN
CVS tags: HEAD
context is now a DEFERed word
various small bugfixes

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

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