Download 1 - Forth

Transcript
Working with
Create
Does>
...
Leonard Morgenstern
Moraga, California
Forth words from the dictionary to the stack.
It has been well said that programs are not written in
Forth. Rather, Forth is extended to make a new language
specifically designed for the application at hand. An
important part of this process is the &Jining word,by which
one can combine a data structure with an action, and create
multiple instances that differ only in detail. One thinks of
a cookie-cutter: all the cookies are the same shape but have
different-colored icing.
Using ;CODE
Just as it is possible to substitute assembler for highlevel Forth by starting an ordinary definition with CODE
instead of :, one can d o the same for defining words by
substituting ;CODE for DOES>. In the alternate defmition
on Screen One, 3CONSTANT is rewritten in this way. ;CODE
is followed directly by the necessary assembler words, and
the definition is terminated by NEXT and END-CODE with
no semicolon (Line Five).
A s another example (Screen Two), we construct numbermachines. The real ones look like rubber stamps, but print
sequence numbers. Their Forth equivalent simply puts the
next number on the stack. Note that commands can
precede CREATE. We can specify that the machines reside
in a vocabulary named #MACHINES. We could make all of
them immediate by writing IMMEDIATEjust before DOES>.
The Basics
Defining words are based on the Forth construct
CREATE ... DOES>. Beginners quickly learn to apply the
method mechanically, using two familiar steps: 1) Start a
colon definition, write CREATE, and follow by the actions
that lay down data or allot RAM. 2) Write DOES> and follow
by the action to be performed on the body of the word, the
address of which has been put on the stack by DOES>.
(Experienced programmers will please forgive certain
oversimplifications.) Although the CREATE ... DOES>pair
What CREATE Does
is easy to use at this basic level, understanding the details
In the Forth-83 Standard, CREATE will "define a word
is hard because there are no fewer than three phases of
that returns the address of the next available user memory
action. Words compiled in one are executed in another.
A simple example is 3CONSTANT, which creates the six- location." Hence, if we write CREATE FOO and then exbyte analog of CONSTANT. (Screen One) It has two stack ecute FOO, an address is returned. Most existing Forths (figdiagrams; the Fist for creating an instance, and the second Forth is the important exception) follow this rule, as does
the ANSI draft standard. Differences derive from the fact
for executing it. The Fist phase is in effect when 3CONSTANT is d e f i e d (Line One). It is a colon definition and that each implementation interprets "the next available
works in the usual way; that is, : sets up a header, after memory location" in its own way. For example, in F83 the
which the CFA's of ordinary Forth words are compiled, and dctionary is confined to a 64K space, and the address
immediate words such as DOES> are executed. The pro- I returned by Foo immediately follows the CFA. In F-PC,
header and body are in separate spaces called the head
cess is ended by the semicolon.
In the second phase (Line Three), 3CONSTANT creates segment and the code segment respectively, and FOO
an instance named 3F00. The CFA's that were compiled in returns an address in the latter. The ANSI draft standard
the first phase are now executed one at a time, as follows: adds specifications as to alignment. The casual user need
CREATE picks up the next word in the input stream, which not be concerned with these details because words that
is 3F00, and makes a header from it. The commas lay allot memory, such as , (comma), C, and ALLOT itself,
down the top three words from the stack; they become the automatically d o so in the proper place, namely, at the first
body. DOES> stops the action and sets the CFA of 3F00 to available memory location.
It is worthwhile to comment here that one should not
execute the Forth words that follow it at Point A. These are
not executed until phase three, in which 3F00 is executed use 2+ to go from the code field to the body of a word. It
(Line Four); the address of its body is put on the stack, and will work in F83, but may not in other versions. Porting
the Forth words at Point A are executed, moving three from one Forth to another is never easy, and a shortcut of
I
I
Forth Dimensions
May 1992June