6.15.2 Name token

A name token (nt) represents a word, primarily a named word, but in Gforth since 1.0 unnamed words have a name token, too.

The name token is a cell-sized abstract data type that occurs as argument or result of the words below.

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”

Find the name c-addr u in the word list wid. Return its nt, if found, otherwise 0.

latest ( – nt  ) gforth-0.6 “latest”

nt is the name token of the last word defined in the current section. nt is 0 if the last word has no name.

latestnt ( – nt  ) gforth-1.0 “latestnt”

nt is the name token of the most recent word (named or unnamed) defined in the current section.

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

nt is the primary name token 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 (before Gforth 1.0) if the primary nt is of an unnamed word. As of Gforth 1.0, every xt has a primary nt. Several words can have the same xt, but only one of them has the primary nt of that 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..

compile-only? ( nt – flag  ) gforth-1.0 “compile-only?”

true if nt is marked as compile-only.

obsolete? ( nt – flag  ) gforth-1.0 “obsolete?”

true if nt is obsolete, i.e., will be removed in a future version of Gforth.

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.

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

: 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,.

Since Gforth 1.0 (but not in earlier versions or many other Forth systems), nameless words (see Anonymous Definitions) have nts, compilation semantics, and name>string works on them (producing a zero-length name). They are not in a wordlist, however. You can get the nt of a nameless word with latestnt.

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.

The closest thing to the nt in classic Forth systems like fig-Forth is the name field address (NFA), but there are significant differences: in older Forth systems each word has a unique NFA, LFA, CFA and PFA (in this order, or LFA, NFA, CFA, PFA) and there are words for getting from one to the next. By contrast, in Gforth in general there is an n:1 relation between name tokens and the xt representing interpretation semantics; i.e., when you pass different nts to name>interpret, the result may be the same xt.

Morover, all of the header fields of the old system correspond to fields in Gforth, but Gforth 1.0 has a few additional ones (see Header fields). One difference is that the name field usually points to the start of the header, whereas the nt in Gforth 1.0 points to the body (and header fields are accessed with a negative offset).