Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!zaphod.mps.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!SUN.COM!wmb From: wmb@SUN.COM (Mitch Bradley) Newsgroups: comp.lang.forth Subject: Re: Allowing control structures in interpret state Message-ID: <8912301510.AA00667@jade.berkeley.edu> Date: 29 Dec 89 22:53:43 GMT Sender: daemon@ucbvax.BERKELEY.EDU Reply-To: Forth Interest Group International List Organization: The Internet Lines: 61 Here's an implementation of interpreted control structures that can be loaded directly on top of F83 (and presumably F-PC too). Although conceived and developed entirely independently from Tom Almy's recently-posted version, it is remarkably similar in many respects. In fact, the actual version that I use inside my own kernel is even more similar to Tom's version (in terms of error handling and location of the transient area away from HERE). You are free to use this code for any purpose whatever, including commercial use. For several years, I have been encouraging other implementors to include this facility in all their systems, just because it is so easy and useful that it is stupid for Forth not to have it. I have also proposed that the ANSI standard remove the "compilation only" restriction from control structures, based on the use of this technique. (If the proposal passes, I'll be pretty surprised, though). To guard against aborts, it's a good idea to include the following code inside QUIT: level @ if saved-dp @ here - allot then For more information, see: Interpreting Control Structures - The Right Way, 1987 FORML Proceedings. variable saved-dp variable level : +level ( -- ) level @ if \ If in compile state, just increment level 1 level +! else state @ 0= if \ If interpret state, switch to compile state 1 level ! here saved-dp ! \ Remember the start r> ['] ] >body >r >r \ Execute ] after the caller then ; : -level ( -- ) state @ 0= abort" Conditionals not paired" level @ if -1 level +! level @ 0= if compile exit \ Finish the definition saved-dp @ here - allot \ Reclaim the memory [compile] [ \ Enter interpret state here >r \ Execute the definition then then ; : begin +level [compile] begin ; immediate : do +level [compile] do ; immediate : ?do +level [compile] ?do ; immediate : if +level [compile] if ; immediate : then [compile] then -level ; immediate : loop [compile] loop -level ; immediate : +loop [compile] +loop -level ; immediate : until [compile] until -level ; immediate : again [compile] again -level ; immediate : repeat [compile] repeat -level ; immediate