-
Notifications
You must be signed in to change notification settings - Fork 0
/
star.lisp
528 lines (483 loc) · 21.8 KB
/
star.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
;;;; Štar
;;; New improved simpler version
;;;
#+org.tfeb.tools.require-module
(org.tfeb.tools.require-module:needs
(("pkg" "common")
:compile t))
#+org.tfeb.tools.require-module
(org.tfeb.tools.require-module:provides :org.tfeb.star)
#-org.tfeb.tools.require-module
(provide :org.tfeb.star)
(in-package :org.tfeb.star/impl)
;;;; Iterator protocol
;;;
;;; An iterator optimizer returns five values for an iterator
;;; - true if it has succeeded
;;; - a list of binding sets (see below)
;;; - a form which returns true if the cursor is valid
;;; - the cursor, which returns the next values
;;; - a wrapper function, or NIL which is called with the entire form
;;;
;;; A binding set is a list of two or more elements:
;;; - variables to bind (for MULTIPLE-VALUE-BIND)
;;; - a form
;;; - zero or more declarations
;;;
(defun make-iterator-optimizer-table (&optional (from nil))
"Make an iterator optimizer table
These are probably just hash-tables, but this means you don't have to
assume they are, and they can be suitably weak where implementations
support that"
(let ((table #+lispWorks (make-hash-table :weak-kind ':key)
#+SBCL (make-hash-table :weakness ':key)
#-(or LispWorks SBCL) (make-hash-table)))
(when from
(maphash (lambda (k v)
(setf (gethash k table) v))
from))
table))
(defvar *builtin-iterator-optimizer-table*
(make-iterator-optimizer-table))
(defparameter *star-bootstrap* t)
(defvar *iterator-optimizers*
(list (make-iterator-optimizer-table)
*builtin-iterator-optimizer-table*)
"The stack of iterator optimizer tables")
(defvar *enable-iterator-optimizers* t
"If true, iterator optimizers will be called at macroexpansion time")
(defun remove-iterator-optimizer (name table)
"Remove an optimizer named NAME from TABLE
It is not an error if it is not present."
(unless *star-bootstrap*
(when (eq table *builtin-iterator-optimizer-table*)
(restart-case
(star-error "Deleting optimizer from builtin table")
(continue ()
:report "do it anyway"))))
(remhash name table))
(defun get-iterator-optimizer (name table)
"Return an optimizer with name NAME in TABLE.
Second value is NIL if it is not present.
This is an accessor."
(gethash name table))
(defun (setf get-iterator-optimizer) (new name table)
(unless *star-bootstrap*
(when (eq table *builtin-iterator-optimizer-table*)
(restart-case
(star-error "Modifying optimizer from builtin table")
(continue ()
:report "do it anyway"))))
(setf (gethash name table) new))
(defun map-iterator-optimizer-table (function table)
"Map FUNCTION over TABLE.
FUNCTION takes two arguments, the optimizer name and the optimizer.
It is allowed to call REMOVE-ITERATOR-OPTIMIZER on the optimizer, but
not on any other one."
(maphash function table))
(defun find-iterator-optimizer (name &optional (optimizers *iterator-optimizers*))
"Find an iterator optimizer in the stack
Returns the optimizer, and the tail of the stack where it was found, or NIL and NIL."
(destructuring-match optimizers
(()
(values nil nil))
((table . more)
(multiple-value-bind (found foundp) (get-iterator-optimizer name table)
(if foundp
(values found optimizers)
(find-iterator-optimizer name more))))
(otherwise
(star-error "optimizer stack isn't"))))
(defmacro define-iterator-optimizer (name/table (form-name &optional
(env-name nil env-name-p)
(stack-name nil stack-name-p))
&body forms)
"Define an iterator optimizer for Štar
See the manual"
(multiple-value-bind (name table)
(destructuring-match name/table
((name table)
(:when (symbolp name))
(values name table))
(name
(:when (symbolp name))
(values name '(car *iterator-optimizers*)))
(otherwise
(syntax-error name/table "bad name / table")))
(multiple-value-bind (decls body) (parse-simple-body forms)
`(eval-when (:compile-toplevel :load-toplevel :execute)
;; There is a question about whether optimizers should be
;; defined before their functions: previously they were not,
;; now they are. See the manual.
(setf (get-iterator-optimizer ',name ,table)
,(cond
(stack-name-p
`(lambda (,form-name &optional ,env-name ,stack-name)
,@decls
(block ,name
,@body)))
(env-name-p
(with-names (<ignored-stack>)
`(lambda (,form-name &optional ,env-name ,<ignored-stack>)
(declare (ignore ,<ignored-stack>))
,@decls
(block ,name
,@body))))
(t
(with-names (<ignored-env> <ignored-stack>)
`(lambda (,form-name &optional ,<ignored-env> ,<ignored-stack>)
(declare (ignore ,<ignored-env> ,<ignored-stack>))
,@decls
(block ,name
,@body))))))
',name))))
(defun iterator-optimizers (iterator environment)
;; Return found values which explain how to optimize an iterator: a
;; list of binding sets, a valid form a cursor form and a wrapper, or NIL
(flet ((fallback-optimizers (valid-name cursor-name)
;; Note this only returns three forms, because it's the
;; fallback: it can't decline to handle it.
(values
;; binding set
`(((,valid-name ,cursor-name)
,iterator
(declare (type function ,valid-name ,cursor-name))))
;; form returning true if more
`(funcall ,valid-name)
;; form returning values
`(funcall ,cursor-name)
;; no wrapper
nil)))
(destructuring-match iterator
((f &rest _)
(:when (symbolp f))
(if *enable-iterator-optimizers*
(multiple-value-bind (found stack) (find-iterator-optimizer f)
(if found
(multiple-value-bind (handled binding-sets valid cursor wrapper)
(funcall found iterator environment stack)
(if handled
(values binding-sets valid cursor wrapper)
(fallback-optimizers
(make-symbol (format nil "~A-VALID" (symbol-name f)))
(make-symbol (format nil "~A-CURSOR" (symbol-name f))))))
(fallback-optimizers
(make-symbol (format nil "~A-VALID" (symbol-name f)))
(make-symbol (format nil "~A-CURSOR" (symbol-name f))))))
(fallback-optimizers
(make-symbol (format nil "~A-VALID" (symbol-name f)))
(make-symbol (format nil "~A-CURSOR" (symbol-name f))))))
(otherwise
(fallback-optimizers (make-symbol "VALID") (make-symbol "CURSOR"))))))
;;;; Štar itself
;;;
;;; Parsing clause descriptions
;;;
;;; We need to make sure iterators are unique objects, hence this hair
;;; of boxing them (could just wrap it in a cons of course)
;;;
(defstruct (unique-iterator
(:constructor unique-iterator (iterator)))
iterator)
(defstruct var
(name (catastrophe "no name for unique iterator"))
(type t)
(special-p nil)
(ignore-p nil)
(ignorable-p nil)
(anonymous-p nil)
(declarations '()))
(defstruct clause
(vars (catastrophe "no vars"))
(unique-iterator (catastrophe "no iterator")))
(defun anonymous-variable-p (s)
(string= (symbol-name s) "_"))
(defun parse-clause-description (clause-description)
;; Parse one clause description returning a CLAUSE, or NIL
(flet ((parse-complex-variable (variable args)
(destructuring-match args
((&key (type t) (anonymous nil anonymousp) (special nil)
(ignore nil) (ignorable nil)
(declarations '()))
(make-var :name variable
:type type
:special-p special
:ignore-p ignore
:ignorable-p ignorable
:anonymous-p (if anonymousp
anonymous
(anonymous-variable-p variable))
:declarations declarations))
(otherwise
(return-from parse-clause-description nil)))))
(destructuring-match clause-description
((variable iterator)
(:when (symbolp variable))
(make-clause :vars (list (make-var :name variable
:anonymous-p (anonymous-variable-p variable)))
:unique-iterator (unique-iterator iterator)))
(((variable &rest args &key name type anonymous special ifnore ignorable declarations)
iterator)
(:when (symbolp variable))
(declare (ignore name type anonymous special ifnore ignorable declarations))
(make-clause :vars (list (parse-complex-variable variable args))
:unique-iterator (unique-iterator iterator)))
(((&rest vardescs) iterator)
(make-clause :vars (mapcar (lambda (vardesc)
(destructuring-match vardesc
(variable
(:when (symbolp variable))
(make-var :name variable
:anonymous-p (anonymous-variable-p variable)))
((variable &rest args &key &allow-other-keys)
(:when (symbolp variable))
(parse-complex-variable variable args))
(otherwise
(return-from parse-clause-description nil))))
vardescs)
:unique-iterator (unique-iterator iterator)))
(otherwise nil))))
(defun parse-clause-descriptions (clause-descriptions)
;; return either NIL and NIL if things are hopeless, or NIL and a
;; list of offending clause descriptions, or T and the parsed
;; descriptions
(multiple-value-bind (goods bads)
(with-collectors (good bad)
(iterate next ((ctail clause-descriptions))
(typecase ctail
(null nil)
(cons
(destructuring-bind (this . more) ctail
(let ((parsed (parse-clause-description this)))
(if parsed
(good parsed)
(bad this))
(next more))))
(t
(return-from parse-clause-descriptions (values nil nil))))))
(if bads
(values nil bads)
(values t goods))))
(defstruct let-binding
(clauses '()))
(defstruct multiple-value-binding
(clause (catastrophe "need a clause")))
(defun clauses->bindings (clauses)
;; Coalesce a bunch of clauses into corresponding binding objects
(collecting
(iterate next ((ctail clauses)
(current '()))
(if (null ctail)
(unless (null current)
(collect (make-let-binding :clauses (nreverse current))))
(destructuring-bind (this . more) ctail
(case (length (clause-vars this))
(0
(catastrophe "empty binding?"))
(1
(next more (cons this current)))
(otherwise
(unless (null current)
(collect (make-let-binding :clauses (nreverse current))))
(collect (make-multiple-value-binding :clause this))
(next more '()))))))))
(defstruct (itable-entry
(:conc-name "IE-"))
;; an entry in the alist from unique iterator to information for it.
valid-form
cursor-form)
(defun call/compiling-iterator-optimizers (f unique-iterators environment)
;; call F, which should return a single form, returning its result
;; wrapped in suitable bindings for the optimizers F is called with
;; an alist which maps from unique-iterators to itable-entries.
;; This is where wrappers are handled
(if (null unique-iterators)
(funcall f '())
(iterate next-iterator ((itail unique-iterators)
(itable '()))
(if (null itail)
(funcall f (nreverse itable))
(destructuring-bind (this-iterator . more-iterators) itail
(multiple-value-bind (binding-sets valid-form cursor-form wrapper)
(iterator-optimizers (unique-iterator-iterator this-iterator) environment)
(funcall
(or wrapper #'identity)
(iterate next-binding-set ((bstail binding-sets))
(if (null bstail)
(next-iterator more-iterators (acons this-iterator
(make-itable-entry
:valid-form valid-form
:cursor-form cursor-form)
itable))
(destructuring-bind ((vars form &rest decls) . more-binding-sets) bstail
`(multiple-value-bind ,vars ,form
,@decls
,(next-binding-set more-binding-sets))))))))))))
(defmacro compiling-iterator-optimizers ((itable-var unique-iterators environment) &body forms)
`(call/compiling-iterator-optimizers
(lambda (,itable-var)
,@forms)
,unique-iterators
,environment))
(defun compile-bindings (body bindings itable)
;; Wrap forms corresponding BINDINGS around BODY, a list of forms.
;; ITABLE is an alist which maps from unique iterators to
;; itable-entries
(if (null bindings)
`(progn ,@body)
(iterate next-binding ((btail bindings))
(destructuring-bind (this-binding . more-bindings) btail
(etypecase this-binding
(let-binding
(multiple-value-bind (vars/inits declarations)
(with-collectors (var/init declaration)
(dolist (clause (let-binding-clauses this-binding))
(let ((cursor-form (let ((ie (cdr (assoc (clause-unique-iterator clause)
itable))))
(unless ie
(catastrophe "no cursor form"))
(ie-cursor-form ie))))
(destructuring-match (clause-vars clause)
((var)
(let ((variable (if (var-anonymous-p var)
(make-symbol (symbol-name (var-name var)))
(var-name var))))
(var/init `(,variable ,cursor-form))
(if (var-anonymous-p var)
(declaration `(declare (ignore ,variable)))
(progn
(unless (eq (var-type var) t)
(declaration `(declare (type ,(var-type var) ,variable))))
(when (var-special-p var)
(declaration `(declare (special ,variable))))
(when (var-ignore-p var)
(declaration `(declare (ignore ,variable))))
(when (var-ignorable-p var)
(declaration `(declare (ignorable ,variable))))
(when (not (null (var-declarations var)))
(declaration `(declare ,@(mapcar (lambda (declaration)
`(,declaration ,variable))
(var-declarations var)))))))))
(otherwise
(catastrophe "multiple varables in a let binding's clause"))))))
`(let ,vars/inits
,@declarations
,@(if (null more-bindings)
body
`(,(next-binding more-bindings))))))
(multiple-value-binding
(let* ((clause (multiple-value-binding-clause this-binding))
(cursor-form (let ((ie (cdr (assoc (clause-unique-iterator clause)
itable))))
(unless ie
(catastrophe "no cursor form"))
(ie-cursor-form ie))))
(multiple-value-bind (variables declarations)
(with-collectors (variable declaration)
(dolist (var (clause-vars clause))
(let ((variable (if (var-anonymous-p var)
(make-symbol (symbol-name (var-name var)))
(var-name var))))
(variable variable)
(if (var-anonymous-p var)
(declaration `(declare (ignore ,variable)))
(progn
(unless (eq (var-type var) t)
(declaration `(declare (type ,(var-type var) ,variable))))
(when (var-special-p var)
(declaration `(declare (special ,variable)))))))))
`(multiple-value-bind ,variables ,cursor-form
,@declarations
,@(if (null more-bindings)
body
`(,(next-binding more-bindings))))))))))))
(defmacro with-blocks (names &body forms)
;; Wrap a bunch of named blocks around something
(cond
((null names)
`(progn ,@forms))
((null (rest names))
`(block ,(first names) ,@forms))
(t
(let* ((name (first names))
(more-names (remove name (rest names))))
(if (null more-names)
`(block ,name ,@forms)
`(block ,name
(with-blocks ,more-names ,@forms)))))))
(defun expand-for (clauses body environment &key (top t) (name 'for))
(multiple-value-bind (ok clauses) (parse-clause-descriptions clauses)
(unless ok
(syntax-error clauses "bad clause or clauses"))
(with-names (<start> <end>)
(let ((bindings (clauses->bindings clauses)))
(compiling-iterator-optimizers (itable (mapcar #'clause-unique-iterator clauses)
environment)
(let ((compiled-bindings (compile-bindings body bindings itable)))
(if top
`(with-blocks (,name nil)
(flet ((final (&rest values)
(declare (dynamic-extent values))
(return (values-list values)))
(final* (&rest values)
(declare (dynamic-extent values))
(return-from ,name (values-list values))))
(declare (inline final final*)
(ignorable (function final) (function final*)))
(tagbody
,<start>
(unless ,(if (= (length itable) 1)
(ie-valid-form (cdr (first itable)))
`(and ,@(mapcar (lambda (ie)
(ie-valid-form (cdr ie)))
itable)))
;; some valid's aren't: we're done
(go ,<end>))
(flet ((next () (go ,<start>))
(next* () (go ,<start>)))
(declare (inline next next*)
(ignorable (function next) (function next*)))
,compiled-bindings)
(go ,<start>)
,<end>
nil)))
`(with-blocks (nil)
(flet ((final (&rest values)
(declare (dynamic-extent values))
(return (values-list values))))
(declare (inline final)
(ignorable (function final)))
(tagbody
,<start>
(unless ,(if (= (length itable) 1)
(ie-valid-form (cdr (first itable)))
`(and ,@(mapcar (lambda (ie)
(ie-valid-form (cdr ie)))
itable)))
;; some valid's aren't: we're done
(go ,<end>))
(flet ((next () (go ,<start>)))
(declare (inline next)
(ignorable (function next)))
,compiled-bindings)
(go ,<start>)
,<end>
nil))))))))))
(defmacro for (clauses &body body &environment environment)
"Iteration construct
See the manual."
(expand-for clauses body environment))
(defmacro for* (clauses &body body &environment environment)
"Nested iteration construct
See the manual."
(if (null clauses)
(expand-for clauses body environment :top t :name 'for*)
(iterate expand-for* ((ctail clauses)
(top t))
(destructuring-bind (this-clause . more-clauses) ctail
(if (null more-clauses)
(expand-for (list this-clause) body environment :top top :name 'for*)
(expand-for (list this-clause)
`(,(expand-for* more-clauses nil))
environment
:top top :name 'for*))))))