File:  [gforth] / gforth / search.fs
Revision 1.19: download - view: text, annotated - select for diffs
Sat Jan 5 17:42:30 2002 UTC (19 years, 9 months ago) by dvdkhlng
Branches: MAIN
CVS tags: HEAD
Made gforth.el hilight see and dbg.
Added dynamic search order stack allocation code to search.fs.

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

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