1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-27 00:40:26 +00:00

Fix match behavior for lone nil.

This commit is contained in:
Calvin Rose 2020-03-05 09:32:43 -06:00
parent 6bc67b70a6
commit 47e8f669f5
2 changed files with 19 additions and 9 deletions

View File

@ -1317,7 +1317,7 @@
~(if (= ,pattern ,expr) ,(onmatch) ,sentinel) ~(if (= ,pattern ,expr) ,(onmatch) ,sentinel)
(do (do
(put seen pattern true) (put seen pattern true)
~(if (= nil (def ,pattern ,expr)) ,sentinel ,(onmatch)))) ~(do (def ,pattern ,expr) ,(onmatch))))
(and (tuple? pattern) (= :parens (tuple/type pattern))) (and (tuple? pattern) (= :parens (tuple/type pattern)))
(if (and (= (pattern 0) '@) (symbol? (pattern 1))) (if (and (= (pattern 0) '@) (symbol? (pattern 1)))
@ -1334,12 +1334,14 @@
(var i -1) (var i -1)
(with-idemp (with-idemp
$arr expr $arr expr
~(if (indexed? ,$arr) ~(if (,indexed? ,$arr)
,((fn aux [] (if (< (,length ,$arr) ,len)
(++ i) ,sentinel
(if (= i len) ,((fn aux []
(onmatch) (++ i)
(match-1 (in pattern i) (tuple in $arr i) aux seen)))) (if (= i len)
(onmatch)
(match-1 (in pattern i) (tuple in $arr i) aux seen)))))
,sentinel))) ,sentinel)))
(dictionary? pattern) (dictionary? pattern)
@ -1347,12 +1349,12 @@
(var key nil) (var key nil)
(with-idemp (with-idemp
$dict expr $dict expr
~(if (dictionary? ,$dict) ~(if (,dictionary? ,$dict)
,((fn aux [] ,((fn aux []
(set key (next pattern key)) (set key (next pattern key))
(if (= key nil) (if (= key nil)
(onmatch) (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))) ,sentinel)))
:else ~(if (= ,pattern ,expr) ,(onmatch) ,sentinel))) :else ~(if (= ,pattern ,expr) ,(onmatch) ,sentinel)))

View File

@ -99,4 +99,12 @@
(assert (= 10 (prompt :a (for i 0 10 (inner-loop2 i)))) "prompt 3") (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) (end-suite)