-
Notifications
You must be signed in to change notification settings - Fork 4
/
combinator.c
302 lines (236 loc) · 5.92 KB
/
combinator.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
Copyright 2012 William Hart. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY William Hart ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL William Hart OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "combinator.h"
void exception(char * err)
{
fprintf(stderr, err);
abort();
}
ast_t * new_ast(tag_t typ, ast_t * child, ast_t * next)
{
ast_t * ast = malloc(sizeof(ast_t));
ast->typ = typ;
ast->child = child;
ast->next = next;
return ast;
}
char read1(input_t * in)
{
if (in->start < in->length)
return in->input[in->start++];
if (in->alloc == in->length)
{
in->input = realloc(in->input, in->alloc + 50);
in->alloc += 50;
}
in->start++;
return in->input[in->length++] = getchar();
}
void skip_whitespace(input_t * in)
{
char c;
while ((c = read1(in)) == ' ' || c == '\n' || c == '\t') ;
in->start--;
}
ast_t * match(input_t * in, tag_t tag, char * str)
{
int start = in->start;
int i = 0, len = strlen(str);
skip_whitespace(in);
while (i < len && str[i] == read1(in)) i++;
if (i != len)
{
in->start = start;
return NULL;
}
return new_ast(tag, NULL, NULL);
}
ast_t * expect(input_t * in, tag_t tag, char * str, char * err)
{
ast_t * ast;
if (!(ast = match(in, tag, str)))
exception(err);
return ast;
}
ast_t * parse(input_t * in, comb_t * c)
{
switch (c->type)
{
case C_COMB:
return c->clos->comb(in, c->clos->cl1, c->clos->cl2);
case C_MATCH:
return match(in, c->tag, c->str);
case C_EXPECT:
return expect(in, c->tag, c->s2.str, c->s2.err);
case C_LIT:
return c->lit(in);
case C_FORWARD:
return parse(in, *c->forward);
default:
printf("tag = %d\n", c->type);
exception("Unknown tag in parse()\n");
}
}
ast_t * comb_and(input_t * in, comb_t * c1, comb_t * c2)
{
ast_t * a1, * a2;
int start = in->start;
if ((a1 = parse(in, c1)) && (a2 = parse(in, c2)))
{
if (c1->tag != T_NULL)
{
if (c2->tag != T_NULL)
{
a1->next = a2;
return a1;
} else
return a1;
}
if (c2->tag != T_NULL)
return a2;
}
in->start = start;
return NULL;
}
ast_t * comb_or(input_t * in, comb_t * c1, comb_t * c2)
{
ast_t * a;
int start = in->start;
if (a = parse(in, c1))
return a;
in->start = start;
if (a = parse(in, c2))
return a;
in->start = start;
return NULL;
}
ast_t * comb_laloop(input_t * in, comb_t * c1, comb_t * c2)
{
ast_t * a, * b, * t, *op;
int start = in->start;
if (a = parse(in, c1))
{
start = in->start;
while (op = parse(in, c2))
{
b = parse(in, c1);
a->next = b;
op->child = a;
a = op;
start = in->start;
}
in->start = start;
return a;
}
in->start = start;
return NULL;
}
comb_t * Match(tag_t tag, char * str)
{
comb_t * c = malloc(sizeof(comb_t));
c->type = C_MATCH;
c->tag = tag;
c->str = str;
return c;
}
comb_t * Expect(tag_t tag, char * str, char * err)
{
comb_t * c = malloc(sizeof(comb_t));
c->type = C_EXPECT;
c->tag = tag;
c->s2.str = str;
c->s2.err = err;
return c;
}
comb_t * Comb(ast_t * (*comb)(input_t *, struct comb_t *, struct comb_t *),
comb_t * cl1, comb_t * cl2)
{
closure_t * clos = malloc(sizeof(closure_t));
clos->comb = comb;
clos->cl1 = cl1;
clos->cl2 = cl2;
comb_t * c = malloc(sizeof(comb_t));
c->type = C_COMB;
c->tag = T_COMB;
c->clos = clos;
return c;
}
#define str_insert(s, c) \
do { \
if (i == alloc) { \
s = realloc(s, alloc + 10); \
alloc += 10; \
} \
str[i++] = c; \
} while (0)
ast_t * integer(input_t * in)
{
int start = in->start;
char c;
int i = 0, alloc = 0;
char * str = NULL;
ast_t * a;
skip_whitespace(in);
if ((c = read1(in)) >= '1' && c <= '9')
{
str_insert(str, c);
start = in->start;
while (isdigit(c = read1(in)))
{
str_insert(str, c);
start = in->start;
}
in->start = start;
str_insert(str, '\0');
a = new_ast(T_INT, NULL, NULL);
a->l = atol(str);
free(str);
return a;
} else if (c == '0')
{
str_insert(str, c);
str_insert(str, '\0');
a = new_ast(T_INT, NULL, NULL);
a->l = atol(str);
free(str);
return a;
}
in->start = start;
return NULL;
}
comb_t * Integer()
{
comb_t * c = malloc(sizeof(comb_t));
c->type = C_LIT;
c->tag = T_INT;
c->lit = integer;
return c;
}
comb_t * forward(comb_t ** comb)
{
comb_t * c = malloc(sizeof(comb_t));
c->type = C_FORWARD;
c->tag = T_FORWARD;
c->forward = comb;
return c;
}