From: Leo <sdl.web@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: Andreas Schwab <schwab@linux-m68k.org>, emacs-devel@gnu.org
Subject: Common Lisp like feature expressions (was: How and when to use GCPRO?)
Date: Mon, 27 Dec 2010 19:26:03 +0000 [thread overview]
Message-ID: <m1fwtjggqc.fsf_-_@cam.ac.uk> (raw)
In-Reply-To: <jwvbp47nr67.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Mon, 27 Dec 2010 11:00:42 -0500")
On 2010-12-27 16:00 +0000, Stefan Monnier wrote:
> Stefan "wondering why eval_feature_expression would need to be
> written in C rather than in Elisp"
I hadn't thought of that :(
I put together a small patch for anyone who would like to try it:
diff --git a/lisp/subr.el b/lisp/subr.el
index 03f7e04..92f3b0a 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -1719,6 +1719,31 @@ FILE should be the name of a library, with no directory name."
(eval-after-load file (read)))
(make-obsolete 'eval-next-after-load `eval-after-load "23.2")
\f
+(defun eval-feature-expression (form)
+ (cond
+ ((atom form) (featurep form nil))
+ ;; ensure form is a proper list
+ ((condition-case nil
+ (and (length form) nil)
+ (error (error "Invalid feature expression: %s" form))))
+ ((eq (car form) 'not)
+ (if (= (length form) 2)
+ (not (eval-feature-expression (cadr form)))
+ (error "Invalid feature expression: %s" form)))
+ ((= (length form) 2)
+ (eval-feature-expression (cadr form)))
+ ((eq (car form) 'and)
+ (if (= (length form) 1)
+ t
+ (and (eval-feature-expression (cadr form))
+ (eval-feature-expression (cons 'and (cddr form))))))
+ ((eq (car form) 'or)
+ (if (= (length form) 1)
+ nil
+ (or (eval-feature-expression (cadr form))
+ (eval-feature-expression (cons 'or (cddr form))))))
+ (t (error "Invalid feature expression: %s" form))))
+\f
;;;; Process stuff.
(defun process-lines (program &rest args)
diff --git a/src/lread.c b/src/lread.c
index 72c01ed..32c36ee 100644
--- a/src/lread.c
+++ b/src/lread.c
@@ -96,6 +96,9 @@ Lisp_Object Qinhibit_file_name_operation;
Lisp_Object Qeval_buffer_list, Veval_buffer_list;
Lisp_Object Qfile_truename, Qdo_after_load_evaluation; /* ACM 2006/5/16 */
+/* Used in feature expression */
+Lisp_Object Qobarray, Qeval_feature_expression;
+
/* Used instead of Qget_file_char while loading *.elc files compiled
by Emacs 21 or older. */
static Lisp_Object Qget_emacs_mule_file_char;
@@ -2426,6 +2429,24 @@ read1 (readcharfun, pch, first_in_list)
UNREAD (c);
invalid_syntax ("#", 1);
}
+ /* feature expressions as in Common Lisp */
+ if (c == '+' || c == '-')
+ {
+ Lisp_Object fexp = read0(readcharfun);
+ fexp = call1 (Qeval_feature_expression, fexp);
+ if (c == '-')
+ fexp = NILP (fexp) ? Qt : Qnil;
+ if (NILP (fexp))
+ {
+ int count = SPECPDL_INDEX ();
+ Lisp_Object tem = Fmake_vector (make_number(17),make_number(0));
+ /* use a temporary obarray */
+ specbind (Qobarray, tem);
+ read0 (readcharfun);
+ unbind_to (count, Qnil);
+ }
+ goto retry;
+ }
if (c == '^')
{
c = READCHAR;
@@ -4536,6 +4557,11 @@ to load. See also `load-dangerous-libraries'. */);
Qcomma_dot = intern_c_string (",.");
staticpro (&Qcomma_dot);
+ Qeval_feature_expression = intern_c_string ("eval-feature-expression");
+ staticpro (&Qeval_feature_expression);
+ Qobarray = intern_c_string ("obarray");
+ staticpro(&Qobarray);
+
Qinhibit_file_name_operation = intern_c_string ("inhibit-file-name-operation");
staticpro (&Qinhibit_file_name_operation);
next prev parent reply other threads:[~2010-12-27 19:26 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-12-27 9:21 How and when to use GCPRO? Leo
2010-12-27 10:15 ` Andreas Schwab
2010-12-27 10:38 ` Leo
2010-12-27 11:17 ` Andreas Schwab
2010-12-27 16:00 ` Stefan Monnier
2010-12-27 17:51 ` Andreas Schwab
2010-12-28 1:01 ` Stefan Monnier
2010-12-28 1:33 ` Daniel Colascione
2010-12-28 2:17 ` Stefan Monnier
2010-12-28 3:37 ` Daniel Colascione
2010-12-28 4:15 ` Stefan Monnier
2010-12-28 4:36 ` Conservative scanning (was: Re: How and when to use GCPRO?) Daniel Colascione
2010-12-28 17:07 ` How and when to use GCPRO? Richard Stallman
2010-12-27 19:15 ` Leo
2010-12-27 19:26 ` Leo [this message]
2010-12-27 19:38 ` Common Lisp like feature expressions (was: How and when to use GCPRO?) Andreas Schwab
2010-12-27 19:57 ` Leo
2010-12-28 1:01 ` Common Lisp like feature expressions Stefan Monnier
2010-12-28 15:26 ` Leo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m1fwtjggqc.fsf_-_@cam.ac.uk \
--to=sdl.web@gmail.com \
--cc=emacs-devel@gnu.org \
--cc=monnier@iro.umontreal.ca \
--cc=schwab@linux-m68k.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).