Skip to content

Commit d0bb19c

Browse files
committed
Merge branch 'rs/grep-expr-cleanup'
Code clean-up. * rs/grep-expr-cleanup: grep: use grep_and_expr() in compile_pattern_and() grep: extract grep_binexp() from grep_or_expr() grep: use grep_not_expr() in compile_pattern_not() grep: use grep_or_expr() in compile_pattern_or()
2 parents 391d85d + 0a6adc2 commit d0bb19c

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

grep.c

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,35 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
595595
}
596596
}
597597

598+
static struct grep_expr *grep_not_expr(struct grep_expr *expr)
599+
{
600+
struct grep_expr *z = xcalloc(1, sizeof(*z));
601+
z->node = GREP_NODE_NOT;
602+
z->u.unary = expr;
603+
return z;
604+
}
605+
606+
static struct grep_expr *grep_binexp(enum grep_expr_node kind,
607+
struct grep_expr *left,
608+
struct grep_expr *right)
609+
{
610+
struct grep_expr *z = xcalloc(1, sizeof(*z));
611+
z->node = kind;
612+
z->u.binary.left = left;
613+
z->u.binary.right = right;
614+
return z;
615+
}
616+
617+
static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
618+
{
619+
return grep_binexp(GREP_NODE_OR, left, right);
620+
}
621+
622+
static struct grep_expr *grep_and_expr(struct grep_expr *left, struct grep_expr *right)
623+
{
624+
return grep_binexp(GREP_NODE_AND, left, right);
625+
}
626+
598627
static struct grep_expr *compile_pattern_or(struct grep_pat **);
599628
static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
600629
{
@@ -638,12 +667,10 @@ static struct grep_expr *compile_pattern_not(struct grep_pat **list)
638667
if (!p->next)
639668
die("--not not followed by pattern expression");
640669
*list = p->next;
641-
CALLOC_ARRAY(x, 1);
642-
x->node = GREP_NODE_NOT;
643-
x->u.unary = compile_pattern_not(list);
644-
if (!x->u.unary)
670+
x = compile_pattern_not(list);
671+
if (!x)
645672
die("--not followed by non pattern expression");
646-
return x;
673+
return grep_not_expr(x);
647674
default:
648675
return compile_pattern_atom(list);
649676
}
@@ -652,7 +679,7 @@ static struct grep_expr *compile_pattern_not(struct grep_pat **list)
652679
static struct grep_expr *compile_pattern_and(struct grep_pat **list)
653680
{
654681
struct grep_pat *p;
655-
struct grep_expr *x, *y, *z;
682+
struct grep_expr *x, *y;
656683

657684
x = compile_pattern_not(list);
658685
p = *list;
@@ -665,31 +692,23 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list)
665692
y = compile_pattern_and(list);
666693
if (!y)
667694
die("--and not followed by pattern expression");
668-
CALLOC_ARRAY(z, 1);
669-
z->node = GREP_NODE_AND;
670-
z->u.binary.left = x;
671-
z->u.binary.right = y;
672-
return z;
695+
return grep_and_expr(x, y);
673696
}
674697
return x;
675698
}
676699

677700
static struct grep_expr *compile_pattern_or(struct grep_pat **list)
678701
{
679702
struct grep_pat *p;
680-
struct grep_expr *x, *y, *z;
703+
struct grep_expr *x, *y;
681704

682705
x = compile_pattern_and(list);
683706
p = *list;
684707
if (x && p && p->token != GREP_CLOSE_PAREN) {
685708
y = compile_pattern_or(list);
686709
if (!y)
687710
die("not a pattern expression %s", p->pattern);
688-
CALLOC_ARRAY(z, 1);
689-
z->node = GREP_NODE_OR;
690-
z->u.binary.left = x;
691-
z->u.binary.right = y;
692-
return z;
711+
return grep_or_expr(x, y);
693712
}
694713
return x;
695714
}
@@ -699,30 +718,13 @@ static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
699718
return compile_pattern_or(list);
700719
}
701720

702-
static struct grep_expr *grep_not_expr(struct grep_expr *expr)
703-
{
704-
struct grep_expr *z = xcalloc(1, sizeof(*z));
705-
z->node = GREP_NODE_NOT;
706-
z->u.unary = expr;
707-
return z;
708-
}
709-
710721
static struct grep_expr *grep_true_expr(void)
711722
{
712723
struct grep_expr *z = xcalloc(1, sizeof(*z));
713724
z->node = GREP_NODE_TRUE;
714725
return z;
715726
}
716727

717-
static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
718-
{
719-
struct grep_expr *z = xcalloc(1, sizeof(*z));
720-
z->node = GREP_NODE_OR;
721-
z->u.binary.left = left;
722-
z->u.binary.right = right;
723-
return z;
724-
}
725-
726728
static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
727729
{
728730
struct grep_pat *p;

0 commit comments

Comments
 (0)