Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!watnot!ccplumb From: ccplumb@watnot.UUCP Newsgroups: comp.lang.forth Subject: An elegant ?DO Message-ID: <12205@watnot.UUCP> Date: Wed, 19-Nov-86 19:17:09 EST Article-I.D.: watnot.12205 Posted: Wed Nov 19 19:17:09 1986 Date-Received: Thu, 20-Nov-86 00:59:25 EST Reply-To: ccplumb@watnot.UUCP (Colin Plumb) Distribution: net Organization: U of Waterloo, Ontario Lines: 47 A while ago, in my second posting about "Forth control structures - a proposal" in net.lang.forth, I made some seriously ugly suggestions for implementing ?DO. Tom Almy (tektronix!tekgvs!toma) pointed out to me that ther is a *much* nicer way to do it: If you just have ?DO put the address that needs to be patched to point to past the end of the loop in the DO-LIST, LOOP (or +LOOP) will handle this quite nicely, along with resolving the LEAVEs. Code for this is as follows: : ?DO ( -- old-list-header back-branch-ptr) COMPILE (?DO) ( ) ( ?DO run-time part) DO-LIST DUP @ ( 'DO-LIST old-list-header) ( save old list header) SWAP DUP 0! ( old-list-header 'DO-LIST) ( Clear DO-LIST) >LISTMARK ( old-list-header) ( put current address in DO-LIST) , ;CODE , etc. (for any other words you may have that wrap up a definition. They probably all call ; to do their work for them, so if you can change that in the source, it should fix everything.) P.S. If you haven't got the original postings, this probably makes no sense - I'd be glad to mail them to you and un-confuse you. -Colin Plumb (ccplumb@watnot.UUCP) Zippy says: ...I see TOILET SEATS...