// Catch Code. // // File: catch.cc // Author: course // Version: 2 #include "lisp.h" // Pointer to top of catch_area stack. NULL is stack // empty. Initialized to NULL. // catch_area * catch_area::current; void catch_area::initialize() { current = NULL; } // Save everything in catch_area. Push this catch_area // into the catch_area stack, and return the address of // the jmp_buf in this catch area for use by setjmp. // jmp_buf_pointer catch_area::save (object * tag) { catch_save = catch_area::current; preserve_save = preserve_stack::current; environment_save = environment; this->tag = tag; value = NULL; catch_area::current = this; return (this->jmp_save); } // Restore everything in this catch_area and return the // value of the area. // object * catch_area::restore () { catch_area::current = catch_save; preserve_stack::current = preserve_save; environment = environment_save; return (value); } // Throw a value to a tag. A search is made of the // catch stack for a catch area with the same tag, // and the first one found is restored. If none are // found, error is called. // void lisp_throw (object * tag, object * value) { for ( catch_area * area = catch_area::current; area != NULL; area = area->catch_save ) { if (tag == area->tag) { area->value = value; longjmp (area->jmp_save, 1); } } error ( "Throw cannot find catch with tag: ", tag); }