Compiling Programmatically

Semantics

Words can have different behaviour depending on whether we are in compilation mode or interpretation mode. The technical term for "behaviour" is semantics.

Compiling

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.

Word Action
[COMPILE]
Takes the following word and compiles the word's code section into the enclosing word, regardless of that word's compilation semantics.
COMPILE,
Takes a given XT and compiles its code section into the enclosing word.

Examples

Example 1
	
	: hi "Hi!" . ;
	: bb [compile] hi "bye!" . ;

	bb
	Hi! bye! ok
	
	see bb
	[0] HI
	[1] Literal(bye!)
	[2] .
	ok
	
Example 2
	
	: aa [ ' dup compile, ] ;

	see aa
	[0] DUP
	ok
	
Example 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
	

Quiz

Question 1

 In Example 1, how would it change if you used 
POSTPONE
instead of
[COMPILE]
? Explain and check your answer using
SEE
.



Question 2

 What are the compilation semantics of 
HI
in Example 1? Is using
[COMPILE]
or
POSTPONE
of any value? Explain and check your answer using
SEE
.



Question 3

 If 
HI
were immediate, what would your answer to Question 2 be? Explain and check your answer with
SEE
.



Question 4

 What does 
AA
in Example 2 do? Why did we have to use the switch mode operators?



Question 5

 Write 
[COMPILE]
in terms of
COMPILE,
and other words you know except for
POSTPONE
or
`
.



Next: Application: Word Usage Counts