Skip to content

Commit

Permalink
Ignore variables in more places.
Browse files Browse the repository at this point in the history
  • Loading branch information
okamsn committed Jan 12, 2024
1 parent aa3eb25 commit 3ad577a
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions loopy-destructure.el
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ Type is one of `list' or `array'."
(map . ,(nreverse map-var))
(allow-other-keys ,allow-other-keys))))

(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)))

(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.
Expand All @@ -237,9 +248,7 @@ MAP-OR-KEY-VARS is whether there are map or key variables."
(cond
(pos-vars `(and (pred consp)
(app car-safe ,(let ((var (car pos-vars)))
(if (seqp var)
`(loopy ,var)
var)))
(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))))
Expand All @@ -248,8 +257,7 @@ MAP-OR-KEY-VARS is whether there are map or key variables."
`(,var)
var)
(car opt-vars)))
(when (seqp var)
(setq var `(loopy ,var)))
(setq var (loopy--get-var-pattern var))
`(and (pred listp)
(app car-safe (or (and (pred null)
,@(when supplied `((let ,supplied nil)))
Expand All @@ -261,7 +269,7 @@ MAP-OR-KEY-VARS is whether there are map or key variables."
(app cdr-safe ,(loopy--pcase-pat-positional-list-pattern
nil (cdr opt-vars)
rest-var map-or-key-vars)))))
(rest-var rest-var)
(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.
Expand All @@ -285,9 +293,7 @@ MAP-OR-KEY-VARS is whether there are map or key variables."
,@(cl-loop for var in pos-vars
for idx from 0
collect `(app (pcase--flip aref ,idx)
,(if (seqp var)
`(loopy ,var)
var)))
,(loopy--get-var-pattern var)))
,@(when opt-vars
(let ((opt-var-specs (seq-into (mapcar (pcase-lambda ((or (seq var default supplied)
(seq var default)
Expand Down Expand Up @@ -316,47 +322,46 @@ MAP-OR-KEY-VARS is whether there are map or key variables."
,@(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)))
append (pcase-let* ((`(,var2 ,_ ,supplied2) (aref opt-var-specs spec-idx2))
(var3 (loopy--get-var-pattern var2)))
(if supplied2
`((app (pcase--flip aref ,arr-idx)
,(if (seqp var2)
`(loopy ,var2)
var2))
,var3)
(let ,supplied2 t))
`((app (pcase--flip aref ,arr-idx)
,(if (seqp var2)
`(loopy ,var2)
var2))))))
,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)
(aref opt-var-specs spec-idx2)))
append (pcase-let* ((`(,var2 ,default2 ,supplied2)
(aref opt-var-specs spec-idx2))
(var3 (loopy--get-var-pattern var2)))
(cond
(supplied2
`((let ,var2 ,default2)
`((let ,var3 ,default2)
(let ,supplied2 nil)))
(default2
`((let ,var2 ,default2)))
`((let ,var3 ,default2)))
(t
`((let ,var2 nil))))))))
`((let ,var3 nil))))))))
;; A pattern for when nothing matches.
(and ,@(cl-loop for spec across opt-var-specs
append (pcase-let ((`(,var2 ,default2 ,supplied2) spec))
append (pcase-let* ((`(,var2 ,default2 ,supplied2) spec)
(var3 (loopy--get-var-pattern var2)))
(cond
(supplied2
`((let ,var2 ,default2)
`((let ,var3 ,default2)
(let ,supplied2 nil)))
(default2
`((let ,var2 ,default2)))
`((let ,var3 ,default2)))
(t
`((let ,var2 nil)))))))))))
`((let ,var3 nil)))))))))))

,@(when rest-var
`((app (pcase--flip seq-subseq ,(+ pos-len opt-len))
,rest-var))))))
,(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.
Expand Down Expand Up @@ -390,9 +395,7 @@ holding the property list."
(substring name 1)
name))))))))
var-form))
(list key (if (seqp var)
`(loopy ,var)
var)
(list key (loopy--get-var-pattern var)
default supplied))))
(if allow-other-keys
`(and ,@(mapcar (lambda (var-form)
Expand Down Expand Up @@ -484,8 +487,7 @@ holding the property list."
`(quote ,var)))))))
(unless var
(signal 'loopy-&map-var-malformed (list var-form)))
(when (seqp var)
(setq var `(loopy ,var)))
(setq var (loopy--get-var-pattern var))
(cond
(supplied
`(app (lambda (,mapsym)
Expand Down Expand Up @@ -516,9 +518,7 @@ AUX-VARS is the list of bindings."
bind))
(unless var
(signal 'loopy-&aux-malformed-var (list var-form)))
`(let (if (seqp var)
`(loopy ,var)
,var)
`(let ,(loopy--get-var-pattern var)
,val)))))

;;;###autoload
Expand Down

0 comments on commit 3ad577a

Please sign in to comment.