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