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

Re: $((++a++))



On Mon, 4 Sep 2017 22:45:46 +0200
Martijn Dekker <martijn@xxxxxxxx> wrote:
> Why is this accepted?
> 
> % a=1
> % echo $((++a++))
> 2
> % echo $((--a--))
> 1
> 
> Looks like that should be an invalid arithmetic expression.

After staring at this for long enough, I realised it was trivial.  The
current code is in fact doing too much work in order to get it wrong...

pws

diff --git a/Src/math.c b/Src/math.c
index f961300..c383160 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -1306,8 +1306,6 @@ op(int what)
 	    spval->type = MN_INTEGER;
 	} else
 	    spval->u.l = !spval->u.l;
-	stack[sp].lval = NULL;
-	stack[sp].pval = NULL;
 	break;
     case COMP:
 	if (spval->type & MN_FLOAT) {
@@ -1315,8 +1313,6 @@ op(int what)
 	    spval->type = MN_INTEGER;
 	} else
 	    spval->u.l = ~spval->u.l;
-	stack[sp].lval = NULL;
-	stack[sp].pval = NULL;
 	break;
     case POSTPLUS:
 	a = *spval;
@@ -1335,16 +1331,12 @@ op(int what)
 	(void)setmathvar(stack + sp, a);
 	break;
     case UPLUS:
-	stack[sp].lval = NULL;
-	stack[sp].pval = NULL;
 	break;
     case UMINUS:
 	if (spval->type & MN_FLOAT)
 	    spval->u.d = -spval->u.d;
 	else
 	    spval->u.l = -spval->u.l;
-	stack[sp].lval = NULL;
-	stack[sp].pval = NULL;
 	break;
     case QUEST:
 	DPUTS(sp < 2, "BUG: math: three shall be the number of the counting.");
@@ -1377,6 +1369,8 @@ op(int what)
 	zerr("bad math expression: out of integers");
 	return;
     }
+    stack[sp].lval = NULL;
+    stack[sp].pval = NULL;
 }
 
 
diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst
index 61da763..30409ad 100644
--- a/Test/C01arith.ztst
+++ b/Test/C01arith.ztst
@@ -420,3 +420,16 @@
 0:type of variable when created in arithmetic context
 >integer
 >scalar
+
+  integer a=1
+  print $(( ++a * 2 ))
+  print $(( ++a ))
+  print $(( a++ * 2 ))
+  print $(( a ))
+  print $(( ++a++ * 2 ))
+1: Allow rvalue but not lvalue operations with result of increment
+>4
+>3
+>6
+>4
+?(eval):6: bad math expression: lvalue required



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