diff --git a/docs/language/core.rst b/docs/language/core.rst index 6124911..81a277e 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -227,13 +227,28 @@ macroexpand Usage: ``(macroexpand form)`` -Returns the macro expansion of 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 83634b3..33db9b3 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -120,10 +120,15 @@ (catch [TypeError] false))) (defn macroexpand [form] - "Return the macro expansion of form" + "Return the full macro expansion of form" (import hy.macros) (hy.macros.macroexpand form --name--)) +(defn macroexpand-1 [form] + "Return the single step macro expansion of form" + (import hy.macros) + (hy.macros.macroexpand-1 form --name--)) + (defn neg? [n] "Return true if n is < 0" (_numeric-check n) @@ -219,6 +224,7 @@ (def *exports* ["cycle" "dec" "distinct" "drop" "drop_while" "empty?" "even?" "filter" "float?" "inc" "instance?" "integer?" "iterable?" - "iterate" "iterator?" "macroexpand" "neg?" "none?" "nth" - "numeric?" "odd?" "pos?" "remove" "repeat" "repeatedly" - "second" "string?" "take" "take_nth" "take_while" "zero?"]) + "iterate" "iterator?" "macroexpand" "macroexpand_1" "neg?" + "none?" "nth" "numeric?" "odd?" "pos?" "remove" "repeat" + "repeatedly" "second" "string?" "take" "take_nth" "take_while" + "zero?"]) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index eeb505f..fc54e0b 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -782,6 +782,14 @@ (defn test-macroexpand [] - "Test macroexpand on ->" - (assert (= (macroexpand '(-> (a b) (x y))) - '(x (a b) y)))) + "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)))))