diff --git a/docs/language/core.rst b/docs/language/core.rst index 300da15..c9b67f2 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -222,6 +222,33 @@ Contrast with :ref:`iterable?-fn`. .. _neg?-fn: +macroexpand +----------- + +Usage: ``(macroexpand form)`` + +Returns the full macro expansion of form. + +.. code-block:: clojure + + => (macroexpand '(-> (a b) (x y))) + (u'x' (u'a' u'b') u'y') + + => (macroexpand '(-> (a b) (-> (c d) (e f)))) + (u'e' (u'c' (u'a' u'b') u'd') u'f') + +macroexpand-1 +------------- + +Usage: ``(macroexpand-1 form)`` + +Returns the single step macro expansion of form. + +.. code-block:: clojure + + => (macroexpand-1 '(-> (a b) (-> (c d) (e f)))) + (u'_>' (u'a' u'b') (u'c' u'd') (u'e' u'f')) + neg? ---- diff --git a/hy/core/language.hy b/hy/core/language.hy index 5c69071..0b8b0d3 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -151,6 +151,24 @@ (try (= x (iter x)) (catch [TypeError] false))) +(defn macroexpand [form] + "Return the full macro expansion of form" + (import inspect) + (import hy.macros) + + (setv f (get (get (.stack inspect) 1) 0)) + (setv name (get f.f_globals "__name__")) + (hy.macros.macroexpand form name)) + +(defn macroexpand-1 [form] + "Return the single step macro expansion of form" + (import inspect) + (import hy.macros) + + (setv f (get (get (.stack inspect) 1) 0)) + (setv name (get f.f_globals "__name__")) + (hy.macros.macroexpand-1 form name)) + (defn neg? [n] "Return true if n is < 0" (_numeric-check n) @@ -254,8 +272,9 @@ (_numeric_check n) (= n 0)) -(def *exports* '[cycle dec distinct drop drop-while empty? even? filter flatten - float? gensym - inc instance? integer integer? iterable? iterate iterator? neg? - nil? none? nth numeric? odd? pos? remove repeat repeatedly second - string string? take take-nth take-while zero?]) +(def *exports* '[cycle dec distinct drop drop-while empty? even? filter + flatten float? gensym inc instance? integer integer? + iterable? iterate iterator? macroexpand macroexpand-1 + neg? nil? none? nth numeric? odd? pos? remove repeat + repeatedly second string string? take take-nth + take-while zero?]) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 30a63e4..952dc49 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -817,16 +817,18 @@ (defn test-continue-continuation [] "NATIVE: test checking if continue actually continues" (setv y []) - (for [x (range 10)] - (if (!= x 5) - (continue)) + (for [x (range 10)] + (if (!= x 5) + (continue)) (.append y x)) (assert (= y [5]))) + (defn test-empty-list [] "Evaluate an empty list to a []" (assert (= () []))) + (defn test-string [] (assert (string? (string "a"))) (assert (string? (string 1))) @@ -846,3 +848,17 @@ (assert (= test [0 1 2 3])) (del (get test 2)) (assert (= test [0 1 3]))) + + +(defn test-macroexpand [] + "Test macroexpand on ->" + (assert (= (macroexpand '(-> (a b) (x y))) + '(x (a b) y))) + (assert (= (macroexpand '(-> (a b) (-> (c d) (e f)))) + '(e (c (a b) d) f)))) + + +(defn test-macroexpand-1 [] + "Test macroexpand-1 on ->" + (assert (= (macroexpand-1 '(-> (a b) (-> (c d) (e f)))) + '(-> (a b) (c d) (e f)))))