Words can have different behaviour depending on whether we are in compilation mode or interpretation mode. The technical term for "behaviour" is semantics.
- The default interpretation semantics of a word is to run its code section.
- The default compilation semantics of a word is to append its code section to the enclosing new word being defined.
- An immediate word breaks the default compilation semantics. The word's code section is run instead of being compiled into the enclosing word.
- Some words may break both default semantics. Some words have no interpretation semantics.
In some situations, you will have to compile code into a new word without expecting it to have the default compilation semantics. There are two words to help you with this.
|Takes the following word and compiles the word's code section into the enclosing word, regardless of that word's compilation semantics.|
|Takes a given XT and compiles its code section into the enclosing word.|
: hi "Hi!" . ; : bb [compile] hi "bye!" . ; bb Hi! bye! ok see bb  HI  Literal(bye!)  . okExample 2
: aa [ ' dup compile, ] ; see aa  DUP okExample 3
COMPILE,becomes extremely useful when you only have an XT but you want to compile it into your word.
:> "I am just an XT." . ; \ puts an XT on the stack ok : bb ( xt -- ) "What's this?" . [ compile, ] ; ok bb What's this? I am just an XT. ok
Question 1In Example 1, how would it change if you used
[COMPILE]? Explain and check your answer using
postponepushes the XT of
HIto the stack and runs
COMPILE,, which takes the
HIXT on the stack and compiles it into the enclosing word. However the program is not running within a word definition, hence there is an error.
Question 2What are the compilation semantics of
HIin Example 1? Is using
POSTPONEof any value? Explain and check your answer using
HIis to append its code section to the enclosing new word being defined. Hence the word
HIwere immediate, what would your answer to Question 2 be? Explain and check your answer with
HIwere immediate, its code section will be run instead of being compiled into the enclosing word.
POSTPONEshould then be used.
Question 4What does
AAin Example 2 do? Why did we have to use the switch mode operators?
AAcompiles the code of
DUPinto its definition. It is the same as
: aa dup ;because
DUPis not immediate. The switch mode operators is needed because
COMPILE,is not immediate.
[COMPILE]in terms of
COMPILE,and other words you know except for
: [COMPILE] ' compile, ; immediate