🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.lang.forth
Article #331

Defining a structure in FORTH?

#331
From: Bruce Florman
Date: Thu, 12 Jun 1986 22:23
37 lines
2077 bytes
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 + @ ;
	: RECTANGLE-RIGHT! ( n a - ) 6 + ! ;
	: MAKE-RECTANGLE ( whatever code necessary to allocate 8 bytes
			   of variable storage and assign a dictionary
			   entry to the word which follows.  This I guess
			   would be implementation specific. ) ;

While I'm sure that this could be done by defining `DEFSTRUCT[' so that it
constructs all of the necessary dictionary headers etc. at the bit and byte
level, this would doubtless be complicated and not very portable.  I wonder
then, if there is a higher level method of defining such a beast?  Any help
(even "no that can't be done") would be appreciated.


Bruce Florman	-----------------------------	florman@rand-unix.ARPA
Technical Details
Message-ID: <364@randvax.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!princeton!caip!think!nike!ucbcad!ucbvax!hplabs!sdcrdcf!randvax!florman

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!princeton!caip!think!nike!ucbcad!ucbvax!hplabs!sdcrdcf!randvax!florman
From: florman@randvax.UUCP (Bruce Florman)
Newsgroups: net.lang.forth
Subject: Defining a structure in FORTH?
Message-ID: <364@randvax.UUCP>
Date: Thu, 12-Jun-86 18:23:40 EDT
Article-I.D.: randvax.364
Posted: Thu Jun 12 18:23:40 1986
Date-Received: Tue, 17-Jun-86 07:54:59 EDT
Distribution: net
Organization: Rand Corp., Santa Monica
Lines: 37