2015-03-02 07:57:51 +00:00
|
|
|
;;; generator.el --- generators -*- lexical-binding: t -*-
|
|
|
|
|
2016-01-01 09:16:19 +00:00
|
|
|
;;; Copyright (C) 2015-2016 Free Software Foundation, Inc.
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
;; Author: Daniel Colascione <dancol@dancol.org>
|
|
|
|
;; Keywords: extensions, elisp
|
|
|
|
;; Package: emacs
|
|
|
|
|
2015-03-03 16:56:24 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
;; GNU Emacs 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 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; This package implements generators for Emacs Lisp through a
|
|
|
|
;; continuation-passing transformation. It provides essentially the
|
2015-03-03 23:10:05 +00:00
|
|
|
;; same generator API and iterator facilities that Python and
|
2015-03-02 07:57:51 +00:00
|
|
|
;; JavaScript ES6 provide.
|
|
|
|
;;
|
|
|
|
;; `iter-lambda' and `iter-defun' work like `lambda' and `defun',
|
|
|
|
;; except that they evaluate to or define, respectively, generator
|
|
|
|
;; functions. These functions, when called, return an iterator.
|
|
|
|
;; An iterator is an opaque object that generates a sequence of
|
|
|
|
;; values. Callers use `iter-next' to retrieve the next value from
|
|
|
|
;; the sequence; when the sequence is exhausted, `iter-next' will
|
|
|
|
;; raise the `iter-end-of-sequence' condition.
|
|
|
|
;;
|
|
|
|
;; Generator functions are written like normal functions, except that
|
|
|
|
;; they can invoke `iter-yield' to suspend themselves and return a
|
|
|
|
;; value to callers; this value becomes the return value of
|
|
|
|
;; `iter-next'. On the next call to `iter-next', execution of the
|
|
|
|
;; generator function resumes where it left off. When a generator
|
|
|
|
;; function returns normally, the `iter-next' raises
|
|
|
|
;; `iter-end-of-sequence' with the value the function returned.
|
|
|
|
;;
|
|
|
|
;; `iter-yield-from' yields all the values from another iterator; it
|
|
|
|
;; then evaluates to the value the sub-iterator returned normally.
|
|
|
|
;; This facility is useful for functional composition of generators
|
|
|
|
;; and for implementing coroutines.
|
|
|
|
;;
|
|
|
|
;; `iter-yield' is illegal inside the UNWINDFORMS of an
|
|
|
|
;; `unwind-protect' for various sordid internal reasons documented in
|
|
|
|
;; the code.
|
|
|
|
;;
|
|
|
|
;; N.B. Each call to a generator function generates a *new* iterator,
|
|
|
|
;; and each iterator maintains its own internal state.
|
|
|
|
;;
|
|
|
|
;; This raw form of iteration is general, but a bit awkward to use, so
|
2015-03-03 23:10:05 +00:00
|
|
|
;; this library also provides some convenience functions:
|
2015-03-02 07:57:51 +00:00
|
|
|
;;
|
|
|
|
;; `iter-do' is like `cl-do', except that instead of walking a list,
|
|
|
|
;; it walks an iterator. `cl-loop' is also extended with a new
|
|
|
|
;; keyword, `iter-by', that iterates over an iterator.
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Implementation:
|
|
|
|
|
|
|
|
;;
|
|
|
|
;; The internal cps transformation code uses the cps- namespace.
|
|
|
|
;; Iteration functions use the `iter-' namespace. Generator functions
|
|
|
|
;; are somewhat less efficient than conventional elisp routines,
|
|
|
|
;; although we try to avoid CPS transformation on forms that do not
|
|
|
|
;; invoke `iter-yield'.
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'cl-lib)
|
|
|
|
(require 'pcase)
|
|
|
|
|
2015-03-03 18:32:21 +00:00
|
|
|
(defvar cps--bindings nil)
|
|
|
|
(defvar cps--states nil)
|
|
|
|
(defvar cps--value-symbol nil)
|
|
|
|
(defvar cps--state-symbol nil)
|
|
|
|
(defvar cps--cleanup-table-symbol nil)
|
|
|
|
(defvar cps--cleanup-function nil)
|
|
|
|
|
2015-03-03 18:56:24 +00:00
|
|
|
(defmacro cps--gensym (fmt &rest args)
|
|
|
|
;; Change this function to use `cl-gensym' if you want the generated
|
|
|
|
;; code to be easier to read and debug.
|
|
|
|
;; (cl-gensym (apply #'format fmt args))
|
2015-05-19 19:37:14 +00:00
|
|
|
`(progn (ignore ,@args) (make-symbol ,fmt)))
|
2015-03-03 18:56:24 +00:00
|
|
|
|
2015-03-03 18:32:21 +00:00
|
|
|
(defvar cps--dynamic-wrappers '(identity)
|
2015-03-02 07:57:51 +00:00
|
|
|
"List of transformer functions to apply to atomic forms we
|
|
|
|
evaluate in CPS context.")
|
|
|
|
|
|
|
|
(defconst cps-standard-special-forms
|
|
|
|
'(setq setq-default throw interactive)
|
|
|
|
"List of special forms that we treat just like ordinary
|
|
|
|
function applications." )
|
|
|
|
|
|
|
|
(defun cps--trace-funcall (func &rest args)
|
|
|
|
(message "%S: args=%S" func args)
|
|
|
|
(let ((result (apply func args)))
|
|
|
|
(message "%S: result=%S" func result)
|
|
|
|
result))
|
|
|
|
|
|
|
|
(defun cps--trace (fmt &rest args)
|
|
|
|
(princ (apply #'format (concat fmt "\n") args)))
|
|
|
|
|
|
|
|
(defun cps--special-form-p (definition)
|
|
|
|
"Non-nil if and only if DEFINITION is a special form."
|
|
|
|
;; Copied from ad-special-form-p
|
|
|
|
(if (and (symbolp definition) (fboundp definition))
|
|
|
|
(setf definition (indirect-function definition)))
|
|
|
|
(and (subrp definition) (eq (cdr (subr-arity definition)) 'unevalled)))
|
|
|
|
|
|
|
|
(defmacro cps--define-unsupported (function)
|
|
|
|
`(defun ,(intern (format "cps--transform-%s" function))
|
|
|
|
(error "%s not supported in generators" ,function)))
|
|
|
|
|
|
|
|
(defmacro cps--with-value-wrapper (wrapper &rest body)
|
|
|
|
"Continue generating CPS code with an atomic-form wrapper
|
|
|
|
to the current stack of such wrappers. WRAPPER is a function that
|
|
|
|
takes a form and returns a wrapped form.
|
|
|
|
|
|
|
|
Whenever we generate an atomic form (i.e., a form that can't
|
|
|
|
iter-yield), we first (before actually inserting that form in our
|
|
|
|
generated code) pass that form through all the transformer
|
|
|
|
functions. We use this facility to wrap forms that can transfer
|
|
|
|
control flow non-locally in goo that diverts this control flow to
|
|
|
|
the CPS state machinery.
|
|
|
|
"
|
|
|
|
(declare (indent 1))
|
2015-03-03 18:32:21 +00:00
|
|
|
`(let ((cps--dynamic-wrappers
|
2015-03-02 07:57:51 +00:00
|
|
|
(cons
|
|
|
|
,wrapper
|
2015-03-03 18:32:21 +00:00
|
|
|
cps--dynamic-wrappers)))
|
2015-03-02 07:57:51 +00:00
|
|
|
,@body))
|
|
|
|
|
|
|
|
(defun cps--make-dynamic-binding-wrapper (dynamic-var static-var)
|
|
|
|
(cl-assert lexical-binding)
|
|
|
|
(lambda (form)
|
|
|
|
`(let ((,dynamic-var ,static-var))
|
|
|
|
(unwind-protect ; Update the static shadow after evaluation is done
|
|
|
|
,form
|
|
|
|
(setf ,static-var ,dynamic-var))
|
|
|
|
,form)))
|
|
|
|
|
|
|
|
(defmacro cps--with-dynamic-binding (dynamic-var static-var &rest body)
|
|
|
|
"Evaluate BODY such that generated atomic evaluations run with
|
|
|
|
DYNAMIC-VAR bound to STATIC-VAR."
|
|
|
|
(declare (indent 2))
|
|
|
|
`(cps--with-value-wrapper
|
|
|
|
(cps--make-dynamic-binding-wrapper ,dynamic-var ,static-var)
|
|
|
|
,@body))
|
|
|
|
|
|
|
|
(defun cps--add-state (kind body)
|
|
|
|
"Create a new CPS state with body BODY and return the state's name."
|
|
|
|
(declare (indent 1))
|
2015-03-03 18:56:24 +00:00
|
|
|
(let* ((state (cps--gensym "cps-state-%s-" kind)))
|
2015-03-03 18:32:21 +00:00
|
|
|
(push (list state body cps--cleanup-function) cps--states)
|
|
|
|
(push state cps--bindings)
|
2015-03-02 07:57:51 +00:00
|
|
|
state))
|
|
|
|
|
|
|
|
(defun cps--add-binding (original-name)
|
2015-03-03 18:56:24 +00:00
|
|
|
(car (push (cps--gensym (format "cps-binding-%s-" original-name))
|
2015-03-03 18:32:21 +00:00
|
|
|
cps--bindings)))
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
(defun cps--find-special-form-handler (form)
|
|
|
|
(let* ((handler-name (format "cps--transform-%s" (car-safe form)))
|
|
|
|
(handler (intern-soft handler-name)))
|
|
|
|
(and (fboundp handler) handler)))
|
|
|
|
|
2015-03-03 18:56:24 +00:00
|
|
|
(defvar cps-inhibit-atomic-optimization nil
|
2015-03-02 07:57:51 +00:00
|
|
|
"When t, always rewrite forms into cps even when they
|
|
|
|
don't yield.")
|
|
|
|
|
|
|
|
(defvar cps--yield-seen)
|
|
|
|
|
|
|
|
(defun cps--atomic-p (form)
|
|
|
|
"Return whether the given form never yields."
|
|
|
|
|
2015-03-03 18:56:24 +00:00
|
|
|
(and (not cps-inhibit-atomic-optimization)
|
2015-03-02 07:57:51 +00:00
|
|
|
(let* ((cps--yield-seen))
|
|
|
|
(ignore (macroexpand-all
|
|
|
|
`(cl-macrolet ((cps-internal-yield
|
|
|
|
(_val)
|
|
|
|
(setf cps--yield-seen t)))
|
2015-03-03 18:56:24 +00:00
|
|
|
,form)
|
|
|
|
macroexpand-all-environment))
|
2015-03-02 07:57:51 +00:00
|
|
|
(not cps--yield-seen))))
|
|
|
|
|
|
|
|
(defun cps--make-atomic-state (form next-state)
|
2015-03-03 18:32:21 +00:00
|
|
|
(let ((tform `(prog1 ,form (setf ,cps--state-symbol ,next-state))))
|
|
|
|
(cl-loop for wrapper in cps--dynamic-wrappers
|
2015-03-02 07:57:51 +00:00
|
|
|
do (setf tform (funcall wrapper tform)))
|
2015-03-03 18:32:21 +00:00
|
|
|
;; Bind cps--cleanup-function to nil here because the wrapper
|
2015-03-02 07:57:51 +00:00
|
|
|
;; function mechanism is responsible for cleanup here, not the
|
|
|
|
;; generic cleanup mechanism. If we didn't make this binding,
|
|
|
|
;; we'd run cleanup handlers twice on anything that made it out
|
|
|
|
;; to toplevel.
|
2015-03-03 18:32:21 +00:00
|
|
|
(let ((cps--cleanup-function nil))
|
2015-03-02 07:57:51 +00:00
|
|
|
(cps--add-state "atom"
|
2015-03-03 18:32:21 +00:00
|
|
|
`(setf ,cps--value-symbol ,tform)))))
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
(defun cps--transform-1 (form next-state)
|
|
|
|
(pcase form
|
|
|
|
|
|
|
|
;; If we're looking at an "atomic" form (i.e., one that does not
|
|
|
|
;; iter-yield), just evaluate the form as a whole instead of rewriting
|
|
|
|
;; it into CPS.
|
|
|
|
|
|
|
|
((guard (cps--atomic-p form))
|
|
|
|
(cps--make-atomic-state form next-state))
|
|
|
|
|
|
|
|
;; Process `and'.
|
|
|
|
|
|
|
|
(`(and) ; (and) -> t
|
|
|
|
(cps--transform-1 t next-state))
|
|
|
|
(`(and ,condition) ; (and CONDITION) -> CONDITION
|
|
|
|
(cps--transform-1 condition next-state))
|
|
|
|
(`(and ,condition . ,rest)
|
|
|
|
;; Evaluate CONDITION; if it's true, go on to evaluate the rest
|
|
|
|
;; of the `and'.
|
|
|
|
(cps--transform-1
|
|
|
|
condition
|
|
|
|
(cps--add-state "and"
|
2015-03-03 18:32:21 +00:00
|
|
|
`(setf ,cps--state-symbol
|
|
|
|
(if ,cps--value-symbol
|
2015-03-02 07:57:51 +00:00
|
|
|
,(cps--transform-1 `(and ,@rest)
|
|
|
|
next-state)
|
|
|
|
,next-state)))))
|
|
|
|
|
|
|
|
;; Process `catch'.
|
|
|
|
|
|
|
|
(`(catch ,tag . ,body)
|
|
|
|
(let ((tag-binding (cps--add-binding "catch-tag")))
|
|
|
|
(cps--transform-1 tag
|
|
|
|
(cps--add-state "cps-update-tag"
|
2015-03-03 18:32:21 +00:00
|
|
|
`(setf ,tag-binding ,cps--value-symbol
|
|
|
|
,cps--state-symbol
|
2015-03-02 07:57:51 +00:00
|
|
|
,(cps--with-value-wrapper
|
|
|
|
(cps--make-catch-wrapper
|
|
|
|
tag-binding next-state)
|
|
|
|
(cps--transform-1 `(progn ,@body)
|
|
|
|
next-state)))))))
|
|
|
|
|
|
|
|
;; Process `cond': transform into `if' or `or' depending on the
|
|
|
|
;; precise kind of the condition we're looking at.
|
|
|
|
|
|
|
|
(`(cond) ; (cond) -> nil
|
|
|
|
(cps--transform-1 nil next-state))
|
|
|
|
(`(cond (,condition) . ,rest)
|
|
|
|
(cps--transform-1 `(or ,condition (cond ,@rest))
|
|
|
|
next-state))
|
|
|
|
(`(cond (,condition . ,body) . ,rest)
|
|
|
|
(cps--transform-1 `(if ,condition
|
|
|
|
(progn ,@body)
|
|
|
|
(cond ,@rest))
|
|
|
|
next-state))
|
|
|
|
|
|
|
|
;; Process `condition-case': do the heavy lifting in a helper
|
|
|
|
;; function.
|
|
|
|
|
|
|
|
(`(condition-case ,var ,bodyform . ,handlers)
|
|
|
|
(cps--with-value-wrapper
|
|
|
|
(cps--make-condition-wrapper var next-state handlers)
|
|
|
|
(cps--transform-1 bodyform
|
|
|
|
next-state)))
|
|
|
|
|
|
|
|
;; Process `if'.
|
|
|
|
|
|
|
|
(`(if ,cond ,then . ,else)
|
|
|
|
(cps--transform-1 cond
|
|
|
|
(cps--add-state "if"
|
2015-03-03 18:32:21 +00:00
|
|
|
`(setf ,cps--state-symbol
|
|
|
|
(if ,cps--value-symbol
|
2015-03-02 07:57:51 +00:00
|
|
|
,(cps--transform-1 then
|
|
|
|
next-state)
|
|
|
|
,(cps--transform-1 `(progn ,@else)
|
|
|
|
next-state))))))
|
|
|
|
|
|
|
|
;; Process `progn' and `inline': they are identical except for the
|
|
|
|
;; name, which has some significance to the byte compiler.
|
|
|
|
|
|
|
|
(`(inline) (cps--transform-1 nil next-state))
|
|
|
|
(`(inline ,form) (cps--transform-1 form next-state))
|
|
|
|
(`(inline ,form . ,rest)
|
|
|
|
(cps--transform-1 form
|
|
|
|
(cps--transform-1 `(inline ,@rest)
|
|
|
|
next-state)))
|
|
|
|
|
|
|
|
(`(progn) (cps--transform-1 nil next-state))
|
|
|
|
(`(progn ,form) (cps--transform-1 form next-state))
|
|
|
|
(`(progn ,form . ,rest)
|
|
|
|
(cps--transform-1 form
|
|
|
|
(cps--transform-1 `(progn ,@rest)
|
|
|
|
next-state)))
|
|
|
|
|
|
|
|
;; Process `let' in a helper function that transforms it into a
|
|
|
|
;; let* with temporaries.
|
|
|
|
|
|
|
|
(`(let ,bindings . ,body)
|
|
|
|
(let* ((bindings (cl-loop for binding in bindings
|
|
|
|
collect (if (symbolp binding)
|
|
|
|
(list binding nil)
|
|
|
|
binding)))
|
2015-05-19 19:37:14 +00:00
|
|
|
(temps (cl-loop for (var _value-form) in bindings
|
2015-03-02 07:57:51 +00:00
|
|
|
collect (cps--add-binding var))))
|
|
|
|
(cps--transform-1
|
|
|
|
`(let* ,(append
|
2015-05-19 19:37:14 +00:00
|
|
|
(cl-loop for (_var value-form) in bindings
|
2015-03-02 07:57:51 +00:00
|
|
|
for temp in temps
|
|
|
|
collect (list temp value-form))
|
2015-05-19 19:37:14 +00:00
|
|
|
(cl-loop for (var _binding) in bindings
|
2015-03-02 07:57:51 +00:00
|
|
|
for temp in temps
|
|
|
|
collect (list var temp)))
|
|
|
|
,@body)
|
|
|
|
next-state)))
|
|
|
|
|
|
|
|
;; Process `let*' binding: process one binding at a time. Flatten
|
|
|
|
;; lexical bindings.
|
|
|
|
|
|
|
|
(`(let* () . ,body)
|
|
|
|
(cps--transform-1 `(progn ,@body) next-state))
|
|
|
|
|
|
|
|
(`(let* (,binding . ,more-bindings) . ,body)
|
|
|
|
(let* ((var (if (symbolp binding) binding (car binding)))
|
|
|
|
(value-form (car (cdr-safe binding)))
|
|
|
|
(new-var (cps--add-binding var)))
|
|
|
|
|
|
|
|
(cps--transform-1
|
|
|
|
value-form
|
|
|
|
(cps--add-state "let*"
|
2015-03-03 18:32:21 +00:00
|
|
|
`(setf ,new-var ,cps--value-symbol
|
|
|
|
,cps--state-symbol
|
2015-03-02 07:57:51 +00:00
|
|
|
,(if (or (not lexical-binding) (special-variable-p var))
|
|
|
|
(cps--with-dynamic-binding var new-var
|
|
|
|
(cps--transform-1
|
|
|
|
`(let* ,more-bindings ,@body)
|
|
|
|
next-state))
|
|
|
|
(cps--transform-1
|
|
|
|
(cps--replace-variable-references
|
|
|
|
var new-var
|
|
|
|
`(let* ,more-bindings ,@body))
|
|
|
|
next-state)))))))
|
|
|
|
|
|
|
|
;; Process `or'.
|
|
|
|
|
|
|
|
(`(or) (cps--transform-1 nil next-state))
|
|
|
|
(`(or ,condition) (cps--transform-1 condition next-state))
|
|
|
|
(`(or ,condition . ,rest)
|
|
|
|
(cps--transform-1
|
|
|
|
condition
|
|
|
|
(cps--add-state "or"
|
2015-03-03 18:32:21 +00:00
|
|
|
`(setf ,cps--state-symbol
|
|
|
|
(if ,cps--value-symbol
|
2015-03-02 07:57:51 +00:00
|
|
|
,next-state
|
|
|
|
,(cps--transform-1
|
|
|
|
`(or ,@rest) next-state))))))
|
|
|
|
|
|
|
|
;; Process `prog1'.
|
|
|
|
|
|
|
|
(`(prog1 ,first) (cps--transform-1 first next-state))
|
|
|
|
(`(prog1 ,first . ,body)
|
|
|
|
(cps--transform-1
|
|
|
|
first
|
|
|
|
(let ((temp-var-symbol (cps--add-binding "prog1-temp")))
|
|
|
|
(cps--add-state "prog1"
|
|
|
|
`(setf ,temp-var-symbol
|
2015-03-03 18:32:21 +00:00
|
|
|
,cps--value-symbol
|
|
|
|
,cps--state-symbol
|
2015-03-02 07:57:51 +00:00
|
|
|
,(cps--transform-1
|
|
|
|
`(progn ,@body)
|
|
|
|
(cps--add-state "prog1inner"
|
2015-03-03 18:32:21 +00:00
|
|
|
`(setf ,cps--value-symbol ,temp-var-symbol
|
|
|
|
,cps--state-symbol ,next-state))))))))
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
;; Process `prog2'.
|
|
|
|
|
|
|
|
(`(prog2 ,form1 ,form2 . ,body)
|
|
|
|
(cps--transform-1
|
|
|
|
`(progn ,form1 (prog1 ,form2 ,@body))
|
|
|
|
next-state))
|
|
|
|
|
|
|
|
;; Process `unwind-protect': If we're inside an unwind-protect, we
|
|
|
|
;; have a block of code UNWINDFORMS which we would like to run
|
|
|
|
;; whenever control flows away from the main piece of code,
|
|
|
|
;; BODYFORM. We deal with the local control flow case by
|
|
|
|
;; generating BODYFORM such that it yields to a continuation that
|
|
|
|
;; executes UNWINDFORMS, which then yields to NEXT-STATE.
|
|
|
|
;;
|
|
|
|
;; Non-local control flow is trickier: we need to ensure that we
|
|
|
|
;; execute UNWINDFORMS even when control bypasses our normal
|
|
|
|
;; continuation. To make this guarantee, we wrap every external
|
|
|
|
;; application (i.e., every piece of elisp that can transfer
|
|
|
|
;; control non-locally) in an unwind-protect that runs UNWINDFORMS
|
|
|
|
;; before allowing the non-local control transfer to proceed.
|
|
|
|
;;
|
|
|
|
;; Unfortunately, because elisp lacks a mechanism for generically
|
|
|
|
;; capturing the reason for an arbitrary non-local control
|
|
|
|
;; transfer and restarting the transfer at a later point, we
|
|
|
|
;; cannot reify non-local transfers and cannot allow
|
|
|
|
;; continuation-passing code inside UNWINDFORMS.
|
|
|
|
|
|
|
|
(`(unwind-protect ,bodyform . ,unwindforms)
|
|
|
|
;; Signal the evaluator-generator that it needs to generate code
|
|
|
|
;; to handle cleanup forms.
|
2015-03-03 18:32:21 +00:00
|
|
|
(unless cps--cleanup-table-symbol
|
2015-03-03 18:56:24 +00:00
|
|
|
(setf cps--cleanup-table-symbol (cps--gensym "cps-cleanup-table-")))
|
2015-03-02 07:57:51 +00:00
|
|
|
(let* ((unwind-state
|
|
|
|
(cps--add-state
|
|
|
|
"unwind"
|
|
|
|
;; N.B. It's safe to just substitute unwindforms by
|
|
|
|
;; sexp-splicing: we've already replaced all variable
|
|
|
|
;; references inside it with lifted equivalents.
|
|
|
|
`(progn
|
|
|
|
,@unwindforms
|
2015-03-03 18:32:21 +00:00
|
|
|
(setf ,cps--state-symbol ,next-state))))
|
|
|
|
(old-cleanup cps--cleanup-function)
|
|
|
|
(cps--cleanup-function
|
|
|
|
(let ((cps--cleanup-function nil))
|
2015-03-02 07:57:51 +00:00
|
|
|
(cps--add-state "cleanup"
|
|
|
|
`(progn
|
|
|
|
,(when old-cleanup `(funcall ,old-cleanup))
|
|
|
|
,@unwindforms)))))
|
|
|
|
(cps--with-value-wrapper
|
|
|
|
(cps--make-unwind-wrapper unwindforms)
|
|
|
|
(cps--transform-1 bodyform unwind-state))))
|
|
|
|
|
|
|
|
;; Process `while'.
|
|
|
|
|
|
|
|
(`(while ,test . ,body)
|
|
|
|
;; Open-code state addition instead of using cps--add-state: we
|
|
|
|
;; need our states to be self-referential. (That's what makes the
|
|
|
|
;; state a loop.)
|
|
|
|
(let* ((loop-state
|
2015-03-03 18:56:24 +00:00
|
|
|
(cps--gensym "cps-state-while-"))
|
2015-03-02 07:57:51 +00:00
|
|
|
(eval-loop-condition-state
|
|
|
|
(cps--transform-1 test loop-state))
|
|
|
|
(loop-state-body
|
|
|
|
`(progn
|
2015-03-03 18:32:21 +00:00
|
|
|
(setf ,cps--state-symbol
|
|
|
|
(if ,cps--value-symbol
|
2015-03-02 07:57:51 +00:00
|
|
|
,(cps--transform-1
|
|
|
|
`(progn ,@body)
|
|
|
|
eval-loop-condition-state)
|
|
|
|
,next-state)))))
|
2015-03-03 18:32:21 +00:00
|
|
|
(push (list loop-state loop-state-body cps--cleanup-function)
|
|
|
|
cps--states)
|
|
|
|
(push loop-state cps--bindings)
|
2015-03-02 07:57:51 +00:00
|
|
|
eval-loop-condition-state))
|
|
|
|
|
|
|
|
;; Process various kinds of `quote'.
|
|
|
|
|
|
|
|
(`(quote ,arg) (cps--add-state "quote"
|
2015-03-03 18:32:21 +00:00
|
|
|
`(setf ,cps--value-symbol (quote ,arg)
|
|
|
|
,cps--state-symbol ,next-state)))
|
2015-03-02 07:57:51 +00:00
|
|
|
(`(function ,arg) (cps--add-state "function"
|
2015-03-03 18:32:21 +00:00
|
|
|
`(setf ,cps--value-symbol (function ,arg)
|
|
|
|
,cps--state-symbol ,next-state)))
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
;; Deal with `iter-yield'.
|
|
|
|
|
|
|
|
(`(cps-internal-yield ,value)
|
|
|
|
(cps--transform-1
|
|
|
|
value
|
|
|
|
(cps--add-state "iter-yield"
|
|
|
|
`(progn
|
2015-03-03 18:32:21 +00:00
|
|
|
(setf ,cps--state-symbol
|
|
|
|
,(if cps--cleanup-function
|
2015-03-02 07:57:51 +00:00
|
|
|
(cps--add-state "after-yield"
|
2015-03-03 18:32:21 +00:00
|
|
|
`(setf ,cps--state-symbol ,next-state))
|
2015-03-02 07:57:51 +00:00
|
|
|
next-state))
|
2015-03-03 18:32:21 +00:00
|
|
|
(throw 'cps--yield ,cps--value-symbol)))))
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
;; Catch any unhandled special forms.
|
|
|
|
|
|
|
|
((and `(,name . ,_)
|
|
|
|
(guard (cps--special-form-p name))
|
|
|
|
(guard (not (memq name cps-standard-special-forms))))
|
|
|
|
name ; Shut up byte compiler
|
|
|
|
(error "special form %S incorrect or not supported" form))
|
|
|
|
|
|
|
|
;; Process regular function applications with nontrivial
|
|
|
|
;; parameters, converting them to applications of trivial
|
|
|
|
;; let-bound parameters.
|
|
|
|
|
|
|
|
((and `(,function . ,arguments)
|
|
|
|
(guard (not (cl-loop for argument in arguments
|
|
|
|
always (atom argument)))))
|
|
|
|
(let ((argument-symbols
|
|
|
|
(cl-loop for argument in arguments
|
|
|
|
collect (if (atom argument)
|
|
|
|
argument
|
2015-03-03 18:56:24 +00:00
|
|
|
(cps--gensym "cps-argument-")))))
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
(cps--transform-1
|
|
|
|
`(let* ,(cl-loop for argument in arguments
|
|
|
|
for argument-symbol in argument-symbols
|
|
|
|
unless (eq argument argument-symbol)
|
|
|
|
collect (list argument-symbol argument))
|
|
|
|
,(cons function argument-symbols))
|
|
|
|
next-state)))
|
|
|
|
|
|
|
|
;; Process everything else by just evaluating the form normally.
|
2015-06-17 00:04:35 +00:00
|
|
|
(_ (cps--make-atomic-state form next-state))))
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
(defun cps--make-catch-wrapper (tag-binding next-state)
|
|
|
|
(lambda (form)
|
|
|
|
(let ((normal-exit-symbol
|
2015-03-03 18:56:24 +00:00
|
|
|
(cps--gensym "cps-normal-exit-from-catch-")))
|
2015-03-02 07:57:51 +00:00
|
|
|
`(let (,normal-exit-symbol)
|
|
|
|
(prog1
|
|
|
|
(catch ,tag-binding
|
|
|
|
(prog1
|
|
|
|
,form
|
|
|
|
(setf ,normal-exit-symbol t)))
|
|
|
|
(unless ,normal-exit-symbol
|
2015-03-03 18:32:21 +00:00
|
|
|
(setf ,cps--state-symbol ,next-state)))))))
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
(defun cps--make-condition-wrapper (var next-state handlers)
|
|
|
|
;; Each handler is both one of the transformers with which we wrap
|
|
|
|
;; evaluated atomic forms and a state to which we jump when we
|
|
|
|
;; encounter the given error.
|
|
|
|
|
|
|
|
(let* ((error-symbol (cps--add-binding "condition-case-error"))
|
2015-03-03 18:56:24 +00:00
|
|
|
(lexical-error-symbol (cps--gensym "cps-lexical-error-"))
|
2015-03-02 07:57:51 +00:00
|
|
|
(processed-handlers
|
|
|
|
(cl-loop for (condition . body) in handlers
|
|
|
|
collect (cons condition
|
|
|
|
(cps--transform-1
|
|
|
|
(cps--replace-variable-references
|
|
|
|
var error-symbol
|
|
|
|
`(progn ,@body))
|
|
|
|
next-state)))))
|
|
|
|
|
|
|
|
(lambda (form)
|
|
|
|
`(condition-case
|
|
|
|
,lexical-error-symbol
|
|
|
|
,form
|
|
|
|
,@(cl-loop
|
|
|
|
for (condition . error-state) in processed-handlers
|
|
|
|
collect
|
|
|
|
`(,condition
|
|
|
|
(setf ,error-symbol
|
|
|
|
,lexical-error-symbol
|
2015-03-03 18:32:21 +00:00
|
|
|
,cps--state-symbol
|
2015-03-02 07:57:51 +00:00
|
|
|
,error-state)))))))
|
|
|
|
|
|
|
|
(defun cps--replace-variable-references (var new-var form)
|
|
|
|
"Replace all non-shadowed references to VAR with NEW-VAR in FORM.
|
|
|
|
This routine does not modify FORM. Instead, it returns a
|
|
|
|
modified copy."
|
|
|
|
(macroexpand-all
|
2015-03-03 18:56:24 +00:00
|
|
|
`(cl-symbol-macrolet ((,var ,new-var)) ,form)
|
|
|
|
macroexpand-all-environment))
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
(defun cps--make-unwind-wrapper (unwind-forms)
|
|
|
|
(cl-assert lexical-binding)
|
|
|
|
(lambda (form)
|
|
|
|
(let ((normal-exit-symbol
|
2015-03-03 18:56:24 +00:00
|
|
|
(cps--gensym "cps-normal-exit-from-unwind-")))
|
2015-03-02 07:57:51 +00:00
|
|
|
`(let (,normal-exit-symbol)
|
|
|
|
(unwind-protect
|
|
|
|
(prog1
|
|
|
|
,form
|
|
|
|
(setf ,normal-exit-symbol t))
|
|
|
|
(unless ,normal-exit-symbol
|
|
|
|
,@unwind-forms))))))
|
|
|
|
|
|
|
|
(put 'iter-end-of-sequence 'error-conditions '(iter-end-of-sequence))
|
|
|
|
(put 'iter-end-of-sequence 'error-message "iteration terminated")
|
|
|
|
|
|
|
|
(defun cps--make-close-iterator-form (terminal-state)
|
2015-03-03 18:32:21 +00:00
|
|
|
(if cps--cleanup-table-symbol
|
|
|
|
`(let ((cleanup (cdr (assq ,cps--state-symbol ,cps--cleanup-table-symbol))))
|
|
|
|
(setf ,cps--state-symbol ,terminal-state
|
|
|
|
,cps--value-symbol nil)
|
2015-03-02 07:57:51 +00:00
|
|
|
(when cleanup (funcall cleanup)))
|
2015-03-03 18:32:21 +00:00
|
|
|
`(setf ,cps--state-symbol ,terminal-state
|
|
|
|
,cps--value-symbol nil)))
|
2015-03-02 07:57:51 +00:00
|
|
|
|
2015-03-03 18:56:24 +00:00
|
|
|
(defun cps-generate-evaluator (body)
|
2015-03-03 18:32:21 +00:00
|
|
|
(let* (cps--states
|
|
|
|
cps--bindings
|
|
|
|
cps--cleanup-function
|
2015-03-03 18:56:24 +00:00
|
|
|
(cps--value-symbol (cps--gensym "cps-current-value-"))
|
|
|
|
(cps--state-symbol (cps--gensym "cps-current-state-"))
|
2015-03-02 07:57:51 +00:00
|
|
|
;; We make *cps-cleanup-table-symbol** non-nil when we notice
|
|
|
|
;; that we have cleanup processing to perform.
|
2015-03-03 18:32:21 +00:00
|
|
|
(cps--cleanup-table-symbol nil)
|
2015-03-02 07:57:51 +00:00
|
|
|
(terminal-state (cps--add-state "terminal"
|
|
|
|
`(signal 'iter-end-of-sequence
|
2015-03-03 18:32:21 +00:00
|
|
|
,cps--value-symbol)))
|
2015-03-02 07:57:51 +00:00
|
|
|
(initial-state (cps--transform-1
|
2015-03-03 18:56:24 +00:00
|
|
|
(macroexpand-all
|
|
|
|
`(cl-macrolet
|
|
|
|
((iter-yield (value)
|
|
|
|
`(cps-internal-yield ,value)))
|
|
|
|
,@body)
|
|
|
|
macroexpand-all-environment)
|
2015-03-02 07:57:51 +00:00
|
|
|
terminal-state))
|
|
|
|
(finalizer-symbol
|
2015-03-03 18:32:21 +00:00
|
|
|
(when cps--cleanup-table-symbol
|
|
|
|
(when cps--cleanup-table-symbol
|
2015-03-03 18:56:24 +00:00
|
|
|
(cps--gensym "cps-iterator-finalizer-")))))
|
2015-03-03 18:32:21 +00:00
|
|
|
`(let ,(append (list cps--state-symbol cps--value-symbol)
|
|
|
|
(when cps--cleanup-table-symbol
|
|
|
|
(list cps--cleanup-table-symbol))
|
2015-03-02 07:57:51 +00:00
|
|
|
(when finalizer-symbol
|
|
|
|
(list finalizer-symbol))
|
2015-03-03 18:32:21 +00:00
|
|
|
(nreverse cps--bindings))
|
2015-03-02 07:57:51 +00:00
|
|
|
;; Order state list so that cleanup states are always defined
|
|
|
|
;; before they're referenced.
|
2015-03-03 18:32:21 +00:00
|
|
|
,@(cl-loop for (state body cleanup) in (nreverse cps--states)
|
2015-03-02 07:57:51 +00:00
|
|
|
collect `(setf ,state (lambda () ,body))
|
|
|
|
when cleanup
|
2015-03-03 18:32:21 +00:00
|
|
|
do (cl-assert cps--cleanup-table-symbol)
|
|
|
|
and collect `(push (cons ,state ,cleanup) ,cps--cleanup-table-symbol))
|
|
|
|
(setf ,cps--state-symbol ,initial-state)
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
(let ((iterator
|
|
|
|
(lambda (op value)
|
|
|
|
(cond
|
|
|
|
,@(when finalizer-symbol
|
|
|
|
`(((eq op :stash-finalizer)
|
|
|
|
(setf ,finalizer-symbol value))
|
|
|
|
((eq op :get-finalizer)
|
|
|
|
,finalizer-symbol)))
|
|
|
|
((eq op :close)
|
|
|
|
,(cps--make-close-iterator-form terminal-state))
|
|
|
|
((eq op :next)
|
2015-03-03 18:32:21 +00:00
|
|
|
(setf ,cps--value-symbol value)
|
2015-03-02 07:57:51 +00:00
|
|
|
(let ((yielded nil))
|
|
|
|
(unwind-protect
|
|
|
|
(prog1
|
|
|
|
(catch 'cps--yield
|
|
|
|
(while t
|
2015-03-03 18:32:21 +00:00
|
|
|
(funcall ,cps--state-symbol)))
|
2015-03-02 07:57:51 +00:00
|
|
|
(setf yielded t))
|
|
|
|
(unless yielded
|
|
|
|
;; If we're exiting non-locally (error, quit,
|
|
|
|
;; etc.) close the iterator.
|
|
|
|
,(cps--make-close-iterator-form terminal-state)))))
|
|
|
|
(t (error "unknown iterator operation %S" op))))))
|
|
|
|
,(when finalizer-symbol
|
|
|
|
`(funcall iterator
|
|
|
|
:stash-finalizer
|
|
|
|
(make-finalizer
|
|
|
|
(lambda ()
|
|
|
|
(iter-close iterator)))))
|
|
|
|
iterator))))
|
|
|
|
|
|
|
|
(defun iter-yield (value)
|
|
|
|
"When used inside a generator, yield control to caller.
|
|
|
|
The caller of `iter-next' receives VALUE, and the next call to
|
|
|
|
`iter-next' resumes execution at the previous
|
|
|
|
`iter-yield' point."
|
|
|
|
(identity value)
|
Go back to grave quoting in source-code docstrings etc.
This reverts almost all my recent changes to use curved quotes
in docstrings and/or strings used for error diagnostics.
There are a few exceptions, e.g., Bahá’í proper names.
* admin/unidata/unidata-gen.el (unidata-gen-table):
* lisp/abbrev.el (expand-region-abbrevs):
* lisp/align.el (align-region):
* lisp/allout.el (allout-mode, allout-solicit-alternate-bullet)
(outlineify-sticky):
* lisp/apropos.el (apropos-library):
* lisp/bookmark.el (bookmark-default-annotation-text):
* lisp/button.el (button-category-symbol, button-put)
(make-text-button):
* lisp/calc/calc-aent.el (math-read-if, math-read-factor):
* lisp/calc/calc-embed.el (calc-do-embedded):
* lisp/calc/calc-ext.el (calc-user-function-list):
* lisp/calc/calc-graph.el (calc-graph-show-dumb):
* lisp/calc/calc-help.el (calc-describe-key)
(calc-describe-thing, calc-full-help):
* lisp/calc/calc-lang.el (calc-c-language)
(math-parse-fortran-vector-end, math-parse-tex-sum)
(math-parse-eqn-matrix, math-parse-eqn-prime)
(calc-yacas-language, calc-maxima-language, calc-giac-language)
(math-read-giac-subscr, math-read-math-subscr)
(math-read-big-rec, math-read-big-balance):
* lisp/calc/calc-misc.el (calc-help, report-calc-bug):
* lisp/calc/calc-mode.el (calc-auto-why, calc-save-modes)
(calc-auto-recompute):
* lisp/calc/calc-prog.el (calc-fix-token-name)
(calc-read-parse-table-part, calc-user-define-invocation)
(math-do-arg-check):
* lisp/calc/calc-store.el (calc-edit-variable):
* lisp/calc/calc-units.el (math-build-units-table-buffer):
* lisp/calc/calc-vec.el (math-read-brackets):
* lisp/calc/calc-yank.el (calc-edit-mode):
* lisp/calc/calc.el (calc, calc-do, calc-user-invocation):
* lisp/calendar/appt.el (appt-display-message):
* lisp/calendar/diary-lib.el (diary-check-diary-file)
(diary-mail-entries, diary-from-outlook):
* lisp/calendar/icalendar.el (icalendar-export-region)
(icalendar--convert-float-to-ical)
(icalendar--convert-date-to-ical)
(icalendar--convert-ical-to-diary)
(icalendar--convert-recurring-to-diary)
(icalendar--add-diary-entry):
* lisp/calendar/time-date.el (format-seconds):
* lisp/calendar/timeclock.el (timeclock-mode-line-display)
(timeclock-make-hours-explicit, timeclock-log-data):
* lisp/calendar/todo-mode.el (todo-prefix, todo-delete-category)
(todo-item-mark, todo-check-format)
(todo-insert-item--next-param, todo-edit-item--next-key)
(todo-mode):
* lisp/cedet/ede/pmake.el (ede-proj-makefile-insert-dist-rules):
* lisp/cedet/mode-local.el (describe-mode-local-overload)
(mode-local-print-binding, mode-local-describe-bindings-2):
* lisp/cedet/semantic/complete.el (semantic-displayor-show-request):
* lisp/cedet/srecode/srt-mode.el (srecode-macro-help):
* lisp/cus-start.el (standard):
* lisp/cus-theme.el (describe-theme-1):
* lisp/custom.el (custom-add-dependencies, custom-check-theme)
(custom--sort-vars-1, load-theme):
* lisp/descr-text.el (describe-text-properties-1, describe-char):
* lisp/dired-x.el (dired-do-run-mail):
* lisp/dired.el (dired-log):
* lisp/emacs-lisp/advice.el (ad-read-advised-function)
(ad-read-advice-class, ad-read-advice-name, ad-enable-advice)
(ad-disable-advice, ad-remove-advice, ad-set-argument)
(ad-set-arguments, ad--defalias-fset, ad-activate)
(ad-deactivate):
* lisp/emacs-lisp/byte-opt.el (byte-compile-inline-expand)
(byte-compile-unfold-lambda, byte-optimize-form-code-walker)
(byte-optimize-while, byte-optimize-apply):
* lisp/emacs-lisp/byte-run.el (defun, defsubst):
* lisp/emacs-lisp/bytecomp.el (byte-compile-lapcode)
(byte-compile-log-file, byte-compile-format-warn)
(byte-compile-nogroup-warn, byte-compile-arglist-warn)
(byte-compile-cl-warn)
(byte-compile-warn-about-unresolved-functions)
(byte-compile-file, byte-compile--declare-var)
(byte-compile-file-form-defmumble, byte-compile-form)
(byte-compile-normal-call, byte-compile-check-variable)
(byte-compile-variable-ref, byte-compile-variable-set)
(byte-compile-subr-wrong-args, byte-compile-setq-default)
(byte-compile-negation-optimizer)
(byte-compile-condition-case--old)
(byte-compile-condition-case--new, byte-compile-save-excursion)
(byte-compile-defvar, byte-compile-autoload)
(byte-compile-lambda-form)
(byte-compile-make-variable-buffer-local, display-call-tree)
(batch-byte-compile):
* lisp/emacs-lisp/cconv.el (cconv-convert, cconv--analyze-use):
* lisp/emacs-lisp/chart.el (chart-space-usage):
* lisp/emacs-lisp/check-declare.el (check-declare-scan)
(check-declare-warn, check-declare-file)
(check-declare-directory):
* lisp/emacs-lisp/checkdoc.el (checkdoc-this-string-valid-engine)
(checkdoc-message-text-engine):
* lisp/emacs-lisp/cl-extra.el (cl-parse-integer)
(cl--describe-class):
* lisp/emacs-lisp/cl-generic.el (cl-defgeneric)
(cl--generic-describe, cl-generic-generalizers):
* lisp/emacs-lisp/cl-macs.el (cl--parse-loop-clause, cl-tagbody)
(cl-symbol-macrolet):
* lisp/emacs-lisp/cl.el (cl-unload-function, flet):
* lisp/emacs-lisp/copyright.el (copyright)
(copyright-update-directory):
* lisp/emacs-lisp/edebug.el (edebug-read-list):
* lisp/emacs-lisp/eieio-base.el (eieio-persistent-read):
* lisp/emacs-lisp/eieio-core.el (eieio--slot-override)
(eieio-oref):
* lisp/emacs-lisp/eieio-opt.el (eieio-help-constructor):
* lisp/emacs-lisp/eieio-speedbar.el:
(eieio-speedbar-child-make-tag-lines)
(eieio-speedbar-child-description):
* lisp/emacs-lisp/eieio.el (defclass, change-class):
* lisp/emacs-lisp/elint.el (elint-file, elint-get-top-forms)
(elint-init-form, elint-check-defalias-form)
(elint-check-let-form):
* lisp/emacs-lisp/ert.el (ert-get-test, ert-results-mode-menu)
(ert-results-pop-to-backtrace-for-test-at-point)
(ert-results-pop-to-messages-for-test-at-point)
(ert-results-pop-to-should-forms-for-test-at-point)
(ert-describe-test):
* lisp/emacs-lisp/find-func.el (find-function-search-for-symbol)
(find-function-library):
* lisp/emacs-lisp/generator.el (iter-yield):
* lisp/emacs-lisp/gv.el (gv-define-simple-setter):
* lisp/emacs-lisp/lisp-mnt.el (lm-verify):
* lisp/emacs-lisp/macroexp.el (macroexp--obsolete-warning):
* lisp/emacs-lisp/map-ynp.el (map-y-or-n-p):
* lisp/emacs-lisp/nadvice.el (advice--make-docstring)
(advice--make, define-advice):
* lisp/emacs-lisp/package-x.el (package-upload-file):
* lisp/emacs-lisp/package.el (package-version-join)
(package-disabled-p, package-activate-1, package-activate)
(package--download-one-archive)
(package--download-and-read-archives)
(package-compute-transaction, package-install-from-archive)
(package-install, package-install-selected-packages)
(package-delete, package-autoremove, describe-package-1)
(package-install-button-action, package-delete-button-action)
(package-menu-hide-package, package-menu--list-to-prompt)
(package-menu--perform-transaction)
(package-menu--find-and-notify-upgrades):
* lisp/emacs-lisp/pcase.el (pcase-exhaustive, pcase--u1):
* lisp/emacs-lisp/re-builder.el (reb-enter-subexp-mode):
* lisp/emacs-lisp/ring.el (ring-previous, ring-next):
* lisp/emacs-lisp/rx.el (rx-check, rx-anything)
(rx-check-any-string, rx-check-any, rx-check-not, rx-=)
(rx-repeat, rx-check-backref, rx-syntax, rx-check-category)
(rx-form):
* lisp/emacs-lisp/smie.el (smie-config-save):
* lisp/emacs-lisp/subr-x.el (internal--check-binding):
* lisp/emacs-lisp/tabulated-list.el (tabulated-list-put-tag):
* lisp/emacs-lisp/testcover.el (testcover-1value):
* lisp/emacs-lisp/timer.el (timer-event-handler):
* lisp/emulation/viper-cmd.el (viper-toggle-parse-sexp-ignore-comments)
(viper-toggle-search-style, viper-kill-buffer)
(viper-brac-function):
* lisp/emulation/viper-macs.el (viper-record-kbd-macro):
* lisp/env.el (setenv):
* lisp/erc/erc-button.el (erc-nick-popup):
* lisp/erc/erc.el (erc-cmd-LOAD, erc-handle-login, english):
* lisp/eshell/em-dirs.el (eshell/cd):
* lisp/eshell/em-glob.el (eshell-glob-regexp)
(eshell-glob-entries):
* lisp/eshell/em-pred.el (eshell-parse-modifiers):
* lisp/eshell/esh-opt.el (eshell-show-usage):
* lisp/facemenu.el (facemenu-add-new-face)
(facemenu-add-new-color):
* lisp/faces.el (read-face-name, read-face-font, describe-face)
(x-resolve-font-name):
* lisp/files-x.el (modify-file-local-variable):
* lisp/files.el (locate-user-emacs-file, find-alternate-file)
(set-auto-mode, hack-one-local-variable--obsolete)
(dir-locals-set-directory-class, write-file, basic-save-buffer)
(delete-directory, copy-directory, recover-session)
(recover-session-finish, insert-directory)
(file-modes-char-to-who, file-modes-symbolic-to-number)
(move-file-to-trash):
* lisp/filesets.el (filesets-add-buffer, filesets-remove-buffer):
* lisp/find-cmd.el (find-generic, find-to-string):
* lisp/finder.el (finder-commentary):
* lisp/font-lock.el (font-lock-fontify-buffer):
* lisp/format.el (format-write-file, format-find-file)
(format-insert-file):
* lisp/frame.el (get-device-terminal, select-frame-by-name):
* lisp/fringe.el (fringe--check-style):
* lisp/gnus/nnmairix.el (nnmairix-widget-create-query):
* lisp/help-fns.el (help-fns--key-bindings)
(help-fns--compiler-macro, help-fns--parent-mode)
(help-fns--obsolete, help-fns--interactive-only)
(describe-function-1, describe-variable):
* lisp/help.el (describe-mode)
(describe-minor-mode-from-indicator):
* lisp/image.el (image-type):
* lisp/international/ccl.el (ccl-dump):
* lisp/international/fontset.el (x-must-resolve-font-name):
* lisp/international/mule-cmds.el (prefer-coding-system)
(select-safe-coding-system-interactively)
(select-safe-coding-system, activate-input-method)
(toggle-input-method, describe-current-input-method)
(describe-language-environment):
* lisp/international/mule-conf.el (code-offset):
* lisp/international/mule-diag.el (describe-character-set)
(list-input-methods-1):
* lisp/mail/feedmail.el (feedmail-run-the-queue):
* lisp/mouse.el (minor-mode-menu-from-indicator):
* lisp/mpc.el (mpc-playlist-rename):
* lisp/msb.el (msb--choose-menu):
* lisp/net/ange-ftp.el (ange-ftp-shell-command):
* lisp/net/imap.el (imap-interactive-login):
* lisp/net/mairix.el (mairix-widget-create-query):
* lisp/net/newst-backend.el (newsticker--sentinel-work):
* lisp/net/newst-treeview.el (newsticker--treeview-load):
* lisp/net/rlogin.el (rlogin):
* lisp/obsolete/iswitchb.el (iswitchb-possible-new-buffer):
* lisp/obsolete/otodo-mode.el (todo-more-important-p):
* lisp/obsolete/pgg-gpg.el (pgg-gpg-process-region):
* lisp/obsolete/pgg-pgp.el (pgg-pgp-process-region):
* lisp/obsolete/pgg-pgp5.el (pgg-pgp5-process-region):
* lisp/org/ob-core.el (org-babel-goto-named-src-block)
(org-babel-goto-named-result):
* lisp/org/ob-fortran.el (org-babel-fortran-ensure-main-wrap):
* lisp/org/ob-ref.el (org-babel-ref-resolve):
* lisp/org/org-agenda.el (org-agenda-prepare):
* lisp/org/org-clock.el (org-clock-notify-once-if-expired)
(org-clock-resolve):
* lisp/org/org-ctags.el (org-ctags-ask-rebuild-tags-file-then-find-tag):
* lisp/org/org-feed.el (org-feed-parse-atom-entry):
* lisp/org/org-habit.el (org-habit-parse-todo):
* lisp/org/org-mouse.el (org-mouse-popup-global-menu)
(org-mouse-context-menu):
* lisp/org/org-table.el (org-table-edit-formulas):
* lisp/org/ox.el (org-export-async-start):
* lisp/proced.el (proced-log):
* lisp/progmodes/ada-mode.el (ada-get-indent-case)
(ada-check-matching-start, ada-goto-matching-start):
* lisp/progmodes/ada-prj.el (ada-prj-display-page):
* lisp/progmodes/ada-xref.el (ada-find-executable):
* lisp/progmodes/ebrowse.el (ebrowse-tags-apropos):
* lisp/progmodes/etags.el (etags-tags-apropos-additional):
* lisp/progmodes/flymake.el (flymake-parse-err-lines)
(flymake-start-syntax-check-process):
* lisp/progmodes/python.el (python-shell-get-process-or-error)
(python-define-auxiliary-skeleton):
* lisp/progmodes/sql.el (sql-comint):
* lisp/progmodes/verilog-mode.el (verilog-load-file-at-point):
* lisp/progmodes/vhdl-mode.el (vhdl-widget-directory-validate):
* lisp/recentf.el (recentf-open-files):
* lisp/replace.el (query-replace-read-from)
(occur-after-change-function, occur-1):
* lisp/scroll-bar.el (scroll-bar-columns):
* lisp/server.el (server-get-auth-key):
* lisp/simple.el (execute-extended-command)
(undo-outer-limit-truncate, list-processes--refresh)
(compose-mail, set-variable, choose-completion-string)
(define-alternatives):
* lisp/startup.el (site-run-file, tty-handle-args, command-line)
(command-line-1):
* lisp/subr.el (noreturn, define-error, add-to-list)
(read-char-choice, version-to-list):
* lisp/term/common-win.el (x-handle-xrm-switch)
(x-handle-name-switch, x-handle-args):
* lisp/term/x-win.el (x-handle-parent-id, x-handle-smid):
* lisp/textmodes/reftex-ref.el (reftex-label):
* lisp/textmodes/reftex-toc.el (reftex-toc-rename-label):
* lisp/textmodes/two-column.el (2C-split):
* lisp/tutorial.el (tutorial--describe-nonstandard-key)
(tutorial--find-changed-keys):
* lisp/type-break.el (type-break-noninteractive-query):
* lisp/wdired.el (wdired-do-renames, wdired-do-symlink-changes)
(wdired-do-perm-changes):
* lisp/whitespace.el (whitespace-report-region):
Prefer grave quoting in source-code strings used to generate help
and diagnostics.
* lisp/faces.el (face-documentation):
No need to convert quotes, since the result is a docstring.
* lisp/info.el (Info-virtual-index-find-node)
(Info-virtual-index, info-apropos):
Simplify by generating only curved quotes, since info files are
typically that ways nowadays anyway.
* lisp/international/mule-diag.el (list-input-methods):
Don’t assume text quoting style is curved.
* lisp/org/org-bibtex.el (org-bibtex-fields):
Revert my recent changes, going back to the old quoting style.
2015-09-07 15:41:44 +00:00
|
|
|
(error "`iter-yield' used outside a generator"))
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
(defmacro iter-yield-from (value)
|
|
|
|
"When used inside a generator function, delegate to a sub-iterator.
|
|
|
|
The values that the sub-iterator yields are passed directly to
|
|
|
|
the caller, and values supplied to `iter-next' are sent to the
|
|
|
|
sub-iterator. `iter-yield-from' evaluates to the value that the
|
|
|
|
sub-iterator function returns via `iter-end-of-sequence'."
|
2015-03-03 18:56:24 +00:00
|
|
|
(let ((errsym (cps--gensym "yield-from-result"))
|
|
|
|
(valsym (cps--gensym "yield-from-value")))
|
2015-03-02 07:57:51 +00:00
|
|
|
`(let ((,valsym ,value))
|
|
|
|
(unwind-protect
|
|
|
|
(condition-case ,errsym
|
|
|
|
(let ((vs nil))
|
|
|
|
(while t
|
|
|
|
(setf vs (iter-yield (iter-next ,valsym vs)))))
|
|
|
|
(iter-end-of-sequence (cdr ,errsym)))
|
|
|
|
(iter-close ,valsym)))))
|
|
|
|
|
|
|
|
(defmacro iter-defun (name arglist &rest body)
|
|
|
|
"Creates a generator NAME.
|
|
|
|
When called as a function, NAME returns an iterator value that
|
|
|
|
encapsulates the state of a computation that produces a sequence
|
|
|
|
of values. Callers can retrieve each value using `iter-next'."
|
|
|
|
(declare (indent defun))
|
|
|
|
(cl-assert lexical-binding)
|
2015-03-03 21:18:00 +00:00
|
|
|
(let* ((parsed-body (macroexp-parse-body body))
|
|
|
|
(declarations (car parsed-body))
|
|
|
|
(exps (cdr parsed-body)))
|
2015-03-03 00:11:51 +00:00
|
|
|
`(defun ,name ,arglist
|
2015-03-03 21:18:00 +00:00
|
|
|
,@declarations
|
|
|
|
,(cps-generate-evaluator exps))))
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
(defmacro iter-lambda (arglist &rest body)
|
|
|
|
"Return a lambda generator.
|
|
|
|
`iter-lambda' is to `iter-defun' as `lambda' is to `defun'."
|
|
|
|
(declare (indent defun))
|
|
|
|
(cl-assert lexical-binding)
|
|
|
|
`(lambda ,arglist
|
2015-03-03 18:56:24 +00:00
|
|
|
,(cps-generate-evaluator body)))
|
2015-03-02 07:57:51 +00:00
|
|
|
|
|
|
|
(defun iter-next (iterator &optional yield-result)
|
|
|
|
"Extract a value from an iterator.
|
Fix minor quoting problems in doc strings
These were glitches regardless of how or whether we tackle the
problem of grave accent in doc strings.
* lisp/calc/calc-aent.el (math-restore-placeholders):
* lisp/ido.el (ido-ignore-buffers, ido-ignore-files):
* lisp/leim/quail/cyrillic.el ("bulgarian-alt-phonetic"):
* lisp/leim/quail/hebrew.el ("hebrew-new")
("hebrew-biblical-sil"):
* lisp/leim/quail/thai.el ("thai-kesmanee"):
* lisp/progmodes/idlw-shell.el (idlwave-shell-file-name-chars):
Used curved quotes to avoid ambiguities like ‘`''’ in doc strings.
* lisp/calendar/calendar.el (calendar-month-abbrev-array):
* lisp/cedet/semantic/mru-bookmark.el (semantic-mrub-cache-flush-fcn):
* lisp/cedet/semantic/symref.el (semantic-symref-tool-baseclass):
* lisp/cedet/semantic/tag.el (semantic-tag-copy)
(semantic-tag-components):
* lisp/cedet/srecode/cpp.el (srecode-semantic-handle-:cpp):
* lisp/cedet/srecode/texi.el (srecode-texi-texify-docstring):
* lisp/emacs-lisp/byte-opt.el (byte-optimize-all-constp):
* lisp/emacs-lisp/checkdoc.el (checkdoc-message-text-engine):
* lisp/emacs-lisp/generator.el (iter-next):
* lisp/gnus/gnus-art.el (gnus-treat-strip-list-identifiers)
(gnus-article-mode-syntax-table):
* lisp/net/rlogin.el (rlogin-directory-tracking-mode):
* lisp/net/soap-client.el (soap-wsdl-get):
* lisp/net/telnet.el (telnet-mode):
* lisp/org/org-compat.el (org-number-sequence):
* lisp/org/org.el (org-remove-highlights-with-change)
(org-structure-template-alist):
* lisp/org/ox-html.el (org-html-link-org-files-as-html):
* lisp/play/handwrite.el (handwrite-10pt, handwrite-11pt)
(handwrite-12pt, handwrite-13pt):
* lisp/progmodes/f90.el (f90-mode, f90-abbrev-start):
* lisp/progmodes/idlwave.el (idlwave-mode, idlwave-check-abbrev):
* lisp/progmodes/verilog-mode.el (verilog-tool)
(verilog-string-replace-matches, verilog-preprocess)
(verilog-auto-insert-lisp, verilog-auto-insert-last):
* lisp/textmodes/makeinfo.el (makeinfo-options):
* src/font.c (Ffont_spec):
Fix minor quoting problems in doc strings, e.g., missing quote,
``x'' where `x' was meant, etc.
* lisp/erc/erc-backend.el (erc-process-sentinel-2):
Fix minor quoting problem in other string.
* lisp/leim/quail/ethiopic.el ("ethiopic"):
* lisp/term/tvi970.el (tvi970-set-keypad-mode):
Omit unnecessary quotes.
* lisp/faces.el (set-face-attribute, set-face-underline)
(set-face-inverse-video, x-create-frame-with-faces):
* lisp/gnus/gnus-group.el (gnus-group-nnimap-edit-acl):
* lisp/mail/supercite.el (sc-attribs-%@-addresses)
(sc-attribs-!-addresses, sc-attribs-<>-addresses):
* lisp/net/tramp.el (tramp-methods):
* lisp/recentf.el (recentf-show-file-shortcuts-flag):
* lisp/textmodes/artist.el (artist-ellipse-right-char)
(artist-ellipse-left-char, artist-vaporize-fuzziness)
(artist-spray-chars, artist-mode, artist-replace-string)
(artist-put-pixel, artist-text-see-thru):
* lisp/vc/ediff-util.el (ediff-submit-report):
* lisp/vc/log-edit.el (log-edit-changelog-full-paragraphs):
Use double-quotes rather than TeX markup in doc strings.
* lisp/skeleton.el (skeleton-pair-insert-maybe):
Reword to avoid the need for grave accent and apostrophe.
* lisp/xt-mouse.el (xterm-mouse-tracking-enable-sequence):
Don't use grave and acute accents to quote.
2015-05-19 21:59:15 +00:00
|
|
|
YIELD-RESULT becomes the return value of `iter-yield' in the
|
2015-03-02 07:57:51 +00:00
|
|
|
context of the generator.
|
|
|
|
|
|
|
|
This routine raises the `iter-end-of-sequence' condition if the
|
|
|
|
iterator cannot supply more values."
|
|
|
|
(funcall iterator :next yield-result))
|
|
|
|
|
|
|
|
(defun iter-close (iterator)
|
|
|
|
"Terminate an iterator early.
|
|
|
|
Run any unwind-protect handlers in scope at the point ITERATOR
|
|
|
|
is blocked."
|
|
|
|
(funcall iterator :close nil))
|
|
|
|
|
|
|
|
(cl-defmacro iter-do ((var iterator) &rest body)
|
|
|
|
"Loop over values from an iterator.
|
|
|
|
Evaluate BODY with VAR bound to each value from ITERATOR.
|
|
|
|
Return the value with which ITERATOR finished iteration."
|
|
|
|
(declare (indent 1))
|
2015-03-03 18:56:24 +00:00
|
|
|
(let ((done-symbol (cps--gensym "iter-do-iterator-done"))
|
|
|
|
(condition-symbol (cps--gensym "iter-do-condition"))
|
|
|
|
(it-symbol (cps--gensym "iter-do-iterator"))
|
|
|
|
(result-symbol (cps--gensym "iter-do-result")))
|
2015-03-02 07:57:51 +00:00
|
|
|
`(let (,var
|
|
|
|
,result-symbol
|
|
|
|
(,done-symbol nil)
|
|
|
|
(,it-symbol ,iterator))
|
|
|
|
(while (not ,done-symbol)
|
|
|
|
(condition-case ,condition-symbol
|
|
|
|
(setf ,var (iter-next ,it-symbol))
|
|
|
|
(iter-end-of-sequence
|
|
|
|
(setf ,result-symbol (cdr ,condition-symbol))
|
|
|
|
(setf ,done-symbol t)))
|
|
|
|
(unless ,done-symbol ,@body))
|
|
|
|
,result-symbol)))
|
|
|
|
|
|
|
|
(defvar cl--loop-args)
|
|
|
|
|
|
|
|
(defmacro cps--advance-for (conscell)
|
|
|
|
;; See cps--handle-loop-for
|
|
|
|
`(condition-case nil
|
|
|
|
(progn
|
|
|
|
(setcar ,conscell (iter-next (cdr ,conscell)))
|
|
|
|
,conscell)
|
|
|
|
(iter-end-of-sequence
|
|
|
|
nil)))
|
|
|
|
|
|
|
|
(defmacro cps--initialize-for (iterator)
|
|
|
|
;; See cps--handle-loop-for
|
2015-03-03 18:56:24 +00:00
|
|
|
(let ((cs (cps--gensym "cps--loop-temp")))
|
2015-03-02 07:57:51 +00:00
|
|
|
`(let ((,cs (cons nil ,iterator)))
|
|
|
|
(cps--advance-for ,cs))))
|
|
|
|
|
|
|
|
(defun cps--handle-loop-for (var)
|
|
|
|
"Support `iter-by' in `loop'. "
|
|
|
|
;; N.B. While the cl-loop-for-handler is a documented interface,
|
|
|
|
;; there's no documented way for cl-loop-for-handler callbacks to do
|
|
|
|
;; anything useful! Additionally, cl-loop currently lexbinds useful
|
|
|
|
;; internal variables, so our only option is to modify
|
|
|
|
;; cl--loop-args. If we substitute a general-purpose for-clause for
|
|
|
|
;; our iterating clause, however, we can't preserve the
|
|
|
|
;; parallel-versus-sequential `loop' semantics for for clauses ---
|
|
|
|
;; we need a terminating condition as well, which requires us to use
|
|
|
|
;; while, and inserting a while would break and-sequencing.
|
|
|
|
;;
|
|
|
|
;; To work around this problem, we actually use the "for var in LIST
|
|
|
|
;; by FUNCTION" syntax, creating a new fake list each time through
|
|
|
|
;; the loop, this "list" being a cons cell (val . it).
|
|
|
|
(let ((it-form (pop cl--loop-args)))
|
|
|
|
(setf cl--loop-args
|
|
|
|
(append
|
|
|
|
`(for ,var
|
|
|
|
in (cps--initialize-for ,it-form)
|
|
|
|
by 'cps--advance-for)
|
|
|
|
cl--loop-args))))
|
|
|
|
|
|
|
|
(put 'iter-by 'cl-loop-for-handler 'cps--handle-loop-for)
|
|
|
|
|
|
|
|
(eval-after-load 'elisp-mode
|
|
|
|
(lambda ()
|
|
|
|
(font-lock-add-keywords
|
|
|
|
'emacs-lisp-mode
|
|
|
|
'(("(\\(iter-defun\\)\\_>\\s *\\(\\(?:\\sw\\|\\s_\\)+\\)?"
|
|
|
|
(1 font-lock-keyword-face nil t)
|
|
|
|
(2 font-lock-function-name-face nil t))
|
2015-03-03 18:56:24 +00:00
|
|
|
("(\\(iter-\\(?:next\\|lambda\\|yield\\|yield-from\\)\\)\\_>"
|
2015-03-02 07:57:51 +00:00
|
|
|
(1 font-lock-keyword-face nil t))))))
|
|
|
|
|
|
|
|
(provide 'generator)
|
|
|
|
|
|
|
|
;;; generator.el ends here
|