Annotation of gforth/search.fs, revision 1.19

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: 
1.19    ! dvdkhlng   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
1.1       anton      24:   0 A, 0 A,  0 A, 0 A,   0 A, 0 A,   0 A, 0 A, 
1.19    ! dvdkhlng   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!
1.1       anton      28: 
1.7       crook      29: : get-current  ( -- wid ) \ search
1.12      crook      30:   \G @i{wid} is the identifier of the current compilation word list.
1.7       crook      31:   current @ ;
                     32: 
                     33: : set-current  ( wid -- )  \ search
1.12      crook      34:   \G Set the compilation word list to the word list identified by @i{wid}.
1.7       crook      35:   current ! ;
1.1       anton      36: 
1.11      anton      37: :noname ( -- addr )
                     38:     vp dup @ cells + ;
                     39: is context
                     40: 
                     41: : vp! ( u -- )
                     42:     vp ! ;
1.7       crook      43: : definitions  ( -- ) \ search
1.13      crook      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.
1.7       crook      46:   context @ current ! ;
1.1       anton      47: 
                     48: \ wordlist Vocabulary also previous                    14may93py
                     49: 
1.2       jwilke     50: Variable slowvoc   0 slowvoc !
1.1       anton      51: 
1.2       jwilke     52: \ Forth-wordlist AConstant Forth-wordlist
1.1       anton      53: 
1.2       jwilke     54: : mappedwordlist ( map-struct -- wid ) \ gforth
1.7       crook      55: \G Create a wordlist with a special map-structure.
1.5       pazsan     56:   here swap A, 0 A, voclink @ A, 0 A,
1.2       jwilke     57:   dup wordlist-link voclink !
                     58:   dup initvoc ;
1.1       anton      59: 
1.7       crook      60: : wordlist  ( -- wid ) \ search
1.12      crook      61:   \G Create a new, empty word list represented by @i{wid}.
1.1       anton      62:   slowvoc @
1.2       jwilke     63:   IF    \ this is now f83search because hashing may be loaded already
                     64:        \ jaw
                     65:        f83search 
1.1       anton      66:   ELSE  Forth-wordlist wordlist-map @   THEN
1.2       jwilke     67:   mappedwordlist ;
1.1       anton      68: 
1.7       crook      69: : Vocabulary ( "name" -- ) \ gforth
                     70:   \G Create a definition "name" and associate a new word list with it.
1.13      crook      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.
1.7       crook      74:   Create wordlist drop  DOES> context ! ;
                     75: 
1.10      anton      76: : check-maxvp ( n -- )
1.19    ! dvdkhlng   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
1.10      anton      93: 
1.16      anton      94: : >order ( wid -- ) \ gforth to-order
1.10      anton      95:     \g Push @var{wid} on the search order.
                     96:     vp @ 1+ dup check-maxvp vp! context ! ;
                     97: 
1.13      crook      98: : also  ( -- ) \ search-ext
1.15      anton      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.
1.16      anton     102:   context @ >order ;
1.1       anton     103: 
1.13      crook     104: : previous ( -- ) \ search-ext
1.15      anton     105:   \G Drop the wordlist at the top of the search order.
1.10      anton     106:   vp @ 1- dup 0= -50 and throw vp! ;
1.1       anton     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 )
1.2       jwilke    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
1.1       anton     149: 
                    150: \ Only root                                            14may93py
                    151: 
1.13      crook     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: 
1.9       crook     156: 
1.7       crook     157: Vocabulary Root ( -- ) \ gforth
1.15      anton     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.
1.7       crook     161: 
1.9       crook     162: : Only ( -- ) \ search-ext
1.7       crook     163:   \G Set the search order to the implementation-defined minimum search
1.8       crook     164:   \G order (for Gforth, this is the word list @code{Root}).
1.7       crook     165:   1 vp! Root also ;
1.1       anton     166: 
                    167: \ set initial search order                             14may93py
                    168: 
1.5       pazsan    169: Forth-wordlist wordlist-id @ ' Forth >body wordlist-id !
1.1       anton     170: 
1.2       jwilke    171: 0 vp! also Root also definitions
1.1       anton     172: Only Forth also definitions
1.2       jwilke    173: lookup ! \ our dictionary search order becomes the law ( -- )
1.1       anton     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: 
1.7       crook     180: : get-order  ( -- widn .. wid1 n ) \ search
1.13      crook     181:   \G Copy the search order to the data stack. The current search order
1.15      anton     182:   \G has @i{n} entries, of which @i{wid1} represents the wordlist
1.13      crook     183:   \G that is searched first (the word list at the top of the search
1.15      anton     184:   \G order) and @i{widn} represents the wordlist that is searched
1.13      crook     185:   \G last.
                    186:   vp @ 0 ?DO vp cell+ I cells + @ LOOP vp @ ;
1.1       anton     187: 
1.13      crook     188: : set-order  ( widn .. wid1 n -- ) \ gforthman- search
1.10      anton     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 ;
1.1       anton     202: 
1.7       crook     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 ;
1.1       anton     207: 
1.2       jwilke    208: : .voc
1.14      anton     209:     body> >head-noprim name>string type space ;
1.1       anton     210: 
1.13      crook     211: : order ( -- )  \  gforthman- search-ext
1.9       crook     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.
1.1       anton     219:     get-order 0
                    220:     ?DO
                    221:        .voc
                    222:     LOOP
                    223:     4 spaces get-current .voc ;
1.2       jwilke    224: 
1.1       anton     225: : vocs ( -- ) \ gforth
1.7       crook     226:     \G List vocabularies and wordlists defined in the system.
1.1       anton     227:     voclink
                    228:     BEGIN
1.2       jwilke    229:        @ dup
1.1       anton     230:     WHILE
                    231:        dup 0 wordlist-link - .voc
                    232:     REPEAT
                    233:     drop ;
                    234: 
                    235: Root definitions
                    236: 
1.9       crook     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
1.7       crook     241: ' forth-wordlist alias forth-wordlist ( -- wid ) \ search
1.12      crook     242:   \G @code{Constant} -- @i{wid} identifies the word list that includes all of the standard words
1.7       crook     243:   \G provided by Gforth. When Gforth is invoked, this word list is the compilation word
1.13      crook     244:   \G list and is at the top of the search order.
1.9       crook     245: ' set-order alias set-order
                    246: ' order alias order
1.1       anton     247: 
                    248: Forth definitions
                    249: 

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