16.10.2013 Views

5 - Forth Interest Group

5 - Forth Interest Group

5 - Forth Interest Group

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

(METHODS) DEFINITIONS<br />

: ... content of new word..;<br />

FORTH DEFINITIONS<br />

Thissimple sequence may<br />

be directly converted to<br />

compiler words as follows.<br />

: METHOD:<br />

[CCMPILE] (METHODS)<br />

[COMPILE] DEFINITIONS<br />

[COMPILE] :<br />

; lM4EDIATE<br />

: METHOD;<br />

[COMPILE] ;<br />

[COMPILE] FORTH<br />

[CaMPILEl DEFINITICPJS<br />

; RWEDIATE<br />

where each immediate word<br />

used for the interactive se-<br />

quence is effectively deferred<br />

by the use of [COMPILE I<br />

until the execution of<br />

METHOD: and METHOD;.<br />

Normal colon defdtions may<br />

be deferred by using COM-<br />

PILE.<br />

NULL is a word to indi-<br />

cate that a search through<br />

the inheritance chain has<br />

been unsuccessful, and<br />

should simply return a mes-<br />

sage to this effect and stop<br />

execution.<br />

: NUU ( flag -- )<br />

ABORT"<br />

No method available "<br />

t<br />

The structure of defining<br />

words in <strong>Forth</strong> provides the<br />

basis for combining data and<br />

execution functions within<br />

one object. The basic form<br />

for a defining word is:<br />

: <br />

CREATE<br />

... storage set up ...<br />

DOES><br />

... run-time operations<br />

,<br />

To match our object syn-<br />

tax to the appropriate <strong>Forth</strong><br />

strumre, the pmgrammer<br />

builds a defintiodased<br />

on the core words of the<br />

language+-which will do<br />

what is needed, as in Figure<br />

Seven.<br />

This may be demon-<br />

strated '3y expanding on the<br />

POINT example. To provide<br />

the facility for anonymous<br />

January 1992 February<br />

Figure Five. Defining a square class. I<br />

METHOD: SIDEM ( s addr -- ) \ Store in both height and width<br />

2DUP PUTHEIGHT IN RECT PUTWIDTH IN RECT<br />

METHOD;<br />

CLASS SQUARE ( side x y -- )<br />

\ A square is a rectangle with height = width<br />

DATA<br />

2DUP , , \ Upper left<br />

2 PICK + , + , \ Lower right<br />

\ Using same data structure as before<br />

METHODS<br />

PUTWIDTH ( w -- ) M: SIDEM M;<br />

PUTHEIGHT ( h -- ) M: SIDEM M;<br />

ASTEXT ( -- stringn ) :: " Square" ;;<br />

BUILD ( side x y -- ) :: DUP >R PUTXY IN PT<br />

R> SIDEM ; ;<br />

INHERIT<br />

RECT<br />

ENDCLASS<br />

Figure Six. Playing with classes.<br />

ASTEXT FRED COUNT TYPE Square ok<br />

GRIPE FRED "FRED" No method available ok<br />

GETX FRED . 12 OK<br />

LOWERRIGHT FRED . . 18 17 ok<br />

6 9 10 BUILD FRED ok<br />

UPPERLEFT FRED . . 10 9 ok<br />

GETWIDTH FRED . 6<br />

Figure Seven. An object as a <strong>Forth</strong> defining word.<br />

: <br />

CREATE<br />

( build the instance data structure )<br />

DOES ><br />

CASE<br />

method1 OF do.method1 ENDOF<br />

method2 OF do.method2 ENDOF<br />

...<br />

inheritance<br />

ENDCASE<br />

,<br />

access, we need the additional<br />

concept: a method<br />

having a TRUE value indicates<br />

that its execution has<br />

been entered through inheritance<br />

or deferral, and that<br />

only its methods are required,<br />

not the data structure. [See<br />

Figure Eight.]<br />

The process of conf~guring<br />

our object-oriented syn-<br />

36<br />

tax is simply matching the<br />

two forms and defining the<br />

necessary compiling words<br />

to handle the operations. [See<br />

Figure Nine.]<br />

The above constructs take<br />

less than one page of co&,<br />

yet provide all the function-<br />

ality discussed at the begin-<br />

ning of this document.<br />

Extending the Example:<br />

Text Window on a PC<br />

We have developed our<br />

syntax and object creation<br />

methods, through an ex-<br />

ample sequence, from a Point<br />

to a Rectangle to a Square. A<br />

text window is an example<br />

of a rectangle with additional<br />

attributes.<br />

<strong>Forth</strong> Dimensions

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!