From 1c12b2870ed438f7916b7375f573609b02b4e5b6 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Sat, 17 Aug 2013 11:37:48 -0400 Subject: [PATCH] Add yield from via macro magic. This will let us use (basic) yield from behavior from Python 2. This isn't complete, and is low-hanging fruit for others willing to hack on hy. I've also changed the macrosystem to allow for proper bootstrapping. This is similar to how it's done elsewhere in the codebase (stdlib stuff). --- hy/core/macros.hy | 6 ++++++ hy/macros.py | 18 ++++++++++++++---- tests/native_tests/native_macros.hy | 8 ++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 hy/core/macros.hy diff --git a/hy/core/macros.hy b/hy/core/macros.hy new file mode 100644 index 0000000..28bb36e --- /dev/null +++ b/hy/core/macros.hy @@ -0,0 +1,6 @@ +;;; hy core macros + +(defmacro yield-from [_hy_yield_from_els] + (quasiquote + (for [_hy_yield_from_x (unquote _hy_yield_from_els)] + (yield _hy_yield_from_x)))) diff --git a/hy/macros.py b/hy/macros.py index 595edbb..baa5e51 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -31,10 +31,14 @@ from hy.util import str_type from collections import defaultdict -MACROS = [ +CORE_MACROS = [ "hy.core.bootstrap", ] +EXTRA_MACROS = [ + "hy.core.macros", +] + _hy_macros = defaultdict(dict) @@ -74,7 +78,7 @@ _wrappers = { def process(tree, module_name): - load_macros() + load_macros(module_name) old = None while old != tree: old = tree @@ -82,8 +86,14 @@ def process(tree, module_name): return tree -def load_macros(): - for module in MACROS: +def load_macros(module_name): + for module in CORE_MACROS: + __import__(module) + + if module_name.startswith("hy.core"): + return + + for module in EXTRA_MACROS: __import__(module) diff --git a/tests/native_tests/native_macros.hy b/tests/native_tests/native_macros.hy index 439ee03..d890d70 100644 --- a/tests/native_tests/native_macros.hy +++ b/tests/native_tests/native_macros.hy @@ -91,3 +91,11 @@ (assert initialized) (assert (test-initialized)) + +(defn test-yield-from [] + "NATIVE: testing yield from" + (defn yield-from-test [] + (for [i (range 3)] + (yield i)) + (yield-from [1 2 3])) + (assert (= (list (yield-from-test)) [0 1 2 1 2 3])))