-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwith.lisp
72 lines (61 loc) · 2.51 KB
/
with.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
;;; Copyright 2020 Google LLC
;;;
;;; Use of this source code is governed by an MIT-style
;;; license that can be found in the LICENSE file or at
;;; https://opensource.org/licenses/MIT.
;;; A universal WITH macro.
;;;
(defpackage #:ace.core.with
(:use #:cl
#:ace.core
#:ace.core.macro)
(:export
#:with
#:defcleanup))
(in-package #:ace.core.with)
(defgeneric cleanup (init-function-symbol &rest variables)
(:documentation
"For a given INIT-FUNCTION-SYBMOL form and VARIABLES' symbols
generate the corresponding cleanup form."))
(defun %cleanup-form (init-form var-symbols)
(expect (typep init-form '(cons symbol)))
(if (consp var-symbols)
(apply #'cleanup (car init-form) var-symbols)
(cleanup (car init-form) var-symbols)))
(defmacro defcleanup (init-function-symbol (&rest variables) &body body)
"For a given INIT-FUNCTION-SYMBOL and VARIABLES binding allocated resources,
generate a cleanup form function. The DEFCLEANUP macro accepts
a list of symbols for the VARIABLES or a '&REST var-symbols' for an undefined
number of VARIABLES. E.g.:
(defcleanup init (&rest vars)
`(release ,@vars))
is a cleanup definition for an INIT function that returns a indefinite number
of values. Only the first of the VARS is used to determine if RELEASE should
be executed. The remaining VARS act as parameters.
"
(with-gensyms (sym vars)
`(eval-always
(defmethod cleanup ((,sym (eql ',init-function-symbol)) &rest ,vars)
(destructuring-bind ,variables ,vars
`(when ,(car ,vars)
,,@body))))))
(defmacro with ((&rest bindings) &body body)
"With evaluates BINDINGS successively binding each variable
to the corresponding init-form value. If the value is NIL,
the evaluation of successive bindings and the BODY aborts.
With provides an UNWIND-PROTECT environment where the cleanup
is populated with forms generated using the WITH:CLEANUP generic function.
The cleanup forms are evaluated in reverse order to the BINDINGS
and are evaluated only if the corresponding (first) value was not NIL.
The bindings can have a multi-value-form. E.g.:
(with (((v0 v1) (reserve-resource :foo)))
#| the body is evaluated if V0 is not NIL. |#)
"
`(let ,(lconc ((v) bindings) (if (consp v) v `(,v)))
(unwind-protect
(when (and ,@(lmap ((v b) bindings)
`(setf ,(if (consp v) `(values ,@v) v) ,b)))
(locally ,@body))
:cleanup
,@(lmap ((vars init-form) (reverse bindings))
(%cleanup-form init-form vars)))))