bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#14892: [PATCH] Enhance Elisp compare functions (< <= > >=) to take v


From: Barry OReilly
Subject: bug#14892: [PATCH] Enhance Elisp compare functions (< <= > >=) to take var args
Date: Fri, 6 Sep 2013 19:02:14 -0400

Here is a patch to implement this. The tests in the patch pass.

diff --git a/src/bytecode.c b/src/bytecode.c
index e0e7b22..b777220 100644
--- a/src/bytecode.c
+++ b/src/bytecode.c
@@ -1364,40 +1364,36 @@ exec_byte_code (Lisp_Object bytestr, Lisp_Object vector, Lisp_Object maxdepth,
 
     CASE (Bgtr):
       {
-        Lisp_Object v1;
         BEFORE_POTENTIAL_GC ();
-        v1 = POP;
-        TOP = Fgtr (TOP, v1);
+        DISCARD (1);
+        TOP = Fgtr (2, &TOP);
         AFTER_POTENTIAL_GC ();
         NEXT;
       }
 
     CASE (Blss):
       {
-        Lisp_Object v1;
         BEFORE_POTENTIAL_GC ();
-        v1 = POP;
-        TOP = Flss (TOP, v1);
+        DISCARD (1);
+        TOP = Flss (2, &TOP);
         AFTER_POTENTIAL_GC ();
         NEXT;
       }
 
     CASE (Bleq):
       {
-        Lisp_Object v1;
         BEFORE_POTENTIAL_GC ();
-        v1 = POP;
-        TOP = Fleq (TOP, v1);
+        DISCARD (1);
+        TOP = Fleq (2, &TOP);
         AFTER_POTENTIAL_GC ();
         NEXT;
       }
 
     CASE (Bgeq):
       {
-        Lisp_Object v1;
         BEFORE_POTENTIAL_GC ();
-        v1 = POP;
-        TOP = Fgeq (TOP, v1);
+        DISCARD (1);
+        TOP = Fgeq (2, &TOP);
         AFTER_POTENTIAL_GC ();
         NEXT;
       }
diff --git a/src/data.c b/src/data.c
index 9f4bd1f..cbfb18f 100644
--- a/src/data.c
+++ b/src/data.c
@@ -2310,41 +2310,53 @@ arithcompare (Lisp_Object num1, Lisp_Object num2, enum comparison comparison)
     }
 }
 
-DEFUN ("=", Feqlsign, Seqlsign, 2, 2, 0,
-       doc: /* Return t if two args, both numbers or markers, are equal.  */)
-  (register Lisp_Object num1, Lisp_Object num2)
+static Lisp_Object
+arithcompare_driver (ptrdiff_t nargs, Lisp_Object *args,
+                     enum comparison comparison)
 {
-  return arithcompare (num1, num2, equal);
+  for (ptrdiff_t argnum = 1; argnum < nargs; ++argnum)
+    {
+      if (EQ (Qnil, arithcompare (args[argnum-1], args[argnum], comparison)))
+        return Qnil;
+    }
+  return Qt;
 }
 
-DEFUN ("<", Flss, Slss, 2, 2, 0,
-       doc: /* Return t if first arg is less than second arg.  Both must be numbers or markers.  */)
-  (register Lisp_Object num1, Lisp_Object num2)
+DEFUN ("=", Feqlsign, Seqlsign, 1, MANY, 0,
+       doc: /* Return t if args, all numbers or markers, are equal.  */)
+  (ptrdiff_t nargs, Lisp_Object *args)
 {
-  return arithcompare (num1, num2, less);
+  return arithcompare_driver (nargs, args, equal);
 }
 
-DEFUN (">", Fgtr, Sgtr, 2, 2, 0,
-       doc: /* Return t if first arg is greater than second arg.  Both must be numbers or markers.  */)
-  (register Lisp_Object num1, Lisp_Object num2)
+DEFUN ("<", Flss, Slss, 1, MANY, 0,
+       doc: /* Return t if each arg is less than the next arg.  All must be numbers or markers.  */)
+  (ptrdiff_t nargs, Lisp_Object *args)
 {
-  return arithcompare (num1, num2, grtr);
+  return arithcompare_driver (nargs, args, less);
 }
 
-DEFUN ("<=", Fleq, Sleq, 2, 2, 0,
-       doc: /* Return t if first arg is less than or equal to second arg.
-Both must be numbers or markers.  */)
-  (register Lisp_Object num1, Lisp_Object num2)
+DEFUN (">", Fgtr, Sgtr, 1, MANY, 0,
+       doc: /* Return t if each arg is greater than the next arg.  All must be numbers or markers.  */)
+  (ptrdiff_t nargs, Lisp_Object *args)
 {
-  return arithcompare (num1, num2, less_or_equal);
+  return arithcompare_driver (nargs, args, grtr);
 }
 
-DEFUN (">=", Fgeq, Sgeq, 2, 2, 0,
-       doc: /* Return t if first arg is greater than or equal to second arg.
-Both must be numbers or markers.  */)
-  (register Lisp_Object num1, Lisp_Object num2)
+DEFUN ("<=", Fleq, Sleq, 1, MANY, 0,
+       doc: /* Return t if each arg is less than or equal to the next arg.
+All must be numbers or markers.  */)
+  (ptrdiff_t nargs, Lisp_Object *args)
+{
+  return arithcompare_driver (nargs, args, less_or_equal);
+}
+
+DEFUN (">=", Fgeq, Sgeq, 1, MANY, 0,
+       doc: /* Return t if each arg is greater than or equal to the next arg.
+All must be numbers or markers.  */)
+  (ptrdiff_t nargs, Lisp_Object *args)
 {
-  return arithcompare (num1, num2, grtr_or_equal);
+  return arithcompare_driver (nargs, args, grtr_or_equal);
 }
 
 DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
diff --git a/src/fileio.c b/src/fileio.c
index a751a73..6a8ab9b 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -5121,7 +5121,8 @@ DEFUN ("car-less-than-car", Fcar_less_than_car, Scar_less_than_car, 2, 2, 0,
        doc: /* Return t if (car A) is numerically less than (car B).  */)
   (Lisp_Object a, Lisp_Object b)
 {
-  return Flss (Fcar (a), Fcar (b));
+  Lisp_Object args[2] = { Fcar (a), Fcar (b), };
+  return Flss (2, args);
 }
 
 /* Build the complete list of annotations appropriate for writing out
diff --git a/test/automated/data-tests.el b/test/automated/data-tests.el
index e69de29..2298fa3 100644
--- a/test/automated/data-tests.el
+++ b/test/automated/data-tests.el
@@ -0,0 +1,75 @@
+;;; data-tests.el --- tests for src/data.c
+
+;; Copyright (C) 2013 Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+
+;; This program 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.
+;;
+;; This program 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 program.  If not, see `http://www.gnu.org/licenses/'.
+
+;;; Commentary:
+
+;;; Code:
+
+(ert-deftest data-tests-= ()
+  (should-error (=))
+  (should (= 1))
+  (should (= 2 2))
+  (should (= 9 9 9 9 9 9 9 9 9))
+  (should-not (apply #'= '(3 8 3)))
+  (should-error (= 9 9 'foo))
+  ;; Short circuits before getting to bad arg
+  (should-not (= 9 8 'foo)))
+
+(ert-deftest data-tests-< ()
+  (should-error (<))
+  (should (< 1))
+  (should (< 2 3))
+  (should (< -6 -1 0 2 3 4 8 9 999))
+  (should-not (apply #'< '(3 8 3)))
+  (should-error (< 9 10 'foo))
+  ;; Short circuits before getting to bad arg
+  (should-not (< 9 8 'foo)))
+
+(ert-deftest data-tests-> ()
+  (should-error (>))
+  (should (> 1))
+  (should (> 3 2))
+  (should (> 6 1 0 -2 -3 -4 -8 -9 -999))
+  (should-not (apply #'> '(3 8 3)))
+  (should-error (> 9 8 'foo))
+  ;; Short circuits before getting to bad arg
+  (should-not (> 8 9 'foo)))
+
+(ert-deftest data-tests-<= ()
+  (should-error (<=))
+  (should (<= 1))
+  (should (<= 2 3))
+  (should (<= -6 -1 -1 0 0 0 2 3 4 8 999))
+  (should-not (apply #'<= '(3 8 3 3)))
+  (should-error (<= 9 10 'foo))
+  ;; Short circuits before getting to bad arg
+  (should-not (<= 9 8 'foo)))
+
+(ert-deftest data-tests->= ()
+  (should-error (>=))
+  (should (>= 1))
+  (should (>= 3 2))
+  (should (>= 666 1 0 0 -2 -3 -3 -3 -4 -8 -8 -9 -999))
+  (should-not (apply #'>= '(3 8 3)))
+  (should-error (>= 9 8 'foo))
+  ;; Short circuits before getting to bad arg
+  (should-not (>= 8 9 'foo)))
+
+;;; data-tests.el ends here
+

I didn't do /=. If I implement /= with Fsort, then it won't behave
quite like in other Lisps.

In SBCL:
  (/= 1 2 3 'foo) signals error
  (/= 1 2 1 'foo) evaluates to NIL

To not get an error for the latter, I would want to use a hash map or
tree map. Are either of these ready-available to Emacs C code?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]