Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #5154 (nested for loop syntax & break behavior) #7430

Merged
merged 4 commits into from
Jul 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ New language features

* Improved reporting of syntax errors ([#6179])

* `break` inside a `for` loop with multiple ranges now exits the entire loop nest ([#5154])

REPL improvements
-----------------

Expand Down
3 changes: 3 additions & 0 deletions doc/manual/control-flow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,9 @@ forming the cartesian product of its iterables:
(2,3)
(2,4)

A ``break`` statement inside such a loop exits the entire nest of loops,
not just the inner one.

.. _man-exception-handling:

Exception Handling
Expand Down
7 changes: 3 additions & 4 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1016,10 +1016,9 @@
(let* ((ranges (parse-comma-separated-iters s))
(body (parse-block s)))
(expect-end s)
(let nest ((r ranges))
(if (null? r)
body
`(for ,(car r) ,(nest (cdr r)))))))
`(for ,(if (length= ranges 1) (car ranges) (cons 'block ranges))
,body)))

((if)
(let* ((test (parse-cond s))
(then (if (memq (require-token s) '(else elseif))
Expand Down
54 changes: 35 additions & 19 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,24 @@
(cadr e)
(caddr e))))

(define (expand-for while lhs X body)
;; (for (= lhs X) body)
(let ((coll (gensy))
(state (gensy)))
`(scope-block
(block (= ,coll ,(expand-forms X))
(= ,state (call (top start) ,coll))
,(expand-forms
`(,while
(call (top !) (call (top done) ,coll ,state))
(scope-block
(block
;; NOTE: enable this to force loop-local var
#;,@(map (lambda (v) `(local ,v)) (lhs-vars lhs))
,(lower-tuple-assignment (list lhs state)
`(call (top next) ,coll ,state))
,body))))))))

(define (map-expand-forms e) (map expand-forms e))

(define (expand-forms e)
Expand Down Expand Up @@ -1793,6 +1811,13 @@
(break-block loop-cont
,(expand-forms (caddr e)))))))

'inner-while
(lambda (e)
`(scope-block
(_while ,(expand-forms (cadr e))
(break-block loop-cont
,(expand-forms (caddr e))))))

'break
(lambda (e)
(if (pair? (cdr e))
Expand All @@ -1803,25 +1828,16 @@

'for
(lambda (e)
(let ((X (caddr (cadr e)))
(lhs (cadr (cadr e)))
(body (caddr e)))
;; (for (= lhs X) body)
(let ((coll (gensy))
(state (gensy)))
`(scope-block
(block (= ,coll ,(expand-forms X))
(= ,state (call (top start) ,coll))
,(expand-forms
`(while
(call (top !) (call (top done) ,coll ,state))
(scope-block
(block
;; NOTE: enable this to force loop-local var
#;,@(map (lambda (v) `(local ,v)) (lhs-vars lhs))
,(lower-tuple-assignment (list lhs state)
`(call (top next) ,coll ,state))
,body)))))))))
(let nest ((ranges (if (eq? (car (cadr e)) 'block)
(cdr (cadr e))
(list (cadr e))))
(first #t))
(expand-for (if first 'while 'inner-while)
(cadr (car ranges))
(caddr (car ranges))
(if (null? (cdr ranges))
(caddr e) ;; body
(nest (cdr ranges) #f)))))

'+= lower-update-op
'-= lower-update-op
Expand Down
10 changes: 10 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1772,3 +1772,13 @@ macro let_with_uninit()
end

@test @let_with_uninit() == 2

# issue #5154
let
v = {}
for i=1:3, j=1:3
push!(v, (i, j))
i == 1 && j == 2 && break
end
@test v == {(1,1), (1,2)}
end