parse.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. #include <stddef.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <limits.h>
  7. #include <termcap.h> // Needed for clear_screen
  8. #include <errno.h>
  9. #include <omp.h>
  10. #include "parse.h"
  11. #include "roll-engine.h"
  12. static const struct cmd_map commands[] = {
  13. { quit, { "quit" } },
  14. { clear, { "clear" } },
  15. };
  16. #define NUMBER_OF_DEFINED_COMMANDS 2
  17. #define CMD_MAX_STR_LEN 5
  18. void clear_screen() {
  19. char buf[1024];
  20. tgetent(buf, getenv("TERM"));
  21. char *str;
  22. str = tgetstr("cl", NULL);
  23. fputs(str, stdout);
  24. }
  25. void token_list_init(struct token *t, const size_t len) {
  26. int toknum;
  27. #pragma omp for schedule(static) private(toknum) nowait
  28. for(toknum = 0; toknum < len; ++toknum) {
  29. t[toknum].type = none;
  30. t[toknum].number = 0;
  31. t[toknum].op = '?';
  32. t[toknum].cmd = -1;
  33. }
  34. }
  35. int lex(struct token *t, int *tokens_found, const char *buf, const size_t len) {
  36. int charnum = 0;
  37. *tokens_found = 0;
  38. while(*(buf + charnum) != '\0' && charnum <= len) {
  39. if(isspace(*(buf + charnum))) {
  40. ++charnum;
  41. } else if(*(buf + charnum) == '#') { //comment detected
  42. break;
  43. } else {
  44. switch(*(buf + charnum)) {
  45. case 'd': case 'D':
  46. {
  47. t[*tokens_found].type = dice_operator;
  48. t[*tokens_found].op = *(buf + charnum);
  49. ++charnum;
  50. }
  51. break;
  52. case '+': case '-':
  53. {
  54. t[*tokens_found].type = additive_operator;
  55. t[*tokens_found].op = *(buf + charnum);
  56. ++charnum;
  57. }
  58. break;
  59. case 'x': case 'X':
  60. {
  61. t[*tokens_found].type = rep_operator;
  62. t[*tokens_found].op = *(buf + charnum);
  63. ++charnum;
  64. }
  65. break;
  66. case '!':
  67. {
  68. t[*tokens_found].type = explode_operator;
  69. t[*tokens_found].op = *(buf + charnum);
  70. ++charnum;
  71. }
  72. break;
  73. case 't': case 'T': case '>':
  74. {
  75. t[*tokens_found].type = threshold_operator;
  76. t[*tokens_found].op = *(buf + charnum);
  77. ++charnum;
  78. }
  79. break;
  80. case 'k': case 'K':
  81. {
  82. t[*tokens_found].type = keep_operator;
  83. t[*tokens_found].op = *(buf + charnum);
  84. ++charnum;
  85. }
  86. break;
  87. case ';':
  88. {
  89. t[*tokens_found].type = statement_delimiter;
  90. t[*tokens_found].op = *(buf + charnum);
  91. ++charnum;
  92. }
  93. break;
  94. default:
  95. {
  96. char *tok_str;
  97. int offset = charnum;
  98. if(isdigit(*(buf + charnum))) {
  99. tok_str = malloc((LONG_MAX_STR_LEN + 1)*sizeof(char));
  100. if(!tok_str) {
  101. fprintf(stderr, "Error allocating memory.\n");
  102. exit(1);
  103. }
  104. memset(tok_str, 0, LONG_MAX_STR_LEN + 1);
  105. while(isdigit(*(buf + charnum)) && charnum - offset < LONG_MAX_STR_LEN) {
  106. tok_str[charnum - offset] = *(buf + charnum);
  107. ++charnum;
  108. }
  109. if(charnum - offset >= LONG_MAX_STR_LEN && isdigit(*(buf + charnum))) {
  110. printf("Invalid numeric input detected. The maximum number allowed is %ld (LONG_MAX).\n", LONG_MAX);
  111. free(tok_str);
  112. return 1;
  113. }
  114. tok_str[charnum - offset] = '\0';
  115. errno = 0;
  116. long num = strtol(tok_str, NULL, 10);
  117. if((errno == ERANGE && (num == LONG_MAX || num == LONG_MIN))
  118. || (errno != 0 && num == 0)) {
  119. printf("Error %d (%s) converting string '%s' to number.\n", errno, strerror(errno), tok_str);
  120. return 1;
  121. }
  122. t[*tokens_found].type = number;
  123. t[*tokens_found].number = num;
  124. } else if(isalpha(*(buf + charnum))) {
  125. int numchars = 0;
  126. while(isalpha(*(buf + charnum))) {
  127. ++numchars;
  128. ++charnum;
  129. }
  130. tok_str = malloc((numchars + 1)*sizeof(char));
  131. if(!tok_str) {
  132. fprintf(stderr, "Error allocating memory.\n");
  133. exit(1);
  134. }
  135. memset(tok_str, 0, numchars + 1);
  136. charnum = offset;
  137. while(isalpha(*(buf + charnum)) && charnum - offset < numchars) {
  138. tok_str[charnum - offset] = *(buf + charnum);
  139. ++charnum;
  140. }
  141. tok_str[charnum - offset] = '\0';
  142. t[*tokens_found].type = command;
  143. int cmd_num = 0;
  144. bool cmd_found = false;
  145. int max_str_len = CMD_MAX_STR_LEN >= numchars ? CMD_MAX_STR_LEN : numchars;
  146. while(cmd_num < NUMBER_OF_DEFINED_COMMANDS) {
  147. if(0 == strncmp(tok_str, commands[cmd_num].cmd_str, max_str_len)) {
  148. t[*tokens_found].cmd = commands[cmd_num].cmd_code;
  149. cmd_found = true;
  150. break;
  151. }
  152. ++cmd_num;
  153. }
  154. if(!cmd_found) {
  155. printf("Unknown command: %s\n", tok_str);
  156. return 1;
  157. }
  158. } else {
  159. printf("Unknown token detected: %c\n", *(buf + charnum));
  160. return 1;
  161. }
  162. if(tok_str) {
  163. free(tok_str);
  164. tok_str = NULL;
  165. }
  166. }
  167. }
  168. (*tokens_found)++;
  169. }
  170. }
  171. t[*tokens_found].type = eol;
  172. ++(*tokens_found);
  173. return 0;
  174. }
  175. void print_state_name(const state_t s) {
  176. switch(s) {
  177. case error:
  178. printf("error");
  179. break;
  180. case start:
  181. printf("start");
  182. break;
  183. case decide_reps_or_rolls:
  184. printf("decide_reps_or_rolls");
  185. break;
  186. case want_number_of_sides:
  187. printf("want_number_of_sides");
  188. break;
  189. case check_number_of_dice:
  190. printf("check_number_of_dice");
  191. break;
  192. case want_roll:
  193. printf("want_roll");
  194. break;
  195. case check_dice_operator:
  196. printf("check_dice_operator");
  197. break;
  198. case check_modifiers_or_more_rolls:
  199. printf("check_modifiers_or_more_rolls");
  200. break;
  201. case check_more_rolls:
  202. printf("check_more_rolls");
  203. break;
  204. case check_end:
  205. printf("check_end");
  206. break;
  207. case finish:
  208. printf("finish");
  209. break;
  210. default:
  211. printf("undefined");
  212. }
  213. }
  214. void print_token_type(const token_t type) {
  215. switch(type) {
  216. case none:
  217. printf("none");
  218. break;
  219. case number:
  220. printf("number");
  221. break;
  222. case dice_operator:
  223. printf("dice_operator");
  224. break;
  225. case rep_operator:
  226. printf("rep_operator");
  227. break;
  228. case additive_operator:
  229. printf("additive_operator");
  230. break;
  231. case explode_operator:
  232. printf("explode_operator");
  233. break;
  234. case command:
  235. printf("command");
  236. break;
  237. case statement_delimiter:
  238. printf("statement_delimiter");
  239. break;
  240. case eol:
  241. printf("eol");
  242. break;
  243. default:
  244. printf("unknown");
  245. }
  246. }
  247. void print_token_payload(const struct token *tok) {
  248. switch(tok->type) {
  249. case number:
  250. printf("%ld", tok->number);
  251. break;
  252. case dice_operator: case rep_operator: case additive_operator: case explode_operator: case statement_delimiter:
  253. printf("%c", tok->op);
  254. break;
  255. case command:
  256. printf("%s", commands[tok->cmd].cmd_str);
  257. break;
  258. default:
  259. printf("N/A");
  260. }
  261. }
  262. void print_token_info(const struct token *tok) {
  263. printf("{type: ");
  264. print_token_type(tok->type);
  265. printf(", payload: ");
  266. print_token_payload(tok);
  267. printf("}");
  268. }
  269. void print_parse_tree(const struct parse_tree *t) {
  270. printf("{suppress: %s", t->suppress ? "true" : "false");
  271. printf(", quit: %s", t->quit ? "true" : "false");
  272. printf(", nreps: %ld", t->nreps);
  273. printf(", ndice: %ld", t->ndice);
  274. printf(", roll string: '");
  275. if(t->dice_specs != NULL) {
  276. print_dice_specs(t->dice_specs);
  277. }
  278. printf("'}");
  279. }
  280. void process_none(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
  281. printf("Nothing to do.\n");
  282. *s = error;
  283. }
  284. void process_number(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
  285. switch(*s) {
  286. case start:
  287. *s = decide_reps_or_rolls;
  288. *tmp = tok->number;
  289. break;
  290. case want_roll:
  291. t->last_roll->ndice = tok->number;
  292. t->ndice += tok->number;
  293. *s = check_dice_operator;
  294. break;
  295. case want_number_of_sides:
  296. if(tok->number > RAND_MAX) {
  297. *s = error;
  298. printf("The maximum number of sides a dice can have is %d (RAND_MAX).\n", RAND_MAX);
  299. } else {
  300. *s = check_modifiers_or_more_rolls;
  301. t->last_roll->nsides = tok->number;
  302. }
  303. break;
  304. case check_number_of_dice:
  305. if(tok->number > LONG_MAX) {
  306. *s = error;
  307. printf("The maximum number of dice is %ld (LONG_MAX).\n", LONG_MAX);
  308. } else {
  309. t->last_roll->ndice = tok->number;
  310. *s = check_dice_operator;
  311. }
  312. break;
  313. case want_threshold:
  314. if(tok->number > LONG_MAX) {
  315. *s = error;
  316. printf("The maximum threshold is %ld (LONG_MAX).\n", LONG_MAX);
  317. } else {
  318. t->threshold = tok->number;
  319. *s = check_end;
  320. }
  321. break;
  322. case want_keep_number:
  323. if(tok->number < 0) {
  324. *s = error;
  325. printf("You can't keep a negative number of dice.\n");
  326. } else {
  327. *s = check_more_rolls;
  328. long raw_discard = t->last_roll->ndice - tok->number;
  329. t->last_roll->discard = raw_discard < 0 ? 0 : raw_discard;
  330. }
  331. break;
  332. default:
  333. printf("Cannot process number '%ld' while in state '", tok->number);
  334. print_state_name(*s);
  335. printf("'\n");
  336. *s = error;
  337. }
  338. }
  339. void process_dice_operator(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
  340. switch(*s) {
  341. case start:
  342. if(t->last_roll == NULL) {
  343. t->dice_specs = malloc(sizeof(struct roll_encoding));
  344. t->last_roll = t->dice_specs;
  345. } else {
  346. t->last_roll->next = malloc(sizeof(struct roll_encoding));
  347. t->last_roll = t->last_roll->next;
  348. }
  349. dice_init(t->last_roll);
  350. t->last_roll->ndice = 1;
  351. t->ndice += 1;
  352. *s = want_number_of_sides;
  353. break;
  354. case want_roll:
  355. if(t->last_roll == NULL) {
  356. t->dice_specs = malloc(sizeof(struct roll_encoding));
  357. t->last_roll = t->dice_specs;
  358. } else {
  359. t->last_roll->next = malloc(sizeof(struct roll_encoding));
  360. t->last_roll = t->last_roll->next;
  361. }
  362. dice_init(t->last_roll);
  363. t->last_roll->ndice = 1;
  364. t->ndice += 1;
  365. *s = want_number_of_sides;
  366. break;
  367. case decide_reps_or_rolls:
  368. if(t->last_roll == NULL) {
  369. t->dice_specs = malloc(sizeof(struct roll_encoding));
  370. t->last_roll = t->dice_specs;
  371. } else {
  372. t->last_roll->next = malloc(sizeof(struct roll_encoding));
  373. t->last_roll = t->last_roll->next;
  374. }
  375. dice_init(t->last_roll);
  376. t->last_roll->ndice = *tmp;
  377. t->ndice += *tmp;
  378. *s = want_number_of_sides;
  379. break;
  380. case check_dice_operator:
  381. *s = want_number_of_sides;
  382. break;
  383. case check_number_of_dice:
  384. t->last_roll->ndice = 1;
  385. *s = want_number_of_sides;
  386. break;
  387. default:
  388. printf("Cannot process operator '%c' while in state '", tok->op);
  389. print_state_name(*s);
  390. printf("'\n");
  391. *s = error;
  392. }
  393. }
  394. void process_rep_operator(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
  395. switch(*s) {
  396. case decide_reps_or_rolls:
  397. t->nreps = *tmp;
  398. *s = want_roll;
  399. if(t->last_roll == NULL) {
  400. t->dice_specs = malloc(sizeof(struct roll_encoding));
  401. t->last_roll = t->dice_specs;
  402. } else {
  403. t->last_roll->next = malloc(sizeof(struct roll_encoding));
  404. t->last_roll = t->last_roll->next;
  405. }
  406. dice_init(t->last_roll);
  407. break;
  408. default:
  409. printf("Cannot process operator '%c' while in state '", tok->op);
  410. print_state_name(*s);
  411. printf("'\n");
  412. *s = error;
  413. }
  414. }
  415. void process_additive_operator(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
  416. switch(*s) {
  417. case start:
  418. *s = check_number_of_dice;
  419. if(t->last_roll == NULL) {
  420. t->dice_specs = malloc(sizeof(struct roll_encoding));
  421. t->last_roll = t->dice_specs;
  422. } else {
  423. t->last_roll->next = malloc(sizeof(struct roll_encoding));
  424. t->last_roll = t->last_roll->next;
  425. }
  426. dice_init(t->last_roll);
  427. t->last_roll->dir = tok->op == '+' ? pos : neg;
  428. break;
  429. case check_modifiers_or_more_rolls: case check_more_rolls:
  430. *s = check_number_of_dice;
  431. if(t->last_roll == NULL) {
  432. t->dice_specs = malloc(sizeof(struct roll_encoding));
  433. t->last_roll = t->dice_specs;
  434. } else {
  435. t->last_roll->next = malloc(sizeof(struct roll_encoding));
  436. t->last_roll = t->last_roll->next;
  437. }
  438. dice_init(t->last_roll);
  439. t->last_roll->dir = tok->op == '+' ? pos : neg;
  440. break;
  441. case decide_reps_or_rolls: // Deal with cases like 1+2d4. Need to process first number then set things up for the following.
  442. if(t->last_roll == NULL) {
  443. t->dice_specs = malloc(sizeof(struct roll_encoding));
  444. t->last_roll = t->dice_specs;
  445. } else {
  446. t->last_roll->next = malloc(sizeof(struct roll_encoding));
  447. t->last_roll = t->last_roll->next;
  448. }
  449. dice_init(t->last_roll);
  450. t->last_roll->nsides = 1;
  451. t->last_roll->ndice = *tmp;
  452. t->ndice += *tmp;
  453. // First number done, now set up for whatever follows.
  454. *s = check_number_of_dice;
  455. t->last_roll->next = malloc(sizeof(struct roll_encoding));
  456. t->last_roll = t->last_roll->next;
  457. dice_init(t->last_roll);
  458. t->last_roll->dir = tok->op == '+' ? pos : neg;
  459. break;
  460. 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.
  461. t->last_roll->nsides = 1;
  462. *s = check_number_of_dice;
  463. t->last_roll->next = malloc(sizeof(struct roll_encoding));
  464. t->last_roll = t->last_roll->next;
  465. dice_init(t->last_roll);
  466. t->last_roll->dir = tok->op == '+' ? pos : neg;
  467. t->last_roll->nsides = 1;
  468. break;
  469. case want_roll:
  470. t->last_roll->dir = tok->op == '+' ? pos : neg;
  471. *s = check_number_of_dice;
  472. break;
  473. default:
  474. printf("Cannot process operator '%c' while in state '", tok->op);
  475. print_state_name(*s);
  476. printf("'\n");
  477. *s = error;
  478. }
  479. }
  480. void process_explode_operator(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
  481. switch(*s) {
  482. case check_modifiers_or_more_rolls:
  483. t->last_roll->explode = true;
  484. break;
  485. default:
  486. printf("Cannot process operator '%c' while in state '", tok->op);
  487. print_state_name(*s);
  488. printf("'\n");
  489. *s = error;
  490. }
  491. }
  492. void process_keep_operator(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
  493. switch(*s) {
  494. case check_modifiers_or_more_rolls:
  495. *s = want_keep_number;
  496. t->last_roll->keep = true;
  497. break;
  498. default:
  499. printf("Cannot process operator '%c' while in state '", tok->op);
  500. print_state_name(*s);
  501. printf("'\n");
  502. *s = error;
  503. }
  504. }
  505. void process_threshold_operator(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
  506. switch(*s) {
  507. case decide_reps_or_rolls:
  508. if(t->last_roll == NULL) {
  509. t->dice_specs = malloc(sizeof(struct roll_encoding));
  510. t->last_roll = t->dice_specs;
  511. } else {
  512. t->last_roll->next = malloc(sizeof(struct roll_encoding));
  513. t->last_roll = t->last_roll->next;
  514. }
  515. dice_init(t->last_roll);
  516. t->last_roll->nsides = 1;
  517. t->last_roll->ndice = *tmp;
  518. t->ndice += *tmp;
  519. *s = want_threshold;
  520. t->use_threshold = true;
  521. break;
  522. case check_dice_operator: case check_modifiers_or_more_rolls: case check_more_rolls:
  523. *s = want_threshold;
  524. t->use_threshold = true;
  525. break;
  526. default:
  527. printf("Cannot process operator '%c' while in state '", tok->op);
  528. print_state_name(*s);
  529. printf("'\n");
  530. *s = error;
  531. }
  532. }
  533. void process_command(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
  534. switch(*s) {
  535. case start:
  536. *s = check_end;
  537. switch(tok->cmd) {
  538. case quit:
  539. *s = finish;
  540. t->suppress = true;
  541. t->quit = true;
  542. break;
  543. case clear:
  544. t->suppress = true;
  545. clear_screen();
  546. break;
  547. default:
  548. printf("Received invalid command.\n");
  549. *s = error;
  550. }
  551. break;
  552. default:
  553. printf("Commands may not follow other expressions.\n");
  554. *s = error;
  555. }
  556. }
  557. void process_statement_delimiter(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
  558. switch(*s) {
  559. case start: case check_modifiers_or_more_rolls: case check_more_rolls: case check_end:
  560. break;
  561. case decide_reps_or_rolls:
  562. if(t->last_roll == NULL) {
  563. t->dice_specs = malloc(sizeof(struct roll_encoding));
  564. t->last_roll = t->dice_specs;
  565. } else {
  566. t->last_roll->next = malloc(sizeof(struct roll_encoding));
  567. t->last_roll = t->last_roll->next;
  568. }
  569. dice_init(t->last_roll);
  570. t->last_roll->nsides = 1;
  571. t->last_roll->ndice = *tmp;
  572. t->ndice += *tmp;
  573. break;
  574. case check_dice_operator:
  575. t->last_roll->nsides = 1;
  576. break;
  577. default:
  578. printf("Cannot process operator '%c' while in state '", tok->op);
  579. print_state_name(*s);
  580. printf("'\n");
  581. *s = error;
  582. return;
  583. }
  584. *s = start;
  585. }
  586. void process_eol(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
  587. switch(*s) {
  588. case decide_reps_or_rolls:
  589. if(t->last_roll == NULL) {
  590. t->dice_specs = malloc(sizeof(struct roll_encoding));
  591. t->last_roll = t->dice_specs;
  592. } else {
  593. t->last_roll->next = malloc(sizeof(struct roll_encoding));
  594. t->last_roll = t->last_roll->next;
  595. }
  596. dice_init(t->last_roll);
  597. t->last_roll->nsides = 1;
  598. t->last_roll->ndice = *tmp;
  599. t->ndice += *tmp;
  600. break;
  601. case check_dice_operator:
  602. t->last_roll->nsides = 1;
  603. break;
  604. default: {}
  605. }
  606. *s = finish;
  607. }
  608. void process_default(struct token *tok, struct parse_tree *t, state_t *s, long* tmp) {
  609. printf("Unknown token type '%d' detected while in state '", tok->type);
  610. print_state_name(*s);
  611. printf("'\n");
  612. *s = error;
  613. }
  614. void parse_tree_initialise(struct parse_tree *t) {
  615. t->suppress = false;
  616. t->quit = false;
  617. t->nreps = 1;
  618. t->ndice = 0;
  619. t->use_threshold = false;
  620. t->threshold = 0;
  621. t->last_roll = NULL;
  622. t->dice_specs = NULL;
  623. t->next = NULL;
  624. t->current = t;
  625. }
  626. void parse_tree_reset(struct parse_tree *t) {
  627. t->suppress = false;
  628. t->quit = false;
  629. t->nreps = 1;
  630. t->ndice = 0;
  631. t->use_threshold = false;
  632. t->threshold = 0;
  633. t->last_roll = NULL;
  634. if(t->dice_specs != NULL) {
  635. dice_reset(t->dice_specs);
  636. t->dice_specs = NULL;
  637. }
  638. if(t->next != NULL) {
  639. parse_tree_reset(t->next);
  640. free(t->next);
  641. t->next = NULL;
  642. }
  643. t->current = t;
  644. }
  645. int parse(struct parse_tree *t, const char *buf, const size_t len) {
  646. int tokens_found = 0;
  647. struct token toks[len];
  648. int lex_err = lex(toks, &tokens_found, buf, len);
  649. if(lex_err != 0) {
  650. return lex_err;
  651. }
  652. state_t s = start;
  653. parse_tree_reset(t);
  654. long tmp = 0;
  655. int toknum;
  656. void (*process_token)(struct token *tok, struct parse_tree *t, state_t *s, long* tmp);
  657. for(toknum = 0; toknum < tokens_found && !(s == finish || s == error); ++toknum) {
  658. bool increment_statement = false;
  659. switch(toks[toknum].type) {
  660. case none:
  661. process_token = process_none;
  662. break;
  663. case number:
  664. process_token = process_number;
  665. break;
  666. case dice_operator:
  667. process_token = process_dice_operator;
  668. break;
  669. case rep_operator:
  670. process_token = process_rep_operator;
  671. break;
  672. case additive_operator:
  673. process_token = process_additive_operator;
  674. break;
  675. case explode_operator:
  676. process_token = process_explode_operator;
  677. break;
  678. case keep_operator:
  679. process_token = process_keep_operator;
  680. break;
  681. case threshold_operator:
  682. process_token = process_threshold_operator;
  683. break;
  684. case command:
  685. process_token = process_command;
  686. break;
  687. case statement_delimiter:
  688. process_token = process_statement_delimiter;
  689. increment_statement = true;
  690. break;
  691. case eol:
  692. process_token = process_eol;
  693. break;
  694. default:
  695. process_token = process_default;
  696. }
  697. process_token(toks + toknum, t->current, &s, &tmp);
  698. if(t->current->quit) {
  699. t->quit = true;
  700. break;
  701. }
  702. if(increment_statement) {
  703. t->current->next = malloc(sizeof(struct parse_tree));
  704. parse_tree_initialise(t->current->next);
  705. t->current = t->current->next;
  706. }
  707. }
  708. if(s == finish) {
  709. return 0;
  710. } else {
  711. if(s == error) {
  712. parse_tree_reset(t->current);
  713. }
  714. return 1;
  715. }
  716. }