zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: $((++a++))
Date: Tue, 05 Sep 2017 11:01:06 +0100	[thread overview]
Message-ID: <20170905110106.16914af6@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <74d2c8e3-7a0f-b705-29c8-e5a2f6e182aa@inlv.org>

On Mon, 4 Sep 2017 22:45:46 +0200
Martijn Dekker <martijn@inlv.org> 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


      reply	other threads:[~2017-09-05 10:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20170904210547epcas2p2e46e1cb14a26b8e5fdd9217c58c077ff@epcas2p2.samsung.com>
2017-09-04 20:45 ` $((++a++)) Martijn Dekker
2017-09-05 10:01   ` Peter Stephenson [this message]

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=20170905110106.16914af6@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.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).