Merge pull request #1664 from brandonwillard/require-in-macroexpand-all

Expand `require`d macros in `macroexpand-all`
This commit is contained in:
gilch 2018-08-02 23:57:32 -06:00 committed by GitHub
commit 109c0b0f5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 4 deletions

View File

@ -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>

View File

@ -91,6 +91,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
==============================

View File

@ -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]
@ -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)))
@ -325,4 +329,3 @@ as can nested let forms.
expander)))
;; (defmacro macrolet [])

View File

@ -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)))

View File

@ -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))