From f25177e9a69902645fc98e061d9c0565ff945235 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Sun, 28 Apr 2013 17:02:08 +0200 Subject: [PATCH] Add a `unless' macro Signed-off-by: Julien Danjou --- hy/core/bootstrap.py | 12 ++++++++++++ tests/__init__.py | 1 + tests/native_tests/unless.hy | 8 ++++++++ 3 files changed, 21 insertions(+) create mode 100644 tests/native_tests/unless.hy diff --git a/hy/core/bootstrap.py b/hy/core/bootstrap.py index 743a92d..a5685cc 100644 --- a/hy/core/bootstrap.py +++ b/hy/core/bootstrap.py @@ -181,3 +181,15 @@ def when_macro(tree): test, HyExpression([HySymbol("do")]) + tree, ]) + + +@macro("unless") +def unless_macro(tree): + tree.pop(0) # "unless" + test = tree.pop(0) + return HyExpression([ + HySymbol('if'), + test, + HySymbol('None'), + HyExpression([HySymbol("do")]) + tree, + ]) diff --git a/tests/__init__.py b/tests/__init__.py index 14bdec2..997a467 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -4,4 +4,5 @@ import hy # noqa from .native_tests.math import * # noqa from .native_tests.language import * # noqa +from .native_tests.unless import * # noqa from .native_tests.when import * # noqa diff --git a/tests/native_tests/unless.hy b/tests/native_tests/unless.hy new file mode 100644 index 0000000..f8c298a --- /dev/null +++ b/tests/native_tests/unless.hy @@ -0,0 +1,8 @@ +(defn test-unless [] + "NATIVE: test unless" + (assert (= (unless false 1) 1)) + (assert (= (unless false 1 2) 2)) + (assert (= (unless false 1 3) 3)) + (assert (= (unless true 2) null)) + (assert (= (unless (!= 1 2) 42) null)) + (assert (= (unless (!= 2 2) 42) 42)))