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.

I Listing Four. PCYerk source code. I<br />

I<br />

\ PCYerk<br />

\ Object-oriented extensions to <strong>Forth</strong><br />

\ a la Yerk (once, NEON)<br />

\ Written for Upper Deck <strong>Forth</strong>, version 2.0<br />

\ R. E. Grehan<br />

\ Sorry, I can't stand "then"<br />

: endif [compile] then ; immediate<br />

\ *************<br />

\ ** STORAGE **<br />

\ *************<br />

34 $variable tstring \ Used in parsing names<br />

\ Method stack<br />

20 constant METH-STACK-SIZE<br />

create mstack<br />

METH-STACK-SIZE allot<br />

\ Objects stack<br />

20 constant OBJ-STACK-SIZE<br />

create ostack<br />

OBJ-STACK-SIZE allot<br />

\ Instance variable names segment<br />

variable ivar-seg \ Segment<br />

20 constant IVAR-SEG-SIZE \ Segment size in paragraphs<br />

variable ivar-next \ Offset to next free loc.<br />

\ Methods names segment<br />

variable methname-seg \ Segment<br />

100 constant METHNAME-SEG-SIZE \ Segment size in paragraphs<br />

variable methname-next \ Offset to next free slot<br />

variable curr-meth# \ Current method #<br />

variable my-meth# \ Method # about to be define1<br />

\ Class definitions<br />

variable curr-class-off \ Offset to current class<br />

variable curr-meth-tail \ Current method tail<br />

\ ..............................<br />

\ ** METHOD AND OBJECT STACKS **<br />

\ ..............................<br />

\ NOTE: Neither stack do any bounds checking (for speed's<br />

\ sake). If bounds checking is added, the stack manipulatior<br />

1 \ words should be written in machine language.<br />

\ Initialize the method stack ... stores selector ids<br />

: mstack-init ( -- )<br />

mstack dup !<br />

\ Push top word onto method stack<br />

: mpush (n-1<br />

mstack @ 2+ ! \ Save item<br />

2 mstack +! \ Increment<br />

\ Copy top of mstack to dstack<br />

: mstack->dstack ( -- n )<br />

mstack @ @<br />

I<br />

<strong>Forth</strong> Dimensions 9<br />

(Tisting continues<br />

)olygon class. Then you<br />

ould store the polygon's<br />

ertex list into the one-di-<br />

nensional array object.<br />

It turns out you can do<br />

his by defLning one of the<br />

)olygon's instance variables<br />

o be a pointer to an object of<br />

lass Ida rray. Essentially,<br />

leu build a headerless ob-<br />

ect; that is, one that does not<br />

lave a head in the <strong>Forth</strong><br />

iictionaly. The word in-<br />

stantiate will build such<br />

L headerless object. All in-<br />

stantiate needs on the<br />

;tack is a starting address in<br />

he variable region (where<br />

he new object will go) and<br />

L pointer to the class defini-<br />

ion. The code showing this<br />

echnique is in Listing Three<br />

:which presumes that you've<br />

lefmed the ldarray class<br />

dready).<br />

Notice that I've defined<br />

init : to multiply the num-<br />

Der of vertex entries by two,<br />

since the vertex list will carry<br />

an x and y coordinate for<br />

each vertex. The next line<br />

stores the address of the next<br />

Free location in the variable<br />

segment into<br />

^vertex-list, which be-<br />

comes a pointer to our one-<br />

dimensional array. I then use<br />

[ ' 1 to retrieve the code ad-<br />

dress of the ldar ray class,<br />

then fetch the address of that<br />

class defmition. The in-<br />

stantiate word actually<br />

creates the array. Keep in<br />

mind that, when instan-<br />

t i a t e executes, the<br />

lda r ra y object's initial<br />

method-which allocates the<br />

memory space-will be ex-<br />

ecuted.<br />

Now we can store an x<br />

and y coordinate into the<br />

ldarray object using a<br />

method called to:, which<br />

we've presumably already<br />

defined for the ldarray<br />

dass. You can extend this idea<br />

as far as you'd like to go,<br />

aeatingpointelsto objwith<br />

pointers to objects, and so on<br />

Nuts and Bolts<br />

Refer to Listing Four, the<br />

complete source for PCYerk.<br />

The heart of PCYerk is<br />

January 1992 February

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

Saved successfully!

Ooh no, something went wrong!