-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext.lisp
56 lines (50 loc) · 1.85 KB
/
ext.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
;;; -*- Mode: Lisp; Package: USER -*-
;;;
;;; PPMX - pretty prints a macro expansion
;;;
;;; From the book "Common Lisp: A Gentle Introduction to
;;; Symbolic Computation" by David S. Touretzky.
;;; The Benjamin/Cummings Publishing Co., 1990.
;;;
;;; Example of use: (ppmx (incf a))
;;; http://www.cs.cmu.edu/~dst/Lisp/ppmx.lisp (2013-03-21)
(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)))
;;;
;;; WITH-GEMSYS - this macro is useful for writing other macros
;;;
;;; From the book "Practical Common Lisp" by Peter Seibel
;;; Similar to its namesake from Paul Graham's book “On Lisp”.
;;;
;;; Example of use (with-gensyms (start mid end) ...)
;;; http://www.gigamonkeys.com/book/macros-defining-your-own.html (2013-03-21)
(defmacro with-gensyms ((&rest names) &body body)
`(let ,(loop for n in names collect `(,n (gensym)))
,@body))
;;;
;;; ONCE-ONLY - this macro is used to generate code that evaluates
;;; certain macro arguments once only and in a particular order
;;;
;;; From the book "Practical Common Lisp" by Peter Seibel
;;;
;;;
;;; Example of use (once-only (start end) ... )
;;; http://www.gigamonkeys.com/book/macros-defining-your-own.html (2013-03-21)
(defmacro once-only ((&rest names) &body body)
(let ((gensyms (loop for n in names collect (gensym))))
`(let (,@(loop for g in gensyms collect `(,g (gensym))))
`(let (,,@(loop for g in gensyms for n in names collect ``(,,g ,,n)))
,(let (,@(loop for n in names for g in gensyms collect `(,n ,g)))
,@body)))))