From 594b7bf1f4d88cdc2073cfccdf9b279892537461 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 23 Jul 2015 07:33:49 -0700 Subject: 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. --- share/txr/stdlib/with-resources.tl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 share/txr/stdlib/with-resources.tl (limited to 'share') 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")))) -- cgit v1.2.3