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

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
1.18      anton      37: ' drop ' 'initvoc >body !
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.15      anton      55: : (vocfind)  ( addr count wid -- nfa|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.15      anton      68: : (localsvocfind)  ( addr count wid -- nfa|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: 
1.20    ! pazsan     78: \ In the kernel the dictionary search works on only one wordlist.
        !            79: \ The following stuff builds a thing that looks to the kernel like one
1.2       anton      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.19      anton      84: Create vocsearch ( -- wordlist-map )
                     85: ' (localsvocfind) A, ' (reveal) A,  ' drop A,
1.1       anton      86: 
                     87: \ Only root                                            14may93py
                     88: 
1.2       anton      89: wordlist \ the wordlist structure
1.10      pazsan     90: vocsearch over wordlist-map A! \ patch the map into it
1.1       anton      91: 
                     92: Vocabulary Forth
                     93: Vocabulary Root
                     94: 
                     95: : Only  vp off  also Root also definitions ;
                     96: 
                     97: \ set initial search order                             14may93py
                     98: 
                     99: Forth-wordlist @ ' Forth >body A!
                    100: 
1.8       pazsan    101: vp off  also Root also definitions
1.1       anton     102: Only Forth also definitions
                    103: 
1.7       pazsan    104: lookup A! \ our dictionary search order becomes the law
1.3       pazsan    105: 
1.12      anton     106: ' Forth >body constant forth-wordlist \ "forth definitions get-current" and "forth-wordlist" should produce the same wid
                    107: 
                    108: 
1.1       anton     109: \ get-order set-order                                  14may93py
                    110: 
                    111: : get-order  ( -- wid1 .. widn n )
                    112:   vp @ 0 ?DO  vp cell+ I cells + @  LOOP  vp @ ;
                    113: 
                    114: : set-order  ( wid1 .. widn n / -1 -- )
                    115:   dup -1 = IF  drop Only exit  THEN  dup vp !
                    116:   ?dup IF  1- FOR  vp cell+ I cells + !  NEXT  THEN ;
                    117: 
                    118: : seal ( -- )  context @ 1 set-order ;
                    119: 
                    120: \ words visible in roots                               14may93py
                    121: 
1.14      anton     122: : .name ( name -- ) \ gforth   dot-name
                    123:     name>string type space ;
                    124: 
                    125: require termsize.fs
                    126: 
                    127: : words ( -- ) \ tools
                    128:     cr 0 context @
                    129:     BEGIN
                    130:        @ dup
                    131:     WHILE
                    132:        2dup name>string nip 2 + dup >r +
                    133:        cols >=
                    134:        IF
                    135:            cr nip 0 swap
                    136:        THEN
                    137:        dup .name space r> rot + swap
                    138:     REPEAT
                    139:     2drop ;
1.1       anton     140: 
1.16      anton     141: ' words alias vlist ( -- ) \ gforth
                    142: \g Old (pre-Forth-83) name for @code{WORDS}.
                    143: 
1.1       anton     144: : body> ( data -- cfa )  0 >body - ;
                    145: 
1.14      anton     146: : .voc
                    147:     body> >name .name ;
1.12      anton     148: : order ( -- )  \  search-ext
                    149:     \g prints the search order and the @code{current} wordlist.  The
                    150:     \g standard requires that the wordlists are printed in the order
                    151:     \g in which they are searched. Therefore, the output is reversed
                    152:     \g with respect to the conventional way of displaying stacks. The
                    153:     \g @code{current} wordlist is displayed last.
                    154:     get-order 0
                    155:     ?DO
                    156:        .voc
                    157:     LOOP
                    158:     4 spaces get-current .voc ;
                    159: : vocs ( -- ) \ gforth
                    160:     \g prints vocabularies and wordlists defined in the system.
                    161:     voclink
                    162:     BEGIN
                    163:        @ dup @
                    164:     WHILE
                    165:        dup 0 wordlist-link - .voc
                    166:     REPEAT
                    167:     drop ;
1.1       anton     168: 
                    169: Root definitions
                    170: 
                    171: ' words Alias words
                    172: ' Forth Alias Forth
1.11      anton     173: ' forth-wordlist alias forth-wordlist
                    174: ' set-order alias set-order
                    175: ' order alias order
1.1       anton     176: 
                    177: Forth definitions
1.5       pazsan    178: 
1.6       anton     179: include hash.fs
1.9       pazsan    180: 
1.17      anton     181: \ table (case-sensitive wordlist)
                    182: 
                    183: : table-find ( addr len wordlist -- nfa / false )
                    184:     >r 2dup r> bucket @ (tablefind) ;
                    185: 
                    186: Create tablesearch-map ( -- wordlist-map )
                    187:     ' table-find A, ' hash-reveal A, ' (rehash) A,
                    188: 
                    189: : table ( -- wid )
                    190:     \g create a case-sensitive wordlist
                    191:     wordlist
                    192:     tablesearch-map over wordlist-map ! ;
                    193: 
1.9       pazsan    194: \ marker                                               18dec94py
                    195: 
                    196: \ Marker creates a mark that is removed (including everything 
                    197: \ defined afterwards) when executing the mark.
                    198: 
                    199: : marker, ( -- mark )  here dup A,
                    200:   voclink @ A, voclink
1.10      pazsan    201:   BEGIN  @ dup @  WHILE  dup 0 wordlist-link - @ A,  REPEAT  drop
1.9       pazsan    202:   udp @ , ;
                    203: 
                    204: : marker! ( mark -- )  dup @ swap cell+
                    205:   dup @ voclink ! cell+
                    206:   voclink
1.10      pazsan    207:   BEGIN  @ dup @  WHILE  over @ over 0 wordlist-link - !
1.9       pazsan    208:         swap cell+ swap
                    209:   REPEAT  drop  voclink
1.10      pazsan    210:   BEGIN  @ dup @  WHILE  dup 0 wordlist-link - rehash  REPEAT  drop
1.9       pazsan    211:   @ udp !  dp ! ;
                    212: 
                    213: : marker ( "mark" -- )
                    214:   marker, Create A,  DOES>  @ marker! ;

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