scl/src/token.c

43 lines
953 B
C
Raw Normal View History

#include "include/token.h"
2024-10-13 23:46:03 -04:00
#include "include/dstr.h"
2024-10-19 09:15:03 -04:00
#include <stdio.h>
2024-10-13 23:46:03 -04:00
Token* token_init(TokenType type, char* val, size_t valn) {
Token* t = malloc(sizeof(Token));
t->type = type;
t->val = val;
2024-10-13 23:46:03 -04:00
t->valn = valn;
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
}