Merge pull request #1419 from rkday/else_multiple_statements

Allow multiple statements in the else branch of for
This commit is contained in:
gilch 2017-09-16 14:10:28 -06:00 committed by GitHub
commit e43d6f5e2f
4 changed files with 15 additions and 11 deletions

2
NEWS
View File

@ -41,6 +41,8 @@ Changes from 0.13.0
* `assoc` now evaluates its arguments only once each * `assoc` now evaluates its arguments only once each
* `break` and `continue` now raise an error when given arguments * `break` and `continue` now raise an error when given arguments
instead of silently ignoring them instead of silently ignoring them
* Multiple expressions are now allowed in the else clause of
a for loop
[ Misc. Improvements ] [ Misc. Improvements ]
* `read`, `read_str`, and `eval` are exposed and documented as top-level * `read`, `read_str`, and `eval` are exposed and documented as top-level

View File

@ -1827,12 +1827,8 @@ class HyASTCompiler(object):
# (for* [] body (else …)) # (for* [] body (else …))
if expression and expression[-1][0] == HySymbol("else"): if expression and expression[-1][0] == HySymbol("else"):
else_expr = expression.pop() else_expr = expression.pop()
if len(else_expr) > 2: for else_body in else_expr[1:]:
raise HyTypeError( orel += self.compile(else_body)
else_expr,
"`else' statement in `for' is too long")
elif len(else_expr) == 2:
orel += self.compile(else_expr[1])
orel += orel.expr_as_stmt() orel += orel.expr_as_stmt()
ret += self.compile(iterable) ret += self.compile(iterable)

View File

@ -337,11 +337,6 @@ def test_ast_valid_for():
can_compile("(for [a 2] (print a))") can_compile("(for [a 2] (print a))")
def test_ast_invalid_for():
"Make sure AST can't compile invalid for"
cant_compile("(for* [a 1] (else 1 2))")
def test_nullary_break_continue(): def test_nullary_break_continue():
can_compile("(while 1 (break))") can_compile("(while 1 (break))")
cant_compile("(while 1 (break 1))") cant_compile("(while 1 (break 1))")

View File

@ -210,6 +210,17 @@
(else (else
(+= count 1))) (+= count 1)))
(assert (= count 151)) (assert (= count 151))
(setv count 0)
; multiple statements in the else branch should work
(for [x [1 2 3 4 5]
y [1 2 3 4 5]]
(setv count (+ count x y))
(else
(+= count 1)
(+= count 10)))
(assert (= count 161))
(assert (= (list ((fn [] (for [x [[1] [2 3]] y x] (yield y))))) (assert (= (list ((fn [] (for [x [[1] [2 3]] y x] (yield y)))))
(list-comp y [x [[1] [2 3]] y x]))) (list-comp y [x [[1] [2 3]] y x])))
(assert (= (list ((fn [] (for [x [[1] [2 3]] y x z (range 5)] (yield z))))) (assert (= (list ((fn [] (for [x [[1] [2 3]] y x z (range 5)] (yield z)))))