afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * s e a r c h . c
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Forth Inspired Command Language
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * ANS Forth SEARCH and SEARCH-EXT word-set written in C
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Author: John Sadler (john_sadler@alum.mit.edu)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Created: 6 June 2000
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * $Id: search.c,v 1.10 2010/08/12 13:57:22 asau Exp $
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * All rights reserved.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome *
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Get the latest Ficl release at http://ficl.sourceforge.net
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome *
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * I am interested in hearing from anyone who uses Ficl. If you have
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * a problem, a success story, a defect, an enhancement request, or
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * if you would like to contribute to the Ficl release, please
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * contact me by email at the address above.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome *
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * L I C E N S E and D I S C L A I M E R
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome *
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Redistribution and use in source and binary forms, with or without
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * modification, are permitted provided that the following conditions
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * are met:
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * 1. Redistributions of source code must retain the above copyright
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * notice, this list of conditions and the following disclaimer.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * notice, this list of conditions and the following disclaimer in the
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * documentation and/or other materials provided with the distribution.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome *
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * SUCH DAMAGE.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#include <string.h>
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#include "ficl.h"
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d e f i n i t i o n s
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * SEARCH ( -- )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Make the compilation word list the same as the first word list in the
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * search order. Specifies that the names of subsequent definitions will
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * be placed in the compilation word list. Subsequent changes in the search
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * order will not affect the compilation word list.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic void
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlPrimitiveDefinitions(ficlVm *vm)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionary *dictionary = ficlVmGetDictionary(vm);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_VM_ASSERT(vm, dictionary);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (dictionary->wordlistCount < 1) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlVmThrowError(vm, "DEFINITIONS error - empty search order");
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome dictionary->compilationWordlist =
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome dictionary->wordlists[dictionary->wordlistCount-1];
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * f o r t h - w o r d l i s t
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * SEARCH ( -- wid )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Return wid, the identifier of the word list that includes all standard
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * words provided by the implementation. This word list is initially the
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * compilation word list and is part of the initial search order.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic void
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlPrimitiveForthWordlist(ficlVm *vm)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlHash *hash = ficlVmGetDictionary(vm)->forthWordlist;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlStackPushPointer(vm->dataStack, hash);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * g e t - c u r r e n t
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * SEARCH ( -- wid )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Return wid, the identifier of the compilation word list.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic void
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlPrimitiveGetCurrent(ficlVm *vm)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionary *dictionary = ficlVmGetDictionary(vm);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(dictionary, FICL_TRUE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlStackPushPointer(vm->dataStack, dictionary->compilationWordlist);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(dictionary, FICL_FALSE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * g e t - o r d e r
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * SEARCH ( -- widn ... wid1 n )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Returns the number of word lists n in the search order and the word list
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * identifiers widn ... wid1 identifying these word lists. wid1 identifies
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * the word list that is searched first, and widn the word list that is
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * searched last. The search order is unaffected.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic void
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlPrimitiveGetOrder(ficlVm *vm)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionary *dictionary = ficlVmGetDictionary(vm);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome int wordlistCount = dictionary->wordlistCount;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome int i;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(dictionary, FICL_TRUE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome for (i = 0; i < wordlistCount; i++) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlStackPushPointer(vm->dataStack, dictionary->wordlists[i]);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlStackPushUnsigned(vm->dataStack, wordlistCount);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(dictionary, FICL_FALSE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * s e a r c h - w o r d l i s t
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * SEARCH ( c-addr u wid -- 0 | xt 1 | xt -1 )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Find the definition identified by the string c-addr u in the word list
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * identified by wid. If the definition is not found, return zero. If the
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * definition is found, return its execution token xt and one (1) if the
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * definition is immediate, minus-one (-1) otherwise.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic void
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlPrimitiveSearchWordlist(ficlVm *vm)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlString name;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlUnsigned16 hashCode;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlWord *word;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlHash *hash = ficlStackPopPointer(vm->dataStack);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome name.length = (ficlUnsigned8)ficlStackPopUnsigned(vm->dataStack);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome name.text = ficlStackPopPointer(vm->dataStack);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome hashCode = ficlHashCode(name);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(ficlVmGetDictionary(vm), FICL_TRUE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome word = ficlHashLookup(hash, name, hashCode);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(ficlVmGetDictionary(vm), FICL_FALSE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (word) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlStackPushPointer(vm->dataStack, word);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlStackPushInteger(vm->dataStack,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (ficlWordIsImmediate(word) ? 1 : -1));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome } else {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlStackPushUnsigned(vm->dataStack, 0);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * s e t - c u r r e n t
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * SEARCH ( wid -- )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Set the compilation word list to the word list identified by wid.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic void
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlPrimitiveSetCurrent(ficlVm *vm)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlHash *hash = ficlStackPopPointer(vm->dataStack);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionary *dictionary = ficlVmGetDictionary(vm);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(dictionary, FICL_TRUE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome dictionary->compilationWordlist = hash;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(dictionary, FICL_FALSE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * s e t - o r d e r
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * SEARCH ( widn ... wid1 n -- )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Set the search order to the word lists identified by widn ... wid1.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Subsequently, word list wid1 will be searched first, and word list
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * widn searched last. If n is zero, empty the search order. If n is minus
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * one, set the search order to the implementation-defined minimum
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * search order. The minimum search order shall include the words
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * FORTH-WORDLIST and SET-ORDER. A system shall allow n to
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * be at least eight.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic void
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlPrimitiveSetOrder(ficlVm *vm)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome int i;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome int wordlistCount = ficlStackPopInteger(vm->dataStack);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionary *dictionary = ficlVmGetDictionary(vm);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (wordlistCount > FICL_MAX_WORDLISTS) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlVmThrowError(vm,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome "set-order error: list would be too large");
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(dictionary, FICL_TRUE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (wordlistCount >= 0) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome dictionary->wordlistCount = wordlistCount;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome for (i = wordlistCount-1; i >= 0; --i) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome dictionary->wordlists[i] =
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlStackPopPointer(vm->dataStack);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome } else {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryResetSearchOrder(dictionary);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(dictionary, FICL_FALSE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * f i c l - w o r d l i s t
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * SEARCH ( -- wid )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Create a new empty word list, returning its word list identifier wid.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * The new word list may be returned from a pool of preallocated word
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * lists or may be dynamically allocated in data space. A system shall
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * allow the creation of at least 8 new word lists in addition to any
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * provided as part of the system.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Notes:
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * 1. Ficl creates a new single-list hash in the dictionary and returns
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * its address.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * 2. ficl-wordlist takes an arg off the stack indicating the number of
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * hash entries in the wordlist. Ficl 2.02 and later define WORDLIST as
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * : wordlist 1 ficl-wordlist ;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic void
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlPrimitiveFiclWordlist(ficlVm *vm)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionary *dictionary = ficlVmGetDictionary(vm);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlHash *hash;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlUnsigned nBuckets;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_STACK_CHECK(vm->dataStack, 1, 1);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome nBuckets = ficlStackPopUnsigned(vm->dataStack);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome hash = ficlDictionaryCreateWordlist(dictionary, nBuckets);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlStackPushPointer(vm->dataStack, hash);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * S E A R C H >
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Ficl ( -- wid )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Pop wid off the search order. Error if the search order is empty
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic void
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlPrimitiveSearchPop(ficlVm *vm)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionary *dictionary = ficlVmGetDictionary(vm);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome int wordlistCount;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(dictionary, FICL_TRUE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome wordlistCount = dictionary->wordlistCount;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (wordlistCount == 0) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlVmThrowError(vm, "search> error: empty search order");
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlStackPushPointer(vm->dataStack,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome dictionary->wordlists[--dictionary->wordlistCount]);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(dictionary, FICL_FALSE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * > S E A R C H
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Ficl ( wid -- )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Push wid onto the search order. Error if the search order is full.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic void
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlPrimitiveSearchPush(ficlVm *vm)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionary *dictionary = ficlVmGetDictionary(vm);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(dictionary, FICL_TRUE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (dictionary->wordlistCount > FICL_MAX_WORDLISTS) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlVmThrowError(vm, ">search error: search order overflow");
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome dictionary->wordlists[dictionary->wordlistCount++] =
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlStackPopPointer(vm->dataStack);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryLock(dictionary, FICL_FALSE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * W I D - G E T - N A M E
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Ficl ( wid -- c-addr u )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Get wid's (optional) name and push onto stack as a counted string
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic void
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlPrimitiveWidGetName(ficlVm *vm)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlHash *hash;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome char *name;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlInteger length;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlCell c;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome hash = ficlVmPop(vm).p;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome name = hash->name;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (name != NULL)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome length = strlen(name);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome else
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome length = 0;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome c.p = name;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlVmPush(vm, c);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome c.i = length;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlVmPush(vm, c);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * W I D - S E T - N A M E
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Ficl ( wid c-addr -- )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Set wid's name pointer to the \0 terminated string address supplied
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic void
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlPrimitiveWidSetName(ficlVm *vm)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome char *name = (char *)ficlVmPop(vm).p;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlHash *hash = ficlVmPop(vm).p;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome hash->name = name;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * setParentWid
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Ficl
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * setparentwid ( parent-wid wid -- )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Set WID's link field to the parent-wid. search-wordlist will
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * iterate through all the links when finding words in the child wid.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic void
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlPrimitiveSetParentWid(ficlVm *vm)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlHash *parent, *child;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_STACK_CHECK(vm->dataStack, 2, 0);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome child = (ficlHash *)ficlStackPopPointer(vm->dataStack);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome parent = (ficlHash *)ficlStackPopPointer(vm->dataStack);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome child->link = parent;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * f i c l C o m p i l e S e a r c h
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Builds the primitive wordset and the environment-query namespace.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomevoid
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlSystemCompileSearch(ficlSystem *system)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionary *dictionary = ficlSystemGetDictionary(system);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionary *environment = ficlSystemGetEnvironment(system);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_SYSTEM_ASSERT(system, dictionary);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_SYSTEM_ASSERT(system, environment);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome /*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * optional SEARCH-ORDER word set
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetPrimitive(dictionary, ">search",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlPrimitiveSearchPush, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetPrimitive(dictionary, "search>",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlPrimitiveSearchPop, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetPrimitive(dictionary, "definitions",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlPrimitiveDefinitions, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetPrimitive(dictionary, "forth-wordlist",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlPrimitiveForthWordlist, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetPrimitive(dictionary, "get-current",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlPrimitiveGetCurrent, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetPrimitive(dictionary, "get-order",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlPrimitiveGetOrder, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetPrimitive(dictionary, "search-wordlist",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlPrimitiveSearchWordlist, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetPrimitive(dictionary, "set-current",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlPrimitiveSetCurrent, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetPrimitive(dictionary, "set-order",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlPrimitiveSetOrder, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetPrimitive(dictionary, "ficl-wordlist",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlPrimitiveFiclWordlist, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome /*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Set SEARCH environment query values
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetConstant(environment, "search-order", FICL_TRUE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetConstant(environment, "search-order-ext", FICL_TRUE);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetConstant(environment, "wordlists", FICL_MAX_WORDLISTS);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetPrimitive(dictionary, "wid-get-name",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlPrimitiveWidGetName, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetPrimitive(dictionary, "wid-set-name",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlPrimitiveWidSetName, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionarySetPrimitive(dictionary, "wid-set-super",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlPrimitiveSetParentWid, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}