6.18 Word Lists

A wordlist is a list of named words; you can add new words and look up words by name (and you can remove words in a restricted way with markers). Every named (and revealed) word is in one wordlist.

The text interpreter searches the wordlists present in the search order (a stack of wordlists), from the top to the bottom. Within each wordlist, the search starts conceptually at the newest word; i.e., if two words in a wordlist have the same name, the newer word is found.

New words are added to the compilation wordlist (aka current wordlist).

A word list is identified by a cell-sized word list identifier (wid) in much the same way as a file is identified by a file handle. The numerical value of the wid has no (portable) meaning, and might change from session to session.

The Standard Forth “Search order” word set is intended to provide a set of low-level tools that allow various different schemes to be implemented. Gforth also provides vocabulary, a traditional Forth word. compat/vocabulary.fs provides an implementation in Standard Forth.

forth-wordlist ( – wid  ) search “forth-wordlist”

Constantwid identifies the word list that includes all of the standard words provided by Gforth. When Gforth is invoked, this word list is the compilation word list and is at the top of the search order.

definitions ( ) search “definitions”

Set the compilation word list to be the same as the word list that is currently at the top of the search order.

get-current ( – wid  ) search “get-current”

wid is the identifier of the current compilation word list.

set-current ( wid –  ) search “set-current”

Set the compilation word list to the word list identified by wid.

in-wordlist ( wordlist "defining-word" –  ) gforth-experimental “in-wordlist”

execute defining-word with wordlist as one-shot current directory. Example: gui-wordlist in-wordlist : init-gl ... ; will define init-gl in the gui-wordlist wordlist.

in ( "voc" "defining-word" –  ) gforth-experimental “in”

execute defining-word with voc as one-shot current directory. Example: in gui : init-gl ... ; will define init-gl in the gui vocabulary.

get-order ( – widn .. wid1 n  ) search “get-order”

Copy the search order to the data stack. The current search order has n entries, of which wid1 represents the wordlist that is searched first (the word list at the top of the search order) and widn represents the wordlist that is searched last.

set-order ( widn .. wid1 n –  ) search “set-order”

If n=0, empty the search order. If n=-1, set the search order to the implementation-defined minimum search order (for Gforth, this is the word list Root). Otherwise, replace the existing search order with the n wid entries such that wid1 represents the word list that will be searched first and widn represents the word list that will be searched last.

wordlist ( – wid  ) search “wordlist”

Create a new, empty word list represented by wid.

table ( – wid  ) gforth-0.2 “table”

Create a lookup table (case-sensitive, no warnings).

cs-wordlist ( – wid  ) gforth-1.0 “cs-wordlist”

Create a case-sensitive wordlist.

cs-vocabulary ( "name" –  ) gforth-1.0 “cs-vocabulary”

Create a case-sensitive vocabulary

>order ( wid –  ) gforth-0.5 “to-order”

Push wid on the search order.

previous ( ) search-ext “previous”

Drop the wordlist at the top of the search order.

also ( ) search-ext “also”

Like DUP for the search order. Usually used before a vocabulary (e.g., also Forth); the combined effect is to push the wordlist represented by the vocabulary on the search order.

Forth ( ) search-ext “Forth”

Replace the wid at the top of the search order with the wid associated with the word list forth-wordlist.

Only ( ) search-ext “Only”

Set the search order to the implementation-defined minimum search order (for Gforth, this is the word list Root).

order ( ) search-ext “order”

Print the search order and the compilation word list. The word lists are printed in the order in which they are searched (which is reversed with respect to the conventional way of displaying stacks). The compilation word list is displayed last.

.voc ( wid –  ) gforth-0.2 “dot-voc”

print the name of the wordlist represented by wid. Can only print names defined with vocabulary or wordlist constant, otherwise prints ‘address’.

find ( c-addr – xt +-1 | c-addr 0  ) core,search “find”

We recommend to use find-name instead of find. Search all word lists in the current search order for the definition named by the counted string at c-addr. If the definition is not found, return 0. If the definition is found return 1 (if the definition has non-default compilation semantics) or -1 (if the definition has default compilation semantics). The xt returned in interpret state represents the interpretation semantics. The xt returned in compile state represented either the compilation semantics (for non-default compilation semantics) or the run-time semantics that the compilation semantics would compile, (for default compilation semantics). The Forth-2012 standard does not specify clearly what the returned xt represents (and also talks about immediacy instead of non-default compilation semantics), so this word is questionable in portable programs. If non-portability is ok, find-name and friends are better (see Name token).

search-wordlist ( c-addr count wid – 0 | xt +-1  ) search “search-wordlist”

Search the word list identified by wid for the definition named by the string at c-addr count. If the definition is not found, return 0. If the definition is found return 1 (if the definition is immediate) or -1 (if the definition is not immediate) together with the xt. In Gforth, the xt returned represents the interpretation semantics. Forth-2012 does not specify clearly what xt represents.

words ( ) tools “words”

Display a list of all of the definitions in the word list at the top of the search order.

vlist ( ) gforth-0.2 “vlist”

Old (pre-Forth-83) name for WORDS.

wordlist-words ( wid –  ) gforth-0.6 “wordlist-words”

Display the contents of the wordlist wid.

mwords ( ["pattern"] –  ) gforth-1.0 “mwords”

list all words matching the optional parameter pattern; if none, all words match. Words are listed old to new. Pattern match like search (default), you can switch to globbing with ' mword-filename-match is mword-match.

Root ( ) gforth-0.2 “Root”

Add the root wordlist to the search order stack. This vocabulary makes up the minimum search order and contains only a search-order words.

Vocabulary ( "name" –  ) gforth-0.2 “Vocabulary”

Create a definition "name" and associate a new word list with it. The run-time effect of "name" is to replace the wid at the top of the search order with the wid associated with the new word list.

seal ( ) gforth-0.2 “seal”

Remove all word lists from the search order stack other than the word list that is currently on the top of the search order stack.

vocs ( ) gforth-0.2 “vocs”

List vocabularies and wordlists defined in the system.

current ( – addr  ) gforth-0.2 “current”

Variable – holds the wid of the compilation word list.

context ( – addr  ) gforth-0.2 “context”

context @ is the wid of the word list at the top of the search order.

map-vocs ( ... xt – ...  ) gforth-1.0 “map-vocs”

Perform xt ( ... wid – ... ) for all wordlists (including tables and cs-wordlists) in the system.