diff --git a/hy/compiler.py b/hy/compiler.py index a93ab26..174a99b 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -176,8 +176,10 @@ class HyASTCompiler(object): raise TypeError("if expects at least 2 arguments, got 1") body = self._code_branch(self.compile(body)) orel = [] - if len(expr) > 0: + if len(expr) == 1: orel = self._code_branch(self.compile(expr.pop(0))) + elif len(expr) > 1: + raise TypeError("if expects 2 or 3 arguments, got %d" % (len(expr) + 2)) return ast.If(test=test, body=body, diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 7db9b57..5f03672 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -69,6 +69,15 @@ def test_ast_bad_if_1_arg(): pass +def test_ast_bad_if_too_much_arg(): + "Make sure AST can't compile invalid if" + try: + hy_compile(tokenize("(if 1 2 3 4 5)")) + assert False + except TypeError: + pass + + def test_ast_valid_if(): "Make sure AST can't compile invalid if" hy_compile(tokenize("(if foo bar)"))