diff --git a/hy/compiler.py b/hy/compiler.py index 284c7fc..0f92131 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -1828,7 +1828,7 @@ class HyASTCompiler(object): return ret @builds("while") - @checkargs(min=2) + @checkargs(min=1) def compile_while_expression(self, expr): expr.pop(0) # "while" cond = expr.pop(0) @@ -1867,7 +1867,8 @@ class HyASTCompiler(object): ret = cond_compiled + asty.While( expr, test=cond_compiled.force_expr, - body=body.stmts, orelse=orel.stmts) + body=body.stmts or [asty.Pass(expr)], + orelse=orel.stmts) ret.contains_yield = body.contains_yield return ret diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index e5a0ddd..faaa772 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -97,7 +97,6 @@ def test_ast_invalid_unary_op(): def test_ast_bad_while(): "Make sure AST can't compile invalid while" cant_compile("(while)") - cant_compile("(while (True))") def test_ast_good_do(): diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 6840f29..d76102c 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -283,7 +283,21 @@ (setv fact (* fact count)) (setv count (- count 1))) (assert (= count 0)) - (assert (= fact 120))) + (assert (= fact 120)) + + (setv l []) + (defn f [] + (.append l 1) + (len l)) + (while (!= (f) 4)) + (assert (= l [1 1 1 1])) + + (setv l []) + (defn f [] + (.append l 1) + (len l)) + (while (!= (f) 4) (do)) + (assert (= l [1 1 1 1]))) (defn test-while-loop-else [] (setv count 5)