Skip to content

Commit

Permalink
Try shifting to the pcase versions. Still need to test.
Browse files Browse the repository at this point in the history
  • Loading branch information
okamsn committed Jan 5, 2024
1 parent 53cfb7e commit 23046c4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
29 changes: 26 additions & 3 deletions loopy-commands.el
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
(require 'generator)
(require 'gv)
(require 'loopy-misc)
(require 'loopy-destructure)
(require 'loopy-instrs)
(require 'loopy-vars)
(require 'map)
(require 'macroexp)
Expand Down Expand Up @@ -3051,7 +3053,7 @@ VALUE-EXPRESSION."
(defalias 'loopy--destructure-generalized-variables
#'loopy--destructure-generalized-sequence)

(defun loopy--destructure-for-iteration-default (var val)
(defun loopy--destructure-for-iteration-default2 (var val)
"Destructure VAL according to VAR.
Returns a list. The elements are:
Expand All @@ -3063,6 +3065,16 @@ Returns a list. The elements are:
(list (cons 'setq (apply #'append bindings))
(cl-remove-duplicates (mapcar #'cl-first bindings)))))

(defun loopy--destructure-for-iteration-default (var val)
"Destructure VAL according to VAR.
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."
(loopy--pcase-destructure-for-iteration `(loopy ,var) val))

;; TODO: Rename these so that the current "iteration" features
;; are "generic" and the new "iteration" features
;; a special case of the new "generic" features.
Expand Down Expand Up @@ -3100,7 +3112,7 @@ A wrapper around `loopy--destructure-for-iteration-command'."
(loopy--convert-iteration-vars-to-other-vars
(loopy--destructure-for-iteration-command var value-expression)))

(cl-defun loopy--parse-destructuring-accumulation-command
(cl-defun loopy--parse-destructuring-accumulation-command-default2
((name var val &rest args))
"Return instructions for destructuring accumulation commands.
Expand Down Expand Up @@ -3235,7 +3247,18 @@ NAME is the name of the command. VAR is a variable name. VAL is a value."
;; Return the instructions in the correct order.
(nreverse instructions)))

(cl-defun loopy--parse-destructuring-accumulation-command2
(cl-defun loopy--parse-destructuring-accumulation-command-default
((name var val &rest args))
"Return instructions for destructuring accumulation commands.
Unlike `loopy--basic-builtin-destructuring', this function
does destructuring and returns instructions.
NAME is the name of the command. VAR is a variable name. VAL is a value."
(loopy--pcase-parse-for-destructuring-accumulation-command
`(,name (loopy ,var) ,val ,@args)))

(cl-defun loopy--parse-destructuring-accumulation-command3
((name var val &rest args))
"Return instructions for destructuring accumulation commands.
Expand Down
20 changes: 11 additions & 9 deletions loopy-destructure.el
Original file line number Diff line number Diff line change
Expand Up @@ -639,10 +639,11 @@ Returns a list. The elements are:
(mapcan (lambda (v)
(let ((destr-var (car v))
;; Use `cadr' for Emacs 28+, `cdr' for less.
(destr-val (if (version< emacs-version "28")
(cdr v)
(warn "loopy-pcase: Update Emacs 28 to use `pcase-compile-patterns'.")
(cadr v))))
(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))))))))
Expand All @@ -656,7 +657,7 @@ Returns a list of two elements:
2. A new list of bindings."
(list 'pcase-let* bindings))

(cl-defun loopy--pcase-parse-destructuring-accumulation-command
(cl-defun loopy--pcase-parse-for-destructuring-accumulation-command
((name var val &rest args))
"Parse the accumulation loop command using `pcase' for destructuring.
Expand Down Expand Up @@ -700,9 +701,11 @@ should only be used if VAR-OR-VAL is a variable."
(dolist (v vars)
(let ((destr-var (car v))
;; Use `cadr' for Emacs 28+, `cdr' for less.
(destr-val (if (version< emacs-version "28")
(cdr v)
(cadr v))))
(destr-val (funcall (eval-when-compile
(if (version< emacs-version "28")
#'cdr
#'cadr))
v)))
(seq-let (main-body other-instructions)
(loopy--extract-main-body
(loopy--parse-loop-command
Expand All @@ -720,6 +723,5 @@ should only be used if VAR-OR-VAL is a variable."
`((loopy--main-body ,full-main-body)
,@(nreverse instructions))))


(provide 'loopy-destructure)
;;; loopy-destructure.el ends here
18 changes: 14 additions & 4 deletions loopy-pcase.el
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

;;; Code:
(require 'loopy)
(require 'loopy-destructure)
(require 'loopy-instrs)
(require 'loopy-misc)
(require 'loopy-vars)
(require 'macroexp)
Expand Down Expand Up @@ -60,7 +62,7 @@
(if (eq loopy--destructuring-accumulation-parser
#'loopy-pcase--parse-destructuring-accumulation-command)
(setq loopy--destructuring-accumulation-parser
#'loopy--parse-destructuring-accumulation-command)))
#'loopy--parse-destructuring-accumulation-command-default)))

(add-to-list 'loopy--flag-settings
(cons 'pcase #'loopy-pcase--enable-flag-pcase))
Expand All @@ -69,7 +71,10 @@
(add-to-list 'loopy--flag-settings
(cons '-pcase #'loopy-pcase--disable-flag-pcase))

(defun loopy-pcase--destructure-for-iteration (var val)
(defalias 'loopy-pcase--destructure-for-iteration
#'loopy--pcase-destructure-for-iteration)

(defun loopy-pcase--destructure-for-iteration2 (var val)
"Destructure VAL according to VAR as by `pcase-let'.
Returns a list. The elements are:
Expand Down Expand Up @@ -113,15 +118,20 @@ Returns a list. The elements are:
vars))))))))
(list destructuring-expression var-list)))

(defun loopy-pcase--destructure-for-with-vars (bindings)
(defalias 'loopy-pcase--destructure-for-with-vars
#'loopy--pcase-destructure-for-with-vars)
(defun loopy-pcase--destructure-for-with-vars2 (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-destructuring-accumulation-command
(defalias 'loopy-pcase--parse-destructuring-accumulation-command
#'loopy--pcase-parse-for-destructuring-accumulation-command)

(cl-defun loopy-pcase--parse-destructuring-accumulation-command2
((name var val &rest args))
"Parse the accumulation loop command using `pcase' for destructuring.
Expand Down
10 changes: 10 additions & 0 deletions loopy.el
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ which will be used to wrap the loop and other code."
(defun loopy--destructure-for-with-vars-default (bindings)
"Destructure BINDINGS into bindings suitable for something like `let*'.
Returns a list of two elements:
1. The symbol `pcase-let*'.
2. A new list of bindings."
(loopy--pcase-destructure-for-with-vars
(cl-loop for (var val) in bindings
collect `((loopy ,var) ,val))))

(defun loopy--destructure-for-with-vars-default2 (bindings)
"Destructure BINDINGS into bindings suitable for something like `let*'.
Returns a list of two elements:
1. The symbol `let*'.
2. A new list of bindings."
Expand Down

0 comments on commit 23046c4

Please sign in to comment.