From d3b9b8d4528e45d3f59636656351ad545d26a94b Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 7 Mar 2020 10:13:10 -0600 Subject: [PATCH] For #293, correct wildcards in dictinoaries. --- src/boot/boot.janet | 4 +++- test/suite8.janet | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index e7bd9491..c5036e6c 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -1368,9 +1368,11 @@ ~(if (,dictionary? ,$dict) ,((fn aux [] (set key (next pattern key)) + (def $val (gensym)) (if (= key nil) (onmatch) - (match-1 [(in pattern key) [not= (in pattern key) nil]] [in $dict key] aux seen)))) + ~(do (def ,$val (,get ,$dict ,key)) + ,(match-1 [(in pattern key) [not= nil $val]] $val aux seen))))) ,sentinel))) :else ~(if (= ,pattern ,expr) ,(onmatch) ,sentinel))) diff --git a/test/suite8.janet b/test/suite8.janet index 242d80db..4c976289 100644 --- a/test/suite8.janet +++ b/test/suite8.janet @@ -117,4 +117,13 @@ # Just don't segfault (assert (peg/match '{:main (replace "S" {"S" :spade})} "S7") "regression #300") +# Test cases for #293 +(assert (= :yes (match [1 2 3] [_ a _] :yes :no)) "match wildcard 1") +(assert (= :no (match [1 2 3] [__ a __] :yes :no)) "match wildcard 2") +(assert (= :yes (match [1 2 [1 2 3]] [_ a [_ _ _]] :yes :no)) "match wildcard 3") +(assert (= :yes (match [1 2 3] (_ (even? 2)) :yes :no)) "match wildcard 4") +(assert (= :yes (match {:a 1} {:a _} :yes :no)) "match wildcard 5") +(assert (= false (match {:a 1 :b 2 :c 3} {:a a :b _ :c _ :d _} :no {:a _ :b _ :c _} false :no)) "match wildcard 6") +(assert (= nil (match {:a 1 :b 2 :c 3} {:a a :b _ :c _ :d _} :no {:a _ :b _ :c _} nil :no)) "match wildcard 7") + (end-suite)