diff --git a/hy/contrib/walk.hy b/hy/contrib/walk.hy index 9fc3771..a69078d 100644 --- a/hy/contrib/walk.hy +++ b/hy/contrib/walk.hy @@ -45,3 +45,11 @@ "Performs depth-first, pre-order traversal of form. Calls f on each sub-form, uses f's return value in place of the original." (walk (partial prewalk f) identity (f form))) + +(defn macroexpand-all [form] + "Recursively performs all possible macroexpansions in form." + (prewalk (fn [x] + (if (instance? HyExpression x) + (macroexpand x) + x)) + form)) diff --git a/tests/native_tests/contrib/walk.hy b/tests/native_tests/contrib/walk.hy index 854dfc7..24feb76 100644 --- a/tests/native_tests/contrib/walk.hy +++ b/tests/native_tests/contrib/walk.hy @@ -22,3 +22,7 @@ (assert (= (walk identity (partial collector acc) walk-form) nil)) (assert (= acc [walk-form])))) + +(defn test-macroexpand-all [] + (assert (= (macroexpand-all '(with [a b c] (for [d c] foo))) + '(with* [a] (with* [b] (with* [c] (do (for* [d c] foo))))))))