@@ -595,6 +595,35 @@ static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
595
595
}
596
596
}
597
597
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
+
598
627
static struct grep_expr * compile_pattern_or (struct grep_pat * * );
599
628
static struct grep_expr * compile_pattern_atom (struct grep_pat * * list )
600
629
{
@@ -638,12 +667,10 @@ static struct grep_expr *compile_pattern_not(struct grep_pat **list)
638
667
if (!p -> next )
639
668
die ("--not not followed by pattern expression" );
640
669
* 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 )
645
672
die ("--not followed by non pattern expression" );
646
- return x ;
673
+ return grep_not_expr ( x ) ;
647
674
default :
648
675
return compile_pattern_atom (list );
649
676
}
@@ -652,7 +679,7 @@ static struct grep_expr *compile_pattern_not(struct grep_pat **list)
652
679
static struct grep_expr * compile_pattern_and (struct grep_pat * * list )
653
680
{
654
681
struct grep_pat * p ;
655
- struct grep_expr * x , * y , * z ;
682
+ struct grep_expr * x , * y ;
656
683
657
684
x = compile_pattern_not (list );
658
685
p = * list ;
@@ -665,31 +692,23 @@ static struct grep_expr *compile_pattern_and(struct grep_pat **list)
665
692
y = compile_pattern_and (list );
666
693
if (!y )
667
694
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 );
673
696
}
674
697
return x ;
675
698
}
676
699
677
700
static struct grep_expr * compile_pattern_or (struct grep_pat * * list )
678
701
{
679
702
struct grep_pat * p ;
680
- struct grep_expr * x , * y , * z ;
703
+ struct grep_expr * x , * y ;
681
704
682
705
x = compile_pattern_and (list );
683
706
p = * list ;
684
707
if (x && p && p -> token != GREP_CLOSE_PAREN ) {
685
708
y = compile_pattern_or (list );
686
709
if (!y )
687
710
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 );
693
712
}
694
713
return x ;
695
714
}
@@ -699,30 +718,13 @@ static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
699
718
return compile_pattern_or (list );
700
719
}
701
720
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
-
710
721
static struct grep_expr * grep_true_expr (void )
711
722
{
712
723
struct grep_expr * z = xcalloc (1 , sizeof (* z ));
713
724
z -> node = GREP_NODE_TRUE ;
714
725
return z ;
715
726
}
716
727
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
-
726
728
static struct grep_expr * prep_header_patterns (struct grep_opt * opt )
727
729
{
728
730
struct grep_pat * p ;
0 commit comments