summaryrefslogtreecommitdiffstats
path: root/share/txr/stdlib/except.tl
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2015-10-14 06:49:33 -0700
committerKaz Kylheku <kaz@kylheku.com>2015-10-14 06:49:33 -0700
commit9cef0547ba0ec81ce6051bb1cba9db5671e08e64 (patch)
treec7f9cf081f07026c01d8aba741c78e67d3b376eb /share/txr/stdlib/except.tl
parentd34d09ef504ba2728821ce5b806bb59f22f378d7 (diff)
downloadtxr-9cef0547ba0ec81ce6051bb1cba9db5671e08e64.tar.gz
txr-9cef0547ba0ec81ce6051bb1cba9db5671e08e64.tar.bz2
txr-9cef0547ba0ec81ce6051bb1cba9db5671e08e64.zip
New way of handling exceptions without unwinding.
* eval.c (handler_bind_s): New symbol variable. (op_handler_bind): New static function. (do_expand): Traverse handler-bind forms. (eval_init): Initialize handler_bind_s variable and register handler-bind operator. * lisplib.c (except_set_entries, except_instantiate): New functions. (lisplib_init): Register new functions in dl_table. * parser.c (intr_s): New symbol variable. (repl_intr): Throw exception of type intr, rather than error. This way we can interrupt accidental exception handling loops involving exceptions derived from error. (parse_init): Initialize intr_s. * share/txr/stdlib/except.tl: New file, defines handle macro. * unwind.c (uw_push_handler): New function. (invoke_handler): New static function. (uw_throw): Search loop looks for and processes handlers in addition to catches. * unwind.h (uw_frtype_t): New enum member, UW_HANDLE. (struct uw_catch): Move member visible so it is in the same position as in struct uw_handler. (struct uw_handler): New struct type. (union uw_frame): New member ha of type struct uw_handler. (uw_push_handler): Function declared. * txr.1: Added introductory paragraphs to Exception Handling section. Documented handler-bind and handle. Some minor errors corrected.
Diffstat (limited to 'share/txr/stdlib/except.tl')
-rw-r--r--share/txr/stdlib/except.tl45
1 files changed, 45 insertions, 0 deletions
diff --git a/share/txr/stdlib/except.tl b/share/txr/stdlib/except.tl
new file mode 100644
index 00000000..e710cacd
--- /dev/null
+++ b/share/txr/stdlib/except.tl
@@ -0,0 +1,45 @@
+;; Copyright 2015
+;; Kaz Kylheku <kaz@kylheku.com>
+;; Vancouver, Canada
+;; All rights reserved.
+;;
+;; Redistribution of this software in source and binary forms, with or without
+;; modification, is permitted provided that the following two conditions are met.
+;;
+;; Use of this software in any manner constitutes agreement with the disclaimer
+;; which follows the two conditions.
+;;
+;; 1. Redistributions of source code must retain the above copyright
+;; notice, this list of conditions and the following disclaimer.
+;; 2. Redistributions in binary form must reproduce the above copyright
+;; notice, this list of conditions and the following disclaimer in
+;; the documentation and/or other materials provided with the
+;; distribution.
+;;
+;; THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+;; WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+;; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE
+;; COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DAMAGES, HOWEVER CAUSED,
+;; AND UNDER ANY THEORY OF LIABILITY, ARISING IN ANY WAY OUT OF THE USE OF THIS
+;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+(defun sys:handle-bad-syntax (item)
+ (throwf 'eval-error "~s: bad clause syntax: ~s" 'handle item))
+
+(defmacro handle (:whole form try-form . handle-clauses)
+ (let* ((exc-sym (gensym))
+ (exc-args (gensym))
+ (syms-fragments (collect-each ((hc handle-clauses))
+ (tree-case hc
+ ((name arglist . body)
+ (unless (symbolp name)
+ (sys:handle-bad-syntax hc))
+ (list name ^(apply (lambda ,arglist ,*body)
+ ,exc-sym ,exc-args)))
+ (else (sys:handle-bad-syntax hc))))))
+ ^(handler-bind (lambda (,exc-sym . ,exc-args)
+ (cond
+ ,*(mapcar (aret ^((exception-subtype-p ,exc-sym ',@1) ,@2))
+ syms-fragments)))
+ ,[mapcar car syms-fragments]
+ ,try-form)))