Fix multi-statement 'for' with 'else' and add test cases for 'else'

This commit is contained in:
Ryan Gonzalez 2015-08-03 17:52:00 -05:00 committed by Gergely Nagy
parent 62e3116529
commit a0cb250f24
2 changed files with 12 additions and 5 deletions

View File

@ -97,16 +97,21 @@
(for* [x foo] (for* [x foo]
(for* [y bar] (for* [y bar]
baz))" baz))"
(setv body (list body))
(setv lst (get body -1))
(setv belse (if (and (isinstance lst HyExpression) (= (get lst 0) "else"))
[(body.pop)]
[]))
(cond (cond
[(odd? (len args)) [(odd? (len args))
(macro-error args "`for' requires an even number of args.")] (macro-error args "`for' requires an even number of args.")]
[(empty? body) [(empty? body)
(macro-error None "`for' requires a body to evaluate")] (macro-error None "`for' requires a body to evaluate")]
[(empty? args) `(do ~@body)] [(empty? args) `(do ~@body ~@belse)]
[(= (len args) 2) `(for* [~@args] (do ~@body))] [(= (len args) 2) `(for* [~@args] (do ~@body) ~@belse)]
[true [true
(let [[alist (cut args 0 nil 2)]] (let [[alist (cut args 0 nil 2)]]
`(for* [(, ~@alist) (genexpr (, ~@alist) [~@args])] ~@body))])) `(for* [(, ~@alist) (genexpr (, ~@alist) [~@args])] (do ~@body) ~@belse))]))
(defmacro -> [head &rest rest] (defmacro -> [head &rest rest]

View File

@ -95,8 +95,10 @@
(setv count 0) (setv count 0)
(for [x [1 2 3 4 5] (for [x [1 2 3 4 5]
y [1 2 3 4 5]] y [1 2 3 4 5]]
(setv count (+ count x y))) (setv count (+ count x y))
(assert (= count 150)) (else
(+= count 1)))
(assert (= count 151))
(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)))))