6.12.2 Name token

Gforth represents named words by the name token, (nt). The name token is a cell-sized abstract data type that occurs as argument or result of the words below.

Since Gforth 1.0, for most words the concrete implementation of their nt is the same address as its xt (this is the primary nt for the xt). However, synonyms, aliases, and words defined with interpret/compile: get their xt from another word, but still have an nt of their own (that is different from the xt). Therefore, you cannot use xts and nts interchangeably, even if you are prepared to write code specific to Gforth 1.0. You do not get these alternate nts for the xt with >name.

You get the nt of a word x with ``x (since Gforth 1.0) or with

find-name ( c-addr u – nt | 0  ) gforth-0.2 “find-name”

Find the name c-addr u in the current search order. Return its nt, if found, otherwise 0.

find-name-in ( c-addr u wid – nt | 0  ) gforth-1.0 “find-name-in”

search the word list identified by wid for the definition named by the string at c-addr u. Return its nt, if found, otherwise 0.

latest ( – nt  ) gforth-0.6 “latest”

nt is the name token of the last word defined; it is 0 if the last word has no name.

latestnt ( – nt  ) gforth-1.0 “latestnt”

nt is the name token of the last word defined.

>name ( xt – nt|0  ) gforth-0.2 “to-name”

The primary name token nt of the word represented by xt. Returns 0 if xt is not an xt (using a heuristic check that has a small chance of misidentifying a non-xt as xt), or if the primary nt is of an unnamed word. As of Gforth 1.0, every xt has a primary nt, but other named words may have the same interpretation sematics xt.

xt>name ( xt – nt  ) gforth-1.0 “xt-to-name”

Produces the primary nt for an xt. If xt is not an xt, nt is not guaranteed to be an nt.

You can get all the nts in a wordlist with

traverse-wordlist ( ... xt wid – ...  ) tools-ext “traverse-wordlist”

perform xt ( ... nt – f ... ) once for every word nt in the wordlist wid, until f is false or the wordlist is exhausted. xt is free to use the stack underneath.

You can use the nt to access the interpretation and compilation semantics of a word, its name, and the next word in the wordlist:

name>interpret ( nt – xt  ) tools-ext “name-to-interpret”

xt represents the interpretation semantics of the word nt.

name>compile ( nt – w xt  ) tools-ext “name-to-compile”

w xt is the compilation token for the word nt.

name>string ( nt – addr u  ) tools-ext “name-to-string”

addr count is the name of the word represented by nt.

id. ( nt –  ) gforth-0.6 “i-d-dot”

Print the name of the word represented by nt.

.id ( nt –  ) gforth-0.6 “dot-i-d”

F83 name for id..

name>link ( nt1 – nt2 / 0  ) gforth-1.0 “name-to-link”

For a word nt1, returns the previous word nt2 in the same wordlist, or 0 if there is no previous word.

A nameless word usually has no interpretation nor compilation semantics, no name, and it’s not in a wordlist. But in Gforth (since 1.0) all words are equal, so even nameless words have an nt (but they are in no wordlist). You can get that nt with latestnt, and the words above that consume nts do something reasonable for these nts.

As a usage example, the following code lists all the words in forth-wordlist with non-default compilation semantics:

: ndcs-words ( wid -- )
  [: dup name>compile ['] compile, <> if over id. then 2drop true ;]
  swap traverse-wordlist ;

forth-wordlist ndcs-words

This code assumes that a word has default compilation semantics if the xt part of its compilation token is the xt of compile,.

The closest thing to the nt in older Forth systems is the name field address (NFA), but there are significant differences: in older Forth systems each word had a unique NFA, LFA, CFA and PFA (in this order, or LFA, NFA, CFA, PFA) and there were words for getting from one to the next. In contrast, in Gforth several nts can get the same xt from name>interpret xt; there is a link field in the structure identified by the name token, but searching usually uses a hash table external to these structures; the name in Gforth has a cell-wide count-and-flags field, and the nt is not implemented as the address of that count field.