-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
603 lines (568 loc) · 15.9 KB
/
parse.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "plumber.h"
enum {
SPACE,
STRING,
SEEK,
COMMENT,
LEX_START,
LEX_FLOAT,
LEX_FLOAT_DOT,
LEX_FLOAT_POSTDOT,
LEX_STRING,
LEX_POS,
LEX_NEG,
LEX_FUNC,
LEX_ERROR,
LEX_IGNORE,
LEX_DASH,
LEX_WORD
};
char * sporth_tokenizer(const char *str,
uint32_t size, uint32_t *pos)
{
char c;
uint32_t offset = 0;
int mode = SEEK;
uint32_t prev = *pos;
char *out;
int running = 1;
while(*pos < size && running) {
c = str[*pos];
switch(mode) {
case SEEK:
switch(c) {
case '(':
case ')':
case '\n':
case ' ':
mode = SPACE;
*pos = *pos + 1;
break;
case '\'':
case '"':
mode = STRING;
*pos = *pos + 1;
offset++;
break;
case '#':
mode = COMMENT;
break;
default:
*pos = *pos + 1;
offset++;
break;
}
break;
case SPACE:
switch(c) {
case ' ':
*pos = *pos + 1;
break;
default:
running = 0;
break;
}
break;
case STRING:
switch(c) {
case '\'':
case '"':
mode = SPACE;
*pos = *pos + 1;
offset++;
break;
default:
*pos = *pos + 1;
offset++;
break;
}
break;
case COMMENT:
/* *pos = *pos + 1; */
switch(c) {
case '\n':
mode = SPACE;
*pos = *pos + 1;
offset++;
break;
default:
*pos = *pos + 1;
/* this was added for sporth_tex so comments could
* be visible. */
offset++;
break;
}
break;
default:
break;
}
}
out = malloc(sizeof(char) * offset + 1);
strncpy(out, &str[prev], offset);
out[offset] = '\0';
return out;
}
int sporth_lexer(char *str, int32_t size)
{
char c;
int mode = LEX_START;
uint32_t pos = 0;
while(pos < size) {
c = str[pos++];
switch(mode) {
case LEX_START:
switch(c) {
case '-':
mode = LEX_DASH;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
mode = LEX_FLOAT;
break;
case '"':
case '\'':
mode = LEX_STRING;
break;
case '_':
mode = LEX_WORD;
break;
case '#':
mode = LEX_IGNORE;
break;
default:
mode = LEX_FUNC;
break;
}
break;
case LEX_DASH:
mode = LEX_FLOAT;
case LEX_FLOAT:
switch(c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
case '.':
mode = LEX_FLOAT_DOT;
break;
default:
return LEX_ERROR;
}
break;
case LEX_FLOAT_DOT:
switch(c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
mode = LEX_FLOAT_POSTDOT;
break;
default:
return LEX_ERROR;
}
break;
case LEX_FLOAT_POSTDOT:
switch(c) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
default:
return LEX_ERROR;
}
break;
case LEX_STRING:
break;
case LEX_WORD:
break;
case LEX_FUNC:
break;
case LEX_IGNORE:
break;
default:
return LEX_ERROR;
}
}
switch(mode) {
case LEX_FLOAT:
case LEX_FLOAT_DOT:
case LEX_FLOAT_POSTDOT:
return SPORTH_FLOAT;
case LEX_STRING:
return SPORTH_STRING;
case LEX_WORD:
return SPORTH_WORD;
case LEX_IGNORE:
return SPORTH_IGNORE;
case LEX_DASH:
case LEX_FUNC:
return SPORTH_FUNC;
case LEX_START:
if(size == 0) {
return SPORTH_IGNORE;
}
default:
return SPORTH_NOTOK;
}
return SPORTH_NOTOK;
}
/* This code is public domain -- Will Hartung 4/9/09 */
size_t sporth_getline(char **lineptr, size_t *n, FILE *stream) {
char *bufptr = NULL;
char *p = bufptr;
size_t size;
int c;
if (lineptr == NULL) {
return -1;
}
if (stream == NULL) {
return -1;
}
if (n == NULL) {
return -1;
}
bufptr = *lineptr;
size = *n;
c = fgetc(stream);
if (c == EOF) {
return -1;
}
if (bufptr == NULL) {
bufptr = malloc(128);
if (bufptr == NULL) {
return -1;
}
size = 128;
}
p = bufptr;
while(c != EOF) {
if ((p - bufptr) > (size - 1)) {
size = size + 128;
bufptr = realloc(bufptr, size);
if (bufptr == NULL) {
return -1;
}
}
/* allow for extended lines by replacing backslash-newline with
a space */
if (c == '\\') {
c = fgetc(stream);
if (c == '\n') {
c = ' ';
}
}
*p++ = c;
if (c == '\n') {
break;
}
c = fgetc(stream);
}
*lineptr = bufptr;
/* Some text editors do not insert a linebreak on the last line.
* For these cases, shift everything by 1.
*/
if(c == EOF) {
p = p + 1;
size += 1;
}
*p++ = '\0';
*n = size;
return p - bufptr - 1;
}
void sporth_print(sporth_data *sporth, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
int plumber_lexer(plumber_data *plumb, plumbing *pipes, char *out, uint32_t len)
{
char *tmp;
float flt = 0;
int rc;
switch(sporth_lexer(out, len)) {
case SPORTH_FLOAT:
#ifdef DEBUG_MODE
plumber_print(plumb, "%s is a float!\n", out);
#endif
flt = atof(out);
plumber_add_float(plumb, pipes, flt);
sporth_stack_push_float(&plumb->sporth.stack, flt);
break;
case SPORTH_STRING:
tmp = out;
tmp[len - 1] = '\0';
tmp++;
#ifdef DEBUG_MODE
plumber_print(plumb, "%s is a string!\n", out);
#endif
tmp = plumber_add_string(plumb, pipes, tmp);
sporth_stack_push_string(&plumb->sporth.stack, &tmp);
break;
case SPORTH_WORD:
/* A sporth word is like a string, except it looks like _this
* instead of "this" or 'this'.
* It saves a character, and it can make things look nicer.
* A sporth word has no spaces, hence the name.
*/
tmp = out;
/* don't truncate the last character here like the string */
tmp[len] = '\0';
tmp++;
#ifdef DEBUG_MODE
plumber_print(plumb, "%s is a word!\n", out);
#endif
tmp = plumber_add_string(plumb, pipes, tmp);
sporth_stack_push_string(&plumb->sporth.stack, &tmp);
break;
case SPORTH_FUNC:
#ifdef DEBUG_MODE
plumber_print(plumb, "%s is a function!\n", out);
#endif
rc = sporth_exec(&plumb->sporth, out);
if(rc == PLUMBER_NOTOK || rc == SPORTH_NOTOK) {
plumber_print(plumb, "%s returned an error.\n", out);
#ifdef DEBUG_MODE
plumber_print(plumb, "plumber_lexer: error with function %s\n", out);
#endif
plumb->sporth.stack.error++;
return PLUMBER_NOTOK;
}
break;
case SPORTH_IGNORE:
break;
default:
#ifdef DEBUG_MODE
plumber_print(plumb,"No idea what %s is!\n", out);
#endif
break;
}
return PLUMBER_OK;
}
int plumbing_parse(plumber_data *plumb, plumbing *pipes)
{
FILE *fp = plumb->fp;
char *line = NULL;
size_t length = 0;
long read;
char *out;
uint32_t pos = 0, len = 0;
int err = PLUMBER_OK;
plumb->mode = PLUMBER_CREATE;
/* save top level tmp variable. */
plumbing *top_tmp = plumb->tmp;
plumb->tmp = pipes;
while((read = sporth_getline(&line, &length, fp)) != -1
&& err == PLUMBER_OK) {
pos = 0;
len = 0;
while(pos < read - 1) {
out = sporth_tokenizer(line, (unsigned int)read - 1, &pos);
len = (unsigned int)strlen(out);
err = plumber_lexer(plumb, pipes, out, len);
free(out);
if(err == PLUMBER_NOTOK) break;
}
}
free(line);
/* restore tmp */
plumb->tmp = top_tmp;
return err;
}
int plumbing_parse_string(plumber_data *plumb, plumbing *pipes, const char *str)
{
char *out;
uint32_t pos = 0, len = 0;
uint32_t size = (unsigned int)strlen(str);
int err = PLUMBER_OK;
pos = 0;
len = 0;
plumb->mode = PLUMBER_CREATE;
/* save top level tmp variable. */
plumbing *top_tmp = plumb->tmp;
plumb->tmp = pipes;
while(pos < size) {
out = sporth_tokenizer(str, size, &pos);
len = (unsigned int)strlen(out);
err = plumber_lexer(plumb, pipes, out, len);
free(out);
if(err == PLUMBER_NOTOK) break;
}
if(plumb->stacksize > 0) {
if(plumb->stacksize != plumber_stack_pos(plumb)) {
plumber_print(plumb, "Stack overflow in evaluated code.\n");
err = PLUMBER_NOTOK;
}
}
/* restore tmp */
plumb->tmp = top_tmp;
return err;
}
int plumber_parse(plumber_data *plumb)
{
return plumbing_parse(plumb, plumb->pipes);
}
int plumber_reparse(plumber_data *plumb)
{
plumbing *pipes = plumb->tmp;
if(plumbing_parse(plumb, pipes) == PLUMBER_OK) {
#ifdef DEBUG_MODE
plumber_print(plumb, "Successful parse...\n");
plumber_print(plumb, "at stack position %d\n",
plumb->sporth.stack.pos);
plumber_print(plumb, "%d errors\n",
plumb->sporth.stack.error);
#endif
plumb->tmp = pipes;
} else {
plumb->tmp = pipes;
return PLUMBER_NOTOK;
}
return PLUMBER_OK;
}
int plumber_reparse_string(plumber_data *plumb, char *str)
{
plumbing *pipes = plumb->tmp;
if(plumbing_parse_string(plumb, pipes, str) == PLUMBER_OK) {
#ifdef DEBUG_MODE
plumber_print(plumb, "Successful parse...\n");
plumber_print(plumb, "at stack position %d\n",
plumb->sporth.stack.pos);
plumber_print(plumb, "%d errors\n",
plumb->sporth.stack.error);
#endif
plumb->tmp = pipes;
} else {
plumb->tmp = pipes;
return PLUMBER_NOTOK;
}
return PLUMBER_OK;
}
int plumber_swap(plumber_data *plumb, int error)
{
if(error == PLUMBER_NOTOK) {
plumber_print(plumb, "Did not recompile...\n");
plumbing_compute(plumb, plumb->tmp, PLUMBER_DESTROY);
plumbing_destroy(plumb->tmp);
sporth_stack_init(&plumb->sporth.stack);
plumber_ftmap_destroy(plumb);
plumb->ftmap = plumb->ftold;
plumb->current_pipe = (plumb->current_pipe == 0) ? 1 : 0;
if(plumb->current_pipe == 1) {
#ifdef DEBUG_MODE
plumber_print(plumb, "Reverting to alt\n");
#endif
plumb->pipes = &plumb->alt;
} else {
#ifdef DEBUG_MODE
plumber_print(plumb, "Reverting to main\n");
#endif
plumb->pipes = &plumb->main;
}
plumb->sp->pos = 0;
} else {
#ifdef DEBUG_MODE
plumber_print(plumb, "Recompiling...\n");
#endif
plumbing_compute(plumb, plumb->pipes, PLUMBER_DESTROY);
plumbing_destroy(plumb->pipes);
plumb->ftmap = plumb->ftold;
plumber_ftmap_destroy(plumb);
plumb->ftmap = plumb->ftnew;
plumb->pipes = plumb->tmp;
plumb->sp->pos = 0;
plumbing_compute(plumb, plumb->pipes, PLUMBER_INIT);
}
return PLUMBER_OK;
}
int plumber_recompile(plumber_data *plumb)
{
int error;
plumber_reinit(plumb);
error = plumber_reparse(plumb);
plumber_swap(plumb, error);
return PLUMBER_OK;
}
int plumber_recompile_string(plumber_data *plumb, char *str)
{
int error;
#ifdef DEBUG_MODE
plumber_print(plumb, "** Attempting to compile string '%s' **\n", str);
#endif
/* file pointer needs to be NULL for reinit to work with strings */
plumb->fp = NULL;
plumber_reinit(plumb);
error = plumber_reparse_string(plumb, str);
plumber_swap(plumb, error);
return PLUMBER_OK;
}
/* This version of plumber_recompile_string includes a callback function,
* to be called after it is reinitialized, but before the string
* is parsed. Useful for adding global ftables.
*/
int plumber_recompile_string_v2(plumber_data *plumb,
char *str,
void *ud,
int (*callback)(plumber_data *, void *))
{
int error;
#ifdef DEBUG_MODE
plumber_print(plumb, "** Attempting to compile string '%s' **\n", str);
#endif
/* file pointer needs to be NULL for reinit to work with strings */
plumb->fp = NULL;
plumber_reinit(plumb);
callback(plumb, ud);
error = plumber_reparse_string(plumb, str);
plumber_swap(plumb, error);
return PLUMBER_OK;
}
int plumber_recompile_v2(plumber_data *plumb,
void *ud,
int (*callback)(plumber_data *, void *))
{
int error;
plumber_reinit(plumb);
callback(plumb, ud);
error = plumber_reparse(plumb);
plumber_swap(plumb, error);
return PLUMBER_OK;
}
int plumber_parse_string(plumber_data *plumb, const char *str)
{
return plumbing_parse_string(plumb, plumb->pipes, str);
}