[

Interpretation:

Interpretation semantics for this word are undefined.

Compilation:

Perform the execution semantics given below.

Execution:

( -- )

Enter interpretation state. [ is an immediate word.

See:

Rationale:

Typical use: : X ... [ 4321 ] LITERAL ... ;

Testing:

T{ : GC3 [ GC1 ] LITERAL ; -> }T
T{ GC3 -> 58 }T

ContributeContributions

ruvavatar of ruv Why "[" is specified using immediacy?Request for clarification2021-07-14 17:47:02

An interesting thing that the word [ is the only immediate word for which interpretation semantics are undefined.

What is a rationale that this word is specified as an immediate word?

It seems, the specification can be simplified by removing the Execution section and making the Compilation section as:

Compilation:
( -- )
Enter interpretation state.

JeanJonethalavatar of JeanJonethal

"[" is used to switch from compiling context to interpreter mode. This allows inlining constants or code into word definition : date-created [ 20210718 ] LITERAL ;
depending on threading model one could create some special words calling machine code instructions : my-special-op [ machine-code param + , ] ; so the code between "[" and "]" is executed during compile-time from user perspective. if "[" was not immediate - forth state could not be switched from compilation mode to interpreter mode. Compiler would compile a call to [ into the code.

AntonErtlavatar of AntonErtl

The question is: Why are the interpretation semantics undefined? Is there any standard system where [ has no interpretation semantics or where they are different from the compilation semantics?

ruvavatar of ruv

It seems, I have realized why [ was specified as immediate: since this word had "I" (immediate) attribute in Forth-83, and this property was just inherited for this word in Forth-94. But it's unnecessary even in the current wording.


@JeanJonethal writes:

if "[" was not immediate - forth state could not be switched from compilation mode to interpreter mode.

It's wrong. For example, take a look at the IF word — immediacy isn't specified for this word, and it is implemented as non immediate (in the standard notion, it can be tested) in many Forth systems; although, it may be implemented as immediate. See also A.6.1.1550: "POSTPONE allowed de-specification of immediacy or non-immediacy for all but a few Forth words whose behavior must be STATE-independent". And indeed, immediacy should not be specified for a word having a STATE-dependent behavior (or that is allowed to have a STATE-dependent behavior, such as a word having undefined interpretation semantics).

Moreover, immediacy cannot be tested by a standard program for a word with undefined interpretation semantics, since the execution token cannot be obtained for such a word. So there is no any sense to mention immediacy for [ even in the current wording.


@AntonErtl writes:

The question is: Why are the interpretation semantics undefined?

It's obvious: in a cmForth-like Forth system this word can be unfound in interpretation state, and it was actually defined only in the COMPILER wordlist in cmForth. Therefore, to support this model, interpretation semantics should be undefined for this word by the standard.

Is there any standard system where [ has no interpretation semantics or where they are different from the compilation semantics?

Strictly speaking, in real Forth systems "no interpretation semantics" is a nonsense. Since even if a Forth system raises an error in interpretation state for some word, then it is a behavior that constitutes the interpretation semantics for this word. But it's another topic.

In many real Forth systems the actual interpretation semantics for [ are to do nothing. But the compilation semantics for this word are to enter interpretation state. Hence, they are different. See more reasonings in my post "About POSTPONE semantics in edge cases" on ForthHub.

Reply New Version