aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2022-06-14 22:06:14 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2022-06-18 16:38:12 -0400
commitfb0e65434f9e20a80a40ceb5088aea69404480f3 (patch)
tree354cde9fc93ab67ad5ca20c07a131c416492bc37
parent093620ac012d011518073620577d5807f7582267 (diff)
Don't allow list destructuring with aliases in bqn.js and vm.bqn
-rw-r--r--docs/bqn.js2
-rw-r--r--test/cases/namespace.bqn1
-rw-r--r--vm.bqn5
3 files changed, 7 insertions, 1 deletions
diff --git a/docs/bqn.js b/docs/bqn.js
index 2ea49108..879859a6 100644
--- a/docs/bqn.js
+++ b/docs/bqn.js
@@ -75,6 +75,8 @@ let set = (d, id, v) => {
let cs=v.sh.slice(1); let c=cs.reduce((a,b)=>a*b,1);
let cell = j => arr(v.slice(c*j,c*j+c),cs,v.fill);
id.map((n,j)=>set(d,n,cell(j)));
+ } else if (id.m) {
+ throw Error("← or ↩: Can't use alias in list destructuring");
} else {
if (v.sh) {
if (!eq(id.sh,v.sh)) throw Error("← or ↩: Target and value shapes don't match");
diff --git a/test/cases/namespace.bqn b/test/cases/namespace.bqn
index d4397fed..421c2e3f 100644
--- a/test/cases/namespace.bqn
+++ b/test/cases/namespace.bqn
@@ -21,6 +21,7 @@
2 % b←{a⇐𝕩}2⋄⟨b⇐a⟩↩b⋄b
6 % (d⇐b)‿a←{b⇐1+a⇐𝕩}2⋄a×d
! % a2←0⋄⟨a2⇐a⟩+↩{a⇐1}
+! % ⟨b⇐c,d⟩←1‿2
# Namespaces
2 % ns←{a⇐2,b⇐3} ⋄ ⟨a⟩←ns ⋄ a
diff --git a/vm.bqn b/vm.bqn
index f00f7fb9..b133cbf7 100644
--- a/vm.bqn
+++ b/vm.bqn
@@ -35,13 +35,15 @@ ref ← {
Get ⇐ {𝕩.Get@}¨ arr˙
# Common code for all setter functions
# 𝕨S𝕩 sets reference 𝕨 to 𝕩, and e indicates error handling
+ al ← ∨´ {⟨al⟩:al;0}¨ arr
_set_ ← {S _𝕣_ e:
Err ← {(e∾": "∾𝕩)!e≡@ ⋄ ⟨1⟩} # e≡@ indicates SetQ, which can't error
c ← (e≡@) ⊑ {𝔽}‿{𝔽⎊1} # GetF or Get in F can error
# Get field for reference 𝕨 from namespace, if possible
F ← {⟨G⇐GetF⟩𝕊𝕩:(G𝕩).Get@ ; !Err"Cannot extract non-name from namespace"}
{
- 0=•Type𝕩 ? arr ≡○≢◶⟨Err∘"Target and value shapes don't match", S¨⟩ 𝕩 ;
+ 0=•Type𝕩 ? Err⍟al "Can't use alias in list destructuring"
+ arr ≡○≢◶⟨Err∘"Target and value shapes don't match", S¨⟩ 𝕩 ;
6=•Type𝕩 ? (⊢ S F⟜𝕩)_c¨ arr ;
Err "Multiple targets but atomic value"
}
@@ -64,6 +66,7 @@ ref ← {
Alias ⇐ {env‿name 𝕊 r:
SetN‿SetU‿SetQ ⇐ r
GetF ⇐ {env.program 𝕩.Field name}
+ al ⇐ 1
}
# Destructuring placeholder ·
not ⇐ { SetU⇐SetN⇐⊢ ⋄ SetQ⇐0˙ }