16.6.1.2192 SEARCH-WORDLIST SEARCH

( c-addr u wid -- 0 | xt 1 | xt -1 )

Find the definition identified by the string c-addr u in the word list identified by wid. If the definition is not found, return zero. If the definition is found, return its execution token xt and one (1) if the definition is immediate, minus-one (-1) otherwise.

See:

Rationale:

When SEARCH-WORDLIST fails to find the word, it does not return the string, unlike FIND. This is in accordance with the general principle that Forth words consume their arguments.

Testing:

ONLY FORTH DEFINITIONS
VARIABLE xt ' DUP xt !
VARIABLE xti ' .( xti ! \ Immediate word

T{ S" DUP" wid1 @ SEARCH-WORDLIST -> xt  @ -1 }T
T{ S" .("  wid1 @ SEARCH-WORDLIST -> xti @  1 }T
T{ S" DUP" wid2 @ SEARCH-WORDLIST ->        0 }T

ContributeContributions

lmravatar of lmr [318] NDCS xtRequest for clarification2023-12-17 18:15:48

When searching for a word with NDCS, what XT should be returned? An xt dependent on STATE at the time of the call as in the spec for FIND, a state-smart xt...? I'm surprised no one asked while FIND has lots of comments

AntonErtlavatar of AntonErtl

One interesting difference between FIND and SEARCH-WORDLIST is that FIND actually mentions that a different result can be returned depending on STATE while SEARCH-WORDLIST does not. Therefore Gforth always returns an xt representing the interpretation semantics (or an xt that throws -14 when executed if the word does not have interpretation semantics in Gforth). Nobody has complained about that yet, indicating that people probably don't search for words without interpretation semantics or words with non-default compilation semantics with SEARCH-WORDLIST.

There are ongoing discussions about topics involving semantics, xts and related topics, and they are unlikely to be resolved soon, so I doubt you will get a definite answer to your request soon.

ruvavatar of ruv

When searching for a word with NDCS, what XT should be returned?

NDCS means "Non-Default Compilation Semantics". Immediate words (in the normative sense) are NDCS words, but an NDCS word can be not immediate (in the normative sense).

At the moment my position is as follows.

Meaning of xt

If a word is found by search-wordlist, the returned xt identifies the execution semantics for the word (standard or system-specific), and it is the same xt regardless of STATE.

Ambiguous conditions

An ambiguous condition exists if interpretation semantics for the word are not defined by the standard and xt is executed (by any means).

An ambiguous condition exists if execution semantics for the word are not defined by the standard and xt is executed in compilation state.

Meaning of the code (the top value)

When a word is found, the top value shall be -1 if the word is an ordinary word (i.e., it has default interpretation semantics and default compilation semantics), and it shall be 1 otherwise.

Thus, the top value is 1 if the found word is an NDCS word. This will not have any unexpected effects concerning immediacy due to the mentioned ambiguous conditions.

Edge cases explanation

If interpretation semantics or execution semantics are not defined by the standard, xt identifies the system-specific execution semantics for the word with the following constrain: if this xt is executed in interpretation state, the interpretation semantics for the word (the standard behavior, or system-specific if not defined by the standard) shall be performed.

For words like to and s", for which interpretation semantics are defined, and execution semantics are not defined, the returned xt, when executed, shall perform the corresponding interpretation semantics in interpretation state regardless of implementation details, and it is not allowed to perform this xt in compilation state because the behavior depends on implementation details.

Ignoring an existing word

Probably, it is conceivable if search-wordlist returns 0 for a word for which interpretation semantics are not defined by the standard — since a standard program cannot execute the returned xt anyway; examples of such words are if, >r, etc.

But a more reliable option is to return an xt that simply throws an exception when executed (or perform a more useful system-defined behavior, if any — see A.3.4.3.2 Interpretation semantics).

ruvavatar of ruv

One important thing is that search-wordlist provides information whether the found word is an ordinary word (or implemented as an ordinary word), or not.

The later standardized words find-name-in, name>compile, name>interpret don't allow to obtain this information at the moment.

So, search-wordlist cannot be implemented via these words.

Reply New Version