summaryrefslogtreecommitdiffstats
path: root/share
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2015-07-23 07:33:49 -0700
committerKaz Kylheku <kaz@kylheku.com>2015-07-23 07:33:49 -0700
commit594b7bf1f4d88cdc2073cfccdf9b279892537461 (patch)
tree760bfb2718c26fe66602ce1ea251e4093ec40e0e /share
parentd6760da67732419d8043d1fdc43e8081f96ef1f1 (diff)
downloadtxr-594b7bf1f4d88cdc2073cfccdf9b279892537461.tar.gz
txr-594b7bf1f4d88cdc2073cfccdf9b279892537461.tar.bz2
txr-594b7bf1f4d88cdc2073cfccdf9b279892537461.zip
Adding with-resources macro.
* share/txr/stdlib/with-resources.tl: New file. * lisplib.c (with_resource_set_entries, with_resources_instantiate): New static functions. (lisplib_init): Register new functions under dlt_register. * txr.1: Document with-resources.
Diffstat (limited to 'share')
-rw-r--r--share/txr/stdlib/with-resources.tl17
1 files changed, 17 insertions, 0 deletions
diff --git a/share/txr/stdlib/with-resources.tl b/share/txr/stdlib/with-resources.tl
new file mode 100644
index 00000000..2a8c6791
--- /dev/null
+++ b/share/txr/stdlib/with-resources.tl
@@ -0,0 +1,17 @@
+(defmacro with-resources (res-bindings . body)
+ (tree-case res-bindings
+ (((var init cleanup) . rest)
+ ^(let ((,var ,init))
+ (when ,var
+ (unwind-protect
+ (with-resources ,rest ,*body)
+ ,cleanup))))
+ (((var init) . rest)
+ ^(let ((,var ,init))
+ (with-resources ,rest ,*body)))
+ ((var . rest)
+ ^(let (,var)
+ (with-resources ,rest ,*body)))
+ (nil
+ ^(progn ,*body))
+ (other (error "with-resources: bad syntax"))))