-
Notifications
You must be signed in to change notification settings - Fork 3
/
loopy-destructure.el
1637 lines (1516 loc) · 77.2 KB
/
loopy-destructure.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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; loopy-destructure.el --- Miscellaneous functions used with Loopy. -*- lexical-binding: t; -*-
;; LocalWords: plists alists plist seqs
;; Copyright (c) 2024 Earl Hyatt
;;; Disclaimer:
;; This file is not part of GNU Emacs.
;;
;; This file 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, or (at your option)
;; any later version.
;;
;; This file 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 file. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; `loopy' is a macro that is used similarly to `cl-loop'. It provides "loop
;; commands" that define a loop body and it's surrounding environment, as well
;; as exit conditions.
;;
;; This library provides features for destructuring for the features provided in
;; the main file. This separation exists for better organization.
;;; Code:
(require 'cl-lib)
(require 'map)
(require 'compat)
(require 'loopy-misc)
(require 'loopy-instrs)
(require 'pcase)
(require 'stream)
(require 'seq)
(require 'subr-x)
(declare-function loopy--parse-loop-command "ext:loopy-commands" (command))
;; This better allows for things to change in the future.
(defun loopy--var-ignored-p (var)
"Return whether VAR should be ignored for destructuring."
(and (symbolp var)
(eq (aref (symbol-name var) 0) ?_)))
(defconst loopy--destructure-symbols '( &seq &whole &optional &rest &body
&key &keys &allow-other-keys
&aux &map)
"Symbols affecting how following elements destructure.")
;; Having a single function for all categories allows us to have most of the
;; ordering rules in once place.
(defconst loopy--get-var-groups-cache (make-hash-table :test 'equal :size 300)
"Cache of variable groups in a pattern.
See also the function `loopy--get-var-groups'.")
(defun loopy--get-var-groups (var-seq)
"Return the alist of variable groups in sequence VAR-SEQ.
Type is one of `list' or `array'."
(or (gethash var-seq loopy--get-var-groups-cache nil)
(let* ((is-seq)
(whole-var) (processing-whole)
(pos-var)
(opt-var) (processing-opts)
(rest-var) (processing-rest) (dotted-rest-var)
(key-var) (processing-keys) (allow-other-keys)
(map-var) (processing-maps)
(aux-var) (processing-auxs)
(proper-list-p (proper-list-p var-seq))
(type (cl-etypecase var-seq
(list 'list)
(array 'array)))
(improper-list (and (eq type 'list)
(not proper-list-p)))
(remaining-seq (if improper-list
(cl-copy-list var-seq)
(copy-sequence var-seq))))
(when improper-list
(cl-shiftf dotted-rest-var
(cdr (last remaining-seq))
nil))
(cl-flet ((missing-after (seq) (or (seq-empty-p seq)
(memq (seq-elt seq 0)
loopy--destructure-symbols)))
(stop-processing () (setq processing-whole nil
processing-opts nil
processing-rest nil
processing-keys nil
processing-maps nil)))
;; Use `seq' functions to support arrays now and maybe other things later.
(while (not (seq-empty-p remaining-seq))
(seq-let [first &rest rest]
remaining-seq
(pcase first
;; Since `&seq' must be first, we could check for it outside of
;; processing, but we keep it with the other processing for
;; consistency.
('&seq (cond
((or is-seq whole-var pos-var opt-var rest-var key-var
allow-other-keys aux-var map-var)
(signal 'loopy-&seq-bad-position (list var-seq)))
((seq-empty-p rest)
(signal 'loopy-bad-desctructuring
(list var-seq)))
(t
(stop-processing)
(setq is-seq t))))
('&whole (cond
;; Make sure there is a variable named.
((missing-after rest)
(signal 'loopy-&whole-missing (list var-seq)))
;; Make sure `&whole' is before all else.
((or whole-var pos-var opt-var rest-var key-var
allow-other-keys aux-var map-var)
(signal 'loopy-&whole-bad-position (list var-seq)))
(t
(stop-processing)
(setq processing-whole t))))
('&optional (cond
((missing-after rest)
(signal 'loopy-&optional-missing
(list var-seq)))
;; Make sure `&optional' does not occur after
;; `&rest'.
((or opt-var rest-var key-var map-var aux-var)
(signal 'loopy-&optional-bad-position
(list var-seq)))
(t
(stop-processing)
(setq processing-opts t))))
((or '&rest '&body) (cond
(dotted-rest-var
(signal 'loopy-&rest-dotted
(list var-seq)))
((missing-after rest)
(signal 'loopy-&rest-missing
(list var-seq)))
((and (> (seq-length rest) 1)
(let ((after-var (seq-elt rest 1)))
(not (memq after-var loopy--destructure-symbols))))
(signal 'loopy-&rest-multiple (list var-seq)))
;; In CL Lib, `&rest' must come before `&key',
;; but we decided to allow it to come after.
((or aux-var rest-var)
(signal 'loopy-&rest-bad-position
(list var-seq)))
(t
(stop-processing)
(setq processing-rest t))))
((or '&key '&keys) (cond
((not (eq type 'list))
(signal 'loopy-&key-array
(list var-seq)))
((missing-after rest)
(signal 'loopy-&key-missing
(list var-seq)))
((or aux-var key-var)
(signal 'loopy-&key-bad-position
(list var-seq)))
(t
(stop-processing)
(setq processing-keys t))))
('&allow-other-keys (cond
((not (eq type 'list))
(signal 'loopy-&key-array
(list var-seq)))
((not processing-keys)
(signal 'loopy-&allow-other-keys-without-&key
(list var-seq)))
(t
(stop-processing)
(setq allow-other-keys t))))
('&map (cond
((missing-after rest)
(signal 'loopy-&map-missing (list var-seq)))
((or aux-var map-var)
(signal 'loopy-&map-bad-position
(list var-seq)))
(t
(stop-processing)
(setq processing-maps t))))
('&aux
(if (or (missing-after rest)
aux-var)
(signal 'loopy-&aux-bad-position (list var-seq))
(stop-processing)
(setq processing-auxs t)))
('&environment
(signal 'loopy-bad-desctructuring (list var-seq)))
((guard processing-whole)
(cond
((loopy--var-ignored-p first)
(signal 'loopy-&whole-missing (list var-seq)))
(t
(setq whole-var first
processing-whole nil))))
((guard processing-rest)
;; `&rest' var can be ignored for clarity,
;; but it is probably an error to ignore it
;; when there are no positional or optional variables.
(if (and (loopy--var-ignored-p first)
(null pos-var)
(null opt-var))
(signal 'loopy-&rest-missing
(list var-seq))
(setq rest-var first
processing-rest nil)))
((guard processing-opts)
(if (and (consp first)
(cdr first)
(loopy--var-ignored-p (car first)))
(signal 'loopy-&optional-ignored-default-or-supplied
(list var-seq))
(push first opt-var)))
((guard processing-keys)
(push first key-var))
((guard processing-maps)
(push first map-var))
((guard processing-auxs)
(push first aux-var))
(_
(if (or opt-var rest-var key-var map-var aux-var
allow-other-keys)
(signal 'loopy-bad-desctructuring (list var-seq))
(push first pos-var)))))
(setq remaining-seq (seq-rest remaining-seq))))
(let ((val `((whole . ,whole-var)
(pos . ,(nreverse pos-var))
(opt . ,(nreverse opt-var))
(rest . ,(or dotted-rest-var rest-var))
(key . ,(nreverse key-var))
(allow-other-keys . ,allow-other-keys)
(map . ,(nreverse map-var))
(aux . ,(nreverse aux-var))
(seq . ,is-seq))))
(puthash var-seq val loopy--get-var-groups-cache)
val))))
;; TODO: Turn these into records?
(defun loopy--get-&optional-spec (form)
"Get the spec of the `&optional' variable FORM as (VAR DEFAULT SUPPLIED LEN)."
(let ((var)
(default)
(supplied)
(len))
(loopy--pcase-let-workaround (var2 def2 sup2)
(pcase form
;; Uses `nil' if not long enough.
((and (seq var2 def2 sup2) form2) (setq var var2
default def2
supplied sup2
len (seq-length form2)))
(form2 (setq var form2
len 0))))
(list var default supplied len)))
(defun loopy--get-&key-spec (var-form)
"Get the spec of `&key' VAR-FORM as (KEY VAR DEFAULT SUPPLIED)."
(loopy--pcase-let-workaround (key var default supplied)
(pcase-let (((or (or (seq (seq key var) default supplied)
(seq (seq key var) default)
(seq (seq key var)))
(and (or (seq var default supplied)
(seq var default)
(seq var)
(and (pred symbolp)
var))
;; Strip a leading underscore, since it
;; only means that this argument is
;; unused, but shouldn't affect the
;; key's name (bug#12367).
(let key (if (seqp var)
(signal 'loopy-&key-key-from-sequence
(list var-form))
(intern
(format ":%s"
(let ((name (symbol-name var)))
(if (eq ?_ (aref name 0))
(substring name 1)
name))))))))
var-form))
(unless var
(signal 'loopy-&key-var-malformed
(list var-form)))
(list key var default supplied))))
(defun loopy--get-&map-spec (var-form)
"Get the spec of `&map' VAR-FORM as (KEY VAR DEFAULT SUPPLIED)."
(loopy--pcase-let-workaround (key var default supplied)
(pcase-let (((or (seq key var default supplied)
(seq key var default)
(seq key var)
(and (or (seq var)
(and (pred symbolp)
var))
(let key
(if (seqp var-form)
(signal 'loopy-&map-key-from-sequence
(list var-form))
`(quote ,var)))))
var-form))
(unless var
(signal 'loopy-&map-var-malformed
(list var-form)))
(list key var default supplied))))
(defun loopy--get-&aux-spec (var-form)
"Get the spec of `&aux' VAR-FORM as (VAR VAL)."
(loopy--pcase-let-workaround (var val)
(pcase-let (((or (seq var val)
(seq var)
(and (pred symbolp)
var))
var-form))
(unless var
(signal 'loopy-&aux-malformed-var (list var-form)))
(list var val))))
(defun loopy--get-var-list (var-seq)
"Get the variables in VAR-SEQ as a flat, unordered list."
(let ((groups (loopy--get-var-groups var-seq))
(result nil))
(cl-labels ((fn (val) (if (seqp val)
(dolist (val2 (loopy--get-var-list val))
(cl-pushnew val2 result :test #'eq))
(cl-pushnew val result)))
(opt-fn (val) (loopy--pcase-let-workaround (var supplied)
(seq-let [var _ supplied _]
(loopy--get-&optional-spec val)
(fn var)
(when supplied
(fn supplied)))))
(key-fn (val) (loopy--pcase-let-workaround (var supplied)
(seq-let [_ var _ supplied]
(loopy--get-&key-spec val)
(fn var)
(when supplied
(fn supplied)))))
(map-fn (val) (loopy--pcase-let-workaround (var supplied)
(seq-let [_ var _ supplied]
(loopy--get-&map-spec val)
(fn var)
(when supplied
(fn supplied)))))
(aux-fn (val) (loopy--pcase-let-workaround (var)
(seq-let [var _]
(loopy--get-&map-spec val)
(fn var)))))
(map-do (lambda (k v)
(when v
(pcase k
((or 'whole 'rest) (fn v))
('pos (mapc #'fn v))
('opt (mapc #'opt-fn v))
('key (mapc #'key-fn v))
('map (mapc #'map-fn v))
('aux (mapc #'aux-fn v))
((or 'seq 'allow-other-keys) nil)
(_ (error "Unknown key")))))
groups))
result))
;;;; Pcase pattern
(defun loopy--pcase-flip (fn arg2)
"Wrapper macro for compatibility with obsoletion of `pcase--flip'.
FN is the function. ARG2 is the argument to move to the second
postion of the call to FN in the pattern."
(static-if (>= emacs-major-version 30)
`(,fn _ ,arg2)
`(pcase--flip ,fn ,arg2)))
(defun loopy--get-var-pattern (var)
"Get the correct variable pattern.
If VAR is ignored according to `loopy--var-ignored-p', return
`_'. Otherwise, if VAR is a sequence according to `seqp',
return `(loopy VAR)'. In all other cases, VAR is returned."
(cond
((loopy--var-ignored-p var) '_)
((seqp var) `(loopy ,var))
(t var)))
;; TODO: Use this in `list' pattern.
(defun loopy--pcase-let-nil-list (pat)
"Return a list of patterns binding variables in PAT to nil."
;; Need to quote `nil' for it to be a `pcase' pattern.
(pcase pat
(`(loopy ,(and (pred seqp) seq))
(cl-loop for v in (loopy--get-var-list seq)
collect `(let ,v 'nil)))
(_
`((let ,pat 'nil)))))
(defun loopy--pcase-pat-positional-list-pattern (pos-vars opt-vars rest-var map-or-key-vars)
"Build a pattern for the positional, `&optional', and `&rest' variables.
POS-VARS is the list of the positional variables. OPT-VARS is the list of
the optional variables. REST-VAR is the `&rest' variable.
MAP-OR-KEY-VARS is whether there are map or key variables."
;; A modified version of the back-quote pattern to better work with
;; optional values.
(cond
(pos-vars `(and (pred consp)
(app car-safe ,(let ((var (car pos-vars)))
(loopy--get-var-pattern var)))
(app cdr-safe ,(loopy--pcase-pat-positional-list-pattern
(cdr pos-vars) opt-vars
rest-var map-or-key-vars))))
(opt-vars (loopy--pcase-let-workaround (var default supplied)
(pcase-let* ((`(,var ,default ,supplied ,_length)
(loopy--get-&optional-spec (car opt-vars)))
(var2 (loopy--get-var-pattern var)))
`(and (pred listp)
(app car-safe (or (and (pred null)
,@(when supplied
`((let ,supplied nil)))
(let ,var2 ,default))
,(if supplied
`(and (let ,supplied t)
,var2)
var2)))
(app cdr-safe ,(loopy--pcase-pat-positional-list-pattern
nil (cdr opt-vars)
rest-var map-or-key-vars))))))
(rest-var (loopy--get-var-pattern rest-var))
;; `pcase' allows `(,a ,b) to match (1 2 3), so we need to make
;; sure there aren't more values left. However, if we are using
;; `&key', then we allow more values.
(map-or-key-vars '_)
;; Unlike `cl-lib', we don't require that all of the positional values have a
;; corresponding variable/pattern. Instead, we do like `seq' and allow the
;; destructuring pattern to be shorter than the sequence.
(t '_)))
(defun loopy--pcase-pat-positional-array-pattern (pos-vars opt-vars rest-var map-or-key-vars)
"Build a pattern for the positional, `&optional', and `&rest' variables.
POS-VARS is the list of the positional variables. OPT-VARS is the list of
the optional variables. REST-VAR is the `&rest' variable.
MAP-OR-KEY-VARS is whether there are map or key variables."
(let ((pos-len (length pos-vars))
(opt-len (length opt-vars)))
;; We allow the variable form to be shorter than the
;; destructured sequence.
`(and (pred ,(loopy--pcase-flip (compat-function length>) (1- pos-len)))
,@(cl-loop for var in pos-vars
for idx from 0
collect `(app ,(loopy--pcase-flip 'aref idx)
,(loopy--get-var-pattern var)))
,@(when opt-vars
(let ((opt-var-specs (seq-into (mapcar #'loopy--get-&optional-spec
opt-vars)
'vector)))
`((or ,@(cl-loop with use->= = (or rest-var map-or-key-vars)
and pat-idx-low = pos-len
and spec-idx-max = (1- opt-len)
for checked-len from (+ pos-len opt-len) downto pos-len
for spec-idx-high downfrom (1- opt-len) to 0
collect
;; If the length matches, then all of the
;; remaining variables were supplied, then
;; the one variable was not supplied and we
;; need to check the remaining ones.
`(and ,(if use->=
`(pred ,(loopy--pcase-flip (compat-function length>) (1- checked-len)))
`(pred ,(loopy--pcase-flip (compat-function length=) checked-len)))
;; Variables that should be bound with the value in
;; the array.
,@(cl-loop
for spec-idx2 from 0 to spec-idx-high
for arr-idx from pat-idx-low
append (pcase-let* ((`(,var2 ,_ ,supplied2 ,len2)
(aref opt-var-specs spec-idx2))
(var3 (loopy--get-var-pattern var2)))
(if (= len2 3)
`((app ,(loopy--pcase-flip 'aref arr-idx)
,var3)
(let ,supplied2 t))
`((app ,(loopy--pcase-flip 'aref arr-idx)
,var3)))))
;; Variables that should be bound to nil or their
;; default.
,@(cl-loop
for spec-idx2 from (1+ spec-idx-high) to spec-idx-max
for arr-idx from pat-idx-low
append (pcase-let* ((`(,var2 ,default2 ,supplied2 ,len2)
(aref opt-var-specs spec-idx2))
(var3 (loopy--get-var-pattern var2)))
(pcase-exhaustive len2
(3 `((let ,var3 ,default2)
(let ,supplied2 nil)))
(2 `((let ,var3 ,default2)))
(_
;; (loopy--pcase-let-nil-list var3)
`((let ,var3 ,default2))
))))))
;; A pattern for when nothing matches.
(and ,@(cl-loop for spec across opt-var-specs
append (pcase-let* ((`(,var2 ,default2 ,supplied2 ,len2) spec)
(var3 (loopy--get-var-pattern var2)))
(pcase-exhaustive len2
(3
`((let ,var3 ,default2)
(let ,supplied2 nil)))
(2
`((let ,var3 ,default2)))
(_
`((let ,var3 ,default2))
;; (loopy--pcase-let-nil-list var3)
)))))))))
,@(when rest-var
(let ((len-sum (+ pos-len opt-len))
(rest-pat (loopy--get-var-pattern rest-var))
(seqsym (gensym "seqsym")))
;; Rec-checking the length is fast for arrays.
`((or (and (pred ,(loopy--pcase-flip (compat-function length>) len-sum))
(app ,(loopy--pcase-flip 'substring len-sum) ; 0-indexed
,rest-pat))
(app (lambda (,seqsym) (substring ,seqsym 0 0))
,rest-pat))))))))
(defun loopy--seq-length= (seq n)
"Check whether the length of SEQ is equal to N."
;; TODO: Simplify when `stream.el' is updated and streams are no longer
;; implemented as lists. See also `loopy--seq-length>'.
(cond
((streamp seq)
;; Avoid traversing long streams.
(let ((s (seq-drop seq (1- n))))
(and (not (stream-empty-p s))
(stream-empty-p (stream-rest s)))))
((listp seq)
(compat-call length= seq n))
(t
(= (seq-length seq) n))))
(defun loopy--seq-length> (seq n)
"Check whether the length of SEQ is greater than to N."
(cond
;; Test streams first, because version 2.3.0 of `stream.el' implements
;; streams as lists. Take advantage of lazy evaluation of streams.
((streamp seq) (not (stream-empty-p (seq-drop seq n))))
;; `length>' only seems to matter for lists, based on its definition.
((listp seq) (compat-call length> seq n))
(t (> (seq-length seq) n))))
(defun loopy--pcase-pat-positional-&seq-pattern (pos-vars opt-vars rest-var map-or-key-vars)
"Build a pattern for the positional, `&optional', and `&rest' variables.
Unlike the build-in `seq' pattern, we don't match the sequence
if the destructuring pattern is longer than the
destructured value.
POS-VARS is the list of the positional variables. OPT-VARS is the list of
the optional variables. REST-VAR is the `&rest' variable.
MAP-OR-KEY-VARS is whether there are map or key variables."
(let ((pos-len (length pos-vars))
(opt-len (length opt-vars)))
(cl-labels ((make-pos-pats ()
(cl-loop for v in pos-vars
for i from 0
collect `(app ,(loopy--pcase-flip 'seq-elt i)
,(loopy--get-var-pattern v)))))
`(and
;; If there are optional values, then we can avoid the length check here
;; by running the length check for the optional values, which we need to
;; do anyway.
,@(when (null opt-vars)
`((pred ,(loopy--pcase-flip 'loopy--seq-length> (1- pos-len)))
,@(make-pos-pats)))
;; Optional variables may or may not be expensive for generic
;; sequences. This is the same logic as for arrays, just using the
;; `seq-' functions.
,@(when opt-vars
(let ((opt-var-specs (seq-into (mapcar #'loopy--get-&optional-spec
opt-vars)
'vector)))
;; TODO: When do we need this to be `=' instead of `> (1- ...)'?
`((or ,@(cl-loop with use->= = (or rest-var map-or-key-vars)
and pat-idx-low = pos-len
and spec-idx-max = (1- opt-len)
for checked-len from (+ pos-len opt-len) downto pos-len
for spec-idx-high downfrom (1- opt-len) to 0
collect
;; If the length matches, then all of the
;; remaining variables were supplied, then
;; the one variable was not supplied and we
;; need to check the remaining ones.
`(and ,(if use->=
`(pred ,(loopy--pcase-flip 'loopy--seq-length> (1- checked-len)))
`(pred ,(loopy--pcase-flip 'loopy--seq-length= checked-len)))
,@(when pos-vars
(make-pos-pats))
;; Variables that should be bound with the value in
;; the array.
,@(cl-loop
for spec-idx2 from 0 to spec-idx-high
for arr-idx from pat-idx-low
append (pcase-let* ((`(,var2 ,_ ,supplied2) (aref opt-var-specs spec-idx2))
(var3 (loopy--get-var-pattern var2)))
(if supplied2
`((app ,(loopy--pcase-flip 'seq-elt arr-idx)
,var3)
(let ,supplied2 t))
`((app ,(loopy--pcase-flip 'seq-elt arr-idx)
,var3)))))
;; Variables that should be bound to nil or their
;; default.
,@(cl-loop
for spec-idx2 from (1+ spec-idx-high) to spec-idx-max
for arr-idx from pat-idx-low
append (pcase-let* ((`(,var2 ,default2 ,supplied2 ,len2)
(aref opt-var-specs spec-idx2))
(var3 (loopy--get-var-pattern var2)))
(pcase-exhaustive len2
(3 `((let ,var3 ,default2)
(let ,supplied2 nil)))
(2 `((let ,var3 ,default2)))
(_
`((let ,var3 ,default2))
;; (loopy--pcase-let-nil-list var3)
))))))
;; A pattern for when nothing matches.
(and ,@(cl-loop for spec across opt-var-specs
append (pcase-let* ((`(,var2 ,default2 ,supplied2 ,_len2) spec)
(var3 (loopy--get-var-pattern var2)))
`(,@(when supplied2
`((let ,supplied2 nil)))
(let ,var3 ,default2)
;; ,@(cond
;; ((or (eq default2 '_)
;; (= len2 1))
;; (loopy--pcase-let-nil-list var3))
;; ((= len2 2)
;; `((let ,var3 ,default2))))
)))
,@(when pos-vars
`((pred ,(loopy--pcase-flip 'loopy--seq-length> (1- pos-len)))
,@(make-pos-pats))))))))
,@(when rest-var
`((app ,(loopy--pcase-flip 'seq-drop (+ pos-len opt-len))
,(loopy--get-var-pattern rest-var))))))))
(defun loopy--pcase-pat-&key-pattern (key-vars allow-other-keys)
"Build a `pcase' pattern for the `&key' variables.
KEY-VARS are the forms of the key variables. ALLOW-OTHER-KEYS is
whether `&allow-other-keys' was used. PLIST-VAR is the variable
holding the property list."
;; If we aren't checking whether all keys in EXPVAL were given,
;; then we can use simpler patterns since we don't need to store the
;; value of the key.
(cl-flet ((get-var-data (var-form)
(loopy--pcase-let-workaround (key var default supplied)
(pcase-let ((`(,key ,var ,default ,supplied)
(loopy--get-&key-spec var-form)))
(list key (loopy--get-var-pattern var)
default supplied)))))
(if allow-other-keys
`(and ,@(mapcar (lambda (var-form)
(pcase-let ((`(,key ,var ,default ,supplied) (get-var-data var-form))
(key-found (gensym "key-found"))
(plist (gensym "plist")))
(cond (supplied
`(app (lambda (,plist)
(let ((,key-found (plist-member ,plist ,key)))
(if ,key-found
(cons t (cadr ,plist))
(cons nil ,default))))
(,'\` ((,'\, ,supplied) . (,'\, ,var)))))
(default
`(app (lambda (,plist)
(let ((,key-found (plist-member ,plist ,key)))
(if ,key-found
(cadr ,plist)
,default)))
,var))
(t
`(app ,(loopy--pcase-flip 'plist-get key)
,var)))))
key-vars))
;; If we are checking whether there are no other keys in EXPVAL,
;; then we use a single function for extracting the associated
;; values and performing the check, whose output we match against
;; a list of patterns.
(let ((res (gensym "res"))
(keys (gensym "keys"))
(plist (gensym "plist"))
(pats nil))
`(app (lambda (,plist)
(let ((,res nil)
(,keys nil))
,@(cl-loop
for (key var default supplied) in (mapcar #'get-var-data key-vars)
collect (macroexp-let2* nil ((keyval key))
`(progn
(push ,keyval ,keys)
,(cond
(supplied
(push supplied pats)
(push var pats)
(cl-once-only ((key-found `(plist-member ,plist ,keyval)))
`(if ,key-found
(progn
(push t ,res)
(push (cadr ,key-found) ,res))
(push nil ,res)
(push ,default ,res))))
(default
(push var pats)
(cl-once-only ((key-found `(plist-member ,plist ,keyval)))
`(if ,key-found
(push (cadr ,key-found) ,res)
(push ,default ,res))))
(t
(push var pats)
`(push (plist-get ,plist ,keyval)
,res))))))
(push (or (plist-get ,plist :allow-other-keys)
(cl-loop for (key _val) on ,plist by #'cddr
always (memq key ,keys)))
,res)
;; Reverse in case a latter pattern use a variable
;; from an earlier pattern.
(nreverse ,res)))
(,'\` ,(cl-loop for pat in (reverse (cons
;; Use `identity' instead
;; of `(not null)' to support
;; older version of Emacs.
'(pred identity)
pats))
collect `(,'\, ,pat))))))))
(defun loopy--pcase-pat-&map-pattern (map-vars)
"Build a `pcase' pattern for the `&map' variables MAP-VARS."
(let ((mapsym (gensym "map")))
`(and (pred mapp)
,@(mapcar (loopy--pcase-let-workaround (key var default supplied)
(lambda (var-form)
(pcase-let ((`(,key ,var ,default ,supplied)
(loopy--get-&map-spec var-form)))
(unless var
(signal 'loopy-&map-var-malformed (list var-form)))
(setq var (loopy--get-var-pattern var))
(cond
(supplied
`(app (lambda (,mapsym)
;; The default implementations of `map-elt'
;; uses `map-contains-key' (which might be
;; expensive) when given a default value, so
;; we use a generated default to avoid
;; calling it twice.
,(let ((defsym (list 'quote (gensym "loopy--map-contains-test")))
(valsym (gensym "loopy--map-elt")))
(macroexp-let2* nil ((keysym key))
`(let ((,valsym (map-elt ,mapsym ,keysym ,defsym)))
(if (equal ,valsym ,defsym)
(cons nil ,default)
(cons t ,valsym))))))
(,'\` ((,'\, ,supplied) . (,'\, ,var)))))
(default
`(app (lambda (,mapsym) (map-elt ,mapsym ,key ,default))
,var))
(t
`(app ,(loopy--pcase-flip 'map-elt key) ,var))))))
map-vars))))
(defun loopy--pcase-pat-&aux-pattern (aux-vars)
"Build `pcase' pattern for `&aux' variables.
AUX-VARS is the list of bindings."
`(and ,@(cl-loop
for bind in aux-vars
for (var val) = (loopy--get-&aux-spec bind)
if (null var)
do (signal 'loopy-&aux-malformed-var (list bind))
else
collect `(let ,(loopy--get-var-pattern var)
,val)
end)))
;;;###autoload
(pcase-defmacro loopy (sequence)
"Match for Loopy destructuring.
See the Info node `(loopy)Basic Destructuring'."
(cond
((loopy--var-ignored-p sequence) '_)
((symbolp sequence) sequence)
(t
(let* ((groups (loopy--get-var-groups sequence))
(is-seq (alist-get 'seq groups))
(whole-var (alist-get 'whole groups))
(pos-vars (alist-get 'pos groups))
(opt-vars (alist-get 'opt groups))
(rest-var (alist-get 'rest groups))
(key-vars (alist-get 'key groups))
(allow-other-keys (alist-get 'allow-other-keys groups))
(map-vars (alist-get 'map groups))
(aux-vars (alist-get 'aux groups)))
(remq nil
`(and ,(when whole-var
whole-var)
,@(when (or pos-vars opt-vars rest-var
key-vars map-vars allow-other-keys)
(cond
(is-seq
`((pred seqp)
,(when (or pos-vars opt-vars rest-var)
(loopy--pcase-pat-positional-&seq-pattern
pos-vars opt-vars
rest-var (or key-vars map-vars)))
,(when key-vars
(signal 'loopy-&key-seq (list sequence)))))
((listp sequence)
`((pred listp)
,(when (or pos-vars opt-vars rest-var)
(loopy--pcase-pat-positional-list-pattern
pos-vars opt-vars
rest-var (or key-vars map-vars)))
,(when key-vars
(cond
((and rest-var
(not (loopy--var-ignored-p rest-var)))
`(app (lambda (_) ,rest-var)
,(loopy--pcase-pat-&key-pattern
key-vars allow-other-keys)))
((or pos-vars opt-vars)
`(app (nthcdr ,(+ (length pos-vars)
(length opt-vars)))
,(loopy--pcase-pat-&key-pattern
key-vars allow-other-keys)))
(t (loopy--pcase-pat-&key-pattern
key-vars allow-other-keys))))))
((arrayp sequence)
`((pred arrayp)
,(when (or pos-vars opt-vars rest-var)
(loopy--pcase-pat-positional-array-pattern
pos-vars opt-vars
rest-var key-vars))
,(when key-vars
(signal 'loopy-&key-array (list sequence)))))
(t
(signal 'loopy-bad-desctructuring
(list sequence)))))
,(when map-vars
(cond
((and rest-var
(not (loopy--var-ignored-p rest-var)))
`(app (lambda (_) ,rest-var)
,(loopy--pcase-pat-&map-pattern map-vars)))
((or pos-vars opt-vars)
`(app ,(loopy--pcase-flip 'seq-drop (+ (length pos-vars)
(length opt-vars)))
,(loopy--pcase-pat-&map-pattern map-vars)))
(t
(loopy--pcase-pat-&map-pattern map-vars))))
,(when aux-vars
(loopy--pcase-pat-&aux-pattern aux-vars))))))))
;;;; Destructuring for Iteration and Accumulation Commands
(cl-defun loopy--pcase-destructure-for-iteration (var val &key error)
"Destructure VAL according to VAR as by `pcase-let'.
Returns a list. The elements are:
1. An expression which binds the variables in VAR to the values
in VAL.
2. A list of variables which exist outside of this expression and
need to be `let'-bound.
If ERROR is non-nil, then signal an error in the produced code if
the pattern doesn't match."
(if (symbolp var)
`((setq ,var ,val)
,var)
(let ((var-list)
(destructuring-expression)
(val-holder (gensym "loopy--pcase-workaround")))
(cl-flet ((signaler (&rest _)
`(signal 'loopy-bad-run-time-destructuring
(list (quote ,var)
,val-holder))))
;; This sets `destructuring-expression' and `var-list'.
(setq destructuring-expression
;; This holding variable seems to be needed for the older method,
;; before the introduction of `pcase-compile-patterns'. In some cases,
;; it just evaluates `VAL' repeatedly, which is bad for functions
;; that work with state and bad for efficiency.
;;
;; Regardless, we also use it to report the value that caused the
;; error.
`(let ((,val-holder ,val))
,(if (fboundp 'pcase-compile-patterns)
(pcase-compile-patterns
val-holder
(remq nil
(list (cons var
(lambda (varvals &rest _)
(cons 'setq (mapcan (cl-function
(lambda ((var val &rest rest))
(push var var-list)
(list var val)))
varvals))))
(when error
(cons '_ #'signaler)))))
;; NOTE: In Emacs versions less than 28, this functionality
;; technically isn't public, but this is what the developers
;; recommend.
(pcase--u
(remq
nil
(list (list (pcase--match val-holder
(pcase--macroexpand
(if error
var
`(or ,var pcase--dontcare))))
(lambda (vars)
(cons 'setq
(mapcan (lambda (v)
(let ((destr-var (car v))
;; Use `cadr' for Emacs 28+, `cdr' for less.
(destr-val (funcall (eval-when-compile
(if (version< emacs-version "28")
#'cdr
#'cadr))
v)))
(push destr-var var-list)
(list destr-var destr-val)))
vars))))
(when error
(list (pcase--match val-holder
(pcase--macroexpand '_))
#'signaler)))))))))
(list destructuring-expression
(seq-uniq var-list #'eq)))))
(defun loopy--pcase-destructure-for-with-vars (bindings)
"Return a way to destructure BINDINGS by `pcase-let*'.
Returns a list of two elements:
1. The symbol `pcase-let*'.
2. A new list of bindings."
(list 'pcase-let* bindings))
(cl-defun loopy--pcase-parse-for-destructuring-accumulation-command
((name var val &rest args) &key error)
"Parse the accumulation loop command using `pcase' for destructuring.
NAME is the name of the command. VAR-OR-VAL is a variable name
or, if using implicit variables, a value . VAL is a value, and
should only be used if VAR-OR-VAL is a variable. ERROR is when
an error should be signaled if the pattern doesn't match."
(let* ((instructions)
(full-main-body)
;; This holding variable seems to be needed for the older method,
;; before the introduction of `pcase-compile-patterns'. In some cases,
;; it just evaluates `VAL' repeatedly, which is bad for functions
;; that work with state and bad for efficiency.
(value-holder (gensym "loopy--pcase-workaround")))
(cl-flet ((signaler (&rest _)
`(signal 'loopy-bad-run-time-destructuring
(list (quote ,var)
,value-holder))))
(if (fboundp 'pcase-compile-patterns)
(setq full-main-body
(pcase-compile-patterns
value-holder
(remq nil
(list (cons var
(lambda (varvals &rest _)
(let ((destr-main-body))
(dolist (varval varvals)