forked from pfalcon/re1.5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.c
179 lines (160 loc) · 2.75 KB
/
compile.c
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2007-2009 Russ Cox. All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#ifdef DEBUG
#include "re1.5.h"
static Inst *pc;
static int count(Regexp*);
static void emit(Regexp*);
Prog*
compile(Regexp *r)
{
int n;
Prog *p;
n = count(r) + 1;
p = mal(sizeof *p + n*sizeof p->start[0]);
p->start = (Inst*)(p+1);
pc = p->start;
emit(r);
pc->opcode = Match;
pc++;
p->len = pc - p->start;
return p;
}
// how many instructions does r need?
static int
count(Regexp *r)
{
switch(r->type) {
default:
re1_5_fatal("bad count");
case Alt:
return 2 + count(r->left) + count(r->right);
case Cat:
return count(r->left) + count(r->right);
case Lit:
case Dot:
return 1;
case Paren:
return 2 + count(r->left);
case Quest:
return 1 + count(r->left);
case Star:
return 2 + count(r->left);
case Plus:
return 1 + count(r->left);
}
}
static void
emit(Regexp *r)
{
Inst *p1, *p2, *t;
switch(r->type) {
default:
re1_5_fatal("bad emit");
case Alt:
pc->opcode = Split;
p1 = pc++;
p1->x = pc;
emit(r->left);
pc->opcode = Jmp;
p2 = pc++;
p1->y = pc;
emit(r->right);
p2->x = pc;
break;
case Cat:
emit(r->left);
emit(r->right);
break;
case Lit:
pc->opcode = Char;
pc->c = r->ch;
pc++;
break;
case Dot:
pc++->opcode = Any;
break;
case Paren:
pc->opcode = Save;
pc->n = 2*r->n;
pc++;
emit(r->left);
pc->opcode = Save;
pc->n = 2*r->n + 1;
pc++;
break;
case Quest:
pc->opcode = Split;
p1 = pc++;
p1->x = pc;
emit(r->left);
p1->y = pc;
if(r->n) { // non-greedy
t = p1->x;
p1->x = p1->y;
p1->y = t;
}
break;
case Star:
pc->opcode = Split;
p1 = pc++;
p1->x = pc;
emit(r->left);
pc->opcode = Jmp;
pc->x = p1;
pc++;
p1->y = pc;
if(r->n) { // non-greedy
t = p1->x;
p1->x = p1->y;
p1->y = t;
}
break;
case Plus:
p1 = pc;
emit(r->left);
pc->opcode = Split;
pc->x = p1;
p2 = pc;
pc++;
p2->y = pc;
if(r->n) { // non-greedy
t = p2->x;
p2->x = p2->y;
p2->y = t;
}
break;
}
}
void
printprog(Prog *p)
{
Inst *pc, *e;
pc = p->start;
e = p->start + p->len;
for(; pc < e; pc++) {
switch(pc->opcode) {
default:
re1_5_fatal("printprog");
case Split:
printf("%2d. split %d, %d\n", (int)(pc-p->start), (int)(pc->x-p->start), (int)(pc->y-p->start));
break;
case Jmp:
printf("%2d. jmp %d\n", (int)(pc-p->start), (int)(pc->x-p->start));
break;
case Char:
printf("%2d. char %c\n", (int)(pc-p->start), pc->c);
break;
case Any:
printf("%2d. any\n", (int)(pc-p->start));
break;
case Match:
printf("%2d. match\n", (int)(pc-p->start));
break;
case Save:
printf("%2d. save %d\n", (int)(pc-p->start), pc->n);
}
}
}
#endif //DEBUG