/[cl-ipc]/cl-ipc/util.lisp
ViewVC logotype

Contents of /cl-ipc/util.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Fri Sep 9 08:50:06 2005 UTC (8 years, 7 months ago) by skamphausen
Branch: MAIN
Branch point for: cl-ipc
Initial revision
1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: CL-USER; Base: 10 -*-
2
3 ;;; $Header: /tiger/var/lib/cvsroots/cl-ipc/cl-ipc/util.lisp,v 1.1 2005/09/09 08:50:06 skamphausen Exp $
4 ;;; $Id: util.lisp,v 1.1 2005/09/09 08:50:06 skamphausen Exp $
5
6 ;;; Copyright (C) 2005 by
7 ;;; Dr. Edmund Weitz http://weitz.de
8 ;;; All rights reserved.
9
10 ;;; Redistribution and use in source and binary forms, with or without
11 ;;; modification, are permitted provided that the following conditions
12 ;;; are met:
13
14 ;;; * Redistributions of source code must retain the above copyright
15 ;;; notice, this list of conditions and the following disclaimer.
16
17 ;;; * Redistributions in binary form must reproduce the above
18 ;;; copyright notice, this list of conditions and the following
19 ;;; disclaimer in the documentation and/or other materials
20 ;;; provided with the distribution.
21
22 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
23 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
26 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34 (in-package :cl-ipc)
35
36 #+:lispworks
37 (import 'lw:with-unique-names)
38
39 #-:lispworks
40 (defmacro with-unique-names ((&rest bindings) &body body)
41 "Syntax: WITH-UNIQUE-NAMES ( { var | (var x) }* ) declaration* form*
42
43 Executes a series of forms with each VAR bound to a fresh,
44 uninterned symbol. The uninterned symbol is as if returned by a call
45 to GENSYM with the string denoted by X - or, if X is not supplied, the
46 string denoted by VAR - as argument.
47
48 The variable bindings created are lexical unless special declarations
49 are specified. The scopes of the name bindings and declarations do not
50 include the Xs.
51
52 The forms are evaluated in order, and the values of all but the last
53 are discarded \(that is, the body is an implicit PROGN)."
54 ;; reference implementation posted to comp.lang.lisp as
55 ;; <cy3bshuf30f.fsf@ljosa.com> by Vebjorn Ljosa - see also
56 ;; <http://www.cliki.net/Common%20Lisp%20Utilities>
57 `(let ,(mapcar #'(lambda (binding)
58 (check-type binding (or cons symbol))
59 (if (consp binding)
60 (destructuring-bind (var x) binding
61 (check-type var symbol)
62 `(,var (gensym ,(etypecase x
63 (symbol (symbol-name x))
64 (character (string x))
65 (string x)))))
66 `(,binding (gensym ,(symbol-name binding)))))
67 bindings)
68 ,@body))
69
70 #+:lispworks
71 (eval-when (:compile-toplevel :load-toplevel :execute)
72 (setf (macro-function 'with-rebinding)
73 (macro-function 'lw:rebinding)))
74
75 #-:lispworks
76 (defmacro with-rebinding (bindings &body body)
77 "WITH-REBINDING ( { var | (var prefix) }* ) form*
78
79 Evaluates a series of forms in the lexical environment that is
80 formed by adding the binding of each VAR to a fresh, uninterned
81 symbol, and the binding of that fresh, uninterned symbol to VAR's
82 original value, i.e., its value in the current lexical environment.
83
84 The uninterned symbol is created as if by a call to GENSYM with the
85 string denoted by PREFIX - or, if PREFIX is not supplied, the string
86 denoted by VAR - as argument.
87
88 The forms are evaluated in order, and the values of all but the last
89 are discarded \(that is, the body is an implicit PROGN)."
90 ;; reference implementation posted to comp.lang.lisp as
91 ;; <cy3wv0fya0p.fsf@ljosa.com> by Vebjorn Ljosa - see also
92 ;; <http://www.cliki.net/Common%20Lisp%20Utilities>
93 (loop for binding in bindings
94 for var = (if (consp binding) (car binding) binding)
95 for name = (gensym)
96 collect `(,name ,var) into renames
97 collect ``(,,var ,,name) into temps
98 finally (return `(let ,renames
99 (with-unique-names ,bindings
100 `(let (,,@temps)
101 ,,@body))))))
102

  ViewVC Help
Powered by ViewVC 1.1.5