Switching Modes

Besides using
IMMEDIATE
and
POSTPONE
to switch immediacy, you can use the words
[
and
]
(OPEN/CLOSE BRACKET) to switch modes in any word's compile time.
The word 
[
changes compilation mode into interpretation mode. The word
]
changes interpretation mode to compilation mode. An example:
		: QQ 1 2 + . [ "hello!" . cr ] ;
		
will display:
		hello! 
		ok
		
as it is being compiled.
		see QQ

		[0]	Literal<1>
		[1]	Literal<2>
		[2]	+
		[3]	.

		ok
		
Clearly, from the results of
SEE
, the words within
[
...
]
are executed at
QQ
's compile time, but are not compiled into it. In other words, the actions within
[
...
]
will not be executed when
QQ
is called.

LITERALs

Sometimes, you need to compute a value at compilation time and insert the result into the word being compiled. 
	To do this you use the word 
LITERAL
.
LITERAL
is an immediate word. It is used only inside a colon definition. At compile time, compiles a value from the stack into the definition as a literal. At run time, the value will be pushed on the stack. For example consider the following word
HEY
:

	bp
	: hey [ "Hey " "Jude!" concat ] ;
	bp
	"seeing hey ..." . cr
	see hey

	--breakpoint #1-- 
	<0> 
	
	--breakpoint #2-- 
	<1> Hey Jude! 
	seeing hey ... 
	ok
	

Clearly, 
HEY
puts the result of
concat
, "Hey Jude!", just once, on the stack at compilation time. (
SEE HEY
therefore shows that the word is empty. ) If we wanted to insert this text onto
HEY
itself, here's what we do:

	bp
	: hey [ "Hey " "Jude!" concat ] LITERAL ;
	bp
	"seeing hey ..." . cr
	see hey
	
	--breakpoint #1-- 
	<0> 
	
	--breakpoint #2-- 
	<0> 
	seeing hey ... 
	[0]	Literal(Hey Jude!)
	ok
	

Quiz

Question 1

Have you understand clearly how does
[
...
]
works? Try out the following:
		: test "test1" [ "test2" [ "test3" ] ] ;
		
What is happening on the stack before and after you run the word
TEST
? Explain clearly using
BP
.



Question 2

Write a word called 
ALLOT
that creates and initializes a data section for any word. The resulting data section should have length N and initilaized with the value 0.
ALLOT
should accept just one argument N. For example:
	
	: say-hi "jello" . ;  6 allot 
	
should create a data section for
SAY-HI
of length 6, initialized with zeros.



Question 3

Another way to create the word's data section is to do it within the word's definition like so:

	
	: say-hi 
		[ 0 , 41 , 777 , ] "yellow jello" .  ; 
	
This creates a
SAY-HI
with three elements in the data section. Note that we used the Mode Switching words
[
and
]
. Ensure that you understand and test this example thoroughly.



Question 4

What would happen if you used 
ALLOT
after this like so
	
	: say-hi 
	 	[ 0 , 41 , 777 , ] "yellow jello" . ; 43 ALLOT
	



Question 5

Would this work?

		
	: say-hi [ 43 ALLOT ] .... ; 
	



Question 6

The word 
THIS
is written as follows:
		: this latestxt postpone literal ; immediate 	
		
THIS
compiles the XT of the currently defined word as a Literal. Recall the word counting example in Postponing Actions*. Use
THIS
to make
;
count as before, but using just one re-definition instead of two.



Question 7

Use
[
and
]
to remove the need to use
THIS
as a separate word in your answer to Question 6. Hint: Why is
POSTPONE
used in
THIS
? Is it needed in Question 7?



Next: Handling Errors