[comp.lang.forth] Suggested kernel implementation using CATCH and THROW

wmb@MITCH.ENG.SUN.COM (01/05/91)

\ Deep in the kernel...
: ABORT  TRUE THROW  ;

...

\ Toward the end of the kernel...
: INTERACT  QUERY INTERPRET  ;
: QUIT
   RP0 @ RP!
   BEGIN
      ['] INTERACT CATCH  IF  ." Aborted"  ELSE  " OK"  THEN
      CR
   AGAIN
;

THROW need not test for a null handler because there is always
a handler at the top level.  Also note that the CATCH in QUIT
automatically cleans up the data stack!

Furthermore, with ABORT defined in terms of THROW (instead of
needing to call QUIT), the kernel no longer has ANY forward
references, so the metacompiler can be simpler (if that matters).

The other (and this is a BIG one) advantage of defining ABORT
in terms of THROW is that the user program can CATCH system aborts.

When I changed my kernel implementation to use this technique, the
kernel got smaller, because it gave me the ability to release
resources at exactly the right places.  In particular, I do a CATCH
in INCLUDE-FILE .  If it receives a THROW , it closes the file and
re-executes the THROW , thus neatly handling the "clean up nested
includes" problem.  I do a similar thing to release allocated memory
buffers, although that tends to be an application problem rather than
a kernel problem.

Mitch Bradley, wmb@Eng.Sun.COM