Next: , Previous: Execution token, Up: Tokens for Words


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. another name of >name

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|0  ) tools-ext “name-to-interpret”

xt represents the interpretation semantics nt; returns 0 if nt has no interpretation semantics

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.

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, you can get it with latestnt, and the words above that consume nts do something reasonable for these nts.

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.