15. Defined words of self-made words

Whoever does not want a variety of data structures provided by the FORTH basic words, that is, they want words that define not only single-word variables and constants but also words that define a vector, a matrix, a double or a three-sphere variables. For example, suppose a person uses vectors consisting of 2 bytes of elements. It would be good if the vectors somehow would automatically know what to do if they wanted access to one of their elements.
We know about variables and constants that their value is stored in their parsers. This will not be the case with data structures of their own imagination; the elements of the vector are kept in the parsing of the word defined as vector. The vectors, similarly to the variables and constants, work equally: the code index is the same as the code field of each vector.
Definition words contain two methods:

Accordingly, the definition words are written in two actions:

<builds

(first act: we make it when defining).
When defining a word counter, it points to the beginning of the parame- ter of the word to be defined.

DOES>

(second act: this is done by the words defined).
At the beginning of the DOES> procedure, the title of the word defined is on the worm.

For example, write the word 2CONSTANT (d ---), which defines double-constant constants. When defining, the expected value in the worm is the constant value; this will be the definition of the word on the stack. So, for example, the

3.1415926 2CONSTANT PI

you will create a double-level constant called PI:

PI D.

Let's see the definition of 2CONSTANT:

: 2CONSTANT
   <BUILDS
      HERE 2!
      4 ALLOT
   DOES>
      2 @
;
(d ---)

(store the double word in the parame- ter)
(drag the dictionary pointer over)
(the parame- ter address in the parame- ter)

Let's get to the vectors! Write the word VECTOR (n ---) defining the number of elements of the vector (16 bit) waiting for the worm; in the parser of the vector, it allocates 2 bytes to each element. The words defined with the VECTOR will then calculate the title of the index element from the stack index and return it to the verme.

: VECTOR (n ---)
   <BUILDS
      2 * ALLOT
   DOES>
      SWAP
      1- (starting with 1, not 0)
      2 * +
;

Thus, for example, we define a 10-element vector:

10 VECTOR TIZ

The font of the word TIZ: (index --- element title). The vectors thus defined work fine until good indexes are given to them; it is only a little misleading, so it is easy to get rid of it in the dictionary. Precaution dictates that vectors check the index obtained and stop if it does not match their scaling. For this, we need to store the size of the vector somewhere, and since we can not hold it somewhere else, we need to use the parser as well. In this case, when the data structure parser contains several data, it is best to design the parser before writing the word (or write it down because it has something else to do with programming).

Then we will start programming:

: VECTOR
   <BUILDS
     DUP,
     2 * ALLOT
   DOES
     >> R


(store the element number of the vector)
(space for the elements)
(put the title of the parameut)
(the index will remain in the vermen )
     DUP 1 <OVER R @> OR
     IF. "Bad index!" QUIT
     ELSE 2 * R> +
     ENDIF
;

Finally, we write a defining word with which we can create text-writing words. The word will be .TEXT; When defining it, you must enter the name of the word to be written. For example the

.TEXT PLD MY SYSTEM OF KETTES, I AM THE TETTES

line with the word PLD (---); it outputs the text that we specified to it at the time of definition (up to the end of the line).
The text to be written in FORTH will be kept in the parody of the word:

: .TEXT
   <BUILDS
     1 WORD


(this is the FORTH thread just in HERE)
         (to the address, ie to the new word)
     HERE C @
     1 +
     ALLOT
   DOES
     COUNT TYPE
;
(length of text)
(along with longitude)
(dictionary pointer behind the text)
(set)

We write a word that tells you how much the length of the text that is written in a word defined with a .TEXT. The word SHOCK (--- n) can be used as follows:

SZHOSSZ PLD.
: HOWTO (--- n)
   -FIND (find the word in the dictionary)
   IF DROP
      2+
      C @
   ELSE. "No such" QUIT
   ENDIF
;


The paramex given from -FIND is not the same as the word found on the word; the first two bytes of the parser are used by the FORTH interpreter.

Examples

15.1. Write a 2VARIABLE (d ---) definition word that defines double-blind variables with the initial value of the bug. The variables put the starting address of their parser on the stack.

: 2VARIABLE (d ---)
   <BUILDS 2! 4 ALLOT DOES>;