You can change the compilation semantics of a word to be the same as the interpretation semantics with
immediate
( – ) core “immediate”
Change the compilation semantics of a word to be the same as its interpretation semantics.
A contrived example:
: [foo] ." foo" ; immediate : bar [foo] ; \ prints "foo" bar \ no output
The immediate
causes [foo]
to perform the interpretation
semantics during the definition of bar
rather than compiling
them. A convention sometimes (but not always) used for immediate
words is to have their names in brackets, e.g. [']
.
A common use of immediate
is to define macros (see Macros).
The text interpretation of a macro in interpret state is often a
mistake, so you can turn the macro into a compile-only
word
with
compile-only
( – ) gforth-0.2 “compile-only”
Mark the last definition as compile-only; as a result, the text
interpreter and '
will warn when they encounter such a word.
Example:
: endif postpone then ; immediate compile-only : foo if ." true" endif ; endif \ "warning: endif is compile-only"
The warning is followed by a stack underflow error because then
wants to consume an orig (see Arbitrary control structures).
Note that compiling code while the text interpreter is in interpret
state is not a problem in itself, even if a number of words are marked
compile-only
. A more serious problem is compiling code if the
current definition is not an unfinished colon definition: there is no
way to run the resulting code. Gforth warns about that even if a word
is not marked compile-only or if you text-interpret it in compile
state:
: compile-+ postpone + ; : foo [ compile-+ ] ; \ no warning; interpretation semantics of compile-+ compile-+ \ warning: Compiling outside a definition if \ warning: IF is compile-only \ warning: Compiling outside a definition compile-+ \ warning: Compiling outside a definition then \ warning: THEN is compile-only ] if \ warning: Compiling outside a definition + \ warning: Compiling outside a definition then [
Note thet switching to compile state in the last four lines silences the “is compile-only” warnings, because in these lines the compilation semantics of the words is performed.
Why does then
not produce “Compiling outside a definition”
warnings in the example above? Then
does not generate any code
by itself, it just changes the target of the code compiled by the
matching if
or ahead
.
restrict
( – ) gforth-0.2 “restrict”
A synonym for compile-only