-
Notifications
You must be signed in to change notification settings - Fork 0
/
for.lisp
95 lines (78 loc) · 2.7 KB
/
for.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
85
86
87
88
89
90
91
92
93
94
95
;;; *The FOR macro*
;;;
;;;This macro serves as simplification macro for some of the complicated
;;;LISP mapping function for iterative processing.
;;;
;;;Eq. (for (x :in l) :do (foo x))
;;; -> (mapc #'foo l)
;;;
;;; (for (x :in l) :filter (foo x))
;;; -> (mapcan #'(lambda (x)
;;; (let ((x (foo x)))
;;; (if x (list x) nil))) l)
;;;
;;;General syntax:
;;;
;;; (FOR { (variable :IN list) }*
;;; :WHEN test
;;; { :DO | :SAVE | :FILTER | :SPLICE } expression)
;;;
(declaim (optimize speed (safety 0)))
(in-package "PS")
;; (export '(For-Vars For-Args For Var-Form-P Var-Form-Var Var-Form-Arg For-Test
;; For-Type For-Body For-Item Make-Mapfn Use-Remove-If-Not-P
;; Make-Body Add-Test Make-Lambda))
(defun For-Vars (l)
(mapcan #'(lambda (x)
(if (var-form-p x)
(list (var-form-var x)) nil)) l))
(defmacro For (&rest l)
(let ((vars (for-vars l))
(args (for-args l))
(test (for-test l))
(type (for-type l))
(body (for-body l)))
`(,(make-mapfn vars test type body)
#',(make-lambda vars
(add-test test
(make-body vars test type body))) ,@args)))
(defun For-Args (l)
(mapcan #'(lambda (x)
(if (var-form-p x)
(list (var-form-arg x)) nil)) l))
(defun Var-Form-P (x)
(and (consp x) (= (length x) 3) (eq (cadr x) :in)))
(defun Var-Form-Var (x) (car x))
(defun Var-Form-Arg (x) (caddr x))
(defun For-Test (l)
(cadr (for-item '(:when) l)))
(defun For-Type (l)
(let ((item (for-item '(:do :save :splice :filter) l)))
(cond (item (car item))
(t (error t "No body in FOR-loop!")))))
(defun For-Body (l)
(let ((item (for-item '(:do :save :splice :filter) l)))
(cond (item (cadr item))
(t (error t "No body in FOR-loop!")))))
(defun For-Item (keywords l)
(some #'(lambda (key) (member key l)) keywords))
(defun Make-Mapfn (vars test type body)
(cond ((eq type :do) 'mapc)
((not (eq type :save)) 'mapcan)
((null test) 'mapcar)
((use-remove-if-not-p vars body) 'remove-if-not)
(t 'mapcan)))
(defun Use-Remove-If-Not-P (vars body)
(and (= (length vars) 1) (equal (car vars) body)))
(defun Make-Body (vars test type body)
(cond ((eq type :filter)
`(let ((x ,body)) (if x (list x) nil)))
((or (not (eq type :save)) (null test)) body)
((use-remove-if-not-p vars body) nil)
(t `(list ,body))))
(defun Add-Test (test body)
(cond ((null test) body)
((null body) test)
(t `(if ,test ,body nil))))
(defun Make-Lambda (vars body)
`(lambda ,vars ,body))