-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd549f1
commit 3aa32b6
Showing
3 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
(in-package :serapeum) | ||
|
||
(defun letf-bindings (bindings env) | ||
(with-collectors (savers setters restorers) | ||
(loop for (place expr) in bindings do | ||
(multiple-value-bind (vars vals stores setter getter) | ||
(get-setf-expansion place env) | ||
(let ((saves (make-gensym-list (length stores)))) | ||
(mapc #'savers (mapcar #'list vars vals)) | ||
(savers | ||
`(,@saves ,getter)) | ||
(setters | ||
`(mvlet ((,@stores ,expr)) | ||
,setter)) | ||
(restorers | ||
`(mvlet ((,@stores (values ,@saves))) | ||
,setter))))))) | ||
|
||
(defmacro letf* (bindings &body body) | ||
(if (no bindings) `(locally ,@body) | ||
`(letf ,(firstn 1 bindings) | ||
(letf* ,(rest bindings) | ||
,@body)))) | ||
|
||
(defmacro letf (bindings &body body &environment env) | ||
(if (no bindings) `(locally ,@body) | ||
(mvlet ((savers setters restorers (letf-bindings bindings env))) | ||
`(mvlet* ,savers | ||
(unwind-protect | ||
(progn | ||
,@setters | ||
(locally ,@body)) | ||
,@restorers))))) | ||
|
||
(comment | ||
(letf (((car xs) 1) | ||
((cdr ys) 2)) | ||
(list xs ys))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters