2026-02-05 20:16:57 +11:00
|
|
|
#include "SolsToken.h"
|
|
|
|
|
#include "SolsLiteral.h"
|
|
|
|
|
#include "../include/error.h"
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
ResultType(SolsToken, charptr) createSolsToken(SolsTokenType type, ...) {
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, type);
|
|
|
|
|
SolsToken token = {
|
|
|
|
|
.type = type
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (type == STT_IDENTIFIER) {
|
|
|
|
|
char* name = va_arg(args, char*);
|
|
|
|
|
if (name == NULL) {
|
|
|
|
|
return Error(SolsToken, charptr, "String passed is NULL (in createSolsToken() function)");
|
|
|
|
|
}
|
|
|
|
|
token.as.idName = malloc(strlen(name) + 1);
|
|
|
|
|
if (token.as.idName == NULL) {
|
|
|
|
|
return Error(SolsToken, charptr, "Couldn't allocate memory (in createSolsToken() function)");
|
|
|
|
|
}
|
|
|
|
|
strcpy(token.as.idName, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type == STT_KW_GROUND) {
|
|
|
|
|
char* ground = va_arg(args, char*);
|
|
|
|
|
if (ground == NULL) {
|
|
|
|
|
return Error(SolsToken, charptr, "String passed is NULL (in createSolsToken() function)");
|
|
|
|
|
}
|
|
|
|
|
token.as.inlineGround = malloc(strlen(ground) + 1);
|
|
|
|
|
if (token.as.inlineGround == NULL) {
|
|
|
|
|
return Error(SolsToken, charptr, "Couldn't allocate memory (in createSolsToken() function)");
|
|
|
|
|
}
|
|
|
|
|
strcpy(token.as.inlineGround, ground);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type == STT_LITERAL) {
|
|
|
|
|
token.as.literal = va_arg(args, SolsLiteral);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type == STT_TYPE) {
|
|
|
|
|
token.as.type = va_arg(args, SolsType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
return Success(SolsToken, charptr, token);
|
|
|
|
|
}
|
2026-02-05 20:22:25 +11:00
|
|
|
|
|
|
|
|
void freeSolsToken(SolsToken* token) {
|
|
|
|
|
if (token->type == STT_IDENTIFIER && token->as.idName != NULL) {
|
|
|
|
|
free(token->as.idName);
|
|
|
|
|
}
|
|
|
|
|
if (token->type == STT_KW_GROUND && token->as.inlineGround != NULL) {
|
|
|
|
|
free(token->as.inlineGround);
|
|
|
|
|
}
|
|
|
|
|
if (token->type == STT_LITERAL) {
|
|
|
|
|
freeSolsLiteral(&token->as.literal);
|
|
|
|
|
}
|
|
|
|
|
if (token->type == STT_TYPE) {
|
|
|
|
|
freeSolsType(&token->as.type);
|
|
|
|
|
}
|
|
|
|
|
}
|