bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: $(( )): binary/unary VAR/NUM inconsistency


From: Chet Ramey
Subject: Re: $(( )): binary/unary VAR/NUM inconsistency
Date: Fri, 8 Jul 2022 10:11:16 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.10.0

On 7/7/22 12:11 PM, Steffen Nurpmeso wrote:
Hello!

Hi.


Funnily my parser has only one (what i know) problem left, the
same as bash.  On the other hand i found more.

The thing about all of this is that these are operators, and so delimit
tokens. Whitespace is siginficant only when determining the length of the
token or operator. Operators have context-dependent meaning (e.g., `+' can
be a unary or binary operator depending on context, and `++10' does not
mean the same thing as `+ +10').

[
     # make this work with (ba)sh \
     command -v shopt && shopt -s expand_aliases;\
     alias p=printf;alias e=echo;alias s=export
     #
     s I=10 J=33
]

   e "<$((  3          +           (      11       )  ))>"
   s I=10 J=33;p "<$(( +10 + + +I ))>";e "<$I>"

Unary plus, constant, binary plus operator, unary plus, unary plus, identifier

   s I=10 J=33;p "<$(( +10 + ++I ))>";e "<$I>"

Unary plus, constant, binary plus operator, prefix increment, identifier

   s I=10 J=33;p "<$(( +10 ++ +I ))>";e "<$I>"

Unary plus, constant, binary plus, unary plus, unary plus, identifier

(this is one place where the bash arithmetic parser is forgiving, since
the above interpretation predated the addition of ++ and --)

   s I=10 J=33;p "<$(( +10 +++ I ))>";e "<$I>"

Unary plus, constant, binary plus, unary plus, unary plus, identifier
(same)

   s I=10 J=33;p "<$(( +10+++I ))>";e "<$I>"

Unary plus, constant, binary plus, prefix increment, identifier


And one more thing.

   -<802379605485813759>
   +<9223372036854775807>

This is from

   $ bash -c 'echo $((999999999999999999999999999999999999999999999))'
   802379605485813759
   $ dash -c 'echo $((999999999999999999999999999999999999999999999))'
   9223372036854775807

dash is what happens when you clamp the value at INTMAX_MAX (LLONG_MAX)
because it overflows -- but don't say anything about it -- instead of just
doing the conversion without checking for overflow. Neither value is
`right', and even the predictability of INTMAX_MAX is useless.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]