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 (2)


From: Aharon Robbins
Subject: Re: [bug-gawk] pretty-print eats parentheses (2)
Date: Sun, 02 Nov 2014 21:36:20 +0200
User-agent: Heirloom mailx 12.5 6/20/10

Hi. Re this:

> Date: Sun, 02 Nov 2014 14:32:06 +0100
> From: Hermann Peifer <address@hidden>
> To: "address@hidden" <address@hidden>
> Subject: [bug-gawk] pretty-print eats parentheses (2)
>
> Hi again,
>
> Below is what I get with latest gawk from git master branch.
>
> Hermann
>
> $ awk    'BEGIN{print 1 / (10 * 10) }'
> 0.01
>
> $ awk -o 'BEGIN{print 1 / (10 * 10) }' && awk -f awkprof.out
> 1
>
> $ cat awkprof.out
> BEGIN {
>       print 1 / 10 * 10
> }

Thanks for the report. Here is the fix (line numbers might be off).
I have just pushed this to the repo.

Arnold
---------------------------------------
diff --git a/profile.c b/profile.c
index ed17e62..316ba39 100644
--- a/profile.c
+++ b/profile.c
@@ -1180,6 +1180,26 @@ pp_parenthesize(NODE *sp)
        sp->flags |= CAN_FREE;
 }
 
+/* div_on_left_mul_on_right --- have / or % on left and * on right */
+
+static bool
+div_on_left_mul_on_right(int o1, int o2)
+{
+       OPCODE op1 = (OPCODE) o1;
+       OPCODE op2 = (OPCODE) o2;
+
+       switch (op1) {
+       case Op_quotient:
+       case Op_quotient_i:
+       case Op_mod:
+       case Op_mod_i:
+               return (op2 == Op_times || op2 == Op_times_i);
+
+       default:
+               return false;
+       }
+}
+
 /* parenthesize --- parenthesize two nodes relative to parent node type */
 
 static void
@@ -1189,9 +1209,11 @@ parenthesize(int type, NODE *left, NODE *right)
        int lprec = prec_level(left->type);
        int prec = prec_level(type);
 
-       if (lprec < prec)
+       if (lprec < prec
+           || (lprec == prec && div_on_left_mul_on_right(left->type, type)))
                pp_parenthesize(left);
-       if (rprec < prec)
+       if (rprec < prec
+           || (rprec == prec && div_on_left_mul_on_right(type, right->type)))
                pp_parenthesize(right);
 }
 



reply via email to

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