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