diff options
Diffstat (limited to 'src/token.c')
| -rw-r--r-- | src/token.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/token.c b/src/token.c new file mode 100644 index 0000000..18ec034 --- /dev/null +++ b/src/token.c @@ -0,0 +1,34 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <token.h> + +void +token_init(token_t *token, TokenType type, char *lexeme, unsigned int line) +{ + token->type = type; + token->lexeme = lexeme; + token->line = line; + token->string = NULL; +} + +char * +token_to_string(token_t *token) +{ + unsigned int line_length = snprintf(NULL, 0, "%ul", token->line) - 1; + /* 2: ": ", 1: "\0" */ + unsigned int final_size = strlen(token->lexeme) + line_length + 2 + 1; + + char *result = (char *) calloc(1, final_size); + snprintf(result, final_size, "%d: %s", token->line, token->lexeme); + token->string = result; + + return token->string; +} + +void +token_clean(token_t *token) +{ + if (token->string != NULL) + free(token->string); +} |
