1 /*******************************************************************
3 ** Forth Inspired Command Language
4 ** ANS Forth SEARCH and SEARCH-EXT word-set written in C
5 ** Author: John Sadler (john_sadler@alum.mit.edu)
6 ** Created: 6 June 2000
7 ** $Id: search.c,v 1.9 2001/12/05 07:21:34 jsadler Exp $
8 *******************************************************************/
10 ** Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)
11 ** All rights reserved.
13 ** Get the latest Ficl release at http://ficl.sourceforge.net
15 ** I am interested in hearing from anyone who uses ficl. If you have
16 ** a problem, a success story, a defect, an enhancement request, or
17 ** if you would like to contribute to the ficl release, please
18 ** contact me by email at the address above.
20 ** L I C E N S E and D I S C L A I M E R
22 ** Redistribution and use in source and binary forms, with or without
23 ** modification, are permitted provided that the following conditions
25 ** 1. Redistributions of source code must retain the above copyright
26 ** notice, this list of conditions and the following disclaimer.
27 ** 2. Redistributions in binary form must reproduce the above copyright
28 ** notice, this list of conditions and the following disclaimer in the
29 ** documentation and/or other materials provided with the distribution.
31 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
32 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
35 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 /**************************************************************************
52 ** Make the compilation word list the same as the first word list in the
53 ** search order. Specifies that the names of subsequent definitions will
54 ** be placed in the compilation word list. Subsequent changes in the search
55 ** order will not affect the compilation word list.
56 **************************************************************************/
57 static void definitions(FICL_VM
*pVM
)
59 FICL_DICT
*pDict
= vmGetDict(pVM
);
62 if (pDict
->nLists
< 1)
64 vmThrowErr(pVM
, "DEFINITIONS error - empty search order");
67 pDict
->pCompile
= pDict
->pSearch
[pDict
->nLists
-1];
72 /**************************************************************************
73 f o r t h - w o r d l i s t
75 ** Return wid, the identifier of the word list that includes all standard
76 ** words provided by the implementation. This word list is initially the
77 ** compilation word list and is part of the initial search order.
78 **************************************************************************/
79 static void forthWordlist(FICL_VM
*pVM
)
81 FICL_HASH
*pHash
= vmGetDict(pVM
)->pForthWords
;
82 stackPushPtr(pVM
->pStack
, pHash
);
87 /**************************************************************************
90 ** Return wid, the identifier of the compilation word list.
91 **************************************************************************/
92 static void getCurrent(FICL_VM
*pVM
)
94 ficlLockDictionary(TRUE
);
95 stackPushPtr(pVM
->pStack
, vmGetDict(pVM
)->pCompile
);
96 ficlLockDictionary(FALSE
);
101 /**************************************************************************
103 ** SEARCH ( -- widn ... wid1 n )
104 ** Returns the number of word lists n in the search order and the word list
105 ** identifiers widn ... wid1 identifying these word lists. wid1 identifies
106 ** the word list that is searched first, and widn the word list that is
107 ** searched last. The search order is unaffected.
108 **************************************************************************/
109 static void getOrder(FICL_VM
*pVM
)
111 FICL_DICT
*pDict
= vmGetDict(pVM
);
112 int nLists
= pDict
->nLists
;
115 ficlLockDictionary(TRUE
);
116 for (i
= 0; i
< nLists
; i
++)
118 stackPushPtr(pVM
->pStack
, pDict
->pSearch
[i
]);
121 stackPushUNS(pVM
->pStack
, nLists
);
122 ficlLockDictionary(FALSE
);
127 /**************************************************************************
128 s e a r c h - w o r d l i s t
129 ** SEARCH ( c-addr u wid -- 0 | xt 1 | xt -1 )
130 ** Find the definition identified by the string c-addr u in the word list
131 ** identified by wid. If the definition is not found, return zero. If the
132 ** definition is found, return its execution token xt and one (1) if the
133 ** definition is immediate, minus-one (-1) otherwise.
134 **************************************************************************/
135 static void searchWordlist(FICL_VM
*pVM
)
140 FICL_HASH
*pHash
= stackPopPtr(pVM
->pStack
);
142 si
.count
= (FICL_COUNT
)stackPopUNS(pVM
->pStack
);
143 si
.cp
= stackPopPtr(pVM
->pStack
);
144 hashCode
= hashHashCode(si
);
146 ficlLockDictionary(TRUE
);
147 pFW
= hashLookup(pHash
, si
, hashCode
);
148 ficlLockDictionary(FALSE
);
152 stackPushPtr(pVM
->pStack
, pFW
);
153 stackPushINT(pVM
->pStack
, (wordIsImmediate(pFW
) ? 1 : -1));
157 stackPushUNS(pVM
->pStack
, 0);
164 /**************************************************************************
165 s e t - c u r r e n t
167 ** Set the compilation word list to the word list identified by wid.
168 **************************************************************************/
169 static void setCurrent(FICL_VM
*pVM
)
171 FICL_HASH
*pHash
= stackPopPtr(pVM
->pStack
);
172 FICL_DICT
*pDict
= vmGetDict(pVM
);
173 ficlLockDictionary(TRUE
);
174 pDict
->pCompile
= pHash
;
175 ficlLockDictionary(FALSE
);
180 /**************************************************************************
182 ** SEARCH ( widn ... wid1 n -- )
183 ** Set the search order to the word lists identified by widn ... wid1.
184 ** Subsequently, word list wid1 will be searched first, and word list
185 ** widn searched last. If n is zero, empty the search order. If n is minus
186 ** one, set the search order to the implementation-defined minimum
187 ** search order. The minimum search order shall include the words
188 ** FORTH-WORDLIST and SET-ORDER. A system shall allow n to
189 ** be at least eight.
190 **************************************************************************/
191 static void setOrder(FICL_VM
*pVM
)
194 int nLists
= stackPopINT(pVM
->pStack
);
195 FICL_DICT
*dp
= vmGetDict(pVM
);
197 if (nLists
> FICL_DEFAULT_VOCS
)
199 vmThrowErr(pVM
, "set-order error: list would be too large");
202 ficlLockDictionary(TRUE
);
207 for (i
= nLists
-1; i
>= 0; --i
)
209 dp
->pSearch
[i
] = stackPopPtr(pVM
->pStack
);
214 dictResetSearchOrder(dp
);
217 ficlLockDictionary(FALSE
);
222 /**************************************************************************
223 f i c l - w o r d l i s t
225 ** Create a new empty word list, returning its word list identifier wid.
226 ** The new word list may be returned from a pool of preallocated word
227 ** lists or may be dynamically allocated in data space. A system shall
228 ** allow the creation of at least 8 new word lists in addition to any
229 ** provided as part of the system.
231 ** 1. ficl creates a new single-list hash in the dictionary and returns
233 ** 2. ficl-wordlist takes an arg off the stack indicating the number of
234 ** hash entries in the wordlist. Ficl 2.02 and later define WORDLIST as
235 ** : wordlist 1 ficl-wordlist ;
236 **************************************************************************/
237 static void ficlWordlist(FICL_VM
*pVM
)
239 FICL_DICT
*dp
= vmGetDict(pVM
);
244 vmCheckStack(pVM
, 1, 1);
246 nBuckets
= stackPopUNS(pVM
->pStack
);
247 pHash
= dictCreateWordlist(dp
, nBuckets
);
248 stackPushPtr(pVM
->pStack
, pHash
);
253 /**************************************************************************
256 ** Pop wid off the search order. Error if the search order is empty
257 **************************************************************************/
258 static void searchPop(FICL_VM
*pVM
)
260 FICL_DICT
*dp
= vmGetDict(pVM
);
263 ficlLockDictionary(TRUE
);
267 vmThrowErr(pVM
, "search> error: empty search order");
269 stackPushPtr(pVM
->pStack
, dp
->pSearch
[--dp
->nLists
]);
270 ficlLockDictionary(FALSE
);
275 /**************************************************************************
278 ** Push wid onto the search order. Error if the search order is full.
279 **************************************************************************/
280 static void searchPush(FICL_VM
*pVM
)
282 FICL_DICT
*dp
= vmGetDict(pVM
);
284 ficlLockDictionary(TRUE
);
285 if (dp
->nLists
> FICL_DEFAULT_VOCS
)
287 vmThrowErr(pVM
, ">search error: search order overflow");
289 dp
->pSearch
[dp
->nLists
++] = stackPopPtr(pVM
->pStack
);
290 ficlLockDictionary(FALSE
);
295 /**************************************************************************
296 W I D - G E T - N A M E
297 ** ficl ( wid -- c-addr u )
298 ** Get wid's (optional) name and push onto stack as a counted string
299 **************************************************************************/
300 static void widGetName(FICL_VM
*pVM
)
302 FICL_HASH
*pHash
= vmPop(pVM
).p
;
303 char *cp
= pHash
->name
;
309 vmPush(pVM
, LVALUEtoCELL(cp
));
310 vmPush(pVM
, LVALUEtoCELL(len
));
314 /**************************************************************************
315 W I D - S E T - N A M E
316 ** ficl ( wid c-addr -- )
317 ** Set wid's name pointer to the \0 terminated string address supplied
318 **************************************************************************/
319 static void widSetName(FICL_VM
*pVM
)
321 char *cp
= (char *)vmPop(pVM
).p
;
322 FICL_HASH
*pHash
= vmPop(pVM
).p
;
328 /**************************************************************************
331 ** setparentwid ( parent-wid wid -- )
332 ** Set WID's link field to the parent-wid. search-wordlist will
333 ** iterate through all the links when finding words in the child wid.
334 **************************************************************************/
335 static void setParentWid(FICL_VM
*pVM
)
337 FICL_HASH
*parent
, *child
;
339 vmCheckStack(pVM
, 2, 0);
341 child
= (FICL_HASH
*)stackPopPtr(pVM
->pStack
);
342 parent
= (FICL_HASH
*)stackPopPtr(pVM
->pStack
);
344 child
->link
= parent
;
349 /**************************************************************************
350 f i c l C o m p i l e S e a r c h
351 ** Builds the primitive wordset and the environment-query namespace.
352 **************************************************************************/
354 void ficlCompileSearch(FICL_SYSTEM
*pSys
)
356 FICL_DICT
*dp
= pSys
->dp
;
360 ** optional SEARCH-ORDER word set
362 dictAppendWord(dp
, ">search", searchPush
, FW_DEFAULT
);
363 dictAppendWord(dp
, "search>", searchPop
, FW_DEFAULT
);
364 dictAppendWord(dp
, "definitions",
365 definitions
, FW_DEFAULT
);
366 dictAppendWord(dp
, "forth-wordlist",
367 forthWordlist
, FW_DEFAULT
);
368 dictAppendWord(dp
, "get-current",
369 getCurrent
, FW_DEFAULT
);
370 dictAppendWord(dp
, "get-order", getOrder
, FW_DEFAULT
);
371 dictAppendWord(dp
, "search-wordlist",
372 searchWordlist
, FW_DEFAULT
);
373 dictAppendWord(dp
, "set-current",
374 setCurrent
, FW_DEFAULT
);
375 dictAppendWord(dp
, "set-order", setOrder
, FW_DEFAULT
);
376 dictAppendWord(dp
, "ficl-wordlist",
377 ficlWordlist
, FW_DEFAULT
);
380 ** Set SEARCH environment query values
382 ficlSetEnv(pSys
, "search-order", FICL_TRUE
);
383 ficlSetEnv(pSys
, "search-order-ext", FICL_TRUE
);
384 ficlSetEnv(pSys
, "wordlists", FICL_DEFAULT_VOCS
);
386 dictAppendWord(dp
, "wid-get-name", widGetName
, FW_DEFAULT
);
387 dictAppendWord(dp
, "wid-set-name", widSetName
, FW_DEFAULT
);
388 dictAppendWord(dp
, "wid-set-super",
389 setParentWid
, FW_DEFAULT
);