-
Notifications
You must be signed in to change notification settings - Fork 0
/
anaphoric-macros.lisp
84 lines (71 loc) · 2.02 KB
/
anaphoric-macros.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
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
(defmacro aif (test-form then-form &optional else-form)
`(let ((it ,test-form))
(if it ,then-form ,else-form)))
(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 ablock (tag &rest args)
`(block ,tag
,(funcall (alambda (args)
(case (length args)
(0 nil)
(1 (car args))
(t `(let ((it ,(car args)))
,(self (cdr args))))))
args)))
(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))
(awhile ,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)))))))
(let ((g (gensym)))
(defun read2 (&optional (str *standard-input*))
(let ((val (read str nil g)))
(unless (equal val g) (values val t)))))
(defmacro do-file (filename &body body)
(let ((str (gensym)))
`(with-open-file (,str ,filename)
(awhile2 (read2 ,str)
,@body))))
(defun our-load (filename)
(do-file filename (eval it)))