From 80feaf57fcc826f55d037cabb393a48f5e460786 Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Fri, 11 Oct 2013 08:09:28 +0200 Subject: [PATCH 1/6] Make HyObject.replace more resilient This makes it work on quoted objects --- hy/models/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hy/models/__init__.py b/hy/models/__init__.py index 42032ca..31820f1 100644 --- a/hy/models/__init__.py +++ b/hy/models/__init__.py @@ -29,7 +29,7 @@ class HyObject(object): if isinstance(other, HyObject): for attr in ["start_line", "end_line", "start_column", "end_column"]: - if not hasattr(self, attr): + if not hasattr(self, attr) and hasattr(other, attr): setattr(self, attr, getattr(other, attr)) else: raise TypeError("Can't replace a non Hy object with a Hy object") From a34db9119b008ea5997aa6ebc291a7021f40293c Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Fri, 11 Oct 2013 11:55:14 +0100 Subject: [PATCH 2/6] Add macroexpand to core/language.hy --- hy/core/language.hy | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/hy/core/language.hy b/hy/core/language.hy index a96b25d..83634b3 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -119,6 +119,11 @@ (try (= x (iter x)) (catch [TypeError] false))) +(defn macroexpand [form] + "Return the macro expansion of form" + (import hy.macros) + (hy.macros.macroexpand form --name--)) + (defn neg? [n] "Return true if n is < 0" (_numeric-check n) @@ -212,9 +217,8 @@ (_numeric_check n) (= n 0)) -(def *exports* ["cycle" "dec" "distinct" "drop" "drop_while" "empty?" - "even?" "filter" "float?" "inc" - "instance?" "integer?" "iterable?" "iterate" "iterator?" "neg?" - "none?" "nth" "numeric?" "odd?" "pos?" "remove" "repeat" - "repeatedly" "second" "string?" "take" "take_nth" "take_while" - "zero?"]) +(def *exports* ["cycle" "dec" "distinct" "drop" "drop_while" "empty?" "even?" + "filter" "float?" "inc" "instance?" "integer?" "iterable?" + "iterate" "iterator?" "macroexpand" "neg?" "none?" "nth" + "numeric?" "odd?" "pos?" "remove" "repeat" "repeatedly" + "second" "string?" "take" "take_nth" "take_while" "zero?"]) From 2e60d6b47b517a9573fcd4bb9e562d5f3d3c5b60 Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Fri, 11 Oct 2013 12:03:52 +0100 Subject: [PATCH 3/6] Added a simple test for macroexpand --- tests/native_tests/language.hy | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index d19ca17..eeb505f 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -775,7 +775,13 @@ (.append y x)) (assert (= y [5]))) + (defn test-empty-list [] "Evaluate an empty list to a []" (assert (= () []))) + +(defn test-macroexpand [] + "Test macroexpand on ->" + (assert (= (macroexpand '(-> (a b) (x y))) + '(x (a b) y)))) From 033198a90eabce89276f60ad9831475f61d47ea4 Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Fri, 11 Oct 2013 12:06:22 +0100 Subject: [PATCH 4/6] Added documentation for macroexpand --- docs/language/core.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/language/core.rst b/docs/language/core.rst index 8a34549..6124911 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -222,6 +222,18 @@ Contrast with :ref:`iterable?-fn`. .. _neg?-fn: +macroexpand +----------- + +Usage: ``(macroexpand form)`` + +Returns the macro expansion of form. + +.. code-block:: clojure + + => (macroexpand '(-> (a b) (x y))) + (u'x' (u'a' u'b') u'y') + neg? ---- From defccc6853f0a9dbc2ef4b9760f0b34ea4a1353f Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Fri, 11 Oct 2013 13:50:10 +0100 Subject: [PATCH 5/6] Added macroexpand-1 --- docs/language/core.rst | 17 ++++++++++++++++- hy/core/language.hy | 14 ++++++++++---- tests/native_tests/language.hy | 14 +++++++++++--- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/docs/language/core.rst b/docs/language/core.rst index 6124911..81a277e 100644 --- a/docs/language/core.rst +++ b/docs/language/core.rst @@ -227,13 +227,28 @@ macroexpand Usage: ``(macroexpand form)`` -Returns the macro expansion of form. +Returns the full macro expansion of form. .. code-block:: clojure => (macroexpand '(-> (a b) (x y))) (u'x' (u'a' u'b') u'y') + => (macroexpand '(-> (a b) (-> (c d) (e f)))) + (u'e' (u'c' (u'a' u'b') u'd') u'f') + +macroexpand-1 +------------- + +Usage: ``(macroexpand-1 form)`` + +Returns the single step macro expansion of form. + +.. code-block:: clojure + + => (macroexpand-1 '(-> (a b) (-> (c d) (e f)))) + (u'_>' (u'a' u'b') (u'c' u'd') (u'e' u'f')) + neg? ---- diff --git a/hy/core/language.hy b/hy/core/language.hy index 83634b3..33db9b3 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -120,10 +120,15 @@ (catch [TypeError] false))) (defn macroexpand [form] - "Return the macro expansion of form" + "Return the full macro expansion of form" (import hy.macros) (hy.macros.macroexpand form --name--)) +(defn macroexpand-1 [form] + "Return the single step macro expansion of form" + (import hy.macros) + (hy.macros.macroexpand-1 form --name--)) + (defn neg? [n] "Return true if n is < 0" (_numeric-check n) @@ -219,6 +224,7 @@ (def *exports* ["cycle" "dec" "distinct" "drop" "drop_while" "empty?" "even?" "filter" "float?" "inc" "instance?" "integer?" "iterable?" - "iterate" "iterator?" "macroexpand" "neg?" "none?" "nth" - "numeric?" "odd?" "pos?" "remove" "repeat" "repeatedly" - "second" "string?" "take" "take_nth" "take_while" "zero?"]) + "iterate" "iterator?" "macroexpand" "macroexpand_1" "neg?" + "none?" "nth" "numeric?" "odd?" "pos?" "remove" "repeat" + "repeatedly" "second" "string?" "take" "take_nth" "take_while" + "zero?"]) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index eeb505f..fc54e0b 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -782,6 +782,14 @@ (defn test-macroexpand [] - "Test macroexpand on ->" - (assert (= (macroexpand '(-> (a b) (x y))) - '(x (a b) y)))) + "Test macroexpand on ->" + (assert (= (macroexpand '(-> (a b) (x y))) + '(x (a b) y))) + (assert (= (macroexpand '(-> (a b) (-> (c d) (e f)))) + '(e (c (a b) d) f)))) + + +(defn test-macroexpand-1 [] + "Test macroexpand-1 on ->" + (assert (= (macroexpand-1 '(-> (a b) (-> (c d) (e f)))) + '(-> (a b) (c d) (e f))))) From c34db29ba8801eed0bfd0ea6a3dc25bd943e1bc1 Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Fri, 11 Oct 2013 14:30:55 +0100 Subject: [PATCH 6/6] Fixed --name-- scope problem using ugly inspect code --- hy/core/language.hy | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/hy/core/language.hy b/hy/core/language.hy index 33db9b3..6450edc 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -121,13 +121,21 @@ (defn macroexpand [form] "Return the full macro expansion of form" + (import inspect) (import hy.macros) - (hy.macros.macroexpand form --name--)) + + (setv f (get (get (.stack inspect) 1) 0)) + (setv name (get f.f_globals "__name__")) + (hy.macros.macroexpand form name)) (defn macroexpand-1 [form] "Return the single step macro expansion of form" + (import inspect) (import hy.macros) - (hy.macros.macroexpand-1 form --name--)) + + (setv f (get (get (.stack inspect) 1) 0)) + (setv name (get f.f_globals "__name__")) + (hy.macros.macroexpand-1 form name)) (defn neg? [n] "Return true if n is < 0"