From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13020 invoked from network); 16 Jun 2008 13:42:41 -0000 X-Spam-Checker-Version: SpamAssassin 3.2.4 (2008-01-01) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.4 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 16 Jun 2008 13:42:41 -0000 Received-SPF: none (ns1.primenet.com.au: domain at sunsite.dk does not designate permitted sender hosts) Received: (qmail 91171 invoked from network); 16 Jun 2008 13:42:35 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 16 Jun 2008 13:42:35 -0000 Received: (qmail 4787 invoked by alias); 16 Jun 2008 13:42:31 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 25160 Received: (qmail 4773 invoked from network); 16 Jun 2008 13:42:31 -0000 Received: from bifrost.dotsrc.org (130.225.254.106) by sunsite.dk with SMTP; 16 Jun 2008 13:42:31 -0000 Received: from mtaout02-winn.ispmail.ntl.com (mtaout02-winn.ispmail.ntl.com [81.103.221.48]) by bifrost.dotsrc.org (Postfix) with ESMTP id B30F78028AC3 for ; Mon, 16 Jun 2008 15:42:28 +0200 (CEST) Received: from aamtaout02-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout02-winn.ispmail.ntl.com with ESMTP id <20080616134659.IEID7070.mtaout02-winn.ispmail.ntl.com@aamtaout02-winn.ispmail.ntl.com> for ; Mon, 16 Jun 2008 14:46:59 +0100 Received: from pws-pc ([81.107.40.67]) by aamtaout02-winn.ispmail.ntl.com with ESMTP id <20080616134535.CAJY29365.aamtaout02-winn.ispmail.ntl.com@pws-pc> for ; Mon, 16 Jun 2008 14:45:35 +0100 Date: Mon, 16 Jun 2008 14:42:11 +0100 From: Peter Stephenson To: Zsh hackers list Subject: Re: arithmetic operator precedence Message-ID: <20080616144211.276fb0e3@pws-pc> In-Reply-To: <20080616080726.GP10734@prunille.vinc17.org> References: <20080612095723.GF5113@sc.homeunix.net> <20080616080726.GP10734@prunille.vinc17.org> X-Mailer: Claws Mail 3.4.0 (GTK+ 2.12.10; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: ClamAV 0.92.1/7488/Mon Jun 16 08:06:58 2008 on bifrost X-Virus-Status: Clean On Mon, 16 Jun 2008 10:07:26 +0200 Vincent Lefevre wrote: > Speaking of precedence, the following one is nasty: > > vin% zsh -c 'echo $((-3**2))' > 9 > vin% bash -c 'echo $((-3**2))' > 9 > vin% ksh93 -c 'echo $((-3**2))' > 9 > > IMHO these shells should be fixed to give -9, i.e. ** should have > the precedence over the unary -, like conventional math writing. That's an interesting point for C_PRECEDENCES since I was trying to get it behave as much as possible like Perl. What does anyone else think? Index: Doc/Zsh/arith.yo =================================================================== RCS file: /cvsroot/zsh/zsh/Doc/Zsh/arith.yo,v retrieving revision 1.13 diff -u -r1.13 arith.yo --- Doc/Zsh/arith.yo 12 Jun 2008 13:45:05 -0000 1.13 +++ Doc/Zsh/arith.yo 16 Jun 2008 13:38:41 -0000 @@ -121,11 +121,13 @@ With the option tt(C_PRECEDENCES) the precedences (but no other properties) of the operators are altered to be the same as those in -most other languages that support the relevant operators: +most other languages that support the relevant operators, in particular +Perl (note that exponentiation, not present in C, has a higher +precedence than unary operators): startsitem() -sitem(tt(PLUS() - ! ~ PLUS()PLUS() --))(unary plus/minus, logical NOT, complement, {pre,post}{in,de}crement) sitem(tt(**))(exponentiation) +sitem(tt(PLUS() - ! ~ PLUS()PLUS() --))(unary plus/minus, logical NOT, complement, {pre,post}{in,de}crement) sitem(tt(* / %))(multiplication, division, modulus (remainder)) sitem(tt(PLUS() -))(addition, subtraction) sitem(tt(<< >>))(bitwise shift left, right) Index: Src/math.c =================================================================== RCS file: /cvsroot/zsh/zsh/Src/math.c,v retrieving revision 1.33 diff -u -r1.33 math.c --- Src/math.c 12 Jun 2008 13:45:06 -0000 1.33 +++ Src/math.c 16 Jun 2008 13:38:42 -0000 @@ -193,9 +193,9 @@ static int c_prec[TOKCOUNT] = { /* M_INPAR M_OUTPAR NOT COMP POSTPLUS */ -/* 0 */ 1, 137, 2, 2, 2, +/* 0 */ 1, 137, 3, 3, 3, /* POSTMINUS UPLUS UMINUS AND XOR */ -/* 5 */ 2, 2, 2, 9, 10, +/* 5 */ 3, 3, 3, 9, 10, /* OR MUL DIV MOD PLUS */ /* 10 */ 11, 4, 4, 4, 5, /* MINUS SHLEFT SHRIGHT LES LEQ */ @@ -211,7 +211,7 @@ /* DANDEQ DOREQ DXOREQ COMMA EOI */ /* 40 */ 17, 17, 17, 18, 200, /* PREPLUS PREMINUS NUM ID POWER */ -/* 45 */ 2, 2, 0, 0, 3, +/* 45 */ 3, 3, 0, 0, 2, /* CID POWEREQ FUNC */ /* 50 */ 0, 17, 0 }; @@ -514,13 +514,9 @@ ptr++; return MINUSEQ; } - if (unary) { - if (idigit(*ptr) || *ptr == '.') { - ptr--; - return lexconstant(); - } else - return UMINUS; - } else + if (unary) + return UMINUS; + else return MINUS; case '(': return M_INPAR; -- Peter Stephenson Web page now at http://homepage.ntlworld.com/p.w.stephenson/