Debian Codegraph @

comlib/macros.lisp

raw
1 ;;;-*- Mode:Lisp; Syntax:Common-Lisp; Package:CHAOS -*-
2 ;;;
3 ;;; Copyright (c) 2000-2015, Toshimi Sawada. All rights reserved.
4 ;;;
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8 ;;;
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11 ;;;
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16 ;;;
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ;;;
29 (in-package :CHAOS)
30 #|==============================================================================
31 System: Chaos
32 Module: comlib
33 File: macros.lisp
34 ==============================================================================|#
35 #-:chaos-debug
36 (declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
37 #+:chaos-debug
38 (declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))
39
40 ;;;== DESCTIPTION ==============================================================
41 ;;; A collection of misc utility macros.
42
43 ;;; *******
44 ;;; DECLAIM_____________________________________________________________________
45 ;;; *******
46 ;;;#-(or cltl2 GCL)
47 ;;;(defmacro declaim (arg) `(proclaim ',arg))
48
49 ;;; ****************
50 ;;; FAMOUS ONCE-ONLY____________________________________________________________
51 ;;; ****************
52
53 (defmacro once-only (vars &body body)
54 (let ((gensym-var (gensym))
55 (run-time-vars (gensym))
56 (run-time-vals (gensym))
57 (expand-time-val-forms ()))
58 (dolist (var vars)
59 (push `(if (or (symbolp ,var)
60 (numberp ,var)
61 (and (listp ,var)
62 (member (car ,var) '(quote function))))
63 ,var
64 (let ((,gensym-var (gensym)))
65 (push ,gensym-var ,run-time-vars)
66 (push ,var ,run-time-vals)
67 ,gensym-var))
68 expand-time-val-forms))
69 `(let* (,run-time-vars
70 ,run-time-vals
71 (wrapped-body
72 (let ,(mapcar #'list vars (reverse expand-time-val-forms))
73 ,@body)))
74 `(let ,(mapcar #'list (reverse ,run-time-vars)
75 (reverse ,run-time-vals))
76 ,wrapped-body))))
77
78 ;;; *********************************
79 ;;; Specialized MEMBER/ASSOC/POSITION___________________________________________
80 ;;; *********************************
81
82 #-:ccl
83 (defmacro memq (item list) `(member ,item ,list :test #'eq))
84 #-:ccl
85 (defmacro assq (item list) `(assoc ,item ,list :test #'eq))
86 (defmacro posq (item list) `(position ,item ,list :test #'eq))
87 (defmacro memeq (item list) `(member ,item ,list :test #'equal))
88 (defmacro asseq (item list) `(assoc ,item ,list :test #'equal))
89 (defmacro poseq (item list) `(position ,item ,list :test #'equal))
90
91 ;;; **********
92 ;;; CASE-EQUAL__________________________________________________________________
93 ;;; **********
94 ;;; same as normal `case' test by equal instead of eql.
95
96 (defmacro case-equal (keyform &rest clauses &aux (form nil) (key (gensym)))
97 (dolist (clause (reverse clauses) `(let ((,key ,keyform)) ,form))
98 #+GCL (declare (object clause))
99 (cond ((or (eq (car clause) 't) (eq (car clause) 'otherwise))
100 (setq form `(progn ,@(cdr clause))))
101 ((consp (car clause))
102 (setq form `(if (member ,key ',(car clause) :test #'equal)
103 (progn ,@(cdr clause))
104 ,form)))
105 ((car clause)
106 (setq form `(if (equal ,key ',(car clause))
107 (progn ,@(cdr clause))
108 ,form)))))
109 )
110
111
112 ;;; **************
113 ;;; DOTIMES-FIXNUM______________________________________________________________
114 ;;; **************
115 ;;; FROM OBJ3 implementation by Tim.Winkler of SRI.
116 ;;; I don't know how this could be efficient in general, but for GCL(KCL..) this
117 ;;; works well.
118 ;;;
119 (defmacro dotimes-fixnum (&rest body)
120 (let ((var (car (car body)))
121 (lim (cadr (car body)))
122 (res (cddr (car body)))
123 (acts (cdr body))
124 (limvar (gensym))
125 (lab (gensym)))
126 ` (block ()
127 (let* ((,limvar ,lim) (,var 0))
128 (declare (type fixnum ,var ,limvar))
129 (tagbody
130 ,lab
131 (if (>= ,var ,limvar) (return (progn ,@res)))
132 (tagbody ,@acts)
133 (setf (the fixnum ,var) (the fixnum (1+ (the fixnum ,var))))
134 (go ,lab)))))
135 )
136
137 ;;; ****************
138 ;;; Syntactic Sugars____________________________________________________________
139 ;;; ****************
140
141 (defmacro msetq (vars value)
142 `(multiple-value-setq ,vars ,value))
143
144 (defmacro mlet (vars value &body body)
145 `(multiple-value-bind ,vars ,value ,@body))
146
147 ;;; let-if
148 ;;; Binds let arguments only if condition is non-nil, and evaluates body
149 ;;; in any case.
150
151 (defmacro let-if (condition bindings &body body)
152 `(if ,condition
153 (let ,bindings
154 ,@body)
155 (progn ,@(if (eq (caar body) 'declare) (cdr body) body))))
156
157 ;;; when-bind
158 ;;; Binds the symbol to predicate and executes body only if predicate
159 ;;; is non-nil."
160
161 (defmacro when-bind ((symbol predicate) &body body)
162 `(let ((,symbol ,predicate))
163 (when ,symbol
164 ,@body)))
165
166 ;;; while
167 ;;; Keeps invoking the body while the test is true; test is tested before each
168 ;;; loop.
169
170 ;;;; #-(or :allegro-v6.0 :allegro-v6.1 :allegro-v6.2 :allegro-v7.0 :allegro-v8.0)
171 #-(or :allegro)
172 (defmacro while (test &body body)
173 (let ((end-test (gensym))
174 (loop (gensym)))
175 `(block nil
176 (tagbody (go ,end-test)
177 ,loop
178 ,@body
179 ,end-test
180 (when ,test (go ,loop))
181 (return)))))
182
183 ;;; while-not
184 ;;; Keeps invoking the body while the test is not true; test is tested before
185 ;;; each loop.
186
187 (defmacro while-not (test &body body)
188 (let ((end-test (gensym))
189 (loop (gensym)))
190 `(block nil
191 (tagbody (go ,end-test)
192 ,loop
193 ,@body
194 ,end-test
195 (unless ,test (go ,loop))
196 (return)))))
197
198 ;;; def-synonym sym1 sym2
199 ;;;
200 (defmacro def-synonym (sym1 sym2)
201 `(setf (symbol-function ',sym1)
202 (symbol-function ',sym2)))
203
204 ;;;
205 ;;; #-:excl
206 #-(or :excl ccl)
207 (defmacro fixnump (num)
208 `(typep ,num 'fixnum))
209
210 ;;; EOF