createSolsToken()
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user