# 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:

• What should be done when defining; essentially the design of the parable of the word to be defined.
For variables and constants it is enough to store the value of the vermin in the parsing; increase the dictionary dial by two to keep the stored 16-bit value in a "protected" area.
• What will be the definable words: the act of the code where the metrics show in the code field of the words created with the definition word.
For variables it is enough to place the title of the parser on the stack; and the 16 bit content of the parsers is placed on the stack.

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           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 ---)           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    > 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 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

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 ---)    ;

 file: /Techref/language/FORTH/z80fig-Forth1_1g_files/self_made_words.htm, 18KB, , updated: 2018/11/8 20:42, local time: 2024/5/30 03:43, TOP NEW HELP FIND:  3.235.20.185:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! 15. Defined words of self-made words

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

 PICList 2024 contributors: o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr.

.