bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] pretty-print eats parentheses


From: Andrew J. Schorr
Subject: Re: [bug-gawk] pretty-print eats parentheses
Date: Thu, 30 Oct 2014 10:09:28 -0400
User-agent: Mutt/1.5.23 (2014-03-12)

On Thu, Oct 30, 2014 at 11:55:18AM +0100, Manuel Collado wrote:
> El 30/10/2014 5:03, Hermann Peifer escribió:
> >...
> ># My parentheses are gone :-(
> >$ gawk -o/dev/stdout 'BEGIN{FS="|"; str = -8 FS (-9) ; print str}'
> >BEGIN {
> >     FS = "|"
> >     str = -8 FS -9
> >     print str
> >}
> 
> And a more striking example:
> 
> $ gawk -o/dev/stdout "BEGIN{ x=3; print -(-x) }"
> 3
>         BEGIN {
>                 x = 3
>                 print --x
>         }

Believe it or not, I think these are totally different bugs.
For the 1st bug, I think the problem is in profile.c:pp_concat,
whereas for the 2nd bug, I think the problem is in profile.c:pprint
in this section:

                case Op_field_spec:
                case Op_field_spec_lhs:
                case Op_unary_minus:
                case Op_not:
                        t1 = pp_pop();
                        if (is_binary(t1->type))
                                pp_parenthesize(t1);

                        /* optypes table (eval.c) includes space after ! */
                        str = pp_group3(op2str(pc->opcode), t1->pp_str, "");
                        pp_free(t1);
                        pp_push(pc->opcode, str, CAN_FREE);
                        break;

The correct fixes are not obvious to me.  The code is trying to be clever
about not including unnecessary parentheses.  If those checks are removed,
there may be lots of extra parentheses.  For example, if that "is_binary" test
is removed, one gets:

bash-4.2$ ./gawk -o/dev/stdout "BEGIN{ x=3; print -(-x) }"
BEGIN {
        x = 3
        print -(-(x))
}

Similarly, making pp_concat dumber gives:

bash-4.2$ gawk -o/dev/stdout 'BEGIN{FS="|"; str = -8 FS (-9) ; print str}'  
BEGIN {
        FS = "|"
        str = (-8) (FS) -9
        print str
}

I guess pp_concat may need to test whether the scalar looks like a negative
number, and if so, put parentheses around it to be safe.

Regards,
Andy



reply via email to

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