1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-23 13:43:16 +00:00

Allow expressions as keys in destructuring.

This commit is contained in:
bakpakin 2018-06-25 15:34:06 -04:00
parent 5e9d1d07b9
commit 23dcfb986e
2 changed files with 14 additions and 1 deletions

View File

@ -107,7 +107,7 @@ static void destructure(DstCompiler *c, Dst left, DstSlot right,
const DstKV *kv = NULL;
while ((kv = dstc_next(left, kv))) {
DstSlot newright;
DstSlot kslot = dstc_cslot(dst_ast_unwrap(kv->key));
DstSlot kslot = dstc_value(dstc_fopts_default(c), kv->key);
Dst subval = kv->value;
localright = dstc_preread(c, ast, 0xFF, 1, right);
localsub = dstc_lslotn(c, 0xFF, 3);

View File

@ -127,6 +127,19 @@
(assert (= (string.find "123" "abc123def") 3) "string.find positive")
(assert (= (string.find "1234" "abc123def") nil) "string.find negative")
# Test destructuring
(do
(def test-tab @{:a 1 :b 2})
(def {:a a :b b} test-tab)
(assert (= a 1) "dictionary destructuring 1")
(assert (= b 2) "dictionary destructuring 2"))
(do
(def test-tab @{'a 1 'b 2 3 4})
(def {'a a 'b b (+ 1 2) c} test-tab)
(assert (= a 1) "dictionary destructuring 3")
(assert (= b 2) "dictionary destructuring 4")
(assert (= c 4) "dictionary destructuring 5 - expression as key"))
# Marshal
(defn testmarsh [x msg]