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