Created
April 17, 2021 01:28
-
-
Save harieamjari/1051791594c965bd740fc93dbbdd9856 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define UNEXPECTED(c) \ | |
{ \ | |
if (c != TOK_ERROR) { \ | |
fprintf(stderr, "stream:%d:%d error: unexpected %s\n", line_tok_found, \ | |
column_tok_found, tok_str(c)); \ | |
} \ | |
if (grid != NULL) \ | |
for (int i = 0; i < sizex; i++) \ | |
free(grid[i]); \ | |
free(grid); \ | |
return 1; \ | |
} | |
typedef enum token_t token_t; | |
enum token_t { | |
TOK_WALL, | |
TOK_TILE, | |
TOK_TRAP, | |
TOK_GOLD, | |
TOK_PERSON, | |
TOK_NUM, | |
TOK_ERROR, | |
TOK_EOF | |
}; | |
int cur_line = 0; | |
int cur_column = 0; | |
int line_tok_found; | |
int column_tok_found; | |
int tok_num; | |
int sizex, sizey; | |
FILE *fp; | |
token_t **grid = NULL; | |
token_t tok; | |
const char *tok_str(const token_t tok) { | |
switch (tok) { | |
case TOK_NUM: | |
return "numeric"; | |
case TOK_WALL: | |
return "'*'"; | |
case TOK_TILE: | |
return "'.'"; | |
case TOK_TRAP: | |
return "'T'"; | |
case TOK_PERSON: | |
return "'P'"; | |
case TOK_GOLD: | |
return "'G'"; | |
case TOK_EOF: | |
return "end of file"; | |
} | |
} | |
token_t n_tok(void) { | |
int c; | |
w: | |
while ((c = fgetc(fp)) == (int)' ') { | |
cur_column++; | |
} | |
if (c == (int)'\n') { | |
cur_line++; | |
cur_column = 1; | |
goto w; | |
} | |
line_tok_found = cur_line; | |
column_tok_found = cur_column; | |
switch (c) { | |
case '0': | |
case '1': | |
case '2': | |
case '3': | |
case '4': | |
case '5': | |
case '6': | |
case '7': | |
case '8': | |
case '9': { | |
ungetc(c, fp); | |
int nchar; | |
char buff[100] = {0}; | |
fscanf(fp, "%99[0-9]%n", buff, &nchar); | |
cur_column += nchar; | |
tok_num = atoi(buff); | |
return TOK_NUM; | |
} | |
case '*': | |
cur_column++; | |
return TOK_WALL; | |
case '.': | |
cur_column++; | |
return TOK_TILE; | |
case 'T': | |
cur_column++; | |
return TOK_TRAP; | |
case 'G': | |
cur_column++; | |
return TOK_GOLD; | |
case 'P': | |
cur_column++; | |
case EOF: | |
return TOK_EOF; | |
default: | |
fprintf(stderr, "stream:%d:%d error unexpected %c\n", line_tok_found, | |
column_tok_found, c); | |
return TOK_ERROR; | |
} | |
} | |
int main() { | |
fp = stdin; | |
if ((tok = n_tok()) != TOK_NUM) | |
UNEXPECTED(tok); | |
sizex = tok_num; | |
grid = malloc(sizeof(token_t *) * sizex); | |
assert(grid != NULL); | |
if ((tok = n_tok()) != TOK_NUM) | |
UNEXPECTED(tok); | |
sizey = tok_num; | |
for (int i = 0; i < sizex; i++) { | |
grid[i] = malloc(sizeof(token_t) * sizey); | |
assert(grid[i] != NULL); | |
} | |
for (int x = 0; x < sizex; x++) | |
memset(grid[x], 0, sizeof(token_t) * sizey); | |
while ((tok = n_tok()) != TOK_EOF) { | |
for (int i = 0; i < sizex; i++) | |
fgetc(fp); | |
} | |
for (int i = 0; i < sizex; i++) | |
free(grid[i]); | |
free(grid); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment