5 - Forth Interest Group
5 - Forth Interest Group
5 - Forth Interest Group
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Simple Ob 'ect-<br />
Oriented 6orth<br />
C. A. Maynard<br />
Wilson, West Australia<br />
F<br />
orth is a minimalist lan- lation and results in im- tional programming necesguage,<br />
by which we proved data security, as only sary to handle a new variamean<br />
that the core of those operations which have tion on an object, If the<br />
the language provides facili- been designed to work with methods defined for the anties<br />
from which the user/ the object's data structure cestor object are valid, the<br />
programmer can build his will be executed, and there is objectwill simply obtain them<br />
own working environment, no direct access to the data via the inheritance chain. If<br />
It has also been described as itself. The user of an object there is the need for a different<br />
a syntaxdirected language need have no knowledge of definition of a method for a<br />
because, if you can define a the details of data storage or new class, that may be insyntax<br />
which will best ex- even details of the appli- cluded in the new class and<br />
press your needs, <strong>Forth</strong> will cable methods. He/she just will only work for the new<br />
allow you to create a func- needs to know the valid op class of object and its<br />
tional equivalent suitable for erations and any parameter- decendents. This ability is<br />
programmer use.<br />
passing requirements which called polynwqhisrn.<br />
The latest thing in pro- may be necessary to operate Ouerlaading is the ability<br />
gramming tools is the use of upon the object.<br />
to have more than one defithe<br />
objj-oriented approach, The user also needs to nition of the same method,<br />
where data and operations know the terminology which and ensuring that the correct<br />
upon that data are all part of fits the use of these tech- one is applied to a particular<br />
the same 'object." This is niques. There are several object.<br />
often refered to as encapsu- variants on the object-ori- <strong>Forth</strong> has the facilities to<br />
create a very simple but effective<br />
object-oriented pro-<br />
To hide complex methods, we gramming environment. The<br />
can set up a new vocabulary. following discussion and<br />
development will not produce<br />
the fastest objea-oriented<br />
theme, but the follow- ented implementation in<br />
ing is adequate for our needs. <strong>Forth</strong>, but will introduce the<br />
I A c& definition s~eci- direct application of defin-<br />
Clive Maynard is a senior lecturer in<br />
fieS the data struciures ing and compiling words to<br />
the Department of Computer Engineeded,<br />
any initialization, establish an appropriate syntax.<br />
The first requirement,<br />
then, is to propose an appropriate<br />
syntax to represent the<br />
object class. (See Figure One.)<br />
By analogy with cooking,<br />
one should consider that<br />
this syntax provides the<br />
recipe to create new objeas<br />
but that one must use the<br />
upon an object.<br />
recipe to make an object<br />
Inhetftance allows the (i.e., create an instance) beuser<br />
to minimize the addi- fore one can use it,<br />
neering at Curtin University of ~ ~ ~ h -<br />
nology, Western Australia, where this and the operations which<br />
article's contents are used as part of may be performed upon the<br />
students' <strong>Forth</strong> instruction. He also -, ~<br />
runs Wawnic AsscciatBS. asvstems- I<br />
1 design consultancy. He teaches real- I An instance is a ~articutime<br />
systems using <strong>Forth</strong> on PCs and lar object of a spec& class,<br />
on the Motorola 68HCl I, and is a co<br />
author of The Art of Lisp Progamming and has built the data struc-<br />
(Springer-Verlag. 1990). Clive has tures defined in the class<br />
' developed a number of embedded &fition.<br />
systems for industrial application.<br />
. 1 Current interests include the develop A nretbOdis an<br />
ment of practical, analytical tods fbr I which can be performed<br />
predicting real-time scheduler performance<br />
in e m ~ systems; ~ d as well<br />
as real-time systems and Al.<br />
<strong>Forth</strong> Dimensions 33<br />
This syntax must provide<br />
the compiler with all the information<br />
needed to construct<br />
the object. The method<br />
names must be available for<br />
use by any class definition,<br />
and so will have to be defined<br />
before use.<br />
It will also be necessary<br />
to provide syntadic delimiters<br />
between the method<br />
name and the method, and<br />
also to separate this from the<br />
following method name.<br />
These methods may result<br />
from short in-line definitions<br />
or may need to access predefined<br />
and hidden method<br />
definitions. Appropriate delimiter<br />
pairs which have been<br />
selected are: : : and ; ; for<br />
in-line definitions, M: andM;<br />
for predefined methods.<br />
Note for undentanding<br />
thefillowing code<br />
1. The operations are made<br />
independent of 16-bit or<br />
32-bit <strong>Forth</strong> implementations<br />
by using the constant<br />
WSIZE which returns the<br />
number of bytes assigned<br />
to storage of an integer<br />
variable. This technique<br />
reduces the efficiency of<br />
definitions but ensures<br />
portability, which is a<br />
reasonable compromise.<br />
2. Hidden methods must be<br />
passed the address of the<br />
beginning of data within<br />
the object itself, and this<br />
is shown in the stack<br />
comments as "addr" on<br />
top of the stack.<br />
3. The stack comments for<br />
the methods indicate the<br />
parameters needed when<br />
applying the method andl<br />
or the results produced<br />
by the method.<br />
4. The class must inherit from<br />
another class or NULL<br />
Putting this together for<br />
an example class which<br />
consists of a point defined by<br />
its x,y coordinates and a va-<br />
riety of useful (and not-so-<br />
useful) methods, [refer to<br />
Figure Two].<br />
To create an instance of<br />
this class called PT we sim-<br />
ply execute:<br />
10 15 POINT PT<br />
January 1992 February