-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpat-comp.c
194 lines (158 loc) · 4.27 KB
/
pat-comp.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
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <pat.ih>
#include <pat.h>
#define instr(...) ins(__VA_ARGS__, 0,)
#define ins(OP, ARG, ...) (struct ins){ .op = OP, .arg = ARG }
static struct token *chld_next(struct token *, struct token *);
static struct token *comp_alt(struct ins **, struct token *, struct token *);
static struct token *comp_lit(struct ins **, struct token *, struct token *);
static struct token *comp_cls(struct ins **, struct token *, struct token *);
static struct token *comp_reg(struct ins **, struct token *, struct token *);
static struct token *comp_kln(struct ins **, struct token *, struct token *);
static struct token *comp_nop(struct ins **, struct token *, struct token *);
static struct token *comp_opt(struct ins **, struct token *, struct token *);
static struct token *comp_rep(struct ins **, struct token *, struct token *);
static struct token *comp_sub(struct ins **, struct token *, struct token *);
static void marshal(struct ins *, struct token *tok);
static struct token *(* const tab_comp[])(struct ins **, struct token *, struct token *) = {
[type_lit] = comp_lit,
[type_cls] = comp_cls,
[type_opt] = comp_opt,
[type_rep] = comp_rep,
[type_kln] = comp_kln,
[type_alt] = comp_alt,
[type_sub] = comp_sub,
[type_reg] = comp_reg,
[type_nop] = comp_nop,
};
static size_t tab_len[] = {
[type_lit] = 1,
[type_cls] = 1,
[type_opt] = 1,
[type_rep] = 1,
[type_kln] = 2,
[type_alt] = 2,
[type_sub] = 2,
[type_reg] = 6,
};
struct token *
chld_next(struct token *tok, struct token *ctx)
{
size_t off;
if (!ctx || tok < ctx) return tok - 1;
if (tok == ctx) return tok->up;
off = tok - ctx + ctx->siz;
if (off >= tok->siz) return tok;
return tok - off;
}
struct token *
comp_alt(struct ins **dst, struct token *tok, struct token *ctx)
{
size_t off;
if (tok == ctx) {
*dst[0]-- = instr(do_fork, tok->siz + 1);
return tok->up;
}
if (tok < ctx) {
off = tok->up->len - tok->len - type_len(tok->up->id);
*dst[0]-- = instr(do_jump, off + 1);
return tok - 1;
}
return chld_next(tok, ctx);
}
struct token *
comp_opt(struct ins **dst, struct token *tok, struct token *ctx)
{
if (tok == ctx) *dst[0]-- = instr(do_fork, tok->len);
return chld_next(tok, ctx);
}
struct token *
comp_rep(struct ins **dst, struct token *tok, struct token *ctx)
{
if (tok < ctx) *dst[0]-- = instr(do_fork, -tok->len + 1);
else if (tok > ctx) return tok->up;
return chld_next(tok, ctx);
}
struct token *
comp_sub(struct ins **dst, struct token *tok, struct token *ctx)
{
if (tok == ctx) *dst[0]-- = instr(do_mark);
if (tok < ctx) *dst[0]-- = instr(do_save);
return chld_next(tok, ctx);
}
struct token *
comp_kln(struct ins **dst, struct token *tok, struct token *ctx)
{
if (tok < ctx) *dst[0]-- = instr(do_fork, -tok->siz + 1);
if (tok == ctx) *dst[0]-- = instr(do_fork, tok->siz);
return chld_next(tok, ctx);
}
struct token *
comp_reg(struct ins **dst, struct token *tok, struct token *ctx)
{
if (!ctx) {
*dst[0]-- = instr(do_halt);
*dst[0]-- = instr(do_save);
return tok - 1;
}
if (tok == ctx) {
*dst[0]-- = instr(do_mark);
*dst[0]-- = instr(do_fork, -1);
*dst[0]-- = instr(do_clss, 0);
*dst[0]-- = instr(do_jump, 2);
return 0x0;
}
return chld_next(tok, ctx);
}
struct token *
comp_lit(struct ins **dst, struct token *tok, struct token *ctx)
{
*dst[0]-- = instr(do_char, tok->ch);
return chld_next(ctx, tok);
}
struct token *
comp_cls(struct ins **dst, struct token *tok, struct token *ctx)
{
*dst[0]-- = instr(do_clss, tok->ch);
return chld_next(ctx, tok);
}
struct token *
comp_nop(struct ins **dst, struct token *tok, struct token *ctx)
{
return chld_next(ctx, tok);
}
size_t
type_len(enum type ty)
{
return tab_len[ty] ? tab_len[ty] : *(volatile size_t*)0x0;
}
void
marshal(struct ins *dst, struct token *tok)
{
struct token *tmp = 0x0;
struct token *ctx = 0x0;
dst += tok->len - 1;
while (tok) {
tmp = tab_comp[tok->id](&dst, tok, ctx);
if (!tmp) break;
if (tmp->siz < tok->siz) {
tmp->up = tok;
ctx = tok;
} else if (tmp->siz < ctx->siz) {
tmp->up = ctx;
} else if (ctx->siz < tmp->siz) {
ctx = tok;
}
tok = tmp;
}
}
int
pat_marshal(struct pattern *pat, struct token *tok)
{
pat->prog = calloc(tok->len, sizeof *pat->prog);
if (!pat->prog) return ENOMEM;
marshal(pat->prog, tok);
return 0;
}