1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-25 07:50:27 +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)
(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)))

View File

@ -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)