From fa24042cb00613e1b448b95443b0bd5ba1d1c0a2 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sun, 26 Jan 2014 03:59:47 +0100 Subject: [PATCH] hy.contrib.walk: Add (macroexpand-all) This function will recursively perform all possible macroexpansions in the supplied form. Unfortunately, it also traverses into quasiquoted parts, where it shouldn't, but it is a useful estimation of macro expansion anyway. Signed-off-by: Gergely Nagy --- hy/contrib/walk.hy | 8 ++++++++ tests/native_tests/contrib/walk.hy | 4 ++++ 2 files changed, 12 insertions(+) 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))))))))