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.

Figure Two. Object class words. I<br />

\ oofclass.fth 910729 rwb<br />

only forth also definitions<br />

#vocs cells constant /context<br />

struct :<br />

; struct /class<br />

#threads cells :field +threads<br />

/context :field +context<br />

cell :field +/body<br />

: >context \ ( 'class -- ) MACROto select object's context.<br />

+context context /context cmove ;<br />

: class: \ ( -- '/body offset )<br />

vocabulary<br />

last @ name> >body >user \ ( -- 'body )<br />

/context cell+ ualloc drop \ allocate space for<br />

\ rest of body<br />

( 'body ) dup context ! \ setup class to be<br />

\ both context..<br />

context over +context /context<br />

\ ( -- 'body 'cxt 'c .cxt /cxt )<br />

cmove definitions \ ( -- 'body ) ..and current vocabs.<br />

+/body dup off 0 \ ( -- '/body offset )<br />

does> ( pfa -- )<br />

>user >context ;<br />

: END \ ( - - I<br />

1 only forth also definitions ;<br />

: ;class \ ( '/body offset -- )<br />

END swap ! ;<br />

1 : METHODS definitions ;<br />

cr . ( oofclass. fth loaded ) cr<br />

that all messages are pref-<br />

aced with a < (less-than)<br />

character. This is done so<br />

that the message and the<br />

methodwill notbe confused.<br />

The < is stripped off before<br />

beiig compiled within the<br />

message with the SWAP 1t<br />

SWAP 1- code.<br />

Figure Four contains<br />

words which construct in-<br />

stance OBJECTS and in-<br />

stance VARiables. An in-<br />

stance contains two fields: its<br />

class pointer and its body.<br />

An instance OBJECT'S body<br />

contains its instance variables.<br />

An instance vivtiable's body<br />

contains its offset within its<br />

parent OBJECT.<br />

Figure Five (pg. 28) con-<br />

tains the initial bootstrapped<br />

object class, called PRIMARY.<br />

I decided that PRIMARY<br />

methods should just go in<br />

the FORTH vocabulary, so that<br />

PRIMARY would not have to<br />

be INHERITed by each class;<br />

thus, the phrase PRIMARY<br />

METHODS has been com-<br />

mented out. The only real<br />

need for a PRIMARY class is<br />

for indirect reference to the<br />

object on top of the stack.<br />

This data must be a declared<br />

class. PRIMARY is declared<br />

just for this situation. Other<br />

than that, its "object-ness"<br />

may be ignored and it can be<br />

treated just like a <strong>Forth</strong> vari-<br />

able or structure field.<br />

Figure Six (page 28) is a<br />

simple example of building<br />

new classes. Note that the<br />

methods' code is very <strong>Forth</strong>-<br />

ish: the implicit stack opera-<br />

my <strong>Forth</strong>macs (by Mitch Bradley)<br />

for the Atari ST is a 32bit<br />

<strong>Forth</strong>, so my CELL is set<br />

to four bytes. Adjust yours<br />

accordingly. A simple method<br />

for grouping data into<br />

named records is provided<br />

by the bee words STRUCT : ,<br />

:FIELD, and ; STRUCT.<br />

Common <strong>Forth</strong> contains<br />

the seeds of an object-oriented<br />

language. CREATE<br />

DOES> provides the abiLity<br />

to abstract data and create<br />

new data types. Vocabularies<br />

can provide privacy, as<br />

well as inheritance. Thus,<br />

only ten new words need be<br />

used to program in OOF:<br />

CLAS S : , ; CLASS, IN-<br />

HERIT, METHODS, END,<br />

MESSAGE, OBJECT, OB-<br />

JECTS, :VAR, and :VARS.<br />

Figure Two provides<br />

definitions of object dur<br />

words. I have implemented<br />

a CLASS as a hybrid of<br />

STRUCT : and VOCABU-<br />

LARY-which reflects the<br />

twofold nature of a class: to<br />

provide the internal data<br />

structure of its type, and to<br />

house the data's routines.<br />

See Figure Seven (page 29)<br />

for a good example of how<br />

these words are used. (In<br />

<strong>Forth</strong>macs, the parameter<br />

Lld of a vocabulary is kept<br />

in the user area. SO, while the<br />

general idea is to build a<br />

vocabulary with two extra<br />

fields-he+contextfieldand<br />

the +/body field-these must<br />

be allocated in the user area<br />

for <strong>Forth</strong>macs.)<br />

MESSAGE is defmed in<br />

Figure Three. A message<br />

merely records its name as a<br />

suing within its body, and<br />

then vies to Fid it in the<br />

context vocabulary (CLASS)<br />

at run time. CT~IS is an example<br />

of late binding.) Note<br />

tors (DUP, SWAP, TUCK, etc.)<br />

srill needed,<br />

are passed on the stack,<br />

andRPNsynmissulluxL<br />

thus, OOF blends with<br />

common <strong>Forth</strong>. It is not necessary<br />

for 2COMPLEX to expli.tly<br />

INHERIT from the<br />

COMPLEX in order for<br />

the (like to pass<br />

the

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

Saved successfully!

Ooh no, something went wrong!