[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: gawk --profile eats parentheses
From: |
Aharon Robbins |
Subject: |
Re: gawk --profile eats parentheses |
Date: |
Tue, 11 Aug 2009 19:40:14 +0300 |
Greetings. Re this:
> Date: Tue, 11 Aug 2009 16:45:02 +0200
> From: Hermann Peifer <address@hidden>
> To: address@hidden
> Subject: gawk --profile eats parentheses
>
> $ gawk --profile '$1=="A" && ( $2=="B" || $3=="C" )'
>
> This results into:
>
> $ cat awkprof.out
> # gawk profile, created Tue Aug 11 16:43:59 2009
>
> # Rule(s)
>
> $1 == "A" && $2 == "B" || $3 == "C" {
> print $0
> }
>
> In awkprof.out, the parentheses are lost which changes the logic of the
> condition.
>
> (I am using gawk 3.1.6).
>
> Hermann
Indeed, it is a bug. Thank you for reporting it. The following
is against 3.1.7 (to which you should upgrade if at all possible :-).
Arnold
-------------------------------------------------------------
Tue Aug 11 19:23:51 2009 Arnold D. Robbins <address@hidden>
* profile.c (parenthesize_expr): New function.
(tree_eval): Use it for Node_and and Node_or.
===================================================================
RCS file: /d/mongo/cvsrep/gawk-stable/profile.c,v
retrieving revision 1.12
diff -u -r1.12 profile.c
--- profile.c 21 Jul 2009 20:24:14 -0000 1.12
+++ profile.c 11 Aug 2009 16:25:13 -0000
@@ -34,6 +34,7 @@
#undef tree_eval
static void tree_eval P((NODE *tree));
static void parenthesize P((NODETYPE parent_type, NODE *tree));
+static void parenthesize_expr P((NODETYPE parent_type, NODE *tree));
static void eval_condition P((NODE *tree));
static void pp_op_assign P((NODE *tree));
static void pp_func_call P((NODE *tree));
@@ -465,15 +466,15 @@
return;
case Node_and:
- eval_condition(tree->lnode);
+ parenthesize_expr(Node_and, tree->lnode);
fprintf(prof_fp, " && ");
- eval_condition(tree->rnode);
+ parenthesize_expr(Node_and, tree->rnode);
return;
case Node_or:
- eval_condition(tree->lnode);
+ parenthesize_expr(Node_or, tree->lnode);
fprintf(prof_fp, " || ");
- eval_condition(tree->rnode);
+ parenthesize_expr(Node_or, tree->rnode);
return;
case Node_not:
@@ -1430,6 +1431,28 @@
in_expr--;
}
+/* parenthesize_expr --- print an expression subtree in parentheses if need be
*/
+
+static void
+parenthesize_expr(NODETYPE parent_type, NODE *tree)
+{
+ NODETYPE child_type;
+
+ if (tree == NULL)
+ return;
+
+ child_type = tree->type;
+
+ in_expr++;
+ if (prec_level(child_type) < prec_level(parent_type)) {
+ fprintf(prof_fp, "(");
+ eval_condition(tree);
+ fprintf(prof_fp, ")");
+ } else
+ eval_condition(tree);
+ in_expr--;
+}
+
/* pp_var --- print builtin variables, do it in one place */
static void