forked from rpgreview/dice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
742 lines (716 loc) · 25.3 KB
/
Copy pathparse.c
File metadata and controls
742 lines (716 loc) · 25.3 KB
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <termcap.h> // Needed for clear_screen
#include <errno.h>
#include <omp.h>
#include "parse.h"
#include "roll-engine.h"
static const struct cmd_map commands[] = {
{ quit, { "quit" } },
{ clear, { "clear" } },
};
#define NUMBER_OF_DEFINED_COMMANDS 2
#define CMD_MAX_STR_LEN 5
void clear_screen() {
char buf[1024];
tgetent(buf, getenv("TERM"));
char *str;
str = tgetstr("cl", NULL);
fputs(str, stdout);
}
void token_list_init(struct token *t, const size_t len) {
int toknum;
#pragma omp for schedule(static) private(toknum) nowait
for(toknum = 0; toknum < len; ++toknum) {
t[toknum].type = none;
t[toknum].number = 0;
t[toknum].op = '?';
t[toknum].cmd = -1;
}
}
int lex(struct token *t, int *tokens_found, const char *buf, const size_t len) {
int charnum = 0;
*tokens_found = 0;
while(*(buf + charnum) != '\0' && charnum <= len) {
if(isspace(*(buf + charnum))) {
++charnum;
} else if(*(buf + charnum) == '#') { //comment detected
break;
} else {
switch(*(buf + charnum)) {
case 'd': case 'D':
{
t[*tokens_found].type = dice_operator;
t[*tokens_found].op = *(buf + charnum);
++charnum;
}
break;
case '+': case '-':
{
t[*tokens_found].type = additive_operator;
t[*tokens_found].op = *(buf + charnum);
++charnum;
}
break;
case 'x': case 'X':
{
t[*tokens_found].type = rep_operator;
t[*tokens_found].op = *(buf + charnum);
++charnum;
}
break;
case '!':
{
t[*tokens_found].type = explode_operator;
t[*tokens_found].op = *(buf + charnum);
++charnum;
}
break;
case 't': case 'T': case '>':
{
t[*tokens_found].type = threshold_operator;
t[*tokens_found].op = *(buf + charnum);
++charnum;
}
break;
case 'k': case 'K':
{
t[*tokens_found].type = keep_operator;
t[*tokens_found].op = *(buf + charnum);
++charnum;
}
break;
case ';':
{
t[*tokens_found].type = statement_delimiter;
t[*tokens_found].op = *(buf + charnum);
++charnum;
}
break;
default:
{
char *tok_str;
int offset = charnum;
if(isdigit(*(buf + charnum))) {
tok_str = malloc((LONG_MAX_STR_LEN + 1)*sizeof(char));
if(!tok_str) {
fprintf(stderr, "Error allocating memory.\n");
exit(1);
}
memset(tok_str, 0, LONG_MAX_STR_LEN + 1);
while(isdigit(*(buf + charnum)) && charnum - offset < LONG_MAX_STR_LEN) {
tok_str[charnum - offset] = *(buf + charnum);
++charnum;
}
if(charnum - offset >= LONG_MAX_STR_LEN && isdigit(*(buf + charnum))) {
printf("Invalid numeric input detected. The maximum number allowed is %ld (LONG_MAX).\n", LONG_MAX);
free(tok_str);
return 1;
}
tok_str[charnum - offset] = '\0';
errno = 0;
long num = strtol(tok_str, NULL, 10);
if((errno == ERANGE && (num == LONG_MAX || num == LONG_MIN))
|| (errno != 0 && num == 0)) {
printf("Error %d (%s) converting string '%s' to number.\n", errno, strerror(errno), tok_str);
return 1;
}
t[*tokens_found].type = number;
t[*tokens_found].number = num;
} else if(isalpha(*(buf + charnum))) {
int numchars = 0;
while(isalpha(*(buf + charnum))) {
++numchars;
++charnum;
}
tok_str = malloc((numchars + 1)*sizeof(char));
if(!tok_str) {
fprintf(stderr, "Error allocating memory.\n");
exit(1);
}
memset(tok_str, 0, numchars + 1);
charnum = offset;
while(isalpha(*(buf + charnum)) && charnum - offset < numchars) {
tok_str[charnum - offset] = *(buf + charnum);
++charnum;
}
tok_str[charnum - offset] = '\0';
t[*tokens_found].type = command;
int cmd_num = 0;
bool cmd_found = false;
int max_str_len = CMD_MAX_STR_LEN >= numchars ? CMD_MAX_STR_LEN : numchars;
while(cmd_num < NUMBER_OF_DEFINED_COMMANDS) {
if(0 == strncmp(tok_str, commands[cmd_num].cmd_str, max_str_len)) {
t[*tokens_found].cmd = commands[cmd_num].cmd_code;
cmd_found = true;
break;
}
++cmd_num;
}
if(!cmd_found) {
printf("Unknown command: %s\n", tok_str);
return 1;
}
} else {
printf("Unknown token detected: %c\n", *(buf + charnum));
return 1;
}
if(tok_str) {
free(tok_str);
tok_str = NULL;
}
}
}
(*tokens_found)++;
}
}
t[*tokens_found].type = eol;
++(*tokens_found);
return 0;
}
void print_state_name(const state_t s) {
switch(s) {
case error:
printf("error");
break;
case start:
printf("start");
break;
case decide_reps_or_rolls:
printf("decide_reps_or_rolls");
break;
case want_number_of_sides:
printf("want_number_of_sides");
break;
case check_number_of_dice:
printf("check_number_of_dice");
break;
case want_roll:
printf("want_roll");
break;
case check_dice_operator:
printf("check_dice_operator");
break;
case check_modifiers_or_more_rolls:
printf("check_modifiers_or_more_rolls");
break;
case check_more_rolls:
printf("check_more_rolls");
break;
case check_end:
printf("check_end");
break;
case finish:
printf("finish");
break;
default:
printf("undefined");
}
}
void print_token_type(const token_t type) {
switch(type) {
case none:
printf("none");
break;
case number:
printf("number");
break;
case dice_operator:
printf("dice_operator");
break;
case rep_operator:
printf("rep_operator");
break;
case additive_operator:
printf("additive_operator");
break;
case explode_operator:
printf("explode_operator");
break;
case command:
printf("command");
break;
case statement_delimiter:
printf("statement_delimiter");
break;
case eol:
printf("eol");
break;
default:
printf("unknown");
}
}
void print_token_payload(const struct token *tok) {
switch(tok->type) {
case number:
printf("%ld", tok->number);
break;
case dice_operator: case rep_operator: case additive_operator: case explode_operator: case statement_delimiter:
printf("%c", tok->op);
break;
case command:
printf("%s", commands[tok->cmd].cmd_str);
break;
default:
printf("N/A");
}
}
void print_token_info(const struct token *tok) {
printf("{type: ");
print_token_type(tok->type);
printf(", payload: ");
print_token_payload(tok);
printf("}");
}
void print_parse_tree(const struct parse_tree *t) {
printf("{suppress: %s", t->suppress ? "true" : "false");
printf(", quit: %s", t->quit ? "true" : "false");
printf(", nreps: %ld", t->nreps);
printf(", ndice: %ld", t->ndice);
printf(", roll string: '");
if(t->dice_specs != NULL) {
print_dice_specs(t->dice_specs);
}
printf("'}");
}
void process_none(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
printf("Nothing to do.\n");
*s = error;
}
void process_number(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
switch(*s) {
case start:
*s = decide_reps_or_rolls;
*tmp = tok->number;
break;
case want_roll:
t->last_roll->ndice = tok->number;
t->ndice += tok->number;
*s = check_dice_operator;
break;
case want_number_of_sides:
if(tok->number > RAND_MAX) {
*s = error;
printf("The maximum number of sides a dice can have is %d (RAND_MAX).\n", RAND_MAX);
} else {
*s = check_modifiers_or_more_rolls;
t->last_roll->nsides = tok->number;
}
break;
case check_number_of_dice:
if(tok->number > LONG_MAX) {
*s = error;
printf("The maximum number of dice is %ld (LONG_MAX).\n", LONG_MAX);
} else {
t->last_roll->ndice = tok->number;
*s = check_dice_operator;
}
break;
case want_threshold:
if(tok->number > LONG_MAX) {
*s = error;
printf("The maximum threshold is %ld (LONG_MAX).\n", LONG_MAX);
} else {
t->threshold = tok->number;
*s = check_end;
}
break;
case want_keep_number:
if(tok->number < 0) {
*s = error;
printf("You can't keep a negative number of dice.\n");
} else {
*s = check_more_rolls;
long raw_discard = t->last_roll->ndice - tok->number;
t->last_roll->discard = raw_discard < 0 ? 0 : raw_discard;
}
break;
default:
printf("Cannot process number '%ld' while in state '", tok->number);
print_state_name(*s);
printf("'\n");
*s = error;
}
}
void process_dice_operator(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
switch(*s) {
case start:
if(t->last_roll == NULL) {
t->dice_specs = malloc(sizeof(struct roll_encoding));
t->last_roll = t->dice_specs;
} else {
t->last_roll->next = malloc(sizeof(struct roll_encoding));
t->last_roll = t->last_roll->next;
}
dice_init(t->last_roll);
t->last_roll->ndice = 1;
t->ndice += 1;
*s = want_number_of_sides;
break;
case want_roll:
if(t->last_roll == NULL) {
t->dice_specs = malloc(sizeof(struct roll_encoding));
t->last_roll = t->dice_specs;
} else {
t->last_roll->next = malloc(sizeof(struct roll_encoding));
t->last_roll = t->last_roll->next;
}
dice_init(t->last_roll);
t->last_roll->ndice = 1;
t->ndice += 1;
*s = want_number_of_sides;
break;
case decide_reps_or_rolls:
if(t->last_roll == NULL) {
t->dice_specs = malloc(sizeof(struct roll_encoding));
t->last_roll = t->dice_specs;
} else {
t->last_roll->next = malloc(sizeof(struct roll_encoding));
t->last_roll = t->last_roll->next;
}
dice_init(t->last_roll);
t->last_roll->ndice = *tmp;
t->ndice += *tmp;
*s = want_number_of_sides;
break;
case check_dice_operator:
*s = want_number_of_sides;
break;
case check_number_of_dice:
t->last_roll->ndice = 1;
*s = want_number_of_sides;
break;
default:
printf("Cannot process operator '%c' while in state '", tok->op);
print_state_name(*s);
printf("'\n");
*s = error;
}
}
void process_rep_operator(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
switch(*s) {
case decide_reps_or_rolls:
t->nreps = *tmp;
*s = want_roll;
if(t->last_roll == NULL) {
t->dice_specs = malloc(sizeof(struct roll_encoding));
t->last_roll = t->dice_specs;
} else {
t->last_roll->next = malloc(sizeof(struct roll_encoding));
t->last_roll = t->last_roll->next;
}
dice_init(t->last_roll);
break;
default:
printf("Cannot process operator '%c' while in state '", tok->op);
print_state_name(*s);
printf("'\n");
*s = error;
}
}
void process_additive_operator(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
switch(*s) {
case start:
*s = check_number_of_dice;
if(t->last_roll == NULL) {
t->dice_specs = malloc(sizeof(struct roll_encoding));
t->last_roll = t->dice_specs;
} else {
t->last_roll->next = malloc(sizeof(struct roll_encoding));
t->last_roll = t->last_roll->next;
}
dice_init(t->last_roll);
t->last_roll->dir = tok->op == '+' ? pos : neg;
break;
case check_modifiers_or_more_rolls: case check_more_rolls:
*s = check_number_of_dice;
if(t->last_roll == NULL) {
t->dice_specs = malloc(sizeof(struct roll_encoding));
t->last_roll = t->dice_specs;
} else {
t->last_roll->next = malloc(sizeof(struct roll_encoding));
t->last_roll = t->last_roll->next;
}
dice_init(t->last_roll);
t->last_roll->dir = tok->op == '+' ? pos : neg;
break;
case decide_reps_or_rolls: // Deal with cases like 1+2d4. Need to process first number then set things up for the following.
if(t->last_roll == NULL) {
t->dice_specs = malloc(sizeof(struct roll_encoding));
t->last_roll = t->dice_specs;
} else {
t->last_roll->next = malloc(sizeof(struct roll_encoding));
t->last_roll = t->last_roll->next;
}
dice_init(t->last_roll);
t->last_roll->nsides = 1;
t->last_roll->ndice = *tmp;
t->ndice += *tmp;
// First number done, now set up for whatever follows.
*s = check_number_of_dice;
t->last_roll->next = malloc(sizeof(struct roll_encoding));
t->last_roll = t->last_roll->next;
dice_init(t->last_roll);
t->last_roll->dir = tok->op == '+' ? pos : neg;
break;
case check_dice_operator: // Deal with cases like 2d4+1+3d6. The middle "roll" needs its nsides set to 1. This only happens if memory has already been allocated for the middle.
t->last_roll->nsides = 1;
*s = check_number_of_dice;
t->last_roll->next = malloc(sizeof(struct roll_encoding));
t->last_roll = t->last_roll->next;
dice_init(t->last_roll);
t->last_roll->dir = tok->op == '+' ? pos : neg;
t->last_roll->nsides = 1;
break;
case want_roll:
t->last_roll->dir = tok->op == '+' ? pos : neg;
*s = check_number_of_dice;
break;
default:
printf("Cannot process operator '%c' while in state '", tok->op);
print_state_name(*s);
printf("'\n");
*s = error;
}
}
void process_explode_operator(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
switch(*s) {
case check_modifiers_or_more_rolls:
t->last_roll->explode = true;
break;
default:
printf("Cannot process operator '%c' while in state '", tok->op);
print_state_name(*s);
printf("'\n");
*s = error;
}
}
void process_keep_operator(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
switch(*s) {
case check_modifiers_or_more_rolls:
*s = want_keep_number;
t->last_roll->keep = true;
break;
default:
printf("Cannot process operator '%c' while in state '", tok->op);
print_state_name(*s);
printf("'\n");
*s = error;
}
}
void process_threshold_operator(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
switch(*s) {
case decide_reps_or_rolls:
if(t->last_roll == NULL) {
t->dice_specs = malloc(sizeof(struct roll_encoding));
t->last_roll = t->dice_specs;
} else {
t->last_roll->next = malloc(sizeof(struct roll_encoding));
t->last_roll = t->last_roll->next;
}
dice_init(t->last_roll);
t->last_roll->nsides = 1;
t->last_roll->ndice = *tmp;
t->ndice += *tmp;
*s = want_threshold;
t->use_threshold = true;
break;
case check_dice_operator: case check_modifiers_or_more_rolls: case check_more_rolls:
*s = want_threshold;
t->use_threshold = true;
break;
default:
printf("Cannot process operator '%c' while in state '", tok->op);
print_state_name(*s);
printf("'\n");
*s = error;
}
}
void process_command(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
switch(*s) {
case start:
*s = check_end;
switch(tok->cmd) {
case quit:
*s = finish;
t->suppress = true;
t->quit = true;
break;
case clear:
t->suppress = true;
clear_screen();
break;
default:
printf("Received invalid command.\n");
*s = error;
}
break;
default:
printf("Commands may not follow other expressions.\n");
*s = error;
}
}
void process_statement_delimiter(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
switch(*s) {
case start: case check_modifiers_or_more_rolls: case check_more_rolls: case check_end:
break;
case decide_reps_or_rolls:
if(t->last_roll == NULL) {
t->dice_specs = malloc(sizeof(struct roll_encoding));
t->last_roll = t->dice_specs;
} else {
t->last_roll->next = malloc(sizeof(struct roll_encoding));
t->last_roll = t->last_roll->next;
}
dice_init(t->last_roll);
t->last_roll->nsides = 1;
t->last_roll->ndice = *tmp;
t->ndice += *tmp;
break;
case check_dice_operator:
t->last_roll->nsides = 1;
break;
default:
printf("Cannot process operator '%c' while in state '", tok->op);
print_state_name(*s);
printf("'\n");
*s = error;
return;
}
*s = start;
}
void process_eol(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
switch(*s) {
case decide_reps_or_rolls:
if(t->last_roll == NULL) {
t->dice_specs = malloc(sizeof(struct roll_encoding));
t->last_roll = t->dice_specs;
} else {
t->last_roll->next = malloc(sizeof(struct roll_encoding));
t->last_roll = t->last_roll->next;
}
dice_init(t->last_roll);
t->last_roll->nsides = 1;
t->last_roll->ndice = *tmp;
t->ndice += *tmp;
break;
case check_dice_operator:
t->last_roll->nsides = 1;
break;
default: {}
}
*s = finish;
}
void process_default(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
printf("Unknown token type '%d' detected while in state '", tok->type);
print_state_name(*s);
printf("'\n");
*s = error;
}
void parse_tree_initialise(struct parse_tree *t) {
t->suppress = false;
t->quit = false;
t->nreps = 1;
t->ndice = 0;
t->use_threshold = false;
t->threshold = 0;
t->last_roll = NULL;
t->dice_specs = NULL;
t->next = NULL;
t->current = t;
}
void parse_tree_reset(struct parse_tree *t) {
t->suppress = false;
t->quit = false;
t->nreps = 1;
t->ndice = 0;
t->use_threshold = false;
t->threshold = 0;
t->last_roll = NULL;
if(t->dice_specs != NULL) {
dice_reset(t->dice_specs);
t->dice_specs = NULL;
}
if(t->next != NULL) {
parse_tree_reset(t->next);
free(t->next);
t->next = NULL;
}
t->current = t;
}
int parse(struct parse_tree *t, const char *buf, const size_t len) {
int tokens_found = 0;
struct token toks[len];
int lex_err = lex(toks, &tokens_found, buf, len);
if(lex_err != 0) {
return lex_err;
}
state_t s = start;
parse_tree_reset(t);
long tmp = 0;
int toknum;
void (*process_token)(struct token *tok, struct parse_tree *t, state_t *s, long* tmp);
for(toknum = 0; toknum < tokens_found && !(s == finish || s == error); ++toknum) {
bool increment_statement = false;
switch(toks[toknum].type) {
case none:
process_token = process_none;
break;
case number:
process_token = process_number;
break;
case dice_operator:
process_token = process_dice_operator;
break;
case rep_operator:
process_token = process_rep_operator;
break;
case additive_operator:
process_token = process_additive_operator;
break;
case explode_operator:
process_token = process_explode_operator;
break;
case keep_operator:
process_token = process_keep_operator;
break;
case threshold_operator:
process_token = process_threshold_operator;
break;
case command:
process_token = process_command;
break;
case statement_delimiter:
process_token = process_statement_delimiter;
increment_statement = true;
break;
case eol:
process_token = process_eol;
break;
default:
process_token = process_default;
}
process_token(toks + toknum, t->current, &s, &tmp);
if(t->current->quit) {
t->quit = true;
break;
}
if(increment_statement) {
t->current->next = malloc(sizeof(struct parse_tree));
parse_tree_initialise(t->current->next);
t->current = t->current->next;
}
}
if(s == finish) {
return 0;
} else {
if(s == error) {
parse_tree_reset(t->current);
}
return 1;
}
}