bel 0.3.0.beta1-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,91 @@
1
+ /*
2
+ * Parses BEL terms.
3
+ */
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+ #include <stdio.h>
7
+ #include "bel-parser.h"
8
+
9
+ #define LINE_CHAR_LEN 1024 * 32 // 32 kilobytes
10
+
11
+ int main(int argc, char *argv[]) {
12
+ FILE *input;
13
+ int len;
14
+ char line[LINE_CHAR_LEN];
15
+ int type;
16
+ int print;
17
+ int verbose;
18
+ bel_ast* tree;
19
+ bel_token_list* token_list;
20
+
21
+ if (argc == 2) {
22
+ type = 0;
23
+ if (strcmp(argv[1], "tk") == 0) {
24
+ type = 1;
25
+ input = stdin;
26
+ } else {
27
+ input = fopen(argv[1], "rb");
28
+ }
29
+ } else if (argc == 3) {
30
+ if (strcmp(argv[1], "tk") == 0) {
31
+ type = 1;
32
+ } else {
33
+ type = 0;
34
+ }
35
+ input = fopen(argv[2], "rb");
36
+ } else {
37
+ type = 0;
38
+ input = stdin;
39
+ }
40
+
41
+ print = 0;
42
+ if (getenv("AST_PRINT") != NULL) {
43
+ print = 1;
44
+ }
45
+
46
+ verbose = 0;
47
+ if (getenv("AST_VERBOSE") != NULL) {
48
+ verbose = 1;
49
+ }
50
+
51
+ if (argc > 1) {
52
+ }
53
+
54
+ while (fgets(line, LINE_CHAR_LEN, input) != NULL) {
55
+ // force newline if statement ends with closed paren; aids parser
56
+ len = strlen(line);
57
+ if (line[len - 1] == ')') {
58
+ line[len - 1] = '\n';
59
+ }
60
+
61
+ if (verbose) {
62
+ fprintf(stdout, "parsing line -> %s\n", line);
63
+ }
64
+
65
+ if (type == 0) {
66
+ if (verbose) {
67
+ fprintf(stdout, "using default statement parser\n");
68
+ }
69
+ tree = bel_parse_statement(line);
70
+
71
+ if (!tree->root) {
72
+ fprintf(stderr, "parse failed\n");
73
+ }
74
+
75
+ if (print) {
76
+ bel_print_ast(tree);
77
+ }
78
+ bel_free_ast(tree);
79
+ } else if (type == 1) {
80
+ if (verbose) {
81
+ fprintf(stdout, "using lookahead term parser\n");
82
+ }
83
+ token_list = bel_tokenize_term(line);
84
+ bel_print_token_list(token_list);
85
+ free_bel_token_list(token_list);
86
+ }
87
+ }
88
+ fclose(input);
89
+ return 0;
90
+ }
91
+ // vim: ft=c sw=4 ts=4 sts=4 expandtab
@@ -0,0 +1,13 @@
1
+ #include "bel-ast.h"
2
+ #include "bel-token.h"
3
+
4
+ #ifndef _BEL_PARSER_H
5
+ #define _BEL_PARSER_H
6
+
7
+ bel_ast* bel_parse_term(char* line);
8
+
9
+ bel_ast* bel_parse_statement(char* line);
10
+
11
+ bel_token_list* bel_tokenize_term(char* line);
12
+
13
+ #endif /* not defined _BEL_PARSER_H */
@@ -0,0 +1,161 @@
1
+ #include <stdlib.h>
2
+ #include <stdio.h>
3
+ #include <string.h>
4
+ #include "bel-token.h"
5
+
6
+ bel_token* bel_new_token(bel_token_type type, char* input, char* ts, char* te) {
7
+ bel_token *new_token;
8
+ int length;
9
+ char *match_copy;
10
+
11
+ new_token = malloc(sizeof(bel_token));
12
+ new_token->type = type;
13
+ new_token->pos_start = ts - input;
14
+ new_token->pos_end = te - input;
15
+
16
+ length = te - ts;
17
+ match_copy = malloc(length + 1);
18
+ strncpy(match_copy, ts, length);
19
+ match_copy[length] = '\0';
20
+ new_token->value = match_copy;
21
+ return new_token;
22
+ };
23
+
24
+ bel_token_list* bel_new_token_list(int length) {
25
+ bel_token_list *list;
26
+ list = malloc(sizeof(bel_token_list));
27
+ list->length = length;
28
+ list->tokens = calloc(length, sizeof(bel_token));
29
+ return list;
30
+ };
31
+
32
+ bel_token_iterator* bel_new_token_iterator(bel_token_list *list) {
33
+ bel_token_iterator *iterator;
34
+ bel_token *tokens;
35
+
36
+ if (!list) {
37
+ return NULL;
38
+ }
39
+
40
+ tokens = list->tokens;
41
+
42
+ iterator = malloc(sizeof(bel_token_iterator));
43
+ iterator->index = 0;
44
+ iterator->list = list;
45
+ iterator->current_token = &tokens[0];
46
+ return iterator;
47
+ };
48
+
49
+ bel_token* bel_token_iterator_get(bel_token_iterator *iterator) {
50
+ if (!iterator) {
51
+ return NULL;
52
+ }
53
+ return iterator->current_token;
54
+ };
55
+
56
+ void bel_token_iterator_next(bel_token_iterator *iterator) {
57
+ bel_token *tokens;
58
+
59
+ if (!iterator || !iterator->list) {
60
+ return;
61
+ }
62
+
63
+ tokens = iterator->list->tokens;
64
+ iterator->current_token = &tokens[++iterator->index];
65
+ };
66
+
67
+ int bel_token_iterator_end(bel_token_iterator *iterator) {
68
+ if (!iterator || !(iterator->list)) {
69
+ return 1;
70
+ }
71
+ return (iterator->index < iterator->list->length) ? 0 : 1;
72
+ };
73
+
74
+ void bel_print_token(bel_token* token) {
75
+ if (!token) {
76
+ return;
77
+ }
78
+
79
+ switch(token->type) {
80
+ case IDENT:
81
+ fprintf(stdout, "IDENT(\"%s\", %d, %d) ", token->value, token->pos_start, token->pos_end);
82
+ break;
83
+ case STRING:
84
+ fprintf(stdout, "STRING(\"%s\", %d, %d) ", token->value, token->pos_start, token->pos_end);
85
+ break;
86
+ case O_PAREN:
87
+ fprintf(stdout, "O_PAREN(\"%s\", %d, %d) ", token->value, token->pos_start, token->pos_end);
88
+ break;
89
+ case C_PAREN:
90
+ fprintf(stdout, "C_PAREN(\"%s\", %d, %d) ", token->value, token->pos_start, token->pos_end);
91
+ break;
92
+ case COLON:
93
+ fprintf(stdout, "COLON(\"%s\", %d, %d) ", token->value, token->pos_start, token->pos_end);
94
+ break;
95
+ case COMMA:
96
+ fprintf(stdout, "COMMA(\"%s\", %d, %d) ", token->value, token->pos_start, token->pos_end);
97
+ break;
98
+ case SPACES:
99
+ fprintf(stdout, "SPACES(\"%s\", %d, %d) ", token->value, token->pos_start, token->pos_end);
100
+ break;
101
+ };
102
+ };
103
+
104
+ void bel_print_token_list(bel_token_list* token_list) {
105
+ bel_token *tokens;
106
+ bel_token *next;
107
+ int token_i;
108
+ int list_length;
109
+
110
+ if (!token_list) {
111
+ return;
112
+ }
113
+
114
+ tokens = token_list->tokens;
115
+ list_length = token_list->length;
116
+ for (token_i = 0; token_i < list_length; token_i++) {
117
+ next = &tokens[token_i];
118
+ bel_print_token(next);
119
+ }
120
+ fprintf(stdout, "\n");
121
+ };
122
+
123
+ void free_bel_token(bel_token* token) {
124
+ if (!token) {
125
+ return;
126
+ }
127
+ if (token->value) {
128
+ free(token->value);
129
+ }
130
+ };
131
+
132
+ void free_bel_token_list(bel_token_list* token_list) {
133
+ int token_i;
134
+ int list_length;
135
+ bel_token *next;
136
+
137
+ if (!token_list) {
138
+ return;
139
+ }
140
+
141
+ list_length = token_list->length;
142
+ for (token_i = 0; token_i < list_length; token_i++) {
143
+ next = &token_list->tokens[token_i];
144
+ if (!next && next->value) {
145
+ free(next->value);
146
+ }
147
+ // FIXME Memory leak, cannot free, yields, "free(): invalid size"
148
+ // free(next);
149
+ }
150
+
151
+ free(token_list->tokens);
152
+ free(token_list);
153
+ };
154
+
155
+ void free_bel_token_iterator(bel_token_iterator *iterator) {
156
+ if (!iterator) {
157
+ return;
158
+ }
159
+ free(iterator);
160
+ };
161
+
@@ -0,0 +1,58 @@
1
+ #include <stdlib.h>
2
+ #include <string.h>
3
+
4
+ #ifndef _BEL_TOKEN_H
5
+ #define _BEL_TOKEN_H
6
+
7
+ typedef enum {
8
+ IDENT,
9
+ STRING,
10
+ O_PAREN,
11
+ C_PAREN,
12
+ COLON,
13
+ COMMA,
14
+ SPACES
15
+ } bel_token_type;
16
+
17
+ typedef struct {
18
+ bel_token_type type;
19
+ int pos_start;
20
+ int pos_end;
21
+ char *value;
22
+ } bel_token;
23
+
24
+ typedef struct {
25
+ int length;
26
+ bel_token *tokens;
27
+ } bel_token_list;
28
+
29
+ typedef struct {
30
+ int index;
31
+ bel_token_list *list;
32
+ bel_token *current_token;
33
+ } bel_token_iterator;
34
+
35
+ bel_token* bel_new_token(bel_token_type type, char* input, char* ts, char* te);
36
+
37
+ bel_token_list* bel_new_token_list(int length);
38
+
39
+ bel_token_iterator* bel_new_token_iterator(bel_token_list *list);
40
+
41
+ bel_token* bel_token_iterator_get(bel_token_iterator *token_iterator);
42
+
43
+ void bel_token_iterator_next(bel_token_iterator *token_iterator);
44
+
45
+ int bel_token_iterator_end(bel_token_iterator *token_iterator);
46
+
47
+ void bel_print_token(bel_token* token);
48
+
49
+ void bel_print_token_list(bel_token_list* token_list);
50
+
51
+ void free_bel_token(bel_token* token);
52
+
53
+ void free_bel_token_list(bel_token_list* token_list);
54
+
55
+ void free_bel_token_iterator(bel_token_iterator *token_iterator);
56
+
57
+ #endif /* not defined _BEL_TOKEN_H */
58
+
@@ -0,0 +1,391 @@
1
+ #include <stdlib.h>
2
+ #include <string.h>
3
+ #include <stdio.h>
4
+ #include "bel-token.h"
5
+
6
+ static const int set_start = 1;
7
+ static const int set_first_final = 1;
8
+ static const int set_error = 0;
9
+ static const int set_en_term = 1;
10
+ bel_token_list* bel_tokenize_term(char* line) {
11
+ int cs;
12
+ char *p;
13
+ char *pe;
14
+ char *ts;
15
+ char *te;
16
+ char *eof;
17
+ int token_i;
18
+ bel_token_list *token_list;
19
+ bel_token *tokens;
20
+ bel_token *new_token;
21
+
22
+ p = line;
23
+ pe = line + strlen(line);
24
+ eof = pe;
25
+ token_i = 0;
26
+ token_list = bel_new_token_list(256);
27
+ tokens = token_list->tokens;
28
+
29
+
30
+ {
31
+ cs = set_start;
32
+ ts = 0;
33
+ te = 0;
34
+ }
35
+
36
+ {
37
+ if ( p == pe )
38
+ goto _test_eof;
39
+
40
+ switch ( cs ) {
41
+ case 1:
42
+ goto st_case_1;
43
+ case 0:
44
+ goto st_case_0;
45
+ case 2:
46
+ goto st_case_2;
47
+ case 3:
48
+ goto st_case_3;
49
+ case 4:
50
+ goto st_case_4;
51
+ case 5:
52
+ goto st_case_5;
53
+
54
+ }
55
+ goto st_out;
56
+ ctr3:
57
+ {{te = p+1;
58
+ {new_token = bel_new_token(O_PAREN, line, ts, te);
59
+ tokens[token_i++] = *new_token;
60
+ }
61
+ }}
62
+
63
+
64
+ goto st1;
65
+ ctr4:
66
+ {{te = p+1;
67
+ {new_token = bel_new_token(C_PAREN, line, ts, te);
68
+ tokens[token_i++] = *new_token;
69
+ }
70
+ }}
71
+
72
+
73
+ goto st1;
74
+ ctr5:
75
+ {{te = p+1;
76
+ {new_token = bel_new_token(COMMA, line, ts, te);
77
+ tokens[token_i++] = *new_token;
78
+ }
79
+ }}
80
+
81
+
82
+ goto st1;
83
+ ctr7:
84
+ {{te = p+1;
85
+ {new_token = bel_new_token(COLON, line, ts, te);
86
+ tokens[token_i++] = *new_token;
87
+ }
88
+ }}
89
+
90
+
91
+ goto st1;
92
+ ctr8:
93
+ {{te = p;
94
+ p = p - 1;
95
+ {new_token = bel_new_token(SPACES, line, ts, te);
96
+ tokens[token_i++] = *new_token;
97
+ }
98
+ }}
99
+
100
+
101
+ goto st1;
102
+ ctr9:
103
+ {{te = p;
104
+ p = p - 1;
105
+ {new_token = bel_new_token(STRING, line, ts, te);
106
+ tokens[token_i++] = *new_token;
107
+ }
108
+ }}
109
+
110
+
111
+ goto st1;
112
+ ctr10:
113
+ {{te = p+1;
114
+ {new_token = bel_new_token(STRING, line, ts, te);
115
+ tokens[token_i++] = *new_token;
116
+ }
117
+ }}
118
+
119
+
120
+ goto st1;
121
+ ctr12:
122
+ {{te = p;
123
+ p = p - 1;
124
+ {new_token = bel_new_token(IDENT, line, ts, te);
125
+ tokens[token_i++] = *new_token;
126
+ }
127
+ }}
128
+
129
+
130
+ goto st1;
131
+ st1:
132
+ {{ts = 0;
133
+ }}
134
+
135
+ p+= 1;
136
+ if ( p == pe )
137
+ goto _test_eof1;
138
+
139
+ st_case_1:
140
+ {{ts = p;
141
+ }}
142
+ switch ( ( (*( p ))
143
+ ) ) {
144
+ case 32:
145
+ {
146
+ goto st2;
147
+ }
148
+ case 34:
149
+ {
150
+ goto st3;
151
+ }
152
+ case 40:
153
+ {
154
+ goto ctr3;
155
+ }
156
+ case 41:
157
+ {
158
+ goto ctr4;
159
+ }
160
+ case 44:
161
+ {
162
+ goto ctr5;
163
+ }
164
+ case 58:
165
+ {
166
+ goto ctr7;
167
+ }
168
+ case 95:
169
+ {
170
+ goto st5;
171
+ }
172
+
173
+ }
174
+ if ( ( (*( p ))
175
+ ) < 65 )
176
+ {
177
+ if ( 48 <= ( (*( p ))
178
+ ) && ( (*( p ))
179
+ ) <= 57 )
180
+ {
181
+ goto st5;
182
+ }
183
+
184
+ }
185
+ else if ( ( (*( p ))
186
+ ) > 90 )
187
+ {
188
+ if ( 97 <= ( (*( p ))
189
+ ) && ( (*( p ))
190
+ ) <= 122 )
191
+ {
192
+ goto st5;
193
+ }
194
+
195
+ }
196
+ else
197
+ {
198
+ goto st5;
199
+ }
200
+
201
+ {
202
+ goto st0;
203
+ }
204
+ st_case_0:
205
+ st0:
206
+ cs = 0;
207
+ goto _out;
208
+ st2:
209
+ p+= 1;
210
+ if ( p == pe )
211
+ goto _test_eof2;
212
+
213
+ st_case_2:
214
+ if ( ( (*( p ))
215
+ ) == 32 )
216
+ {
217
+ goto st2;
218
+ }
219
+
220
+ if ( 33 <= ( (*( p ))
221
+ ) )
222
+ {
223
+ goto ctr8;
224
+ }
225
+
226
+ {
227
+ goto ctr8;
228
+ }
229
+ st3:
230
+ p+= 1;
231
+ if ( p == pe )
232
+ goto _test_eof3;
233
+
234
+ st_case_3:
235
+ switch ( ( (*( p ))
236
+ ) ) {
237
+ case 34:
238
+ {
239
+ goto ctr10;
240
+ }
241
+ case 92:
242
+ {
243
+ goto st4;
244
+ }
245
+
246
+ }
247
+ if ( ( (*( p ))
248
+ ) > 91 )
249
+ {
250
+ if ( 93 <= ( (*( p ))
251
+ ) )
252
+ {
253
+ goto st3;
254
+ }
255
+
256
+ }
257
+ else if ( ( (*( p ))
258
+ ) >= 35 )
259
+ {
260
+ goto st3;
261
+ }
262
+
263
+ {
264
+ goto st3;
265
+ }
266
+ st4:
267
+ p+= 1;
268
+ if ( p == pe )
269
+ goto _test_eof4;
270
+
271
+ st_case_4:
272
+ if ( ( (*( p ))
273
+ ) == 92 )
274
+ {
275
+ goto st4;
276
+ }
277
+
278
+ if ( 93 <= ( (*( p ))
279
+ ) )
280
+ {
281
+ goto st3;
282
+ }
283
+
284
+ {
285
+ goto st3;
286
+ }
287
+ st5:
288
+ p+= 1;
289
+ if ( p == pe )
290
+ goto _test_eof5;
291
+
292
+ st_case_5:
293
+ switch ( ( (*( p ))
294
+ ) ) {
295
+ case 95:
296
+ {
297
+ goto st5;
298
+ }
299
+ case 96:
300
+ {
301
+ goto ctr12;
302
+ }
303
+
304
+ }
305
+ if ( ( (*( p ))
306
+ ) < 65 )
307
+ {
308
+ if ( ( (*( p ))
309
+ ) > 57 )
310
+ {
311
+ {
312
+ goto ctr12;
313
+ }
314
+ }
315
+ else if ( ( (*( p ))
316
+ ) >= 48 )
317
+ {
318
+ goto st5;
319
+ }
320
+
321
+ }
322
+ else if ( ( (*( p ))
323
+ ) > 90 )
324
+ {
325
+ if ( ( (*( p ))
326
+ ) < 97 )
327
+ {
328
+ if ( ( (*( p ))
329
+ ) <= 94 )
330
+ {
331
+ goto ctr12;
332
+ }
333
+
334
+ }
335
+ else if ( ( (*( p ))
336
+ ) > 122 )
337
+ {
338
+ {
339
+ goto ctr12;
340
+ }
341
+ }
342
+ else
343
+ {
344
+ goto st5;
345
+ }
346
+
347
+ }
348
+ else
349
+ {
350
+ goto st5;
351
+ }
352
+
353
+ {
354
+ goto ctr12;
355
+ }
356
+ st_out:
357
+ _test_eof1: cs = 1;
358
+ goto _test_eof;
359
+ _test_eof2: cs = 2;
360
+ goto _test_eof;
361
+ _test_eof3: cs = 3;
362
+ goto _test_eof;
363
+ _test_eof4: cs = 4;
364
+ goto _test_eof;
365
+ _test_eof5: cs = 5;
366
+ goto _test_eof;
367
+
368
+ _test_eof: {}
369
+ if ( p == eof )
370
+ {
371
+ switch ( cs ) {
372
+ case 2:
373
+ goto ctr8;
374
+ case 3:
375
+ goto ctr9;
376
+ case 4:
377
+ goto ctr9;
378
+ case 5:
379
+ goto ctr12;
380
+
381
+ }
382
+ }
383
+
384
+
385
+ _out: {}
386
+ }
387
+ token_list->length = token_i;
388
+ return token_list;
389
+ };
390
+ // vim: ft=c sw=4 ts=4 sts=4 expandtab
391
+