/usr/share/common-lisp/source/kmrcl/macros.lisp is in cl-kmrcl 1.106-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
;;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
;;;; Name: gentils.lisp
;;;; Purpose: Main general utility functions for KMRCL package
;;;; Programmer: Kevin M. Rosenberg
;;;; Date Started: Apr 2000
;;;;
;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
;;;;
;;;; KMRCL users are granted the rights to distribute and use this software
;;;; as governed by the terms of the Lisp Lesser GNU Public License
;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
;;;; *************************************************************************
(in-package #:kmrcl)
(defmacro let-when ((var test-form) &body body)
`(let ((,var ,test-form))
(when ,var ,@body)))
(defmacro let-if ((var test-form) if-true &optional if-false)
`(let ((,var ,test-form))
(if ,var ,if-true ,if-false)))
;; Anaphoric macros
(defmacro aif (test then &optional else)
`(let ((it ,test))
(if it ,then ,else)))
(defmacro awhen (test-form &body body)
`(aif ,test-form
(progn ,@body)))
(defmacro awhile (expr &body body)
`(do ((it ,expr ,expr))
((not it))
,@body))
(defmacro aand (&rest args)
(cond ((null args) t)
((null (cdr args)) (car args))
(t `(aif ,(car args) (aand ,@(cdr args))))))
(defmacro acond (&rest clauses)
(if (null clauses)
nil
(let ((cl1 (car clauses))
(sym (gensym)))
`(let ((,sym ,(car cl1)))
(if ,sym
(let ((it ,sym)) ,@(cdr cl1))
(acond ,@(cdr clauses)))))))
(defmacro alambda (parms &body body)
`(labels ((self ,parms ,@body))
#'self))
(defmacro aif2 (test &optional then else)
(let ((win (gensym)))
`(multiple-value-bind (it ,win) ,test
(if (or it ,win) ,then ,else))))
(defmacro awhen2 (test &body body)
`(aif2 ,test
(progn ,@body)))
(defmacro awhile2 (test &body body)
(let ((flag (gensym)))
`(let ((,flag t))
(while ,flag
(aif2 ,test
(progn ,@body)
(setq ,flag nil))))))
(defmacro acond2 (&rest clauses)
(if (null clauses)
nil
(let ((cl1 (car clauses))
(val (gensym))
(win (gensym)))
`(multiple-value-bind (,val ,win) ,(car cl1)
(if (or ,val ,win)
(let ((it ,val)) ,@(cdr cl1))
(acond2 ,@(cdr clauses)))))))
(defmacro mac (form &key (stream *standard-output*) (full nil) (width 80)
(downcase t)
&environment env)
(multiple-value-bind (expanded expanded-p)
(funcall (if full #'macroexpand #'macroexpand-1) form env)
(write expanded
:stream stream
:pretty t
:right-margin width
:case (if downcase :downcase :upcase)
:length nil
:level nil
:circle nil
:gensym nil)
(fresh-line stream)
expanded-p))
(defmacro print-form-and-results (form)
(let ((r (gensym "RES-")))
`(let ((r ,form))
(format t "~&~A --> ~S~%" ',form r)
r)))
;;; Loop macros
(defmacro until (test &body body)
`(do ()
(,test)
,@body))
(defmacro while (test &body body)
`(do ()
((not ,test))
,@body))
(defmacro for ((var start stop) &body body)
(let ((gstop (gensym "STOP-")))
`(do ((,var ,start (1+ ,var))
(,gstop ,stop))
((> ,var ,gstop))
,@body)))
(defmacro with-each-stream-line ((var stream) &body body)
(let ((eof (gensym "EOF-"))
(eof-value (gensym "EOF-VALUE-"))
(strm (gensym "STREAM-")))
`(let ((,strm ,stream)
(,eof ',eof-value))
(do ((,var (read-line ,strm nil ,eof) (read-line ,strm nil ,eof)))
((eql ,var ,eof))
,@body))))
(defmacro with-each-file-line ((var file) &body body)
(let ((stream (gensym)))
`(with-open-file (,stream ,file :direction :input)
(with-each-stream-line (,var ,stream)
,@body))))
(defmacro in (obj &rest choices)
(let ((insym (gensym)))
`(let ((,insym ,obj))
(or ,@(mapcar #'(lambda (c) `(eql ,insym ,c))
choices)))))
(defmacro mean (&rest args)
`(/ (+ ,@args) ,(length args)))
(defmacro with-gensyms (syms &body body)
`(let ,(mapcar #'(lambda (s) `(,s (gensym ,(format nil "~A-" s))))
syms)
,@body))
(defmacro time-seconds (&body body)
(let ((t1 (gensym)))
`(let ((,t1 (get-internal-real-time)))
(values
(progn ,@body)
(coerce (/ (- (get-internal-real-time) ,t1)
internal-time-units-per-second)
'double-float)))))
(defmacro time-iterations (n &body body)
(let ((i (gensym))
(count (gensym)))
`(progn
(let ((,count ,n))
(format t "~&Test with ~d iterations: ~W" ,count (quote ,body))
(let ((t1 (get-internal-real-time)))
(dotimes (,i ,count)
,@body)
(let* ((t2 (get-internal-real-time))
(secs (coerce (/ (- t2 t1)
internal-time-units-per-second)
'double-float)))
(format t "~&Total time: ")
(print-seconds secs)
(format t ", time per iteration: ")
(print-seconds (coerce (/ secs ,n) 'double-float))))))))
(defmacro mv-bind (vars form &body body)
`(multiple-value-bind ,vars ,form
,@body))
;; From USENET
(defmacro deflex (var val &optional (doc nil docp))
"Defines a top level (global) lexical VAR with initial value VAL,
which is assigned unconditionally as with DEFPARAMETER. If a DOC
string is provided, it is attached to both the name |VAR| and the
name *STORAGE-FOR-DEFLEX-VAR-|VAR|* as a documentation string of
kind 'VARIABLE. The new VAR will have lexical scope and thus may
be shadowed by LET bindings without affecting its global value."
(let* ((s0 (load-time-value (symbol-name '#:*storage-for-deflex-var-)))
(s1 (symbol-name var))
(p1 (symbol-package var))
(s2 (load-time-value (symbol-name '#:*)))
(backing-var (intern (concatenate 'string s0 s1 s2) p1)))
`(progn
(defparameter ,backing-var ,val ,@(when docp `(,doc)))
,@(when docp
`((setf (documentation ',var 'variable) ,doc)))
(define-symbol-macro ,var ,backing-var))))
(defmacro def-cached-vector (name element-type)
(let ((get-name (concat-symbol "get-" name "-vector"))
(release-name (concat-symbol "release-" name "-vector"))
(table-name (concat-symbol "*cached-" name "-table*"))
(lock-name (concat-symbol "*cached-" name "-lock*")))
`(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar ,table-name (make-hash-table :test 'equal))
(defvar ,lock-name (kmrcl::make-lock ,name))
(defun ,get-name (size)
(kmrcl::with-lock-held (,lock-name)
(let ((buffers (gethash (cons size ,element-type) ,table-name)))
(if buffers
(let ((buffer (pop buffers)))
(setf (gethash (cons size ,element-type) ,table-name) buffers)
buffer)
(make-array size :element-type ,element-type)))))
(defun ,release-name (buffer)
(kmrcl::with-lock-held (,lock-name)
(let ((buffers (gethash (cons (array-total-size buffer)
,element-type)
,table-name)))
(setf (gethash (cons (array-total-size buffer)
,element-type) ,table-name)
(cons buffer buffers))))))))
(defmacro def-cached-instance (name)
(let* ((new-name (concat-symbol "new-" name "-instance"))
(release-name (concat-symbol "release-" name "-instance"))
(cache-name (concat-symbol "*cached-" name "-instance-table*"))
(lock-name (concat-symbol "*cached-" name "-instance-lock*")))
`(eval-when (:compile-toplevel :load-toplevel :execute)
(defvar ,cache-name nil)
(defvar ,lock-name (kmrcl::make-lock ',name))
(defun ,new-name ()
(kmrcl::with-lock-held (,lock-name)
(if ,cache-name
(pop ,cache-name)
(make-instance ',name))))
(defun ,release-name (instance)
(kmrcl::with-lock-held (,lock-name)
(push instance ,cache-name))))))
(defmacro with-ignore-errors (&rest forms)
`(progn
,@(mapcar
(lambda (x) (list 'ignore-errors x))
forms)))
(defmacro ppmx (form)
"Pretty prints the macro expansion of FORM."
`(let* ((exp1 (macroexpand-1 ',form))
(exp (macroexpand exp1))
(*print-circle* nil))
(cond ((equal exp exp1)
(format t "~&Macro expansion:")
(pprint exp))
(t (format t "~&First step of expansion:")
(pprint exp1)
(format t "~%~%Final expansion:")
(pprint exp)))
(format t "~%~%")
(values)))
(defmacro defconstant* (sym value &optional doc)
"Ensure VALUE is evaluated only once."
`(defconstant ,sym (if (boundp ',sym)
(symbol-value ',sym)
,value)
,@(when doc (list doc))))
(defmacro defvar-unbound (sym &optional (doc ""))
"defvar with a documentation string."
`(progn
(defvar ,sym)
(setf (documentation ',sym 'variable) ,doc)))
|