Allow while with an empty body

This commit is contained in:
Kodi Arfer 2018-04-15 16:00:36 -07:00
parent 1eccd10d24
commit b7e5c5f17a
3 changed files with 18 additions and 4 deletions

View File

@ -1828,7 +1828,7 @@ class HyASTCompiler(object):
return ret return ret
@builds("while") @builds("while")
@checkargs(min=2) @checkargs(min=1)
def compile_while_expression(self, expr): def compile_while_expression(self, expr):
expr.pop(0) # "while" expr.pop(0) # "while"
cond = expr.pop(0) cond = expr.pop(0)
@ -1867,7 +1867,8 @@ class HyASTCompiler(object):
ret = cond_compiled + asty.While( ret = cond_compiled + asty.While(
expr, test=cond_compiled.force_expr, 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 ret.contains_yield = body.contains_yield
return ret return ret

View File

@ -97,7 +97,6 @@ def test_ast_invalid_unary_op():
def test_ast_bad_while(): def test_ast_bad_while():
"Make sure AST can't compile invalid while" "Make sure AST can't compile invalid while"
cant_compile("(while)") cant_compile("(while)")
cant_compile("(while (True))")
def test_ast_good_do(): def test_ast_good_do():

View File

@ -283,7 +283,21 @@
(setv fact (* fact count)) (setv fact (* fact count))
(setv count (- count 1))) (setv count (- count 1)))
(assert (= count 0)) (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 [] (defn test-while-loop-else []
(setv count 5) (setv count 5)