Annotation of gforth/search-order.fs, revision 1.13

1.1       anton       1: \ search order wordset                                 14may93py
                      2: 
1.13    ! anton       3: \ Copyright (C) 1995 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: 
1.1       anton      21: $10 constant maxvp
                     22: Variable vp
1.5       pazsan     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, 
1.1       anton      25: 
                     26: : get-current  ( -- wid )  current @ ;
                     27: : set-current  ( wid -- )  current ! ;
                     28: 
                     29: : context ( -- addr )  vp dup @ cells + ;
                     30: : definitions  ( -- )  context @ current ! ;
                     31: 
1.5       pazsan     32: \ wordlist Vocabulary also previous                    14may93py
1.3       pazsan     33: 
1.5       pazsan     34: AVariable voclink
1.3       pazsan     35: 
1.5       pazsan     36: Defer 'initvoc
                     37: ' drop IS 'initvoc
1.1       anton      38: 
1.5       pazsan     39: Variable slowvoc   slowvoc off
1.1       anton      40: 
                     41: : wordlist  ( -- wid )
1.10      pazsan     42:   here  0 A, Forth-wordlist wordlist-map @ A, voclink @ A, slowvoc @ A,
                     43:   dup wordlist-link dup voclink ! 'initvoc ;
1.1       anton      44: 
                     45: : Vocabulary ( -- ) Create wordlist drop  DOES> context ! ;
                     46: 
                     47: : also  ( -- )
                     48:   context @ vp @ 1+ dup maxvp > abort" Vocstack full"
                     49:   vp ! context ! ;
                     50: 
                     51: : previous ( -- )  vp @ 1- dup 0= abort" Vocstack empty" vp ! ;
                     52: 
                     53: \ vocabulary find                                      14may93py
                     54: 
1.5       pazsan     55: : (vocfind)  ( addr count nfa1 -- nfa2|false )
1.2       anton      56:     \ !! generalize this to be independent of vp
1.10      pazsan     57:     drop vp dup @ 1- cells over +
                     58:     DO  2dup I 2@ over <>
                     59:         IF  (search-wordlist) dup
                     60:            IF  nip nip  UNLOOP EXIT
                     61:            THEN  drop
                     62:         ELSE  drop 2drop  THEN
                     63:     [ -1 cells ] Literal +LOOP
1.2       anton      64:     2drop false ;
                     65: 
                     66: 0 value locals-wordlist
                     67: 
1.5       pazsan     68: : (localsvocfind)  ( addr count nfa1 -- nfa2|false )
1.2       anton      69:     \ !! use generalized (vocfind)
                     70:     drop locals-wordlist
                     71:     IF 2dup locals-wordlist (search-wordlist) dup
                     72:        IF nip nip
                     73:            EXIT
                     74:        THEN drop
                     75:     THEN
                     76:     0 (vocfind) ;
                     77: 
                     78: \ In the kernal the dictionary search works on only one wordlist.
                     79: \ The following stuff builds a thing that looks to the kernal like one
                     80: \ wordlist, but when searched it searches the whole search order
                     81: \  (including locals)
1.1       anton      82: 
1.2       anton      83: \ this is the wordlist-map of the dictionary
1.5       pazsan     84: Create vocsearch       ' (localsvocfind) A, ' (reveal) A,  ' drop A,
1.1       anton      85: 
                     86: \ Only root                                            14may93py
                     87: 
1.2       anton      88: wordlist \ the wordlist structure
1.10      pazsan     89: vocsearch over wordlist-map A! \ patch the map into it
1.1       anton      90: 
                     91: Vocabulary Forth
                     92: Vocabulary Root
                     93: 
                     94: : Only  vp off  also Root also definitions ;
                     95: 
                     96: \ set initial search order                             14may93py
                     97: 
                     98: Forth-wordlist @ ' Forth >body A!
                     99: 
1.8       pazsan    100: vp off  also Root also definitions
1.1       anton     101: Only Forth also definitions
                    102: 
1.7       pazsan    103: lookup A! \ our dictionary search order becomes the law
1.3       pazsan    104: 
1.12      anton     105: ' Forth >body constant forth-wordlist \ "forth definitions get-current" and "forth-wordlist" should produce the same wid
                    106: 
                    107: 
1.1       anton     108: \ get-order set-order                                  14may93py
                    109: 
                    110: : get-order  ( -- wid1 .. widn n )
                    111:   vp @ 0 ?DO  vp cell+ I cells + @  LOOP  vp @ ;
                    112: 
                    113: : set-order  ( wid1 .. widn n / -1 -- )
                    114:   dup -1 = IF  drop Only exit  THEN  dup vp !
                    115:   ?dup IF  1- FOR  vp cell+ I cells + !  NEXT  THEN ;
                    116: 
                    117: : seal ( -- )  context @ 1 set-order ;
                    118: 
                    119: \ words visible in roots                               14may93py
                    120: 
                    121: : .name ( name -- ) name>string type space ;
                    122: : words  cr 0 context @
                    123:   BEGIN  @ dup  WHILE  2dup cell+ c@ $1F and 2 + dup >r +
                    124:          &79 >  IF  cr nip 0 swap  THEN
                    125:          dup .name space r> rot + swap  REPEAT 2drop ;
                    126: 
                    127: : body> ( data -- cfa )  0 >body - ;
                    128: 
                    129: : .voc  body> >name .name ;
1.12      anton     130: : order ( -- )  \  search-ext
                    131:     \g prints the search order and the @code{current} wordlist.  The
                    132:     \g standard requires that the wordlists are printed in the order
                    133:     \g in which they are searched. Therefore, the output is reversed
                    134:     \g with respect to the conventional way of displaying stacks. The
                    135:     \g @code{current} wordlist is displayed last.
                    136:     get-order 0
                    137:     ?DO
                    138:        .voc
                    139:     LOOP
                    140:     4 spaces get-current .voc ;
                    141: : vocs ( -- ) \ gforth
                    142:     \g prints vocabularies and wordlists defined in the system.
                    143:     voclink
                    144:     BEGIN
                    145:        @ dup @
                    146:     WHILE
                    147:        dup 0 wordlist-link - .voc
                    148:     REPEAT
                    149:     drop ;
1.1       anton     150: 
                    151: Root definitions
                    152: 
                    153: ' words Alias words
                    154: ' Forth Alias Forth
1.11      anton     155: ' forth-wordlist alias forth-wordlist
                    156: ' set-order alias set-order
                    157: ' order alias order
1.1       anton     158: 
                    159: Forth definitions
1.5       pazsan    160: 
1.6       anton     161: include hash.fs
1.9       pazsan    162: 
                    163: \ marker                                               18dec94py
                    164: 
                    165: \ Marker creates a mark that is removed (including everything 
                    166: \ defined afterwards) when executing the mark.
                    167: 
                    168: : marker, ( -- mark )  here dup A,
                    169:   voclink @ A, voclink
1.10      pazsan    170:   BEGIN  @ dup @  WHILE  dup 0 wordlist-link - @ A,  REPEAT  drop
1.9       pazsan    171:   udp @ , ;
                    172: 
                    173: : marker! ( mark -- )  dup @ swap cell+
                    174:   dup @ voclink ! cell+
                    175:   voclink
1.10      pazsan    176:   BEGIN  @ dup @  WHILE  over @ over 0 wordlist-link - !
1.9       pazsan    177:         swap cell+ swap
                    178:   REPEAT  drop  voclink
1.10      pazsan    179:   BEGIN  @ dup @  WHILE  dup 0 wordlist-link - rehash  REPEAT  drop
1.9       pazsan    180:   @ udp !  dp ! ;
                    181: 
                    182: : marker ( "mark" -- )
                    183:   marker, Create A,  DOES>  @ marker! ;

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