Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Sigh, another math-parsing issue



On Thu, 14 May 2015 10:56:56 -0700
Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> Ksh:
> 
> $ float X=27.9
> $ print $(( 1 + $(( X )) ))
> 28.9
> 
> Zsh 5.0.7-dev-2:
> 
> torch% print $(( 1 + $(( X )) ))
> zsh: failed to find end of math substitution

What a stupid language.  No wonder it seems like I've been fixing
several bugs a week for over twenty years.

Luckily, the parsing is OK; we simply need to do a bit more counting
when it comes to execution.

pws

diff --git a/Src/subst.c b/Src/subst.c
index 5a12e12..d4a04b8 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -259,10 +259,19 @@ stringsubst(LinkList list, LinkNode node, int pf_flags, int asssub)
 #endif
 		str--;
 	    } else if (c == Inparmath) {
-		/* Math substitution of the form $((...)) */
+		/*
+		 * Math substitution of the form $((...)).
+		 * These can be nested, for goodness sake...
+		 */
+		int mathpar = 1;
 		str[-1] = '\0';
-		while (*str != Outparmath && *str)
+		while (mathpar && *str) {
 		    str++;
+		    if (*str == Outparmath)
+			mathpar--;
+		    else if (*str == Inparmath)
+			mathpar++;
+		}
 		if (*str != Outparmath) {
 		    zerr("failed to find end of math substitution");
 		    return NULL;
diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst
index e2dfe56..7474fef 100644
--- a/Test/C01arith.ztst
+++ b/Test/C01arith.ztst
@@ -387,3 +387,11 @@
   print $((`:`))
 0:Null string in arithmetic evaluation after command substitution
 >0
+
+  print $(( 1 + $(( 2 + 3 )) ))
+  print $(($((3+4))))
+  print $((1*$((2*$((3))*4))*5))
+0:Nested match substitutions.  Yes, I know, very useful.
+>6
+>7
+>120



Messages sorted by: Reverse Date, Date, Thread, Author