[comp.lang.forth] Tagged CATCH

wmb@MITCH.ENG.SUN.COM (Mitch Bradley) (01/19/91)

> : NCATCH ( cfa errno -- errno/0 )
>   >R CATCH ( cfa errno -- retcode )
>   R> OVER - ( retcode -- retcode errno=retcode )
>   OVER AND IF ( retcode flag -- retcode )
>            THROW
>            THEN
> ;

Since "0 THROW" is a no-op, the above reduces to:

        : NCATCH  ( cfa errno -- errno/0 )
           >R CATCH DUP R> -  IF  THROW  THEN
        ;

I think efficiency considerations for this function are down in the noise.

> P.S.: Anyway, with the above code we have something similar to SETJMP.H C
> library, but not SIGNAL.H. A generic Catch would provide both features in
> one.  See, SIGNAL.H can be implemented with SETJMP.H...

Actually, signal handlers and CATCH/THROW are different things.
A signal handler usually executes, does something, and then control
returns to where it left off, as though nothing happened.  To this
end, the system signal handling mechanism has to create a new stack
context for the handler, rather than going back to some previously-
established context.

However, it is sometimes useful to execute THROW from a signal handler,
just as C signal handlers sometimes execute longjmp().  To this end, a
good Forth signal handling implementation would probably create the
"new" stack context on the stack of the task that receives the signal,
so if the handler does a THROW, the signal handler stack will be
automatically "deallocated".  Actually, with the usual implementation
of CATCH and THROW, it doesn't really matter where where put the signal
handler stack; so long as you have set the user pointer to the right
task (a necessity anyway), THROW will put you back in the right place.

Mitch

bouma@cs.purdue.EDU (William J. Bouma) (01/20/91)

>        : NCATCH  ( cfa errno -- errno/0 )
>           >R CATCH DUP R> -  IF  THROW  THEN
>        ;

  You win!
  Sorry I brought it up. 8^(
  I am still gonna use variables for error numbers, though.
-- 
Bill <bouma@cs.purdue.edu>

UNBCIC@BRFAPESP.BITNET (02/01/91)

Mitch, I think your implementation of TCATCH (NCATCH), that was small than
mine because 0 THROW is a do-nothing operation (is NOOP in ANS Forth? It
would be very useful for CFA's Tables...), was wrong. Unfortunely, I lost
your implementation. I think that there was an error because when CATCH
terminates succesfully (i.e. no 'true' THROWs), it returns 0.

                              (8-DCS)
Daniel C. Sobral
UNBCIC@BRFAPESP.BITNET

P.S.: I realized that your CForth has no Catch&Throw, although you claim
that it has error handling support; it's something like Catch&Throw?

wmb@MITCH.ENG.SUN.COM (02/04/91)

Daniel Sobral just convinced me of the error of my ways.  His implementation
of "tagged CATCH" is correct, and my "simplification" of his implementation
is incorrect.

Embarrassed,
Mitch Bradley, wmb@Eng.Sun.COM