Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!rutgers!rochester!pt!gnome.cs.cmu.edu!ns From: ns@gnome.cs.cmu.edu (Nicholas Spies) Newsgroups: comp.lang.forth Subject: Pascal-like records in FORTH Message-ID: <49@gnome.cs.cmu.edu> Date: Tue, 4-Aug-87 03:59:18 EDT Article-I.D.: gnome.49 Posted: Tue Aug 4 03:59:18 1987 Date-Received: Wed, 5-Aug-87 04:52:49 EDT Organization: Carnegie-Mellon University, CS/RI Lines: 55 Keywords: records, program example Here is some simple (perhaps trivial) code that might be useful: The following enable the definition of "records" in FORTH, which have two properties; the record name returns the length of the record it names (in bytes), useful for ALLOT and such; the field words will, when given the start address of an instantiated record, return the address of the field relative to that location. This implementation assumes your FORTH does not pass stuff on the stack when compiling, because END-RECORD needs to have a valid address to store the final byte count in the word defined by RECORD. If stuff IS passed on the stack, just use [ and ] to arrange it so that HERE 0 are intact after RECORD executes... ------- cut here --------- ( Records and Field Offsets ) ( Words to create words that add offset to start address, by field type: ) : RECORD ( -- here 0 ) CREATE HERE 0 DUP , DOES> @ ; ( create record header) : END-RECORD ( addr bytecount -- ) SWAP ! ; ( store length in record ) ( Defining word to create field-offset defining words ) : FIELD ( length -- ) CREATE , DOES> @ CREATE SWAP DUP , + DOES> @ + ; ( Sample FIELD words ... 1 FIELD Byte: 2 FIELD Int: 4 FIELD Long: 4 FIELD Ptr: 4 FIELD Handle: 4 FIELD Point: 8 FIELD Rect: 8 FIELD Pattern: Etc... ) ( Sample record, particularly useful for Mac programming... RECORD Rectangle Int: +top Int: +left Int: +bottom Int: +right END-RECORD ) ( Sample usage ... ) ( Create aRect Rectangle ALLOT 10 aRect +top ! 20 aRect +left ! aRect +top @ 20 + aRect +bottom ! aRect +left @ 30 + aRect +right ! ) ( RECORDs must be defined in execute mode, obviously) ( Error-checks could, and probably should, be added )