File:  [gforth] / gforth / search.fs
Revision 1.8: download - view: text, annotated - select for diffs
Tue Mar 23 20:24:20 1999 UTC (22 years, 6 months ago) by crook
Branches: MAIN
CVS tags: HEAD
Makefile.in

-- changes to make documentation build with moofglos.fs
   rather than with mini-oof.fs (since the former contains glossary
   entries and the latter does not)

assert.fs blocks.fs debug.fs environ.fs errors.fs extend.fs float.fs
glocals.fs moofglos.fs prim search.fs struct.fs stuff.fs vt100.fs
kernel/args.fs kernel/basics.fs kernel/comp.fs kernel/cond.fs
kernel/files.fs kernel/getdoers.fs kernel/int.fs kernel/io.fs
kernel/nio.fs kernel/paths.fs kernel/require.fs kernel/special.fs
kernel/tools.fs kernel/toolsext.fs kernel/vars.fs

-- many small changes to glossary entries.. I think most are done
   now, so I hope to change far fewer files next time!

doc/gforth.ds

-- many, many small changes and a few large ones. Moved some sections
   around, fixed typos and formatting errors, added new section on
   exception handling, rearranged 'files' section.

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

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