123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743 |
- #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;
- }
- }
|