zsh-workers
 help / color / mirror / code / Atom feed
* Can't call multi-arg math function from array subscript
@ 2017-04-02 18:51 ` Bart Schaefer
  2017-04-02 20:36   ` Mikael Magnusson
  2017-04-03  8:51   ` Peter Stephenson
  0 siblings, 2 replies; 3+ messages in thread
From: Bart Schaefer @ 2017-04-02 18:51 UTC (permalink / raw)
  To: zsh-workers

Single-argument functions are OK:

% string='abcde'
% onearg() { return $1 }
% functions -M onearg
% print ${string[1,onearg(3)]
abc

Add another argument and strange things start happening:

% twoarg() { return $(( $2 - $1 )) }
% functions -M twoarg
% print ${string[1,twoarg(1,4)]}
twoarg: bad math expression: operand expected at end of string
twoarg: bad math expression: operand expected at end of string

Why two error messages?

I assume the comma is messing things up; let's try it like this:

% print ${string[1,$(( twoarg(1,4) ))]}
zsh: parse error
zsh: parse error

Just to confirm:

% print ${string[1,$(( onearg(4) ))]} 
abcd


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Can't call multi-arg math function from array subscript
  2017-04-02 18:51 ` Can't call multi-arg math function from array subscript Bart Schaefer
@ 2017-04-02 20:36   ` Mikael Magnusson
  2017-04-03  8:51   ` Peter Stephenson
  1 sibling, 0 replies; 3+ messages in thread
From: Mikael Magnusson @ 2017-04-02 20:36 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh workers

On Sun, Apr 2, 2017 at 8:51 PM, Bart Schaefer <schaefer@brasslantern.com> wrote:
> Single-argument functions are OK:
>
> % string='abcde'
> % onearg() { return $1 }
> % functions -M onearg
> % print ${string[1,onearg(3)]
> abc
>
> Add another argument and strange things start happening:
>
> % twoarg() { return $(( $2 - $1 )) }
> % functions -M twoarg
> % print ${string[1,twoarg(1,4)]}
> twoarg: bad math expression: operand expected at end of string
> twoarg: bad math expression: operand expected at end of string
>
> Why two error messages?
>
> I assume the comma is messing things up; let's try it like this:
>
> % print ${string[1,$(( twoarg(1,4) ))]}
> zsh: parse error
> zsh: parse error
>
> Just to confirm:
>
> % print ${string[1,$(( onearg(4) ))]}
> abcd

I guess this basically confirms that the comma messes up parsing:

% func='twoarg(1,4)'
% print ${string[1,func]}
abc


-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Can't call multi-arg math function from array subscript
  2017-04-02 18:51 ` Can't call multi-arg math function from array subscript Bart Schaefer
  2017-04-02 20:36   ` Mikael Magnusson
@ 2017-04-03  8:51   ` Peter Stephenson
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2017-04-03  8:51 UTC (permalink / raw)
  To: zsh-workers

On Sun, 2 Apr 2017 11:51:56 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> Add another argument and strange things start happening:
> 
> % twoarg() { return $(( $2 - $1 )) }
> % functions -M twoarg
> % print ${string[1,twoarg(1,4)]}
> twoarg: bad math expression: operand expected at end of string
> twoarg: bad math expression: operand expected at end of string

It's going to be because getarg() doesn't do much parsing.  The answer
will be something like this, though I'm not sure what to do about
quoting.

pws

diff --git a/Src/params.c b/Src/params.c
index 785b9ea..a9683a6 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -1183,7 +1183,7 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w,
        int *prevcharlen, int *nextcharlen)
 {
     int hasbeg = 0, word = 0, rev = 0, ind = 0, down = 0, l, i, ishash;
-    int keymatch = 0, needtok = 0, arglen, len;
+    int keymatch = 0, needtok = 0, arglen, len, inpar = 0;
     char *s = *str, *sep = NULL, *t, sav, *d, **ta, **p, *tt, c;
     zlong num = 1, beg = 0, r = 0, quote_arg = 0;
     Patprog pprog = NULL;
@@ -1322,8 +1322,9 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w,
     }
 
     for (t = s, i = 0;
-	 (c = *t) && ((c != Outbrack &&
-		       (ishash || c != ',')) || i); t++) {
+	 (c = *t) &&
+	     ((c != Outbrack && (ishash || c != ',')) || i || inpar);
+	 t++) {
 	/* Untokenize inull() except before brackets and double-quotes */
 	if (inull(c)) {
 	    c = t[1];
@@ -1344,6 +1345,10 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w,
 	    i++;
 	else if (c == ']' || c == Outbrack)
 	    i--;
+	if (c == '(' || c == Inpar)
+	    inpar++;
+	else if (c == ')' || c == Outpar)
+	    inpar--;
 	if (ispecial(c))
 	    needtok = 1;
     }
diff --git a/Test/D06subscript.ztst b/Test/D06subscript.ztst
index 1449236..f0a858b 100644
--- a/Test/D06subscript.ztst
+++ b/Test/D06subscript.ztst
@@ -266,3 +266,10 @@
 >of the gang
 >of the gang
 >of the gang
+
+ string='abcde'
+ twoarg() { return $(( $2 - $1 )) }
+ functions -M twoarg
+ print ${string[1,twoarg(1,4)]}
+0:Commas inside parentheses do not confuse subscripts
+>abc


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-04-03  8:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20170402185230epcas1p467fce7a708bf94d7f2c8e032327196d9@epcas1p4.samsung.com>
2017-04-02 18:51 ` Can't call multi-arg math function from array subscript Bart Schaefer
2017-04-02 20:36   ` Mikael Magnusson
2017-04-03  8:51   ` Peter Stephenson

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