Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand required macros in macroexpand-all #1664

Merged
merged 3 commits into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@
* Simon Gomizelj <simon@vodik.xyz>
* Yigong Wang <wang@yigo.ng>
* Oskar Kvist <oskar.kvist@gmail.com>
* Brandon T. Willard <brandonwillard@gmail.com>
1 change: 1 addition & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Misc. Improvements
----------------------------
* `hy-repr` uses registered functions instead of methods.
* `hy-repr` supports more standard types.
* `macroexpand-all` will now expand macros introduced by a `require` in the body of a macro.

0.14.0
==============================
Expand Down
9 changes: 6 additions & 3 deletions hy/contrib/walk.hy
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
(defn macroexpand-all [form &optional module-name]
"Recursively performs all possible macroexpansions in form."
(setv module-name (or module-name (calling-module-name))
quote-level [0]) ; TODO: make nonlocal after dropping Python2
quote-level [0]
ast-compiler (HyASTCompiler module-name)) ; TODO: make nonlocal after dropping Python2
(defn traverse [form]
(walk expand identity form))
(defn expand [form]
Expand All @@ -64,7 +65,10 @@
[True (traverse form)])]
[(= (first form) 'quote) form]
[(= (first form) 'quasiquote) (+quote)]
[True (traverse (mexpand form (HyASTCompiler module-name)))])
[(= (first form) (HySymbol "require"))
(ast-compiler.compile form)
(return)]
[True (traverse (mexpand form ast-compiler))])
(if (coll? form)
(traverse form)
form)))
Expand Down Expand Up @@ -325,4 +329,3 @@ as can nested let forms.
expander)))

;; (defmacro macrolet [])

10 changes: 9 additions & 1 deletion tests/native_tests/contrib/walk.hy
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@
(do '(with [b 2])
`(with [c 3]
~(with* [d 4] (do))
~@[(with* [e 5] (do))]))))))
~@[(with* [e 5] (do))])))))

(defmacro require-macro []
`(do
(require [tests.resources.macros [test-macro :as my-test-macro]])
(my-test-macro)))

(assert (= (last (macroexpand-all '(require-macro)))
'(setv blah 1))))

(defn test-let-basic []
(assert (zero? (let [a 0] a)))
Expand Down
3 changes: 3 additions & 0 deletions tests/resources/macros.hy
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
(defn f [&rest args] (.join "" (+ (, "c") args)))
(setv variable (HySymbol (->> "d" (f))))
`(setv ~variable 5))

(defmacro test-macro []
'(setv blah 1))