5 - Forth Interest Group
5 - Forth Interest Group
5 - Forth Interest Group
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Figure Eight. Dropping data address during method inheritance.<br />
: POINT ( x y -- )<br />
CREATE<br />
I I<br />
DOES>( method dataaddr I method dataaddr true dataaddr2 -- )<br />
\ Check if execution is entered through inheritance process<br />
\ and drop the address provided by DOES> if it is.<br />
OVER TRUE = IF 2DROP THEN SWAP<br />
CASE<br />
GETY OF @ ENDOF \ etc.<br />
...<br />
SWAPXY OF (METHODS) SWAPXYM FORTH ENDOF<br />
\ Switch vocabularies to find the right word<br />
SWAP TRUE NULL TRUE<br />
\ Deal with inheritance and stack requirements of ENDCASE<br />
ENDCASE<br />
I<br />
Figure Nine. Defining the required compiling words. 1<br />
: CLASS [COMPILE] : ; IMMEDIATE<br />
: DATA COMPILE CREATE ; IMMEDIATE<br />
: METHODS<br />
COMPILE DOES> COMPILE OVER<br />
COMPILE = [COMPILE] IF<br />
[COMPILE] THEN COMPILE SWAP<br />
; IMMEDIATE<br />
\ The following two are really deferred aliases<br />
. ,. [COMPILE] OF ; IMMEDIATE<br />
. .*<br />
[COMPILE] ENDOF ; IMMEDIATE<br />
- ~t<br />
: M: [COMPILE] OF<br />
; IMMEDIATE<br />
1. The rectangle may be dis-<br />
played<br />
2. The contents may be<br />
cleared.<br />
3. Text may be placed any-<br />
where within the window.<br />
4. Cmnt text should be<br />
sadable in the window.<br />
COMPILE TRUE<br />
COMPILE 2DROP<br />
[COMPILE] CASE<br />
[COMPILE] (METHODS)<br />
: M; [COMPILE] FORTH [COMPILE] ENDOF<br />
; IMMEDIATE<br />
\ IN is required to access an anonymous object within a new class<br />
\ which, in practice, operates exactly the same as inheritance.<br />
: IN COMPILE SWAP COMPILE TRUE ; IATE<br />
: INHERIT COMPILE SWAP COMPILE TRUE ; IMMEDIATE<br />
: ENDCLASS<br />
COMPILE TRUE [COMPILE] ENDCASE [COMPILE] ;<br />
; IMMEDIATE<br />
To create such a window,<br />
we would expect to have to<br />
use the operating system<br />
commands of the IBM-PC,<br />
but these commands should<br />
not be visible to the user of<br />
the window. All such detailed<br />
operations should be<br />
confined to the hidden vocabulary.<br />
The user should<br />
expect to see a text window<br />
object characterized by the<br />
, code in Figure Ten.<br />
The user will still be able<br />
to apply any methods associated<br />
with a nxtangle object<br />
to the text window, as well<br />
as the new methods specific<br />
to the text window itself. The<br />
following code is derived<br />
from the UR-FORTH access<br />
to IBM-PC internals, and<br />
demonstrates what is needed<br />
to mte the new facilities for<br />
our object, and also the ability<br />
to keep such details from<br />
the normal programmer.<br />
Most of the code in Figure<br />
Eleven is derived from a<br />
demonstration example by<br />
Ray Duncan ofLMI, but takes<br />
advantage of our predefined<br />
objects by building on the<br />
POINT facibies.<br />
We may now complete<br />
our def~tion of a text window<br />
object (see Figure<br />
Twelve, page 40).<br />
Etildency & G endty<br />
The approach we have<br />
used above to aeate an ob-<br />
ject-oriented syntax leads to<br />
a direct implementation of<br />
the requirements, but does<br />
not lead to fast execution. By<br />
eliminating the use of in-line<br />
definitions and by complet-<br />
ing all definitions within the<br />
hidden vocabulary, it is<br />
possible to use vectored ex-<br />
ecution techniques for<br />
method access, which re-<br />
sults in very fast chaining<br />
through the inheritance list.<br />
The remaining limitation<br />
of this implementation is that<br />
it only supports single in-<br />
heritance, by whichwe mean<br />
that there is a path of inher-<br />
itance from anv articular<br />
class to a class wkih inherits<br />
NULL, and failure to find the<br />
method within this search<br />
halts the process. A more<br />
general solution would be to<br />
have multiple inheritance for<br />
a class and allow the search<br />
to to find the requested<br />
meth*b~~chingthrough<br />
a specified set of class chains<br />
until it finds the appropriate<br />
method. From the user's<br />
syntax requirements, this can<br />
be accomplished by simply<br />
introducing a list of inheritances<br />
to re~lace the single<br />
instance diicussed above.<br />
From an implementation<br />
viewpoint, this is not such a<br />
simple task--but a very good<br />
analysis-and-programming<br />
exercise.<br />
<strong>Forth</strong> Dimensions 37 January 1992 February