afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t . c
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Forth Inspired Command Language - dictionary methods
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Author: John Sadler (john_sadler@alum.mit.edu)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Created: 19 July 1997
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * $Id: dictionary.c,v 1.2 2010/09/12 15:14:52 asau Exp $
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * This file implements the dictionary -- Ficl's model of
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * memory management. All Ficl words are stored in the
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * dictionary. A word is a named chunk of data with its
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * associated code. Ficl treats all words the same, even
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * precompiled ones, so your words become first-class
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * extensions of the language. You can even define new
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * control structures.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * 29 jun 1998 (sadler) added variable sized hash table support
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * All rights reserved.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Get the latest Ficl release at http://ficl.sourceforge.net
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 * L I C E N S E and D I S C L A I M E R
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 * 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 * 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#define FICL_SAFE_CALLBACK_FROM_SYSTEM(system) \
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (((system) != NULL) ? &((system)->callback) : NULL)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#define FICL_SAFE_SYSTEM_FROM_DICTIONARY(dictionary) \
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (((dictionary) != NULL) ? (dictionary)->system : NULL)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#define FICL_DICTIONARY_ASSERT(dictionary, expression) \
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_SYSTEM_ASSERT(FICL_SAFE_SYSTEM_FROM_DICTIONARY(dictionary), \
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t A b o r t D e f i n i t i o n
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Abort a definition in process: reclaim its memory and unlink it
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * from the dictionary list. Assumes that there is a smudged
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * definition in process...otherwise does nothing.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * NOTE: this function is not smart enough to unlink a word that
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * has been successfully defined (ie linked into a hash). It
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * only works for defs in process. If the def has been unsmudged,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * nothing happens.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAbortDefinition(ficlDictionary *dictionary)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t A l i g n
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Align the dictionary's free space pointer
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome dictionary->here = ficlAlignPointer(dictionary->here);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t A l l o t
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Allocate or remove n chars of dictionary space, with
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * checks for underrun and overrun
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAllot(ficlDictionary *dictionary, int n)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t A l l o t C e l l s
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Reserve space for the requested number of ficlCells in the
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * dictionary. If nficlCells < 0 , removes space from the dictionary.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAllotCells(ficlDictionary *dictionary, int nficlCells)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t A p p e n d C e l l
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Append the specified ficlCell to the dictionary
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAppendCell(ficlDictionary *dictionary, ficlCell c)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t A p p e n d C h a r
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Append the specified char to the dictionary
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAppendCharacter(ficlDictionary *dictionary, char c)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t A p p e n d U N S
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Append the specified ficlUnsigned to the dictionary
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAppendUnsigned(ficlDictionary *dictionary, ficlUnsigned u)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAppendData(ficlDictionary *dictionary, void *data,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t C o p y N a m e
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Copy up to FICL_NAME_LENGTH characters of the name specified by s into
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * the dictionary starting at "here", then NULL-terminate the name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * point "here" to the next available byte, and return the address of
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * the beginning of the name. Used by dictAppendWord.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * N O T E S :
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * 1. "here" is guaranteed to be aligned after this operation.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * 2. If the string has zero length, align and return "here"
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAppendString(ficlDictionary *dictionary, ficlString s)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlInteger length = FICL_STRING_GET_LENGTH(s);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (ficlDictionaryAppendData(dictionary, data, length));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAppendConstantInstruction(ficlDictionary *dictionary,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlString name, ficlInstruction instruction, ficlInteger value)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlWord *word = ficlDictionaryAppendWord(dictionary, name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (ficlPrimitive)instruction, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlDictionaryAppendUnsigned(dictionary, value);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAppend2ConstantInstruction(ficlDictionary *dictionary,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlString name, ficlInstruction instruction, ficl2Integer value)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlWord *word = ficlDictionaryAppendWord(dictionary, name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (ficlPrimitive)instruction, FICL_WORD_DEFAULT);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAppendConstant(ficlDictionary *dictionary, char *name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (ficlDictionaryAppendConstantInstruction(dictionary, s,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAppend2Constant(ficlDictionary *dictionary, char *name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (ficlDictionaryAppend2ConstantInstruction(dictionary, s,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionarySetConstantInstruction(ficlDictionary *dictionary,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlString name, ficlInstruction instruction, ficlInteger value)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlWord *word = ficlDictionaryLookup(dictionary, name);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome word = ficlDictionaryAppendConstantInstruction(dictionary,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionarySetConstant(ficlDictionary *dictionary, char *name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (ficlDictionarySetConstantInstruction(dictionary, s,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionarySet2ConstantInstruction(ficlDictionary *dictionary, ficlString s,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlInstruction instruction, ficl2Integer value)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * only reuse the existing word if we're sure it has space for a
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ((((ficlInstruction)word->code) == ficlInstruction2ConstantParen) ||
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (((ficlInstruction)word->code) == ficlInstructionF2ConstantParen)))
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ((((ficlInstruction)word->code) == ficlInstruction2ConstantParen)))
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#endif /* FICL_WANT_FLOAT */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome word->param[0].u = FICL_2UNSIGNED_GET_HIGH(value);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome word->param[1].u = FICL_2UNSIGNED_GET_LOW(value);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome word = ficlDictionaryAppend2ConstantInstruction(dictionary, s,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionarySet2Constant(ficlDictionary *dictionary, char *name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (ficlDictionarySet2ConstantInstruction(dictionary, s,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionarySetConstantString(ficlDictionary *dictionary, char *name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_2INTEGER_SET(strlen(value), (intptr_t)value, valueAs2Integer);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (ficlDictionarySet2ConstantInstruction(dictionary, s,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlInstruction2ConstantParen, valueAs2Integer));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t A p p e n d W o r d
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Create a new word in the dictionary with the specified
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * ficlString, code, and flags. Does not require a NULL-terminated
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAppendWord(ficlDictionary *dictionary, ficlString name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlUnsigned8 length = (ficlUnsigned8)FICL_STRING_GET_LENGTH(name);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * NOTE: ficlDictionaryAppendString advances "here" as a side-effect.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * It must execute before word is initialized.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome nameCopy = ficlDictionaryAppendString(dictionary, name);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome word->flags = (ficlUnsigned8)(flags | FICL_WORD_SMUDGED);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Point "here" to first ficlCell of new word's param area...
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t A p p e n d W o r d
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Create a new word in the dictionary with the specified
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * name, code, and flags. Name must be NULL-terminated.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAppendPrimitive(ficlDictionary *dictionary, char *name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (ficlDictionaryAppendWord(dictionary, s, code, flags));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionarySetPrimitive(ficlDictionary *dictionary, char *name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome word = ficlDictionaryAppendPrimitive(dictionary, name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryAppendInstruction(ficlDictionary *dictionary, char *name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (ficlDictionaryAppendPrimitive(dictionary, name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (ficlPrimitive)i, (ficlUnsigned8)(FICL_WORD_INSTRUCTION | flags)));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionarySetInstruction(ficlDictionary *dictionary, char *name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (ficlDictionarySetPrimitive(dictionary, name,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (ficlPrimitive)i, (ficlUnsigned8)(FICL_WORD_INSTRUCTION | flags)));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t C e l l s A v a i l
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Returns the number of empty ficlCells left in the dictionary
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryCellsAvailable(ficlDictionary *dictionary)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (dictionary->size - ficlDictionaryCellsUsed(dictionary));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t C e l l s U s e d
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Returns the number of ficlCells consumed in the dicionary
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryCellsUsed(ficlDictionary *dictionary)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t C r e a t e
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Create and initialize a dictionary with the specified number
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * of ficlCells capacity, and no hashing (hash size == 1).
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryCreate(ficlSystem *system, unsigned size)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (ficlDictionaryCreateHashed(system, size, 1));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryCreateHashed(ficlSystem *system, unsigned size,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome nAlloc = sizeof (ficlDictionary) + (size * sizeof (ficlCell))
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome + sizeof (ficlHash) + (bucketCount - 1) * sizeof (ficlWord *);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_SYSTEM_ASSERT(system, dictionary != NULL);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t C r e a t e W o r d l i s t
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Create and initialize an anonymous wordlist
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryCreateWordlist(ficlDictionary *dictionary, int bucketCount)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome sizeof (ficlHash) + (bucketCount - 1) * sizeof (ficlWord *));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t D e l e t e
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Free all memory allocated for the given dictionary
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryDestroy(ficlDictionary *dictionary)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_DICTIONARY_ASSERT(dictionary, dictionary != NULL);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t E m p t y
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Empty the dictionary, reset its hash table, and reset its search order.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Clears and (re-)creates the hash table with the size specified by nHash.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryEmpty(ficlDictionary *dictionary, unsigned bucketCount)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome sizeof (ficlHash) + (bucketCount - 1) * sizeof (ficlWord *));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * i s A F i c l W o r d
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Vet a candidate pointer carefully to make sure
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * it's not some chunk o' inline data...
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * It has to have a name, and it has to look
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * like it's in the dictionary address range.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * NOTE: this excludes :noname words!
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryIsAWord(ficlDictionary *dictionary, ficlWord *word)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if ((((ficlInstruction)word) > ficlInstructionInvalid) &&
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (((ficlInstruction)word) < ficlInstructionLast))
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (!ficlDictionaryIncludes(dictionary, word->name))
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome !ficlDictionaryIncludes(dictionary, word->link))
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if ((word->length <= 0) || (word->name[word->length] != '\0'))
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * f i n d E n c l o s i n g W o r d
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Given a pointer to something, check to make sure it's an address in the
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * dictionary. If so, search backwards until we find something that looks
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * like a dictionary header. If successful, return the address of the
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * ficlWord found. Otherwise return NULL. nSEARCH_CELLS sets the maximum
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * neighborhood this func will search before giving up
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryFindEnclosingWord(ficlDictionary *dictionary, ficlCell *cell)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (!ficlDictionaryIncludes(dictionary, (void *)cell))
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (cell + 1 - (sizeof (ficlWord) / sizeof (ficlCell)));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t I n c l u d e s
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Returns FICL_TRUE iff the given pointer is within the address range of
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * the dictionary.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryIncludes(ficlDictionary *dictionary, void *p)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (p < (void *)(&dictionary->base + dictionary->size)));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t L o o k u p
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Find the ficlWord that matches the given name and length.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * If found, returns the word's address. Otherwise returns NULL.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Uses the search order list to search multiple wordlists.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryLookup(ficlDictionary *dictionary, ficlString name)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_DICTIONARY_ASSERT(dictionary, dictionary != NULL);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome for (i = (int)dictionary->wordlistCount - 1; (i >= 0) && (!word); --i) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * TOOLS ( "<spaces>name" -- )
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Display a human-readable representation of the named word's definition.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * The source of the representation (object-code decompilation, source
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * block, etc.) and the particular form of the display is implementation
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * ficlSeeColon (for proctologists only)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Walks a colon definition, decompiling
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * on the fly. Knows about primitive control structures.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#define FICL_TOKEN(token, description) description,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#define FICL_INSTRUCTION_TOKEN(token, description, flags) description,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionarySee(ficlDictionary *dictionary, ficlWord *word,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome for (; cell->i != ficlInstructionSemiParen; cell++) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome trace += sprintf(trace, "%3ld ", (long)(cell - param0));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome "argument %ld (%#lx)",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (long)word, (long)c.i, (unsigned long)c.u);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome "%s :: executes %s (instruction word %ld)",
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (unsigned long)c.u);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome "literal %ld (%#lx)", (long)c.i,
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (unsigned long)c.u);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (unsigned long)c.u);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (double)c.f, (unsigned long)c.u);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#endif /* FICL_WANT_FLOAT */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome /* probably not a word - punt and print value */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (unsigned long)cell->u);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t R e s e t S e a r c h O r d e r
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Initialize the dictionary search order list to sane state
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryResetSearchOrder(ficlDictionary *dictionary)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_DICTIONARY_ASSERT(dictionary, dictionary);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome dictionary->compilationWordlist = dictionary->forthWordlist;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome dictionary->wordlists[0] = dictionary->forthWordlist;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t S e t F l a g s
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Changes the flags field of the most recently defined word:
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Set all bits that are ones in the set parameter.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionarySetFlags(ficlDictionary *dictionary, ficlUnsigned8 set)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_DICTIONARY_ASSERT(dictionary, dictionary->smudge);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t C l e a r F l a g s
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Changes the flags field of the most recently defined word:
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Clear all bits that are ones in the clear parameter.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryClearFlags(ficlDictionary *dictionary, ficlUnsigned8 clear)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_DICTIONARY_ASSERT(dictionary, dictionary->smudge);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t S e t I m m e d i a t e
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Set the most recently defined word as IMMEDIATE
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionarySetImmediate(ficlDictionary *dictionary)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome FICL_DICTIONARY_ASSERT(dictionary, dictionary->smudge);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome dictionary->smudge->flags |= FICL_WORD_IMMEDIATE;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t U n s m u d g e
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Completes the definition of a word by linking it
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * into the main list
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas SoomeficlDictionaryUnsmudge(ficlDictionary *dictionary)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome ficlHash *hash = dictionary->compilationWordlist;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * :noname words never get linked into the list...
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * d i c t W h e r e
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Returns the value of the HERE pointer -- the address
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * of the next free ficlCell in the dictionary