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.

stance variable names in<br />

these alternate segments,<br />

how does <strong>Forth</strong> find those<br />

names during compilation?<br />

You have to patch inter-<br />

pret.<br />

InUD <strong>Forth</strong>, interpret<br />

first tries to find the word in<br />

the dictionary. If that fails,<br />

interpret tries to parse<br />

the word as a number. If the<br />

number conversion routine<br />

can't digest it, interpret<br />

executes do-undef ined<br />

(which prints out the offend-<br />

ing word and executes<br />

quit.) I overwrite the call to<br />

do-undef ined to point to<br />

. The<br />

word<br />

(see Listing Four) looks fmt<br />

in the method segment, then<br />

in the instance variable seg-<br />

ment If <br />

finds the word in either seg-<br />

ment, it takes appropriate<br />

adion. Ofcourse, ultimately falls<br />

through into do-unde-<br />

fined<br />

You have to execute<br />

start-ob jects befoe you<br />

begin defining any classes.<br />

The start-ob jectsword<br />

allocates and initializes the<br />

instance variable and method<br />

names segments, then<br />

patches interpret and<br />

clears the methods and ob-<br />

jects stacks. Finally, when<br />

you're ready to create a stan-<br />

dalone application, execute<br />

end-ob jects. This<br />

repatches interpret to put<br />

it back the way it was, and<br />

releases the allocated<br />

memory blocks (which ae<br />

unnecessary in the run-time<br />

code.)<br />

(PCYerk Listing Four, continued.)<br />

\ Given that addr points to a counted string that represents<br />

\ an instance variable name, return the associated offset.<br />

\ If you can't find that variable, return a -1.<br />

: search-ivar ( addr -- n )<br />

vars swap \ String is in vars segment<br />

ivar-seg @ \ Search through ivars segment<br />

ivar-next @ \ Max. to look for in ivar segment<br />

search-tseg \ Search a temp. segment<br />

I<br />

\ addr points to a counted string that represents an instance<br />

\ variable name. n is the offset to attach to that instance<br />

\ variable. Add this name to the list.<br />

: add-ivar ( n addr -- )<br />

dup c@ >r \ Save byte count<br />

vars swap \ Source address<br />

ivar-seg @ ivar-next @ \ Destination<br />

$!l \ Copy the string in<br />

ivar-seg @ ivar-next @ r@ + 1+ !1 \ Store associated value<br />

r> 3 + ivar-next +! \ Advance next<br />

\ **<br />

\ ** Methods name segment handling<br />

\ **<br />

\ The methods segment looks a lot like the IVARS segment.<br />

\ it holds the list of methods defined within the svstem.<br />

\ Associated with each method name is a unique 2-byte<br />

\ id.<br />

\ Allocate space for the method name segment<br />

: alloc-methname-seg ( -- )<br />

METHNAME - SEG-SIZE alloc<br />

error \ Fetch error<br />

if abort" Methname allocation error"<br />

endif<br />

methname-seg ! \ Save pointer<br />

\ Clear the method segment<br />

: clear-methname-seg ( -- )<br />

0 methname-next !<br />

\ Search for a method in the methods segment. Return -1 if<br />

\ not found. Else return method #<br />

: methname-find ( addr -- n )<br />

vars swap \ String is in vars segment<br />

methname-seg @ \ Search through ivars segment<br />

methname-next @ \ Max. to look for in ivar segment<br />

search-tseg \ Search a temp. segment<br />

I<br />

\ Add a new method to methods segment. Associate n with that<br />

\ method as the method's id<br />

: add-methname ( n addr -- )<br />

dup c@ >r \ Save byte count<br />

vars swap \ Source address<br />

methname-seg @ methname-next @ \ Destination<br />

$!l \ Copy the string in<br />

methname-seg @ methname-next @ r@ + 1+ !1<br />

\ Store associated value<br />

January 1992 February 12 <strong>Forth</strong> Dimensions<br />

..<br />

I

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

Saved successfully!

Ooh no, something went wrong!