The Data Section
In addition to a code section, every word also has an optional data section. You can think of a word's data section as a list of slots, in which you can stuff various items: integers, reals, text, XTs, etc.
The list of slots is created right after the word is defined using the word
,known as COMMA. COMMA pops the top item on the stack and inserts it into the last defined word's data section. This of course extends the length of the data section by one item. As an example, the program:
: hello "how are you?" . ; 41 , 34.0 , "Yo" ,creates the word
HELLOwith 3 slots in its data section. These slots contain the items 41 (integer), 34.0 (real) and the text "Yo" on the last slot.
This process of creating data section slots using COMMA is known as initialization.
Fetch and Store
After initialization, you will often want to store and fetch data from a Word's data section:
Word | Action |
---|---|
@(Called FETCH). |
Fetches the data from a given Word. There are two forms: @ ( xt -- * )and @ ( xt n -- * )In the first form, the item in the first slot is fetched and pushed onto the stack. In the second form, n is the slot index from which to retrieve the item. The first slot has index 0, the second slot has index 1 and so on. As an example, to retrieve the second item from the word HELLO(defined above): ' hello 1 @ .Recall the index 1 represents the second item on HELLO's data section. |
? |
Print the first item (index 0): ? ( xt -- )For example, ' hello @ .is the same as ' hello ?. |
!(Called STORE). |
STORE replaces data into an existing slot. This has also two forms: ! ( * xt -- )and ! ( * xt n -- )In the first form, the item in the first slot is replaced by the item on the stack. In the second form, the item at the given index n is replaced by what's on the stack. Carrying on the previous example, "gibraltar" ' hello !will place the text "gibraltar" on the first slot on the word HELLO. |
+! |
Add to the first item (index 0): +! ( * xt -- )For example, ' hello @ 3 + ' hello !is the same as 3 ' hello +!. |
Quiz
Question 1
Test out the programs above. Verify that you can retrieve the first item onHELLOafter it has been replaced with the text "gibraltar".
: hello "how are you?" . ; 41 , 34.0 , "Yo" , ' hello @ . \ 41 "gibraltar" ' hello ! ' hello @ . \ gibraltar
Question 2
Write a program with two words:
- A word called
REF
that has no action but has one data section slot. Initialize this slot to zero. - A word called
COUNTER
that displays the number of times it has been executed.
COUNTERneeds to use
REF.
Remember to test your answer.
: ref ; 0 , : counter ['] ref @ 1+ dup ['] ref ! . ; counter \ 1 counter \ 2 counter \ 3 counter \ 4 counter \ 5
Question 3
Building on your answer in Question 2, create a new wordINCthat increments the slot in
REF. How can your word
COUNTERbe amended to use
INC?
As always, remember to test your answer!
: ref ; 0 , : inc ['] ref @ 1+ ['] ref ! ; : counter inc ['] ref ? ; counter \ 1 counter \ 2 counter \ 3 counter \ 4 counter \ 5
Question 4*
Define a word::that can be used to simultaneously define new words and create their data section like so:
32 :: xx "hello!" . ;would create the word
XXand initialize its data section to be a 32 element array.
As always, remember to test your answer!
: :: : 0 DO 0 , LOOP ; : test ( n xt -- ) { xt } 0 do xt i ? loop ; 5 :: xx "hello!" . ; 5 ' xx test \ 0 0 0 0 0
Bonus Question*
What does this program do?
: : : 0 , ;
Build a program to test your answer.
: : \ (1) : \ (2) 0 , \ (3) ; : greet "hey" . cr ; ' greet ? \ 0(1) Redefine
:
(2) Use previous definition of
:
(3) In addition, add 0 to the data section of the word that are defined using the new
:
For example,
greetis defined using the new
:so
greethas a data element 0.