Next: , Previous: , Up: Tokens for Words   [Contents][Index]


5.11.1 Execution token

An execution token (XT) represents some behaviour of a word. You can use execute to invoke this behaviour.

You can use ' to get an execution token that represents the interpretation semantics of a named word:

5 ' .   ( n xt ) 
execute ( )      \ execute the xt (i.e., ".")
' ( "name" – xt  ) core “tick”

xt represents name’s interpretation semantics. Perform -14 throw if the word has no interpretation semantics.

' parses at run-time; there is also a word ['] that parses when it is compiled, and compiles the resulting XT:

: foo ['] . execute ;
5 foo
: bar ' execute ; \ by contrast,
5 bar .           \ ' parses "." when bar executes
['] ( compilation. "name" – ; run-time. – xt  ) core “bracket-tick”

xt represents name’s interpretation semantics. Perform -14 throw if the word has no interpretation semantics.

If you want the execution token of word, write ['] word in compiled code and ' word in interpreted code. Gforth’s ' and ['] warns 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 executed.

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). For named words, latestxt produces an XT for the same behaviour it would produce if the word was defined anonymously.

: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). For old hands: In Gforth, the XT is implemented as a code field address (CFA).

execute ( xt – ) core “execute”

Perform the semantics represented by the execution token, xt.

perform ( a-addr – ) gforth-0.4 “perform”

@ execute.


Next: Compilation token, Previous: Tokens for Words, Up: Tokens for Words   [Contents][Index]