Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[goal] Add labels, break, and continue to loops (#3426)
Uses (block) and (return-from) to support (break) and (continue) with labeling Supports `(while) (until) (dotimes) (countdown) (loop) (doarray)` Test cases: ``` lisp (dotimes (i 5) (when (= i 2) (break) ) (format #t "i: ~D~%" i) ) ;; Output: ;; i: 0 ;; i: 1 (dotimes (i 5) (when (= i 2) (continue) ) (format #t "i: ~D~%" i) ) ;; Output: ;; i: 0 ;; i: 1 ;; i: 3 ;; i: 4 (dotimes (i 3) (when (= i 2) (continue) ) (format #t "outer: ~D~%" i) (dotimes (i 3) (when (= i 0) (continue) ) (format #t "inner: ~D~%" i) ) ) ;; Output: ;; outer: 0 ;; inner: 1 ;; inner: 2 ;; outer: 1 ;; inner: 1 ;; inner: 2 (dotimes (i 5) :label outer (when (= i 2) (continue :from outer) ) (format #t "outer: ~D~%" i) (dotimes (i 3) (when (= i 1) (continue :from outer) ) (format #t "inner: ~D~%" i) ) ) ;; Output: ;; outer: 0 ;; inner: 0 ;; outer: 1 ;; inner: 0 ;; outer: 3 ;; inner: 0 ;; outer: 4 ;; inner: 0 (dotimes (i 5) :label outer (when (= i 2) (continue :from outer) ) (format #t "outer: ~D~%" i) (dotimes (i 3) (when (= i 0) (break :from outer) ) (format #t "inner: ~D~%" i) ) ) ;; Output: ;; outer: 0 (dotimes (i 5) :label outer (when (= i 2) (continue :from outer) ) (format #t "outer2: ~D~%" i) (dotimes (i 3) (when (= i 1) (break) ) (format #t "inner2: ~D~%" i) ) ) ;; Output: ;; outer2: 0 ;; inner2: 0 ;; outer2: 1 ;; inner2: 0 ;; outer2: 3 ;; inner2: 0 ;; outer2: 4 ;; inner2: 0 (countdown (i 5) (when (= i 2) (continue) ) (format #t "i: ~D~%" i) ) ;; Output: ;; i: 4 ;; i: 3 ;; i: 1 ;; i: 0 (let ((i 0)) (while (< i 5) (when (= i 1) (break) ) (format #t "i: ~D~%" i) (1+! i) ) ) ;; Output: ;; i: 0 (let ((i 0)) (until (> i 5) :label outer (loop (break :from outer) ) (format #t "i: ~D~%" i) (1+! i) ) ) ;; Output: ;; nothing (define *array* (new 'global 'boxed-array uint32 3)) (doarray (i *array*) (break) (format #t "doarray") ) ;; Output: ;; nothing (doarray (i *array*) :label arrayloop (dotimes (i 5) (when (= i 2) (continue :from arrayloop) ) (format #t "i: ~D~%" i) ) (format #t "doarray~%") ) ;; Output: ;; i: 0 ;; i: 1 ;; i: 0 ;; i: 1 ;; i: 0 ;; i: 1 ```
- Loading branch information