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: negative bases in arithmetic expressions
Date: Wed, 8 Aug 2018 17:05:34 +0100	[thread overview]
Message-ID: <20180808160535eucas1p2f17bcad971b1f1190aed12c5bd707b6c~I9FjdWE6a1044010440eucas1p2I@eucas1p2.samsung.com> (raw)
In-Reply-To: <20180808153706.GB16265@chaz.gmail.com>

On Wed, 8 Aug 2018 16:37:06 +0100
Stephane Chazelas <stephane.chazelas@gmail.com> wrote:
> $ zsh -c 'echo $((-8#10))'
> zsh:1: invalid base (must be 2 to 36 inclusive): -8
> $ zsh -c 'echo $((- 8#10))'
> -8
> 
> I'd rather zsh interpreted $((-8#10)) the same as $((- 8#10))
> like other shells do as it's a bit pointless to consider the
> sign as being part of the base and then reject anything negative
> afterwise.

No real argument there, I don't think.

pws


diff --git a/Src/math.c b/Src/math.c
index 4b7ecf0..b08e05c 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -640,8 +640,19 @@ zzlex(void)
 	    }
 	    if (unary) {
 		if (idigit(*ptr) || *ptr == '.') {
-		    ptr--;
-		    return lexconstant();
+		    int ctype = lexconstant();
+		    if (ctype == NUM)
+		    {
+			if (yyval.type == MN_FLOAT)
+			{
+			    yyval.u.d = -yyval.u.d;
+			}
+			else
+			{
+			    yyval.u.l = -yyval.u.l;
+			}
+		    }
+		    return ctype;
 		} else
 		    return UMINUS;
 	    } else
diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst
index 77a46eb..f1364ab 100644
--- a/Test/C01arith.ztst
+++ b/Test/C01arith.ztst
@@ -467,3 +467,7 @@
 >6
 >4
 ?(eval):6: bad math expression: lvalue required
+
+  print $(( -2#101-16#f ))
+0: Unary minus doesn't apply to base but to number as a whole.
+>-20


      reply	other threads:[~2018-08-08 16:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20180808153741epcas3p346a9ba58cddb7bd5d5e91aaf3a63d51c@epcas3p3.samsung.com>
2018-08-08 15:37 ` Stephane Chazelas
2018-08-08 16:05   ` 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='20180808160535eucas1p2f17bcad971b1f1190aed12c5bd707b6c~I9FjdWE6a1044010440eucas1p2I@eucas1p2.samsung.com' \
    --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).