2024-10-19 10:59:05 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2024-09-30 20:55:46 -04:00
|
|
|
#include "include/token.h"
|
2024-10-13 23:46:03 -04:00
|
|
|
#include "include/dstr.h"
|
2024-10-19 10:59:05 -04:00
|
|
|
#include "include/util.h"
|
2024-10-19 09:15:03 -04:00
|
|
|
|
2024-10-19 10:59:05 -04:00
|
|
|
static char* tokentype_names[] = {
|
|
|
|
[TOKEN_TYPE_CALL] = "CALL",
|
|
|
|
[TOKEN_TYPE_NUMBER] = "NUMBER",
|
|
|
|
};
|
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;
|
2024-10-13 23:46:03 -04:00
|
|
|
t->valn = valn;
|
2024-10-19 10:59:05 -04:00
|
|
|
t->val = val;
|
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-19 10:59:05 -04:00
|
|
|
void token_print(Token* token) { token_print_i(token, 0); }
|
|
|
|
|
|
|
|
void token_print_i(Token *token, int ilvl) {
|
|
|
|
INDENT_BEGIN(ilvl);
|
|
|
|
|
|
|
|
INDENT_TITLE("Token", token);
|
|
|
|
INDENT_FIELD_NONL("type");
|
|
|
|
tokentype_print_raw(token->type);
|
2024-10-25 11:20:07 -04:00
|
|
|
putchar('\n');
|
|
|
|
INDENT_FIELD("valn", "%ld", token->valn);
|
|
|
|
INDENT_FIELD_NL("val", "\"%s\"", token->val);
|
2024-10-19 10:59:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void tokentype_print_raw(TokenType t) {
|
|
|
|
if (t > TOKEN_TYPE_MAX) {
|
|
|
|
printf("Unknown (%d)", t);
|
|
|
|
log_dbgf("%d is not a valid TokenType (max: %d)", t, TOKEN_TYPE_MAX);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("%s", tokentype_names[t]);
|
|
|
|
}
|
2024-10-13 23:46:03 -04:00
|
|
|
|
2024-10-19 10:59:05 -04:00
|
|
|
void tokentype_print(TokenType t) { tokentype_print_i(t, 0); }
|
2024-10-13 23:46:03 -04:00
|
|
|
|
2024-10-19 10:59:05 -04:00
|
|
|
void tokentype_print_i(TokenType t, int i) {
|
|
|
|
INDENT_BEGIN(i);
|
2024-10-13 23:46:03 -04:00
|
|
|
|
2024-10-19 10:59:05 -04:00
|
|
|
if (t > TOKEN_TYPE_MAX) {
|
|
|
|
INDENT_FIELD("val", "Unknown (%d)", t);
|
|
|
|
log_dbgf("%d is not a valid TokenType (max: %d)", t, TOKEN_TYPE_MAX);
|
|
|
|
return;
|
|
|
|
}
|
2024-10-13 23:46:03 -04:00
|
|
|
|
2024-10-19 10:59:05 -04:00
|
|
|
INDENT_FIELD("val", "%s", tokentype_names[t]);
|
2024-10-07 11:48:53 -04:00
|
|
|
}
|