-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfor-helper.el
233 lines (202 loc) · 9.3 KB
/
for-helper.el
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
;;; for-helper.el --- Helper -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Wing Hei Chan
;; Author: Wing Hei Chan <[email protected]>
;; URL: https://github.com/usaoc/elisp-for
;; Keywords: extensions
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see
;; <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file provides internal helpers.
;;; Code:
;;;; Require
(require 'cl-lib)
(eval-when-compile
(require 'subr-x))
;;;; Interface
(defmacro for--defspecial (name arglist docstring &rest cases-or-body)
"Define the special clause operator NAME.
ARGLIST, DOCSTRING, and CASES-OR-BODY are as in
`define-for-special-clause' forms.
\(fn NAME ARGLIST DOCSTRING [CASES-OR-BODY...])"
(declare (debug define-for-special-clause)
(doc-string 3) (indent 2))
`(define-for-special-clause ,name ,arglist
,(concat docstring "\n\n"
"See Info node `(for)Special-Clause Operators'.")
. ,cases-or-body))
(defmacro for--defspecial-let (name head docstring)
"Define the special clause operator NAME.
HEAD is a `let'-like macro. DOCSTRING is the documentation string."
(declare (debug (&define [&name keywordp] symbolp))
(doc-string 3) (indent 2))
(cl-flet ((make-def (name head)
`(for--defspecial ,name (#1=#:body)
,(format docstring head)
(`(,_ . ,#2=#:bindings)
,(let ((body `((,head . ,'(,#2# . ,#1#)))))
(list '\` body)))))
(make-star (name)
(intern (concat (symbol-name name) "*"))))
`(prog1 ,(make-def name head)
,(make-def (make-star name) (make-star head)))))
(defmacro for--defmacro (name arglist docstring decl &rest body)
"Define the iteration macro NAME with the starred version.
ARGLIST, DOCSTRING, DECL, and BODY are as in normal `defmacro'
forms.
\(fn NAME ARGLIST DOCSTRING DECL BODY...)"
(declare (debug defmacro) (doc-string 3) (indent 2))
(let ((docstring
(let ((extra (concat (pcase name
('for-do "BODY = [BODY-FORM...]
BODY-FORM = SPECIAL-CLAUSE | EXPRESSION
FOR-CLAUSES = ([FOR-CLAUSE...])")
(_ "\
BODY = [[BODY-FORM...] MULTIPLE-VALUE-FORM]
BODY-FORM = SPECIAL-CLAUSE | EXPRESSION
FOR-CLAUSES = ([[FOR-CLAUSE...] MULTIPLE-VALUE-FORM])"))
"\n\n" "\
FOR-CLAUSE = SPECIAL-CLAUSE | ITERATION-CLAUSE
SPECIAL-CLAUSE = (KEYWORD [SUBFORM...])
ITERATION-CLAUSE = ([IDENTIFIER] SEQUENCE-FORM)
See Info node `(for)Iteration Macros'.")))
(save-match-data
(if (string-match (rx bol "..." eol) docstring)
(replace-match extra 'fixedcase 'literal docstring)
(let ((arglist (mapconcat
(lambda (symbol)
(upcase (symbol-name symbol)))
(remq '&rest arglist)
" ")))
(concat docstring "\n\n" extra "\n\n"
"(fn " arglist ")")))))))
`(prog1 (defmacro ,name ,arglist ,docstring ,decl . ,body)
(defmacro ,(intern (concat (symbol-name name) "*")) ,arglist
,docstring ,decl
(cl-flet ((for--parse-body (for-clauses body)
(pcase-let ((`(,for-clauses . ,value-form)
(for--parse-body for-clauses body)))
`(,(for--nest-for-clauses for-clauses)
. ,value-form))))
. ,body)))))
(defmacro for--defseq (name arglist docstring &rest subforms)
"Define a sequence constructor NAME with ARGLIST and DOCSTRING.
A SUBFORM in SUBFORMS can be either a `:type', `:expander', or
`:expander-case' form as in `define-for-sequence' forms.
\(fn NAME ARGLIST DOCSTRING [DECLARATION] [SUBFORM...] [BODY...])"
(declare (debug define-for-sequence) (doc-string 3) (indent 2))
(let ((extra "See Info node `(for)Sequence Constructors'."))
(cl-flet* ((make-arg (arg) (upcase (symbol-name arg)))
(make-rest (arg) (concat "[" (make-arg arg) "...]"))
(make-docstring (args)
(concat docstring "\n\n" extra "\n\n"
"(fn " (string-join args " ") ")")))
`(define-for-sequence ,name ,arglist
,(save-match-data
(cond ((string-match (rx bol "..." eol) docstring)
(replace-match extra
'fixedcase 'literal docstring))
((memq '&optional arglist)
(make-docstring
(named-let parse ((arglist arglist))
(pcase-exhaustive arglist
('() '())
(`(&optional . ,arglist)
`(,(named-let parse ((arglist arglist))
(pcase-exhaustive arglist
(`(,arg)
(concat "[" (make-arg arg) "]"))
(`(&rest ,arg) (make-rest arg))
(`(,arg . ,arglist)
(concat "[" (make-arg arg) " "
(parse arglist) "]"))))))
(`(,arg . ,arglist)
`(,(make-arg arg) . ,(parse arglist)))))))
((memq '&rest arglist)
(make-docstring
(named-let parse ((arglist arglist))
(pcase-exhaustive arglist
('() '())
(`(&rest ,arg) `(,(make-rest arg)))
(`(,arg . ,arglist)
`(,(make-arg arg) . ,(parse arglist)))))))
(t (concat docstring "\n\n" extra))))
. ,(pcase-let
(((or `(,(and `(declare . ,_) declaration)
. ,subforms)
(and subforms
(let declaration
'(declare (side-effect-free t)))))
subforms))
`(,declaration
(:alias ,(intern (string-remove-prefix
"for-" (symbol-name name))))
. ,subforms))))))
(defmacro for--in-list (id form)
"Return an iteration clause suitable for (ID (in-list FORM))."
(cl-flet ((\, (form) (list '\, form)))
(cl-flet ((make-id (form)
(pcase form
((or `(,id ,_) id) (make-symbol (symbol-name id)))))
(make-binding (id form)
(pcase form ((or `(,form ,_) form) `(,,id ,,form))))
(make-default (id form)
(pcase form
(`(,_ ,default) `((,,id (or ,,id ,default))))
(_ '()))))
(pcase form
((or (and `(,head . ,(and (app (mapcar #'make-id) ids)
(app (cl-mapcar #'make-binding ids)
bindings)
(app (cl-mapcan #'make-default ids)
defaults)))
(let tail-form `(,head . ,(mapcar #'\, ids))))
(and (let list '#:list)
(let ids `(,list))
(let bindings `((,,list ,,form)))
(let defaults '())
(let tail-form ,list)))
(let ((tail '#:tail))
`(for--with-gensyms (,@ids ,tail)
,(list '\` `(,,id (:do-in ,bindings ,defaults
((,,tail ,tail-form)) (,,tail)
((,,id (car ,,tail)))
((cdr ,,tail))))))))))))
(defmacro for--with-gensyms (names &rest body)
"Bind NAMEs in NAMES to generated identifiers and evaluate BODY.
\(fn (NAME...) BODY...)"
(declare (debug ((&rest symbolp) body)) (indent 1))
`(let ,(mapcar (lambda (name) `(,name (gensym ,(symbol-name name))))
names)
. ,body))
(pcase-defmacro for--funcall (&rest args)
"Matches EXPVAL as an arglist according to ARGS."
(named-let parse ((mand-args '()) (args args))
(pcase args
(`(&optional . ,opt-args)
(cl-flet
((\, (form) (list '\, form))
(args-pat (args) (list '\` (cons (list '\, '_) args))))
(let ((args (mapcar #'\, (nreverse mand-args))))
(named-let build
((args args) (opt-args opt-args) (pat (args-pat args)))
(pcase opt-args
('() pat)
(`(,arg . ,opt-args)
(let ((args `(,@args ,,arg)))
(build args opt-args
`(or (and ,pat (let ,arg nil))
,(args-pat args))))))))))
(`(,arg . ,args) (parse `(,arg . ,mand-args) args)))))
;;;; Provide
(provide 'for-helper)
;;; for-helper.el ends here