zsh-workers
 help / color / mirror / code / Atom feed
* Sigh, another math-parsing issue
@ 2015-05-14 17:56 Bart Schaefer
  2015-05-15  9:05 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Bart Schaefer @ 2015-05-14 17:56 UTC (permalink / raw)
  To: zsh-workers

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

-- 
Barton E. Schaefer


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

* Re: Sigh, another math-parsing issue
  2015-05-14 17:56 Sigh, another math-parsing issue Bart Schaefer
@ 2015-05-15  9:05 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2015-05-15  9:05 UTC (permalink / raw)
  To: zsh-workers

On Thu, 14 May 2015 10:56:56 -0700
Bart Schaefer <schaefer@brasslantern.com> 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


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

end of thread, other threads:[~2015-05-15  9:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-14 17:56 Sigh, another math-parsing issue Bart Schaefer
2015-05-15  9:05 ` 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).