Files
assm-test/src/token.c
T

45 lines
1.1 KiB
C

#include "../includes/token.h"
TokenClass GetTokenClass(TokenType);
Token* CreateToken(char* lexeme, void* value, int lineNumber, TokenType type) {
Token* token = calloc(1, sizeof(Token));
if (!token) {
fprintf(stderr, "Failed to calloc memory for Token. %s.\n", strerror(errno));
return NULL;
}
token->type = type;
token->line = lineNumber;
token->lexeme = lexeme;
token->value = value;
token->token_class = GetTokenClass(type);
return token;
}
TokenClass GetTokenClass(TokenType type) {
if (type >= R1 && type <= R8) return Reg;
if (type >= COPY && type <= OUT) return Nmomic;
if (type >= DB && type <= ORG) return Directive;
switch(type) {
case STRING:
case IDENTIFIER:
case LABEL:
return Address;
case NUMBER:
return Constant;
default:
return None;
}
}
void FreeToken(Token* token) {
if (!token) return;
if (token->value && (token->type >= STRING || token->type == NUMBER)) free(token->value);
free(token);
}