All of lore.kernel.org
 help / color / mirror / Atom feed
* $(()) pre/postfix bug
@ 2022-07-01 23:14 Steffen Nurpmeso
  2022-07-01 23:36 ` Steffen Nurpmeso
  2022-07-02  0:28 ` Robert Elz
  0 siblings, 2 replies; 4+ messages in thread
From: Steffen Nurpmeso @ 2022-07-01 23:14 UTC (permalink / raw)
  To: DASH shell mailing list; +Cc: Robert Elz

Hello.

I am currently writing a $(()) expression parser, using Dijkstra's
two stack algorithm (heavily impressed by the busybox one).

While doing so i encountered diverging behaviour in between
shells, and a bug in dash (and a memory failure in NetBSD 9 ksh).

For my own thing i hope i end up like bash (that is: just do it
(tm)), but the NetBSD and OpenBSD variants i also like, as they
mimic C and give an understandable error message.

I Cc: some of you, just for your interest.

Ciao.

==
Name:         dash
Path:         /usr/ports/core
Version:      0.5.11.5

#?2|kent:$ dash -c '(i=10; echo $((++-+++i)))'
-10
#?2|kent:$ dash -c '(i=10; echo $((+++++i)))'
10
#?0|kent:vm$ dash -c '(i=10; echo $((+ + + ++i)))'
10

==
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)

#?0|kent:$ bash -c '(i=10; echo $((++-+++i)))'
-11

==
BusyBox v1.34.0 (2022-01-03 21:34:20 CET) multi-call binary.

#?0|kent:$ busybox.static sh -c '(i=10; echo $((++-+++i)))'
sh: arithmetic syntax error
#?0|kent:$ busybox.static sh -c '(i=10; echo $((+ + - + ++i)))'
-11

==
FreeBSD 13.1-RELEASE releng/13.1-n250148-fc952ac2212 GENERIC

FreeBSD 13.1
#|f-1301-x86:~$ sh -c '(i=10; echo $((++-+++i)))'
sh: arithmetic expression: expecting primary: "++-+++i"

==
OpenBSD 7.1 (GENERIC.MP) #465: Mon Apr 11 18:03:57 MDT 2022

#?1|o-0701:steffen$ ksh -c '(i=10; echo $((++-+++i)))'
ksh: ++-+++i: ++ requires lvalue
#?1|o-0701:steffen$ ksh -c '(i=10; echo $((+++i)))'
ksh: +++i: ++ requires lvalue
#?1|o-0701:steffen$ ksh -c '(i=10; echo $((++i)))'
11
#?0|o-0701:steffen$ ksh -c '(i=10; echo $((+ + + ++i)))'
11
#?0|o-0701:steffen$ ksh -c '(i=10; echo $((+ + - + ++i)))'
-11

==
NetBSD n-0900 9.0 NetBSD 9.0 (GENERIC) #0: Fri Feb 14 00:06:28 UTC 2020  mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/amd64/compile/GENERIC amd64

#?0|n-0900:steffen$ ksh -c '(i=10; echo $((++-+++i)))'
Memory fault
#?2|n-0900:steffen$ sh -c '(i=10; echo $((+++++i)))'
sh: arithmetic expression: incr/decr require var name: "+++++i"
#?2|n-0900:steffen$ sh -c '(i=10; echo $((+ ++i)))'
11
#?0|n-0900:steffen$ sh -c '(i=10; echo $((+ - ++i)))'
-11

--steffen
|
|Der Kragenbaer,                The moon bear,
|der holt sich munter           he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: $(()) pre/postfix bug
  2022-07-01 23:14 $(()) pre/postfix bug Steffen Nurpmeso
@ 2022-07-01 23:36 ` Steffen Nurpmeso
  2022-07-02  0:28 ` Robert Elz
  1 sibling, 0 replies; 4+ messages in thread
From: Steffen Nurpmeso @ 2022-07-01 23:36 UTC (permalink / raw)
  To: DASH shell mailing list, Robert Elz

Steffen Nurpmeso wrote in
 <20220701231439.egWzS%steffen@sdaoden.eu>:
 ..
 |While doing so i encountered diverging behaviour in between
 |shells, and a bug in dash (and a memory failure in NetBSD 9 ksh).

Nonsense.  dash simply does not support pre/postfix operators.
Sorry for the noise.

  ...
 |NetBSD n-0900 9.0 NetBSD 9.0 (GENERIC) #0: Fri Feb 14 00:06:28 UTC \
 |2020  mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/amd64/compile/GENERIC \
 |amd64
 |
 |#?0|n-0900:steffen$ ksh -c '(i=10; echo $((++-+++i)))'
 |Memory fault
 ...

Ok, maybe a bug report over there.

--steffen
|
|Der Kragenbaer,                The moon bear,
|der holt sich munter           he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: $(()) pre/postfix bug
  2022-07-01 23:14 $(()) pre/postfix bug Steffen Nurpmeso
  2022-07-01 23:36 ` Steffen Nurpmeso
@ 2022-07-02  0:28 ` Robert Elz
  2022-07-02  8:20   ` Harald van Dijk
  1 sibling, 1 reply; 4+ messages in thread
From: Robert Elz @ 2022-07-02  0:28 UTC (permalink / raw)
  To: Steffen Nurpmeso; +Cc: DASH shell mailing list

    Date:        Sat, 02 Jul 2022 01:14:39 +0200
    From:        Steffen Nurpmeso <steffen@sdaoden.eu>
    Message-ID:  <20220701231439.egWzS%steffen@sdaoden.eu>

  | While doing so i encountered diverging behaviour in between
  | shells, and a bur in dash

Support for ++/-- in sh arith is not required, it l9oks as
if dash does not support them.  Without ++ support ++i is
just + + i

  | (and a memory failure in NetBSD 9 ksh).

that thing is full of bugs.

  | For my own thing i hope i end up like bash (that is: just do it
  | (tm)),

If anything, tgat one is br9ken, tokens are supposed to always
be interpreted left to right, longest possible token matches
(not the one that allows the rest of the expression to parse).

But as ++ support is optional, the rules for it are a bit up
in the air ... but $(( )) is supposed to evwluate just like 
Ç with the various caveats about what is not required.

  | #?0|kent:$ bash -c '(i=10; echo $((++-+++i)))'
  | -11

So that should be equiv to ++ - ++ + i [assuming support for ++]
in which there are two ++ operators not followed (or following)
a var name (aka lvalue).  Error.

  | FreeBSD 13.1
  | #|f-1301-x86:~$ sh -c '(i=10; echo $((++-+++i)))'
  | sh: arithmetic expression: expecting primary: "++-+++i"

We used to do that as well (our arith parser is derived from
theirs, which is derived from dash's with extensions) but no-one ever
understood what a "primary" was (relates to internal implementation
function names) so I altered the err msg.

kre

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: $(()) pre/postfix bug
  2022-07-02  0:28 ` Robert Elz
@ 2022-07-02  8:20   ` Harald van Dijk
  0 siblings, 0 replies; 4+ messages in thread
From: Harald van Dijk @ 2022-07-02  8:20 UTC (permalink / raw)
  To: Robert Elz, Steffen Nurpmeso; +Cc: DASH shell mailing list

On 02/07/2022 01:28, Robert Elz wrote:
>    | FreeBSD 13.1
>    | #|f-1301-x86:~$ sh -c '(i=10; echo $((++-+++i)))'
>    | sh: arithmetic expression: expecting primary: "++-+++i"
> 
> We used to do that as well (our arith parser is derived from
> theirs, which is derived from dash's with extensions) but no-one ever
> understood what a "primary" was (relates to internal implementation
> function names) so I altered the err msg.

While there is a function called primary, I actually think that may not 
be what the error message refers to, not directly. The grammar is 
supposed to be the C grammar with unsupported constructs removed, the C 
grammar defines a "primary-expression", and the error message in dash 
shows up where a straightforward parser directly based on the C grammar 
would result in an error where the grammar demands a primary-expression.

The function called primary ended up doing more than parse a 
primary-expression, but that does not change which cases result in that 
error.

Cheers,
Harald van Dijk

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-07-02  8:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-01 23:14 $(()) pre/postfix bug Steffen Nurpmeso
2022-07-01 23:36 ` Steffen Nurpmeso
2022-07-02  0:28 ` Robert Elz
2022-07-02  8:20   ` Harald van Dijk

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.