diff --git a/hy/compiler.py b/hy/compiler.py index 8000d25..e860195 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -151,8 +151,16 @@ class HyASTCompiler(object): @builds("if") def compile_if_expression(self, expr): expr.pop(0) - test = self.compile(expr.pop(0)) - body = self._code_branch(self.compile(expr.pop(0))) + try: + test = expr.pop(0) + except IndexError: + raise TypeError("if expects at least 2 arguments, got 0") + test = self.compile(test) + try: + body = expr.pop(0) + except IndexError: + raise TypeError("if expects at least 2 arguments, got 1") + body = self._code_branch(self.compile(body)) orel = [] if len(expr) > 0: orel = self._code_branch(self.compile(expr.pop(0))) diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index fafcbcf..8714486 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -43,6 +43,29 @@ def test_ast_bad_type(): pass +def test_ast_bad_if_0_arg(): + "Make sure AST can't compile invalid if" + try: + hy_compile(tokenize("(if)")) + assert False + except TypeError: + pass + + +def test_ast_bad_if_1_arg(): + "Make sure AST can't compile invalid if" + try: + hy_compile(tokenize("(if foobar)")) + assert False + except TypeError: + pass + + +def test_ast_valid_if(): + "Make sure AST can't compile invalid if" + hy_compile(tokenize("(if foo bar)")) + + def test_ast_expression_basics(): """ Ensure basic AST expression conversion works. """ code = hy_compile(tokenize("(foo bar)")).body[0]