-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathappendixa.tex
More file actions
165 lines (146 loc) · 6.16 KB
/
appendixa.tex
File metadata and controls
165 lines (146 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
%% Thinking Forth
%% Copyright (C) 2004 Leo Brodie
%% Initial transcription by Ed Beroset
%%
%% Chapter: Appendix A, Overview of \Forth{} (For Newcomers)
\appendix{}
\Chapmark{A}
\chapter{Overview of~\Forth{} (For~Newcomers)}
\section{The Dictionary}%
\index{D!Dictionary:!defined|(}%
\index{F!forth@\Forth{}!overview of|(}%
\expandafter\initialb\Forth{} is expressed in words (and numbers) and is separated by spaces:
\begin{Code}
HAND OPEN ARM LOWER HAND CLOSE ARM RAISE
\end{Code}
Such commands may be typed directly from the keyboard, or edited onto
mass storage then ``\forthb{LOAD}''ed.
\index{D!Defining words:!procedure|(}
All words, whether included with the system or user-defined, exist in the
``dictionary,'' a linked list. A ``defining word'' is used to add new
names to the dictionary. One defining word is \forthb{:} (pronounced
``colon''), which is used to define a new word in terms of previously
defined words. Here is how one might define a new word called \forth{LIFT}:
\begin{Code}
: LIFT HAND OPEN ARM LOWER HAND CLOSE ARM RAISE ;
\end{Code}
The \forthb{;} terminates the definition. The new word \forth{LIFT} may
now be used instead of the long sequence of words that comprise its
definition.
\Forth{} words can be nested like this indefinitely. Writing a
\Forth{} application consists of building increasingly powerful definitions,
such as this one, in terms of previously defined ones.
Another defining word is \forthb{CODE},\index{C!CODE}
which is used in place of colon to define a command in terms of machine
instructions for the native processor. Words defined with \forthb{CODE}
are indistinguishable to the user from words defined with colon.
\forthb{CODE} definitions are needed only for the most time-critical
portions of an applications, if at all.
\index{D!Defining words:!procedure|)}
\section{Data Structures}%
\index{D!Data structures:!operators}%
Still another defining word is \forthb{CONSTANT},\index{C!CONSTANT} which
is used like this:
\begin{Code}
17 Constant SEVENTEEN
\end{Code}
The new word \forth{SEVENTEEN} can now be used in place of the actual
number 17.
{\sloppy
The defining word \forth{VARIABLE}\index{V!VARIABLE} creates a location
for temporary data. \forth{VARIABLE} is used like this:
\begin{Code}
Variable BANANAS
\end{Code}
This reserves a location which is identified by the name \forth{BANANAS}.}
Fetching the contents of this location is the job of the word \forthb{@}
(pronounced ``fetch''). For instance,
\begin{Code}
BANANAS @
\end{Code}
fetches the contents of the variable \forth{BANANAS}. Its counterpart is
\forthb{!} (pronounced ``store''), which stores a value into the location,
as in:
\begin{Code}
100 BANANAS !
\end{Code}
\Forth{} also provides a word to increment the current value by the given
value; for instance, the phrase
\begin{Code}
2 BANANAS +!
\end{Code}
increments the count by two, making it 102.
\Forth{} provides many other
data structure operators\index{D!Data structures:!operators}, but more
importantly, it provides the tools necessary for the programmer to
create any type of data structure needed for the application.%
\index{D!Dictionary:!defined|)}%
\section{The Stack}
\index{D!Data stacks!defined|(}
In \Forth{}, variables and arrays are used for saving values that may be
required by many other routines and/or at unpredictable times. They are
\emph{not} used for the local passing of data between the definitions.
For this, \Forth{} employs a much simpler mechanism: the data stack.
When you type a number, it goes on the stack. When you invoke a word
which has numeric input, it will take it from the stack. Thus the phrase
\begin{Code}
17 spaces
\end{Code}
will display seventeen blanks on the current output device. ``17'' pushes
the binary value 17 onto the stack; the word \forthb{SPACES} consumes it.
A constant also pushes its value onto the stack; thus the phrase:
\begin{Code}
SEVENTEEN spaces
\end{Code}
has the same effect.
The stack operates on a ``last-in, first-out'' (LIFO) basis. This means
that data can be passed between words in an orderly, modular way,
consistent with the nesting of colon definitions.
For instance, a definition called \forth{GRID} might invoke the phrase
\forth{17 SPACES}. This temporary activity on the stack will be
transparent to any other definition that invokes \forth{GRID} because the
value placed on the stack is removed before the definition of \forth{GRID}
ends. The calling definition might have placed some numbers of its own on
the stack prior to calling \forth{GRID}. These will remain on the stack,
unharmed, until \forth{GRID} has been executed and the calling definition
continues.
\index{D!Data stacks!defined|)}
\section{Control Structures}
\Forth{} provides all the control structures%
\index{C!Control structures:!defined}
needed for structured, GOTO-less programming.
The syntax of the \forthb{IF THEN} construct is as follows:
\begin{Code}
... ( flag ) IF KNOCK THEN OPEN ...
\end{Code}
The ``flag''\index{F!Flags} is a value on the stack, consumed by IF. A
non-zero value indicates true, zero indicates false. A true flag causes
the code after \forthb{IF} (in this case, the word \forth{KNOCK}) to be
executed. The word \forthb{THEN} marks the end of the conditional phrase;
execution resumes with the word \forth{OPEN}. A false flag causes the
code between \forthb{IF} and \forthb{THEN} to {\em not} be executed. In
either case, \forth{OPEN} will be performed.
The word \forthb{ELSE}\index{E!ELSE} allows an alternate phrase to be
executed in the false case. In the phrase:
\begin{Code}
( flag ) IF KNOCK ELSE RING THEN OPEN ...
\end{Code}
the word \forth{KNOCK} will be performed if the flag is true, otherwise
the word \forth{RING} will be performed. Either way, execution will
continue starting with \forth{OPEN}.
\Forth{} also provides for indexed loops\index{L!Loops} in the form
\begin{Code}
( limit) ( index) DO ... LOOP
\end{Code}
and indefinite loops in the forms:
\begin{Code}
... BEGIN ... ( flag) UNTIL
\end{Code}
and
\begin{Code}
... BEGIN ... ( flag) WHILE ... REPEAT ;
\end{Code}
\section{For the Whole Story}
For a complete introduction to the \Forth{} command set, read
\emph{Starting \Forth{}}, published by Prentice-Hall.%
\index{F!forth@\Forth{}!overview of|)}%