16.10.2013 Views

5 - Forth Interest Group

5 - Forth Interest Group

5 - Forth Interest Group

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure Three. Using the point class. I<br />

GETXY PT . . 15 10 ok \ Note the order printed!<br />

SWAPXY PT ok<br />

GETXY PT . . 10 15 0k<br />

ASTEXT PT COUNT TYPE Point ok<br />

22 7 BUILD PT ok<br />

GETXY PT . . 7 22 ok<br />

Figure Four. Defining a rectangle class.<br />

METHODNAME PUTHEIGHT METHODNAME GETHEIGHT<br />

METHODNAME PUTWIDTH METHODNAME GETWIDTH<br />

METHODNAME UPPERLEFT METHODNAME LOWERRIGHT<br />

METHOD: PUTHEIGHTM ( h addr -- )<br />

DUP @ ROT + \ The new lower right -> ylr<br />

SWAP WSIZE 2" + !<br />

METHOD ;<br />

METHOD: PUTWIDTHM ( w addr -- )<br />

DUP WSIZE + @ ROT + \ The new xlr<br />

SWAP WSIZE 3 * + !<br />

METHOD ;<br />

METHOD: GETHEIGHTM ( addr -- h 1<br />

DUP WSIZE 2" + @ SWAP @ -<br />

METHOD ;<br />

METHOD: GETWIDTHM ( addr -- W<br />

DUP WSIZE 3 * + @ SWAP WSIZE + @ -<br />

METHOD ;<br />

CLASS RECTANGLE ( xlr ylr xu1 yul -- )<br />

\ Rectangle aligned to the x y axes<br />

DATA<br />

I \ Upper left POINT yul xu1<br />

I I \ Lower right POINT ylr xlr<br />

METHODS<br />

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

GETWIDTH ( -- w ) M: GETWIDTHM M;<br />

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

GETHEIGHT ( -- h ) M: GETHEIGHTM M;<br />

UPPERLEFT ( -- x y ) :: GETXY IN PT ;;<br />

LOWERRIGHT ( -- x y ) :: WSIZE 2* + GETXY IN PT ;;<br />

ASTEXT ( -- stringA ) :: " Rectangle" ;;<br />

BUILD ( xlr ylr xu1 yul -- ) :: DUP >R PUTXY IN PT<br />

R> WSIZE 2* +<br />

PUTXY IN PT ;;<br />

INHERIT<br />

PT \ NOTE: Instance of POINT needed, not the class.<br />

ENDCLASS<br />

<strong>Forth</strong> Dimensions 35<br />

may only be used within a<br />

ckass depnitian, as it is only<br />

within the class definition<br />

that tbm is knowledge of the<br />

internal data stncctures to<br />

allow theprog~ammerac~4~~.<br />

NOW for the SQUARE class,<br />

defined in Figure Five. Producing<br />

a new square :<br />

5 12 13 SQUARE FRED<br />

We now have a single<br />

inheritance chain of classes<br />

to experiment with! [See Figure<br />

Six.]<br />

creating the<br />

Object Syntax<br />

The use of defining and<br />

compiling words in <strong>Forth</strong><br />

provides the programmer<br />

with the ability to produce<br />

new language constructs, and<br />

is the core of the syntax-<br />

generation process.<br />

To generate the syntax,<br />

some preliminary functions<br />

will prove useful later. Dur-<br />

ing creation of a specific<br />

syntax, these will be speci-<br />

fied as the need arises; but<br />

here we will separate them<br />

from the detailed discussion<br />

of the syntax itself.<br />

To generate distinct<br />

method names, it is only a<br />

matter of making a func-<br />

tional equivalent to a vari-<br />

able without using the stor-<br />

age. In many systems where<br />

the dictionary and vocabu-<br />

lary coexist, simple name<br />

creation would be enough;<br />

but to be completely gen-<br />

eral, we will define them as<br />

follows.<br />

: METHODNAME<br />

CREATE 0 I ;<br />

To hide the more com-<br />

plex methods from the nor-<br />

mal programming emiron-<br />

men4 we can set up a new<br />

vocabulary into which all of<br />

these definitions can be<br />

placed.<br />

VOCABULARY (METHODS )<br />

If we wished to interac-<br />

tively open this vocabulary<br />

for the storage of a new<br />

defhtion and then return to<br />

the normal FORTH vocabu-<br />

lary, we would enter the<br />

sequence:<br />

January 1992 February

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

Saved successfully!

Ooh no, something went wrong!