diff --git a/hy/contrib/walk.hy b/hy/contrib/walk.hy index 3bed72b46..4e6433e55 100644 --- a/hy/contrib/walk.hy +++ b/hy/contrib/walk.hy @@ -13,7 +13,7 @@ [collections [OrderedDict]] [hy.macros [macroexpand :as mexpand]] [hy.compiler [HyASTCompiler calling-module]] - [hy.extra.reserved [special]]) + hy.extra.reserved) (defn walk [inner outer form] "``walk`` traverses ``form``, an arbitrary data structure. Applies @@ -220,13 +220,15 @@ [(= (get form 0) (hy.models.Symbol "require")) (ast-compiler.compile form) (return)] - [True (traverse (mexpand form ast-compiler.module ast-compiler))]) + [(in (get form 0) '[except unpack-mapping]) + (hy.models.Expression [(get form 0) #* (traverse (cut form 1 None))])] + [True (traverse (mexpand form ast-compiler.module ast-compiler :result-ok False))]) (if (coll? form) (traverse form) form))) (expand form)) -(setv _mangled-special (frozenset (map hy.mangle (special)))) +(setv _mangled-core-macs (frozenset (map hy.mangle (hy.extra.reserved.macros)))) (defn lambda-list [form] @@ -466,7 +468,7 @@ [(= head 'match) (self.handle-match)] [(= head 'quasiquote) (self.+quote)] ;; must be checked last! - [(in (hy.mangle head) _mangled-special) (self.handle-special-form)] + [(in (hy.mangle head) _mangled-core-macs) (self.handle-special-form)] ;; Not a special form. Traverse it like a coll [True (self.handle-coll)])) diff --git a/hy/core/language.hy b/hy/core/language.hy index 0ae12228a..e9c468fbc 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -309,7 +309,7 @@ " (+ n 1)) -(defn macroexpand [form] +(defn macroexpand [form [result-ok False]] "Return the full macro expansion of `form`. .. versionadded:: 0.10.0 @@ -327,7 +327,7 @@ " (import hy.macros) (setv module (calling-module)) - (hy.macros.macroexpand form module (HyASTCompiler module))) + (hy.macros.macroexpand form module (HyASTCompiler module) :result-ok result-ok)) (defn macroexpand-1 [form] "Return the single step macro expansion of `form`. diff --git a/hy/macros.py b/hy/macros.py index b90138192..480507e3a 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -273,7 +273,7 @@ def __exit__(self, exc_type, exc_value, exc_traceback): return False -def macroexpand(tree, module, compiler=None, once=False): +def macroexpand(tree, module, compiler=None, once=False, result_ok=True): """Expand the toplevel macros for the given Hy AST tree. Load the macros from the given `module`, then expand the (top-level) macros @@ -338,7 +338,7 @@ def macroexpand(tree, module, compiler=None, once=False): from hy.compiler import Result from ast import AST if isinstance(obj, (Result, AST)): - return obj + return obj if result_ok else tree if isinstance(obj, Expression): obj.module = inspect.getmodule(m)