summaryrefslogtreecommitdiff
path: root/regex.c
blob: 63bd7487ff828f05aa9aebdc9b4be14c4962cdf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdlib.h>

#include "ast.h"

static void repeat(struct atom *a)
{
	struct atom *child = a->u.repeat.child;
	simplify(child);

	const struct repeat *r = &a->u.repeat.counts;
	if (r->min == 1 && r->max == 1) {
		*a = *child;
		free(child);
	}
}

static void children(struct atom *a)
{
	simplify(a->u.children[0]);
	simplify(a->u.children[1]);
}

void simplify(struct atom *a)
{
	switch (a->type) {
	case ATOM_REPETITION:
		repeat(a);
		break;

	case ATOM_ALTERNATION:
	case ATOM_SEQUENCE:
		children(a);
	}
}