~bakpakin/janet

631622aa485e7d947501623247d913ea9bd14324 — Calvin Rose a month ago aaeaa3a
Use gensym in do-compare macro.

Improve hygiene of macro in case later changes introduce subtle bugs.
1 files changed, 13 insertions(+), 9 deletions(-)

M src/boot/boot.janet
M src/boot/boot.janet => src/boot/boot.janet +13 -9
@@ 832,17 832,21 @@

(defmacro- do-compare
  [x y]
  (def f (gensym))
  (def f-res (gensym))
  (def g (gensym))
  (def g-res (gensym))
  ~(do
     (def f (get ,x :compare))
     (def f-res (if f (f ,x ,y)))
     (if f-res
       f-res
     (def ,f (,get ,x :compare))
     (def ,f-res (if ,f (,f ,x ,y)))
     (if ,f-res
       ,f-res
       (do
         (def g (get ,y :compare))
         (def g-res (if g (- (g ,y ,x))))
         (if g-res
           g-res
           (cmp ,x ,y))))))
         (def ,g (,get ,y :compare))
         (def ,g-res (if ,g (,- (,g ,y ,x))))
         (if ,g-res
           ,g-res
           (,cmp ,x ,y))))))

(defn compare
  ``Polymorphic compare. Returns -1, 0, 1 for x < y, x = y, x > y respectively.