Path: utzoo!attcan!uunet!decwrl!ucbvax!MITCH.ENG.SUN.COM!wmb From: wmb@MITCH.ENG.SUN.COM (Mitch Bradley) Newsgroups: comp.lang.forth Subject: Re: Formatting Forth source code Message-ID: <9008011408.AA07945@ucbvax.Berkeley.EDU> Date: 1 Aug 90 06:40:22 GMT Sender: daemon@ucbvax.BERKELEY.EDU Reply-To: Mitch Bradley Organization: The Internet Lines: 44 Wil Baden> I'm looking for short but non-trivial examples of code that you Wil Baden> believe is readable. I hereby submit the following implementation of CATCH/THROW. Go to it, Wil! \ CATCH/THROW Error Handling Wordset \ by Mitch Bradley \ \ This implementation uses the non-standard words SP@ , SP! , RP@ , and \ RP! . These words, or their equivalents, are present in most systems. \ Another implementation which does not use those non-standard words \ follows this implementation. \ Thanks to Don Colburn and Dean Sanderson for implementation suggestions. VARIABLE HANDLER \ Most recent error handler (should be a USER variable) : CATCH ( cfa -- error# | 0 ) ( cfa ) \ Return address is already on the stack SP@ >R ( cfa ) \ Save data stack pointer EXCEPTION @ >R ( cfa ) \ Previous handler RP@ HANDLER ! ( cfa ) \ Set current handler to this one EXECUTE ( ) \ Execute the word passed in on the stack R> HANDLER ! ( ) \ Restore previous handler R> DROP ( ) \ Discard saved stack pointer 0 ( 0 ) \ Signify normal completion ; : THROW ( ??? error# -- ??? error# ) \ Returns in saved context ?DUP IF HANDLER @ RP! ( err# ) \ Return to saved return stack context R> HANDLER ! ( err# ) \ Restore previous handler \ Remember error# on return stack before changing data stack pointer R> SWAP >R ( saved-sp ) \ err# is on return stack SP! R> ( err# ) \ Change stack pointer \ This return will return to the caller of catch, because the return \ stack has been restored to the state that existed when CATCH began \ execution . THEN ;