Often when programming it's necessary to refer to collections of data as a single entity. Most Languages have mechanisms ( C structs, Pascal records, VB types etc ) for declaring structures built in: Forth is to some extent an exception in this respect because its extensible nature makes it easy to implement custom structures tailored to your application. Nevertheless WIN32FORTH contains some words to help with this process as well as three optional structure packages ( ExtStruct.f, Struct.f and eStruct.f )
FIELD+ is a defining word that creates a new word that adds an offset to the address on the stack when it executes. When compiling it stores the second from top value as the offset and returns the sum of the top 2 stack values. Its use is best illustrated by example thus:
0 nostack1 \ thing record cell field+ >This cell field+ >That cell field+ >Other constant SizeOfThing
where the 1st line sets up the initial value ( usually 0 ) and subsequent lines define the offsets ( the best way to arrange the code is in a tabular format as shown ). The final line line stores the value remaining as a CONSTANT ( alternately if it's not needed it can be DROPed ).
You can define a thing with:
create MyThing SizeOfThing Allot
or alternately any number of things with:
: THING create SizeOfThing Allot ; Thing MyThing Thing AnyThing Thing Something
or on a per thread basis:
SizeOfThing NewUser MyThing
and then reference individual fields with:
MyThing >This @ 22 MyThing >That ! 1 MyThing >Other +!
Document $Id: p-structures.htm,v 1.2 2005/12/20 18:10:15 dbu_de Exp $