From a3228f49970c486f1518759c48dae025419ee061 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Thu, 9 Nov 2023 11:18:03 -0600 Subject: [PATCH] Add changes and test cases for #1324 --- src/boot/boot.janet | 12 ++++++++---- test/suite-boot.janet | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index de66320f..1bf336cb 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -420,10 +420,14 @@ (defn- range-template [binding object kind rest op comparison] - (let [[start stop step] (check-indexed object)] - (case kind - :range (for-template binding (if stop start 0) (or stop start) (or step 1) comparison op [rest]) - :down (for-template binding start (or stop 0) (or step 1) comparison op [rest])))) + (check-indexed object) + (def [a b c] object) + (def [start stop step] + (case (length object) + 1 (case kind :range [0 a 1] :down [a 0 1]) + 2 [a b 1] + [a b c])) + (for-template binding start stop step comparison op [rest])) (defn- each-template [binding inx kind body] diff --git a/test/suite-boot.janet b/test/suite-boot.janet index 76049078..6394e725 100644 --- a/test/suite-boot.janet +++ b/test/suite-boot.janet @@ -241,6 +241,16 @@ (assert (pos? (% x 4)) "generate in loop")) (assert (= gencount 75) "generate loop count") +# more loop checks +(assert (deep= (seq [i :range [0 10]] i) @[0 1 2 3 4 5 6 7 8 9]) "seq 1") +(assert (deep= (seq [i :range [0 10 2]] i) @[0 2 4 6 8]) "seq 2") +(assert (deep= (seq [i :range [10]] i) @[0 1 2 3 4 5 6 7 8 9]) "seq 3") +(assert (deep= (seq [i :range-to [10]] i) @[0 1 2 3 4 5 6 7 8 9 10]) "seq 4") +(def gen (generate [x :range-to [0 nil 2]] x)) +(assert (deep= (take 5 gen) @[0 2 4 6 8]) "generate nil limit") +(def gen (generate [x :range [0 nil 2]] x)) +(assert (deep= (take 5 gen) @[0 2 4 6 8]) "generate nil limit 2") + # Even and odd # ff163a5ae (assert (odd? 9) "odd? 1")