diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 55328943..1b61e6df 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -1317,7 +1317,7 @@ ~(if (= ,pattern ,expr) ,(onmatch) ,sentinel) (do (put seen pattern true) - ~(if (= nil (def ,pattern ,expr)) ,sentinel ,(onmatch)))) + ~(do (def ,pattern ,expr) ,(onmatch)))) (and (tuple? pattern) (= :parens (tuple/type pattern))) (if (and (= (pattern 0) '@) (symbol? (pattern 1))) @@ -1334,12 +1334,14 @@ (var i -1) (with-idemp $arr expr - ~(if (indexed? ,$arr) - ,((fn aux [] - (++ i) - (if (= i len) - (onmatch) - (match-1 (in pattern i) (tuple in $arr i) aux seen)))) + ~(if (,indexed? ,$arr) + (if (< (,length ,$arr) ,len) + ,sentinel + ,((fn aux [] + (++ i) + (if (= i len) + (onmatch) + (match-1 (in pattern i) (tuple in $arr i) aux seen))))) ,sentinel))) (dictionary? pattern) @@ -1347,12 +1349,12 @@ (var key nil) (with-idemp $dict expr - ~(if (dictionary? ,$dict) + ~(if (,dictionary? ,$dict) ,((fn aux [] (set key (next pattern key)) (if (= key nil) (onmatch) - (match-1 (in pattern key) (tuple in $dict key) aux seen)))) + (match-1 [(in pattern key) [not= (in pattern key) nil]] [in $dict key] aux seen)))) ,sentinel))) :else ~(if (= ,pattern ,expr) ,(onmatch) ,sentinel))) diff --git a/test/suite8.janet b/test/suite8.janet index c967a3ea..4c948511 100644 --- a/test/suite8.janet +++ b/test/suite8.janet @@ -99,4 +99,12 @@ (assert (= 10 (prompt :a (for i 0 10 (inner-loop2 i)))) "prompt 3") +# Match checks + +(assert (= :hi (match nil nil :hi)) "match 1") +(assert (= :hi (match {:a :hi} {:a a} a)) "match 2") +(assert (= nil (match {:a :hi} {:a a :b b} a)) "match 3") +(assert (= nil (match [1 2] [a b c] a)) "match 4") +(assert (= 2 (match [1 2] [a b] b)) "match 5") + (end-suite)