1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-24 09:17:17 +00:00

Use gensym in do-compare macro.

Improve hygiene of macro in case later changes introduce subtle bugs.
This commit is contained in:
Calvin Rose 2024-07-31 11:19:44 -05:00
parent aaeaa3a944
commit 631622aa48

View File

@ -832,17 +832,21 @@
(defmacro- do-compare (defmacro- do-compare
[x y] [x y]
(def f (gensym))
(def f-res (gensym))
(def g (gensym))
(def g-res (gensym))
~(do ~(do
(def f (get ,x :compare)) (def ,f (,get ,x :compare))
(def f-res (if f (f ,x ,y))) (def ,f-res (if ,f (,f ,x ,y)))
(if f-res (if ,f-res
f-res ,f-res
(do (do
(def g (get ,y :compare)) (def ,g (,get ,y :compare))
(def g-res (if g (- (g ,y ,x)))) (def ,g-res (if ,g (,- (,g ,y ,x))))
(if g-res (if ,g-res
g-res ,g-res
(cmp ,x ,y)))))) (,cmp ,x ,y))))))
(defn compare (defn compare
``Polymorphic compare. Returns -1, 0, 1 for x < y, x = y, x > y respectively. ``Polymorphic compare. Returns -1, 0, 1 for x < y, x = y, x > y respectively.