An execution token (xt) represents some behaviour of a word.
You can use execute
to invoke the behaviour represented by the
xt and compile,
(see Macros) to compile it into the current
definition. Other uses include deferred words (see Deferred Words).
In particular, there is the execution token of a word that represents its interpretation semantics aka execution semantics.24
For a named word x, you can use `x
to get its execution
token:
5 `. ( n xt ) execute ( ) \ execute the xt (i.e., ".") : foo `. execute ; 5 foo
However, the `
prefix is a Gforth extension, so you may prefer
to use the standard Forth words:
'
( "name" – xt ) core “tick”
xt represents name’s interpretation
semantics. Perform -14 throw
if the word has no
interpretation semantics.
[']
( compilation. "name" – ; run-time. – xt ) core “bracket-tick”
xt represents name’s interpretation
semantics. Perform -14 throw
if the word has no
interpretation semantics.
These are parsing words (whereas `x
is treated as a literal
by a recognizer), and you may find the behaviour in interpreted and
compiled code unintuitive:
5 ' . ( n xt ) execute ( ) \ execute the xt of . \ does not work as intended: \ : foo ' . ; \ 5 foo execute \ instead: : foo ['] . ; 5 foo execute \ execute the xt of . \ Usage of ' in colon definition: : bar ' execute ; 5 bar . \ execute the xt of .
'
parses at run-time, so if you put it in a colon definition,
as in bar
, it does not consume the next word in the colon
definition, but the next word at run-time (i.e., the .
in the
invocation of bar
). If you want to put a literal xt in a colon
definition without writing `x
, write ['] x
.
Gforth’s `x
, '
and [']
warn when you use them
on compile-only words, because such usage may be non-portable between
different Forth systems.
You can avoid that warning as well as the portability problems by defining an immediate variant of the word, e.g.:
: if postpone if ; immediate : test [ ' if execute ] ." test" then ;
The resulting execution token performs the compilation semantics of
if
when execute
d.
Another way to get an xt is :noname
or latestxt
(see Anonymous Definitions). For anonymous words this gives an xt
for the only behaviour the word has (the execution semantics), but you
can also use it after defining a named word to get its xt.
:noname ." hello" ; execute
An xt occupies one cell and can be manipulated like any other cell.
In Standard Forth the xt is just an abstract data type (i.e., defined by the operations that produce or consume it). The concrete implementation (since Gforth 1.0) is the body address (for old hands: PFA) of the word; in Gforth 0.7 and earlier, the xt was implemented as code field addres (CFA, 2 cells before the PFA).
execute
( xt – ) core “execute”
Perform the semantics represented by the execution token, xt.
execute-exit
( compilation – ; run-time xt nest-sys – ) gforth-1.0 “execute-exit”
Execute xt
and return from the current definition, in a
tail-call-optimized way: The return address nest-sys
and
the locals are deallocated before executing xt
.
perform
( a-addr – ) gforth-0.2 “perform”
@ execute
.
Noop
is sometimes used to have a placeholder execution token:
noop
( – ) gforth-0.2 “noop”
The Forth standard has words with undefined
interpretation semantics (e.g., r@
) and words without defined
execution semantics (e.g., s"
) and words with neither (e.g.,
if
), but in cases where both interpretation and execution
semantics are defined, they are the same; so we treat them as being
the same.