Forth Structures
Volume Number: 2
Issue Number: 9
Column Tag: Threaded Code
Adding Record Structures to Forth 
By Jörg Langowski, EMBL, c/o I.L.L., Grenoble, Cedex, France,
MacTutor Editorial Board
Records with local field names
Data representation is a field that is neglected by many Forth dialects. Basic
Forth-83 doesn't even provide for simple one and two dimensional matrices, neither
are more complex types of data supported, such as Pascal records or C structs. These
latter forms of data representation play a most important role in Toolbox
programming, since very many traps expect pointers to records as parameters.
A letter received through BITNET from a reader who was wondering how to
install a way to handle such data structures in Forth got me started on this month's
column:
"I posted the following article to the USENET, but got little in the way of a
response. Any help you can give will be much appreciated. By the way, I know that the
rectangle definitions given below are inaccurate for the Mac, but I was trying to be
machine independent in posting to the Forth language newsgroup.
From postnews Thu Jun 12 15:23:34 1986
Subject: Defining a structure in FORTH?
Newsgroups: net.lang.forth
Distribution: net
I am very much a novice FORTH programmer, and I don't even have a good
textbook to go by. I recently purchased a FORTH for my Macintosh at home (MACH1,
distributed by the Palo Alto Shipping Co.), and would like some advice. Professionally I
do a lot of work with LISP, and I would like to implement something similar to a
`DEFSTRUCT' package in FORTH. In other words, I'd like to be able to do something like:
DEFSTRUCT[ RECTANGLE
TOP 2
LEFT 2
BOTTOM 2
RIGHT 2 ]ENDSTRUCT
Which would automatically define the following:
8 CONSTANT RECTANGLE-SIZE
: RECTANGLE-TOP@ ( a - n ) @ ;
: RECTANGLE-TOP! ( n a - ) ! ;
: RECTANGLE-LEFT@ ( a - n ) 2 + @ ;
: RECTANGLE-LEFT! ( n a - ) 2 + ! ;
: RECTANGLE-BOTTOM@ ( a - n ) 4 + @ ;
: RECTANGLE-BOTTOM! ( n a - ) 4 + ! ;
: RECTANGLE-RIGHT@ ( a - n ) 6 + @ ;