Annotation of gforth/search.fs, revision 1.18

1.1       anton       1: \ search order wordset                                 14may93py
                      2: 
1.17      anton       3: \ Copyright (C) 1995,1996,1997,1998,2000 Free Software Foundation, Inc.
1.1       anton       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
1.18    ! anton      19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1       anton      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: 
1.7       crook      26: : get-current  ( -- wid ) \ search
1.12      crook      27:   \G @i{wid} is the identifier of the current compilation word list.
1.7       crook      28:   current @ ;
                     29: 
                     30: : set-current  ( wid -- )  \ search
1.12      crook      31:   \G Set the compilation word list to the word list identified by @i{wid}.
1.7       crook      32:   current ! ;
1.1       anton      33: 
1.11      anton      34: :noname ( -- addr )
                     35:     vp dup @ cells + ;
                     36: is context
                     37: 
                     38: : vp! ( u -- )
                     39:     vp ! ;
1.7       crook      40: : definitions  ( -- ) \ search
1.13      crook      41:   \G Set the compilation word list to be the same as the word list
                     42:   \G that is currently at the top of the search order.
1.7       crook      43:   context @ current ! ;
1.1       anton      44: 
                     45: \ wordlist Vocabulary also previous                    14may93py
                     46: 
1.2       jwilke     47: Variable slowvoc   0 slowvoc !
1.1       anton      48: 
1.2       jwilke     49: \ Forth-wordlist AConstant Forth-wordlist
1.1       anton      50: 
1.2       jwilke     51: : mappedwordlist ( map-struct -- wid ) \ gforth
1.7       crook      52: \G Create a wordlist with a special map-structure.
1.5       pazsan     53:   here swap A, 0 A, voclink @ A, 0 A,
1.2       jwilke     54:   dup wordlist-link voclink !
                     55:   dup initvoc ;
1.1       anton      56: 
1.7       crook      57: : wordlist  ( -- wid ) \ search
1.12      crook      58:   \G Create a new, empty word list represented by @i{wid}.
1.1       anton      59:   slowvoc @
1.2       jwilke     60:   IF    \ this is now f83search because hashing may be loaded already
                     61:        \ jaw
                     62:        f83search 
1.1       anton      63:   ELSE  Forth-wordlist wordlist-map @   THEN
1.2       jwilke     64:   mappedwordlist ;
1.1       anton      65: 
1.7       crook      66: : Vocabulary ( "name" -- ) \ gforth
                     67:   \G Create a definition "name" and associate a new word list with it.
1.13      crook      68:   \G The run-time effect of "name" is to replace the @i{wid} at the
                     69:   \G top of the search order with the @i{wid} associated with the new
                     70:   \G word list.
1.7       crook      71:   Create wordlist drop  DOES> context ! ;
                     72: 
1.10      anton      73: : check-maxvp ( n -- )
                     74:     maxvp > -49 and throw ;
                     75: 
1.16      anton      76: : >order ( wid -- ) \ gforth to-order
1.10      anton      77:     \g Push @var{wid} on the search order.
                     78:     vp @ 1+ dup check-maxvp vp! context ! ;
                     79: 
1.13      crook      80: : also  ( -- ) \ search-ext
1.15      anton      81:   \G Like @code{DUP} for the search order. Usually used before a
                     82:   \G vocabulary (e.g., @code{also Forth}); the combined effect is to push
                     83:   \G the wordlist represented by the vocabulary on the search order.
1.16      anton      84:   context @ >order ;
1.1       anton      85: 
1.13      crook      86: : previous ( -- ) \ search-ext
1.15      anton      87:   \G Drop the wordlist at the top of the search order.
1.10      anton      88:   vp @ 1- dup 0= -50 and throw vp! ;
1.1       anton      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 )
1.2       jwilke    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
1.1       anton     131: 
                    132: \ Only root                                            14may93py
                    133: 
1.13      crook     134: Vocabulary Forth ( -- ) \ gforthman- search-ext
                    135:   \G Replace the @i{wid} at the top of the search order with the
                    136:   \G @i{wid} associated with the word list @code{forth-wordlist}.
                    137: 
1.9       crook     138: 
1.7       crook     139: Vocabulary Root ( -- ) \ gforth
1.15      anton     140:   \G Add the root wordlist to the search order stack.  This vocabulary
                    141:   \G makes up the minimum search order and contains only a
                    142:   \G search-order words.
1.7       crook     143: 
1.9       crook     144: : Only ( -- ) \ search-ext
1.7       crook     145:   \G Set the search order to the implementation-defined minimum search
1.8       crook     146:   \G order (for Gforth, this is the word list @code{Root}).
1.7       crook     147:   1 vp! Root also ;
1.1       anton     148: 
                    149: \ set initial search order                             14may93py
                    150: 
1.5       pazsan    151: Forth-wordlist wordlist-id @ ' Forth >body wordlist-id !
1.1       anton     152: 
1.2       jwilke    153: 0 vp! also Root also definitions
1.1       anton     154: Only Forth also definitions
1.2       jwilke    155: lookup ! \ our dictionary search order becomes the law ( -- )
1.1       anton     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: 
1.7       crook     162: : get-order  ( -- widn .. wid1 n ) \ search
1.13      crook     163:   \G Copy the search order to the data stack. The current search order
1.15      anton     164:   \G has @i{n} entries, of which @i{wid1} represents the wordlist
1.13      crook     165:   \G that is searched first (the word list at the top of the search
1.15      anton     166:   \G order) and @i{widn} represents the wordlist that is searched
1.13      crook     167:   \G last.
                    168:   vp @ 0 ?DO vp cell+ I cells + @ LOOP vp @ ;
1.1       anton     169: 
1.13      crook     170: : set-order  ( widn .. wid1 n -- ) \ gforthman- search
1.10      anton     171:     \G If @var{n}=0, empty the search order.  If @var{n}=-1, set the
                    172:     \G search order to the implementation-defined minimum search order
                    173:     \G (for Gforth, this is the word list @code{Root}). Otherwise,
                    174:     \G replace the existing search order with the @var{n} wid entries
                    175:     \G such that @var{wid1} represents the word list that will be
                    176:     \G searched first and @var{widn} represents the word list that will
                    177:     \G be searched last.
                    178:     dup -1 = IF
                    179:        drop only exit
                    180:     THEN
                    181:     dup check-maxvp
                    182:     dup vp!
                    183:     ?dup IF 1- FOR vp cell+ I cells + !  NEXT THEN ;
1.1       anton     184: 
1.7       crook     185: : seal ( -- ) \ gforth
                    186:   \G Remove all word lists from the search order stack other than the word
                    187:   \G list that is currently on the top of the search order stack.
                    188:   context @ 1 set-order ;
1.1       anton     189: 
1.2       jwilke    190: : .voc
1.14      anton     191:     body> >head-noprim name>string type space ;
1.1       anton     192: 
1.13      crook     193: : order ( -- )  \  gforthman- search-ext
1.9       crook     194:   \G Print the search order and the compilation word list.  The
                    195:   \G word lists are printed in the order in which they are searched
                    196:   \G (which is reversed with respect to the conventional way of
                    197:   \G displaying stacks). The compilation word list is displayed last.
                    198:   \ The standard requires that the word lists are printed in the order
                    199:   \ in which they are searched. Therefore, the output is reversed
                    200:   \ with respect to the conventional way of displaying stacks.
1.1       anton     201:     get-order 0
                    202:     ?DO
                    203:        .voc
                    204:     LOOP
                    205:     4 spaces get-current .voc ;
1.2       jwilke    206: 
1.1       anton     207: : vocs ( -- ) \ gforth
1.7       crook     208:     \G List vocabularies and wordlists defined in the system.
1.1       anton     209:     voclink
                    210:     BEGIN
1.2       jwilke    211:        @ dup
1.1       anton     212:     WHILE
                    213:        dup 0 wordlist-link - .voc
                    214:     REPEAT
                    215:     drop ;
                    216: 
                    217: Root definitions
                    218: 
1.9       crook     219: ' words Alias words  ( -- ) \ tools
                    220: \G Display a list of all of the definitions in the word list at the top
                    221: \G of the search order.
                    222: ' Forth Alias Forth
1.7       crook     223: ' forth-wordlist alias forth-wordlist ( -- wid ) \ search
1.12      crook     224:   \G @code{Constant} -- @i{wid} identifies the word list that includes all of the standard words
1.7       crook     225:   \G provided by Gforth. When Gforth is invoked, this word list is the compilation word
1.13      crook     226:   \G list and is at the top of the search order.
1.9       crook     227: ' set-order alias set-order
                    228: ' order alias order
1.1       anton     229: 
                    230: Forth definitions
                    231: 

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