-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_eval.c
619 lines (548 loc) · 12.5 KB
/
parse_eval.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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#include <stdint.h>
#ifdef DEBUGGING
#include <stdio.h>
#endif
#define STACK_SIZE 20
/*
Grammar:
VAR = 't' | 'a' | 'b' | 'c'
NUM = { '0'..'9' }
exp0 = { ':' VAR = exp6 ';' } exp6
exp6 = exp7 { '|' exp7}
exp7 = exp8 { '^' exp8}
exp8 = exp9 { '&' exp9}
exp9 = exp10 ('==' | '!=') exp10
exp10 = exp11 ('<' | '<=' | '>' | '>=') exp11
exp11 = exp12 ('<<' | '>>') exp12
exp12 = exp13 { ('+' | '-') exp13 }
exp13 = exp15 { ('*' | '/' | '%') exp15 }
exp15 = [ '+' | '-' | '!' | '~' ] exp16
exp16 =
VAR
| NUM
| '(' exp6 ')'
*/
enum compiled_exp_op {
OP_CONSTANT, OP_VAR,
OP_NEGATE, OP_BOOLEAN_NOT, OP_LOGICAL_NOT,
OP_MULTIPLY, OP_DIVIDE, OP_MODULO,
OP_PLUS, OP_MINUS,
OP_LEFT_SHIFT, OP_RIGHT_SHIFT,
OP_LESS_THAN, OP_LESS_THAN_OR_EQUAL, OP_GREATER_THAN, OP_GREATER_THAN_OR_EQUAL,
OP_EQUAL, OP_NOT_EQUAL,
OP_LOGICAL_AND,
OP_LOGICAL_XOR,
OP_LOGICAL_OR,
OP_ASSIGN
};
enum compiled_exp_var {
VAR_T = 0, VAR_A = 1, VAR_B = 2, VAR_C = 3,
MAX_VAR
};
int32_t variable_values[MAX_VAR];
int32_t eval_stack[STACK_SIZE];
uint16_t eval_stack_pointer = 0;
static void
stack_push(int32_t val)
{
/*
ToDo: We could actually avoid this stack overflow check.
Instead, we could pre-compute the stack usage of a compiled expression.
And just check once at start of eval (or even check at compile) that no
overflow will occur.
*/
if (eval_stack_pointer < STACK_SIZE)
eval_stack[eval_stack_pointer++] = val;
}
static int32_t
stack_pop(void)
{
if (eval_stack_pointer > 0)
--eval_stack_pointer;
return eval_stack[eval_stack_pointer];
}
int32_t
eval_compiled(uint8_t compiled_exp[], uint16_t compiled_len, int32_t vars[])
{
uint16_t pc;
int32_t arg1, arg2;
pc = 0;
eval_stack_pointer = 0;
while (pc < compiled_len) {
uint8_t op = compiled_exp[pc++];
switch (op) {
case OP_CONSTANT:
stack_push((uint32_t)compiled_exp[pc] |
((uint32_t)compiled_exp[pc+1] << 8) |
((uint32_t)compiled_exp[pc+2] << 16) |
((uint32_t)compiled_exp[pc+3] << 24));
pc += 4;
break;
case OP_VAR:
stack_push(vars[compiled_exp[pc++]]);
break;
case OP_NEGATE:
stack_push(-stack_pop());
break;
case OP_BOOLEAN_NOT:
stack_push(!stack_pop());
break;
case OP_LOGICAL_NOT:
stack_push(~(uint32_t)stack_pop());
break;
case OP_MULTIPLY:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 * arg2);
break;
case OP_DIVIDE:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push((arg2 == 0) ? 0 : arg1 / arg2);
break;
case OP_MODULO:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push((arg2 == 0) ? 0 : arg1 % arg2);
break;
case OP_PLUS:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 + arg2);
break;
case OP_MINUS:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 - arg2);
break;
case OP_LEFT_SHIFT:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 << arg2);
break;
case OP_RIGHT_SHIFT:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 >> arg2);
break;
case OP_LESS_THAN:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 < arg2);
break;
case OP_LESS_THAN_OR_EQUAL:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 <= arg2);
break;
case OP_GREATER_THAN:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 > arg2);
break;
case OP_GREATER_THAN_OR_EQUAL:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 >= arg2);
break;
case OP_EQUAL:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 == arg2);
break;
case OP_NOT_EQUAL:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 != arg2);
break;
case OP_LOGICAL_AND:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 & arg2);
break;
case OP_LOGICAL_XOR:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 ^ arg2);
break;
case OP_LOGICAL_OR:
arg2 = stack_pop();
arg1 = stack_pop();
stack_push(arg1 | arg2);
break;
case OP_ASSIGN:
vars[compiled_exp[pc++]] = stack_pop();
break;
default:
/* Should not happen... */
#ifdef DEBUGGING
fprintf(stderr, "ERROR unknown op: %u\n", (unsigned)op);
#endif
;
}
}
return stack_pop();
}
const char *exp_to_parse;
uint8_t *compile_next_loc;
uint16_t compile_remain_loc;
static void parse_error(void);
void
emit_op(uint8_t op, int32_t arg)
{
if (compile_remain_loc < 1) {
parse_error();
return;
}
*compile_next_loc++ = op;
--compile_remain_loc;
if (op == OP_VAR || op == OP_ASSIGN) {
if (compile_remain_loc < 1) {
parse_error();
return;
}
*compile_next_loc++ = (uint8_t)arg;
--compile_remain_loc;
} else if (op == OP_CONSTANT) {
if (compile_remain_loc < 4) {
parse_error();
return;
}
*compile_next_loc++ = arg;
*compile_next_loc++ = (uint32_t)arg >> 8;
*compile_next_loc++ = (uint32_t)arg >> 16;
*compile_next_loc++ = (uint32_t)arg >> 24;
compile_remain_loc -= 4;
}
}
uint8_t current_token;
int32_t current_token_value;
uint8_t got_parser_error;
const char *parser_error_location;
void
next_token(void)
{
const char *p = exp_to_parse;
char c;
/* Skip white space. */
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
++p;
c = *p++;
#ifdef DEBUGGING
fprintf(stderr, " current char: %c%s\n", (c ? c : '\\'), (c ? "" : "0"));
#endif
if (c >= '0' && c <= '9') {
/* Number. */
uint32_t val = c - '0';
while (*p >= '0' && *p <= '9')
val = (val * 10) + (uint32_t)(*p++ - '0');
current_token = '0';
current_token_value = val;
} else if (c == '=') {
if (*p == '=') { /* Comparison == */
++p;
current_token = '=';
} else { /* Assignment = */
current_token = 'A';
}
} else if (c == '<') {
if (*p == '=') { /* Under-or-equal <= */
++p;
current_token = 'U';
} else if (*p == '<') { /* Left shift << */
++p;
current_token = 'L';
} else { /* Less-than < */
current_token = '<';
}
} else if (c == '>') {
if (*p == '=') { /* Greater-or-equal >= */
++p;
current_token = 'G';
} else if (*p == '>') { /* Right shift >> */
++p;
current_token = 'R';
} else { /* Greater-than > */
current_token = '>';
}
} else if (c == '!') {
if (*p == '=') { /* Not equals != */
++p;
current_token = '#';
} else { /* Boolean not ! */
current_token = '!';
}
} else {
switch (c) {
case '\0': /* End-of-file. */
case '+':
case '-':
case ':':
case ';':
case '|':
case '^':
case '&':
case '*':
case '/':
case '%':
case '!':
case '~':
case '(':
case ')':
current_token = c;
break;
case 't':
current_token = 'V';
current_token_value = VAR_T;
break;
case 'a':
current_token = 'V';
current_token_value = VAR_A;
break;
case 'b':
current_token = 'V';
current_token_value = VAR_B;
break;
case 'c':
current_token = 'V';
current_token_value = VAR_C;
break;
default:
/* Parse error, unknown character. */
current_token = 'E';
break;
}
}
exp_to_parse = p;
#ifdef DEBUGGING
fprintf(stderr, "NEXT TOKEN: %c%s\n",
(current_token ? current_token : '('),
(current_token ? "" : "EOF)"));
#endif
}
static void
parse_error(void)
{
if (!got_parser_error) {
got_parser_error = 1;
parser_error_location = exp_to_parse;
}
}
static uint8_t
parse_accept(uint8_t tok)
{
if (current_token == tok) {
if (current_token != '\0') /* Don't move past EOF */
next_token();
return 1;
}
return 0;
}
static uint8_t
parse_expect(uint8_t tok)
{
if (parse_accept(tok))
return 1;
parse_error();
return 0;
}
void parse_exp6(void);
void
parse_exp16(void)
{
int32_t tok_val = current_token_value;
if (parse_accept('V')) {
emit_op(OP_VAR, tok_val);
} else if (parse_accept('0')) {
emit_op(OP_CONSTANT, tok_val);
} else {
if (parse_expect('(')) {
parse_exp6();
parse_expect(')');
}
}
}
void
parse_exp15(void)
{
if (parse_accept('+')) {
parse_exp16();
/* Unary '+' is a no-operation, like + 5. */
} else if (parse_accept('-')) {
parse_exp16();
emit_op(OP_NEGATE, 0);
} else if (parse_accept('!')) {
parse_exp16();
emit_op(OP_BOOLEAN_NOT, 0);
} else if (parse_accept('~')) {
parse_exp16();
emit_op(OP_LOGICAL_NOT, 0);
} else {
parse_exp16();
}
}
void
parse_exp13(void)
{
parse_exp15();
while (current_token == '*' || current_token == '/' || current_token == '%') {
uint8_t op = (current_token == '*' ? OP_MULTIPLY :
(current_token == '/' ? OP_DIVIDE : OP_MODULO));
next_token();
parse_exp15();
emit_op(op, 0);
}
}
void
parse_exp12(void)
{
parse_exp13();
while (current_token == '+' || current_token == '-') {
uint8_t op = (current_token == '+' ? OP_PLUS : OP_MINUS);
next_token();
parse_exp13();
emit_op(op, 0);
}
}
void
parse_exp11(void)
{
parse_exp12();
if (current_token == 'L' || current_token == 'R') {
uint8_t op = (current_token == 'L' ? OP_LEFT_SHIFT : OP_RIGHT_SHIFT);
next_token();
parse_exp12();
emit_op(op, 0);
}
}
void
parse_exp10(void)
{
parse_exp11();
if (current_token == '<' || current_token == 'U' ||
current_token == '>' || current_token == 'G') {
uint8_t op;
switch (current_token) {
case '<': op = OP_LESS_THAN; break;
case 'U': op = OP_LESS_THAN_OR_EQUAL; break;
case '>': op = OP_GREATER_THAN; break;
default: op = OP_GREATER_THAN_OR_EQUAL; break;
}
next_token();
parse_exp11();
emit_op(op, 0);
}
}
void
parse_exp9(void)
{
parse_exp10();
if (current_token == '=' || current_token == '#') {
uint8_t op = (current_token == '=' ? OP_EQUAL : OP_NOT_EQUAL);
next_token();
parse_exp10();
emit_op(op, 0);
}
}
void
parse_exp8(void)
{
parse_exp9();
while (current_token == '&') {
next_token();
parse_exp9();
emit_op(OP_LOGICAL_AND, 0);
}
}
void
parse_exp7(void)
{
parse_exp8();
while (current_token == '^') {
next_token();
parse_exp8();
emit_op(OP_LOGICAL_XOR, 0);
}
}
void
parse_exp6(void)
{
parse_exp7();
while (current_token == '|') {
next_token();
parse_exp7();
emit_op(OP_LOGICAL_OR, 0);
}
}
void
parse_exp0(void)
{
while (parse_accept(':')) {
uint8_t tok_val = current_token_value;
if (parse_expect('V')) {
if (parse_expect('A')) {
parse_exp6();
if (parse_expect(';'))
emit_op(OP_ASSIGN, tok_val);
}
}
}
parse_exp6();
}
void
start_parse()
{
next_token();
parse_exp0();
parse_expect('\0');
}
uint16_t
parse_exp(const char *exp, uint8_t *buffer, uint16_t buffer_len,
uint16_t *error_pos)
{
exp_to_parse = exp;
compile_next_loc = buffer;
compile_remain_loc = buffer_len;
got_parser_error = 0;
start_parse();
if (got_parser_error) {
if (error_pos)
*error_pos = parser_error_location - exp;
return 0;
} else {
if (error_pos)
*error_pos = 0;
return compile_next_loc - buffer;
}
}
#ifdef TESTING
#include <stdlib.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
int i;
for (i = 1; i < argc; ++i) {
uint16_t error_pos;
uint8_t compiled_code[256];
uint16_t compiled_size;
int32_t t;
printf("Parsing: %s\n", argv[i]);
compiled_size = parse_exp(argv[i], compiled_code, sizeof(compiled_code), &error_pos);
if (!compiled_size) {
printf("Parse error at position %u\n", (unsigned)error_pos);
continue;
}
for (t = 0; t < 10; ++t) {
int32_t val;
uint16_t i;
variable_values[0] = t;
for (i = 1; i < MAX_VAR; ++i)
variable_values[i] = 0;
val = eval_compiled(compiled_code, compiled_size, variable_values);
printf("%2ld %10ld\n", (long)t, (long)val);
}
}
return 0;
}
#endif