From e77ce92635fa6cc442628ab8ac57f86152789e4f Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sun, 21 Apr 2019 11:45:58 -0400 Subject: [PATCH] Simplify gensym format --- NEWS.rst | 3 +++ docs/language/api.rst | 4 ++-- hy/core/language.hy | 4 ++-- tests/native_tests/core.hy | 6 +++--- tests/native_tests/native_macros.hy | 18 +++++++++--------- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index 18aa8ab..f925d01 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -15,6 +15,9 @@ New Features * Added a special form `setx` to create Python 3.8 assignment expressions. * New list? function. * New tuple? function. +* Gensyms now have a simpler format that's more concise when + mangled (e.g., `_hyx_XsemicolonXfooXvertical_lineX1235` is now + `_hyx_fooXUffffX1`). Bug Fixes ------------------------------ diff --git a/docs/language/api.rst b/docs/language/api.rst index f37f0c4..2a690f6 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -968,10 +968,10 @@ written without accidental variable name clashes. .. code-block:: clj => (gensym) - u':G_1235' + HySymbol('_G\uffff1') => (gensym "x") - u':x_1236' + HySymbol('_x\uffff2') .. seealso:: diff --git a/hy/core/language.hy b/hy/core/language.hy index 93f2fa3..53af7fb 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -214,7 +214,7 @@ Return series of accumulated sums (or other binary function results)." (instance? HySymbol s)) (import [threading [Lock]]) -(setv _gensym_counter 1234) +(setv _gensym_counter 0) (setv _gensym_lock (Lock)) (defn gensym [&optional [g "G"]] @@ -224,7 +224,7 @@ Return series of accumulated sums (or other binary function results)." (global _gensym_lock) (.acquire _gensym_lock) (try (do (setv _gensym_counter (inc _gensym_counter)) - (setv new_symbol (HySymbol (.format "_;{0}|{1}" g _gensym_counter)))) + (setv new_symbol (HySymbol (.format "_{}\uffff{}" g _gensym_counter)))) (finally (.release _gensym_lock))) new_symbol) diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index 995ff53..39a389c 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -282,12 +282,12 @@ result['y in globals'] = 'y' in globals()") (import [hy.models [HySymbol]]) (setv s1 (gensym)) (assert (isinstance s1 HySymbol)) - (assert (= 0 (.find s1 "_;G|"))) + (assert (= 0 (.find s1 "_G\uffff"))) (setv s2 (gensym "xx")) (setv s3 (gensym "xx")) - (assert (= 0 (.find s2 "_;xx|"))) + (assert (= 0 (.find s2 "_xx\uffff"))) (assert (not (= s2 s3))) - (assert (not (= (str s2) (str s3))))) + (assert (not (= (string s2) (string s3))))) (defn test-identity [] "NATIVE: testing the identity function" diff --git a/tests/native_tests/native_macros.hy b/tests/native_tests/native_macros.hy index d5c48c7..835939e 100644 --- a/tests/native_tests/native_macros.hy +++ b/tests/native_tests/native_macros.hy @@ -166,9 +166,9 @@ (setv _ast2 (hy-compile (hy-parse macro1) __name__)) (setv s1 (to_source _ast1)) (setv s2 (to_source _ast2)) - ;; and make sure there is something new that starts with _;G| - (assert (in (mangle "_;G|") s1)) - (assert (in (mangle "_;G|") s2)) + ;; and make sure there is something new that starts with _G\uffff + (assert (in (mangle "_G\uffff") s1)) + (assert (in (mangle "_G\uffff") s2)) ;; but make sure the two don't match each other (assert (not (= s1 s2)))) @@ -193,8 +193,8 @@ (setv _ast2 (hy-compile (hy-parse macro1) __name__)) (setv s1 (to_source _ast1)) (setv s2 (to_source _ast2)) - (assert (in (mangle "_;a|") s1)) - (assert (in (mangle "_;a|") s2)) + (assert (in (mangle "_a\uffff") s1)) + (assert (in (mangle "_a\uffff") s2)) (assert (not (= s1 s2)))) (defn test-defmacro/g! [] @@ -217,8 +217,8 @@ (setv _ast2 (hy-compile (hy-parse macro1) __name__)) (setv s1 (to_source _ast1)) (setv s2 (to_source _ast2)) - (assert (in (mangle "_;res|") s1)) - (assert (in (mangle "_;res|") s2)) + (assert (in (mangle "_res\uffff") s1)) + (assert (in (mangle "_res\uffff") s2)) (assert (not (= s1 s2))) ;; defmacro/g! didn't like numbers initially because they @@ -247,8 +247,8 @@ (setv _ast2 (hy-compile (hy-parse macro1) __name__)) (setv s1 (to_source _ast1)) (setv s2 (to_source _ast2)) - (assert (in (mangle "_;res|") s1)) - (assert (in (mangle "_;res|") s2)) + (assert (in (mangle "_res\uffff") s1)) + (assert (in (mangle "_res\uffff") s2)) (assert (not (= s1 s2))) ;; defmacro/g! didn't like numbers initially because they