zsh-workers
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: zsh-workers@zsh.org
Subject: RFC PATCH: Sketch at :@ subscripting
Date: Fri, 18 Dec 2020 14:18:15 +0100	[thread overview]
Message-ID: <20201218131815.25999-1-mikachu@gmail.com> (raw)

As the subject says, this is in a very early idea stage, but it does work for basic cases at least.

The idea is that you can do this:
% typeset -a somearray=( 'data here' 'some words' etc etc 1 2 3 4 ) idx=(1 3 5)
% echo ${somearray:@idx}
data here etc 1

The way it currently works also lets you do this:
% typeset -a somearray=( 'data here' 'some words' etc etc 1 2 3 4 ) idx=( '(r)<->' '(I)etc' )
% echo ${somearray:@idx}
1 4

Not decided on if that's desirable :).

Anyway, the code as it is written now is very hacky and I'm wondering if I missed the existence of some useful functions, or if the stuff I'm using now would need to be refactored a bit to make this actually possible without doing stupid things like zhtricat("[", *sub_it, "]").

Also curious why getindex() unconditionally *writes* '[' to the first character in the input string passed to it...

---
 Src/subst.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/Src/subst.c b/Src/subst.c
index 8de201b663..943b2546c4 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -3167,6 +3167,67 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
 	    }
 	    break;
 	}
+    } else if (inbrace && (*s == '@')) {
+	/* use every element of the array specified by @ as a subscript */
+	char **idx;
+	++s;
+	if (*itype_end(s, IIDENT, 0)) {
+	    untokenize(s);
+	    zerr("not an identifier: %s", s);
+	    return NULL;
+	}
+	if (vunset) {
+	    if (vunset > 0 && unset(UNSET)) {
+		*idend = '\0';
+		zerr("%s: parameter not set", idbeg);
+		return NULL;
+	    }
+	    isarr = 0;
+	    val = dupstring("");
+	} else {
+	    char *sval;
+	    idx = getaparam(s);
+	    if (!idx) {
+		sval = getsparam(s);
+		if (sval)
+		    idx = hmkarray(sval);
+	    }
+	    if (idx) {
+		if (isarr) {
+		    if (PM_TYPE(vbuf.pm->node.flags) & PM_ARRAY) {
+			/* actual code is here */
+			LinkList list = newlinklist();
+			char **sub_it, **val_it;
+			for (sub_it = idx; *sub_it; sub_it++) {
+			    char *tmp = zhtricat("[", *sub_it, "]");
+			    /* getindex() modifies vbuf in ways that break further
+			     * subscripting with differing flags, so better not to
+			     * touch it at all. */
+			    struct value vbuf_copy = vbuf;
+			    getindex(&tmp, &vbuf_copy, 0);
+			    for (val_it = getarrvalue(&vbuf_copy); *val_it; val_it++) {
+				addlinknode(list, *val_it);
+			    }
+			}
+			aval = hlinklist2array(list, !copied);
+			copied = 1;
+		    } else {
+			zerr("assoc @ not implemented");
+		    }
+		} else {
+		    zerr("scalar @ not implemented");
+		}
+	    } else {
+
+		if (unset(UNSET)) {
+		    zerr("%s: parameter not set", s);
+		    return NULL;
+		}
+		isarr = 0;
+		val = dupstring("");
+	    }
+	}
     } else if (inbrace && (*s == '^' || *s == Hat)) {
 	char **zip;
 	int shortest = 1;
-- 
2.15.1



             reply	other threads:[~2020-12-18 13:18 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-18 13:18 Mikael Magnusson [this message]
2020-12-19  9:13 ` Stephane Chazelas
2021-03-27 20:27   ` Bart Schaefer
2021-04-03 19:48     ` Lawrence Velázquez
2021-04-04 17:30       ` Daniel Shahaf
2021-04-10 20:38         ` Lawrence Velázquez
2021-04-10 21:59           ` Bart Schaefer
2021-04-13 11:54             ` Daniel Shahaf
2021-04-13 21:25               ` Bart Schaefer
2021-03-27 19:41 ` Lawrence Velázquez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201218131815.25999-1-mikachu@gmail.com \
    --to=mikachu@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).