diff options
| author | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-08 23:50:41 +0530 |
|---|---|---|
| committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-08 23:50:41 +0530 |
| commit | 73d8ee51a9bf3175a1b4b339b043f45d17c9d92a (patch) | |
| tree | 9285b29ad316df4dd7398ff8d25234ab7c231d72 /src/token.cc | |
| parent | 1de0b57e396d1964aba2c8ddc663bd55d01f5058 (diff) | |
tokens: init
Diffstat (limited to 'src/token.cc')
| -rw-r--r-- | src/token.cc | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/token.cc b/src/token.cc new file mode 100644 index 0000000..a776d01 --- /dev/null +++ b/src/token.cc @@ -0,0 +1,33 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <token.h> + +Token::Token(TokenType type, char *lexeme, unsigned int line) +{ + this->type = type; + this->lexeme = lexeme; + this->line = line; + this->string = NULL; +} + +char * +Token::to_string(void) +{ + unsigned int line_length = snprintf(NULL, 0, "%ul", line) - 1; + /* 2: ": ", 1: "\0" */ + unsigned int final_size = strlen(lexeme) + line_length + 2 + 1; + + char *result = (char *) calloc(1, final_size); + snprintf(result, final_size, "%d: %s", line, lexeme); + string = result; + + return result; +} + +void +Token::clean(void) +{ + if (string != NULL) + free(string); +} |
