diff --git a/src/core/core.janet b/src/core/core.janet index 53dfd802..2116c087 100644 --- a/src/core/core.janet +++ b/src/core/core.janet @@ -960,21 +960,28 @@ value, one key will be ignored." (do (def len (length pattern)) (var i -1) - (with-idemp $arr expr - ((fn aux [] - (++ i) - (if (= i len) - (onmatch) - (match-1 pattern.i ~(get ,$arr ,i) aux seen)))))) + (with-idemp + $arr expr + ~(if (indexed? ,$arr) + ,((fn aux [] + (++ i) + (if (= i len) + (onmatch) + (match-1 pattern.i (tuple get $arr i) aux seen)))) + ,sentinel))) (dictionary? pattern) (do (var key nil) - (with-idemp $dict expr ((fn aux [] - (:= key (next pattern key)) - (if (= key nil) - (onmatch) - (match-1 (get pattern key) ~(get ,$dict ,key) aux seen)))))) + (with-idemp + $dict expr + ~(if (dictionary? ,$dict) + ,((fn aux [] + (:= key (next pattern key)) + (if (= key nil) + (onmatch) + (match-1 (get pattern key) (tuple get $dict key) aux seen)))) + ,sentinel))) :else ~(if (= ,pattern ,expr) ,(onmatch) ,sentinel))) diff --git a/test/suite3.janet b/test/suite3.janet index bd5236fc..0fbe9570 100644 --- a/test/suite3.janet +++ b/test/suite3.janet @@ -25,4 +25,25 @@ (assert (= (length (range 1 10)) 9) "(range 1 10)") (assert (deep= @{:a 1 :b 2 :c 3} (zipcoll '[:a :b :c] '[1 2 3])) "zipcoll") +(def- a 100) +(assert (= a 100) "def-") + +(assert (= :first + (match @[1 3 5] + @[x y z] :first + :second)) "match 1") + +(def val1 :avalue) +(assert (= :second + (match val1 + @[x y z] :first + :avalue :second + :third)) "match 2") + +(assert (= 100 + (match @[50 40] + @[x x] (* x 3) + @[x y] (+ x y 10) + 0)) "match 3") + (end-suite)