-
Notifications
You must be signed in to change notification settings - Fork 0
/
expand.lisp
246 lines (226 loc) · 7.2 KB
/
expand.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
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
(in-package #:explicit-bind-expander)
(defvar *expanders* (make-hash-table :test 'eq))
(defclass expander ()
((%name :initarg :name
:reader name
:type (and symbol (not null)))
(%function :initarg :function
:reader function
:type (or cl:function symbol))
(%specs-lambda-list :initarg :specs-lambda-list
:reader specs-lambda-list
:type list)
(%forms-lambda-list :initarg :forms-lambda-list
:reader forms-lambda-list
:type list)))
(defun find (name &key (errorp t))
(check-type name symbol)
(or (gethash name *expanders*)
(when errorp
(error "There is no ~S called ~S." 'expander name))))
(defun (setf find) (new name &key (errorp t))
(declare (ignore errorp))
(check-type new expander)
(check-type name symbol)
(setf (gethash name *expanders*) new))
(defmacro define (name ((op &rest specifications) &rest forms) &body body)
(etypecase name
((cons (eql or))
`(progn ,@(mapcar
(lambda (name)
`(define ,name
((,op ,@specifications)
,@forms)
,@body))
(cdr name))))
(symbol
(let ((binding (gensym (string '#:binding))))
`(setf (find ',name)
(make-instance
'expander
:name ',name
:function
(lambda (,binding)
(unless (typep ,binding '(cons (cons (eql ,name) t) t))
(error "Invalid binding spec ~S ~
passed to ~S ~S."
,binding ',name 'expander))
(destructuring-bind
((,op ,@specifications) ,@forms) ,binding
,@body))
:specs-lambda-list ',specifications
:forms-lambda-list ',forms))))))
(defun expand (binding)
(typecase binding
((cons (cons symbol t) t)
(funcall (function (find (caar binding))) binding))
((cons symbol t)
(destructuring-bind (var init-form) binding
(expand `((variable ,var) ,init-form))))
(t (error "Don't know how to ~S ~S." 'expand binding))))
(define declare ((op &rest declaration-specifiers))
(%expand-special-form op declaration-specifiers))
(define shadow ((op &rest binding-names))
(%expand-special-form op binding-names))
(define progn ((op &body forms))
(%expand-special-form op forms))
(define (or variable
cl:function
ignore
ignorable
special
dynamic-extent
inline
notinline)
((op specification) form)
(%expand-v/f-spec `(,op ,specification) form))
(define the ((op type-specifier specification) form)
(%expand-v/f-spec `(,op ,type-specifier ,specification) form))
(define values ((op &rest specifications) form)
(%expand-v/f-spec `(,op ,@specifications) form))
(define :destructuring ((op pattern) form)
#+nil(%expand-v/f-spec `(,op ,pattern) form)
(list `(,op ,pattern ,form)))
(define :accessors ((op specifications) form)
(list `(,op ,specifications ,form)))
(define :slots ((op specifications) form)
(list `(,op ,specifications ,form)))
(defun %expand-special-form (name specifications &optional forms)
(check-type forms null)
(list (cons name specifications)))
(defun %expand-v/f-spec (specification form)
(flet ((extract-decls (analyzed)
(values (butlast analyzed)
(car (last analyzed))))
(conc (analyzed declarations)
(cons analyzed
(when declarations
(list `(declare ,@declarations))))))
(let ((analyzed (%analyze-v/f-spec specification)))
(case (length analyzed)
(0 (list `(progn ,form)))
(1 (multiple-value-bind (analyzed declarations)
(extract-decls (first analyzed))
(conc (append analyzed (list form))
declarations)))
(t (let (declarationss analyzeds)
(dolist (analyzed analyzed
(conc `(:bindings ,(nreverse analyzeds)
,form)
(apply #'append
(nreverse declarationss))))
(multiple-value-bind (analyzed declarations)
(extract-decls analyzed)
(push analyzed analyzeds)
(push declarations declarationss)))))))))
(defun %analyze-v/f-spec (spec)
(case (let ((count 0))
(labels ((recurse (tree)
(cond ((consp tree)
(mapc #'recurse tree))
((eq tree 'values)
(incf count)))))
(recurse spec))
count)
(0)
(1 (setf spec (bubble-op-up:bubble-operator-upwards 'values spec)))
(t (error "The symbol ~S was found more than once in ~S."
'values
spec)))
(let ((undeclared
(load-time-value (gensym (string "undeclared")))))
(labels
((var-spec (kind name declare-flags type-decl)
(list kind
name
(let ((declarations
(mapcar
(ecase kind
(variable
(lambda (flag)
(case flag
((inline notinline)
(error "Attempted to declare ~
variable ~S ~S.~@
Only functions ~
can be declared ~:*~S."
name flag))
(t (list flag name)))))
(cl:function
(lambda (flag)
(list flag
(ecase flag
((ignore ignorable dynamic-extent)
`#',name)
((inline notinline)
name))))))
(nreverse declare-flags))))
(if (eq type-decl undeclared)
declarations
(cons (list (ecase kind
(variable 'type)
(cl:function 'ftype))
type-decl
name)
declarations)))))
(analyze (kind spec declare-flags type-decl)
(etypecase spec
(symbol
(if kind
(var-spec kind spec declare-flags type-decl)
(analyze 'variable spec declare-flags type-decl)))
((cons (eql setf))
(cond ((not (symbolp (second spec)))
(error "Second part of SETF function name, ~
~S, is not a symbol."
spec))
((not kind)
(error "~A should be ~A."
(prin1-to-string spec)
(prin1-to-string `#',spec)))
((eq kind 'variable)
(error "~S is a function name, not a variable name."
spec))
((eq kind 'cl:function)
(var-spec kind spec declare-flags type-decl))))
(cons
(let ((operator (first spec)))
(ecase operator
(:destructuring
(error "Sorry, ~S ~S is only ~
implemented at \"top-level\" at this time."
'bind :destructuring))
(the (analyze
kind
(third spec)
declare-flags
(if (eq type-decl undeclared)
(second spec)
(flet ((normalize (type)
(if (typep type '(cons (eql and)))
(cdr type)
(list type))))
`(and ,@(normalize type-decl)
,@(normalize (second spec)))))))
((variable cl:function)
(if kind
(if (eq operator kind)
(error "Duplicate ~S kind declaration."
kind)
(error "Contradictory ~S and ~S kind declarations."
kind operator))
(analyze operator
(second spec)
declare-flags
type-decl)))
((ignore ignorable special dynamic-extent inline notinline)
(check-type (cddr spec) null)
(analyze kind
(second spec)
(cons operator declare-flags)
type-decl))))))))
(mapcar (lambda (spec)
(analyze nil spec nil undeclared))
(if (typep spec '(cons (eql values)))
(cdr spec)
(list spec))))))