Add a `when' macro

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2013-04-28 16:57:29 +02:00
parent 9b261a5004
commit f68e3b972b
3 changed files with 20 additions and 0 deletions

View File

@ -170,3 +170,14 @@ def drop_macro(tree):
return HyExpression([HySymbol('slice'),
ret,
HyInteger(n)])
@macro("when")
def when_macro(tree):
tree.pop(0) # "when"
test = tree.pop(0)
return HyExpression([
HySymbol('if'),
test,
HyExpression([HySymbol("do")]) + tree,
])

View File

@ -4,3 +4,4 @@
import hy # noqa
from .native_tests.math import * # noqa
from .native_tests.language import * # noqa
from .native_tests.when import * # noqa

View File

@ -0,0 +1,8 @@
(defn test-when []
"NATIVE: test when"
(assert (= (when true 1) 1))
(assert (= (when true 1 2) 2))
(assert (= (when true 1 3) 3))
(assert (= (when false 2) null))
(assert (= (when (= 1 2) 42) null))
(assert (= (when (= 2 2) 42) 42)))