-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpat-exec.c
268 lines (207 loc) · 4.08 KB
/
pat-exec.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <pat.h>
#include <pat.ih>
static void ctx_fini(struct context *);
static int ctx_init(struct context *, struct pattern *);
static int ctx_next(struct context *, char const *);
static void ctx_prune(struct context *);
static void ctx_rm(struct context *);
static void ctx_shift(struct context *);
static int ctx_step(struct context *, char const *);
static struct thread *ctx_get(struct context *);
static int pat_exec(struct context *);
struct thread *
ctx_get(struct context *ctx)
{
struct thread *ret;
if (!ctx->frl[0] && thr_alloc(ctx->frl)) {
return 0;
}
ret = ctx->frl[0];
if (ctx->frl[0] == ctx->frl[1]) {
ctx->frl[1] = 0;
}
ctx->frl[0] = ctx->frl[0]->next;
ret->next = 0x0;
return ret;
}
void
ctx_fini(struct context *ctx)
{
thr_free(ctx->thr);
thr_free(ctx->que[0]);
thr_free(ctx->frl[0]);
thr_free(ctx->res);
}
int
ctx_init(struct context *ctx, struct pattern *pat)
{
int err = 0;
ctx->que[0] = ctx_get(ctx);
if (!ctx->que[0]) return ENOMEM;
err = thr_init(ctx->que[0], pat->prog);
if (err) goto fail;
return 0;
fail:
ctx_fini(ctx);
return err;
}
int
ctx_next(struct context *ctx, char const *txt)
{
ctx_prune(ctx);
if (!ctx->thr) return 0;
return ctx_step(ctx, txt);
}
void
ctx_prune(struct context *ctx)
{
while (ctx->thr) {
if (thr_cmp(ctx->res, ctx->thr) < 0) {
break;
} else ctx_rm(ctx);
}
}
void
ctx_rm(struct context *ctx)
{
thr_mv(ctx->frl, &ctx->thr);
}
void
ctx_que(struct context *ctx)
{
thr_mv(ctx->que, &ctx->thr);
}
void
ctx_shift(struct context *ctx)
{
ctx->thr = ctx->que[0];
ctx->que[0] = 0;
ctx->que[1] = 0;
}
int
ctx_step(struct context *ctx, char const *txt)
{
return ctx->thr->ip->op(ctx, txt);
}
int
do_char(struct context *ctx, char const *txt)
{
char ch = ctx->thr->ip->arg;
if (txt && ch == *txt) {
++ctx->thr->ip;
ctx_que(ctx);
} else ctx_rm(ctx);
return ctx_next(ctx, txt);
}
int
do_clss(struct context *ctx, char const *txt)
{
bool res;
if (txt) switch (ctx->thr->ip->arg) {
case 0: res = true; break;
case '.': res = *txt != '\n' && *txt != '\0'; break;
}
if (txt && res) {
++ctx->thr->ip;
ctx_que(ctx);
}
else ctx_rm(ctx);
return ctx_next(ctx, txt);
}
int
do_fork(struct context *ctx, char const *txt)
{
struct thread *new;
new = ctx_get(ctx);
if (!new) return ENOMEM;
thr_fork(new, ctx->thr);
new->ip += ctx->thr->ip->arg;
++ctx->thr->ip;
new->next = ctx->thr;
ctx->thr = new;
return ctx->thr->ip->op(ctx, txt);
}
int
do_halt(struct context *ctx, char const *txt)
{
struct thread *th;
if (thr_cmp(ctx->res, ctx->thr) > 0) {
ctx_rm(ctx);
return ctx_next(ctx, txt);
}
if (ctx->res) thr_mv(ctx->frl, &ctx->res);
th = ctx->thr;
ctx->thr = ctx->thr->next;
ctx->res = th;
ctx->res->next = 0;
return ctx_next(ctx, txt);
}
int
do_jump(struct context *ctx, char const *txt)
{
ctx->thr->ip += ctx->thr->ip->arg;
return ctx->thr->ip->op(ctx, txt);
}
int
do_mark(struct context *ctx, char const *txt)
{
struct thread *th = ctx->thr;
if (th->nmat < 10) {
th->mat[th->nmat++] = (struct patmatch){ ctx->pos, -1 };
}
++th->ip;
return th->ip->op(ctx, txt);
}
int
do_save(struct context *ctx, char const *txt)
{
struct thread *th = ctx->thr;
size_t off = th->nmat;
while (th->mat[--off].ext != -1UL) continue;
th->mat[off].ext = ctx->pos - th->mat[off].off;
++th->ip;
return th->ip->op(ctx, txt);
}
int
pat_exec(struct context *ctx)
{
int err;
while (ctx->pos < ctx->len) {
ctx_shift(ctx);
err = ctx_next(ctx, ctx->str + ctx->pos);
if (err) break;
++ctx->pos;
}
if (err) return err;
return 0;
}
int
pat_fini(struct context *ctx)
{
int err;
ctx_shift(ctx);
err = ctx_next(ctx, 0x0);
if (err) return err;
if (!ctx->res) return PAT_ERR_NOMATCH;
return 0;
}
int
pat_match(struct pattern *pat, struct context *ctx)
{
int err;
err = ctx_init(ctx, pat);
if (err) return err;
err = pat_exec(ctx);
if (err) goto finally;
err = pat_fini(ctx);
if (err) goto finally;
memcpy(pat->mat, ctx->res->mat, sizeof pat->mat);
pat->nmat = ctx->res->nmat;
finally:
ctx_fini(ctx);
return err;
}