2024-09-30 20:55:46 -04:00
|
|
|
#include "include/token.h"
|
2024-10-13 23:46:03 -04:00
|
|
|
#include "include/dstr.h"
|
|
|
|
#include <stdlib.h>
|
2024-09-30 20:55:46 -04:00
|
|
|
|
2024-10-13 23:46:03 -04:00
|
|
|
Token* token_init(TokenType type, char* val, size_t valn) {
|
2024-09-30 20:55:46 -04:00
|
|
|
Token* t = malloc(sizeof(Token));
|
|
|
|
|
|
|
|
t->type = type;
|
|
|
|
t->val = val;
|
2024-10-13 23:46:03 -04:00
|
|
|
t->valn = valn;
|
2024-09-30 20:55:46 -04:00
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void token_destroy(Token* t) {
|
|
|
|
free(t->val);
|
|
|
|
free(t);
|
|
|
|
}
|
2024-10-07 11:48:53 -04:00
|
|
|
|
2024-10-13 23:46:03 -04:00
|
|
|
Dstr* token_to_dstr(Token* token) {
|
|
|
|
Dstr* str = dstr_init();
|
|
|
|
|
|
|
|
size_t titlesz = sizeof("Token @ 0x00000000");
|
|
|
|
char title[titlesz];
|
|
|
|
sprintf(title, "Token @ %p", token);
|
|
|
|
dstr_append(str, title, titlesz - 1);
|
|
|
|
dstr_append(str, "\n", 1);
|
|
|
|
|
|
|
|
size_t typesz = sizeof("type: 1");
|
|
|
|
char type[typesz];
|
|
|
|
// If token_to_dstr starts breaking, it might be because there're more than
|
|
|
|
// 10 types. FIXME.
|
|
|
|
sprintf(type, "type: %d", token->type);
|
|
|
|
dstr_append(str, type, typesz - 1);
|
|
|
|
dstr_append(str, "\n", 1);
|
|
|
|
|
|
|
|
dstr_append(str, "val: ", 5);
|
|
|
|
dstr_append(str, token->val, token->valn);
|
|
|
|
|
|
|
|
return str;
|
2024-10-07 11:48:53 -04:00
|
|
|
}
|