Cleaned up the TokenStringify function and started toying with ways to handle syncing in the parser.

This commit is contained in:
2025-06-26 00:36:37 -05:00
parent b759589e84
commit 3a5788f4ab
6 changed files with 70 additions and 116 deletions
+38 -86
View File
@@ -1,5 +1,7 @@
#include "../includes/token.h"
const Token TokenEmpty = { 0 };
struct _token* TokenCreate(TokenType type, int lineNumber, int columnNumber, bool resolved) {
struct _token* token = calloc(1, sizeof(Token));
@@ -12,112 +14,62 @@ struct _token* TokenCreate(TokenType type, int lineNumber, int columnNumber, boo
}
char* TokenStringify(const struct _token* token) {
if (!token) return false;
if (!token) return NULL;
char word[32];
char* buffer = NULL;
size_t bufferSize = 64;
char* str;
if (token->Type == String) {
// Add three for 2 quotes and a NUL byte.
bufferSize = strlen(token->Value.String) + 3;
str = malloc(bufferSize);
snprintf(str, bufferSize, "\"%s\"", token->Value.String);
return str;
}
else if (token->Type == Symbol) {
bufferSize = strlen(token->Value.String) + 1;
str = calloc(bufferSize, sizeof(char));
strncpy(str, token->Value.String, bufferSize);
return str;
}
else {
str = malloc(bufferSize * sizeof(char));
}
char buffer[32] = { 0 };
switch(token->Type) {
case Register:
GetRegisterText(token->Value.Register, word);
buffer = calloc(sizeof(word), sizeof(char));
strncpy(buffer, word, sizeof(word));
GetRegisterText(token->Value.Register, buffer);
break;
case Mnemonic:
GetMnemonicText(token->Value.Mnemonic, word);
buffer = calloc(sizeof(word), sizeof(char));
strncpy(buffer, word, sizeof(word));
GetMnemonicText(token->Value.Mnemonic, buffer);
break;
case Keyword:
GetKeywordText(token->Value.Keyword, word);
buffer = calloc(sizeof(word), sizeof(char));
strncpy(buffer, word, sizeof(word));
break;
case String:
case Symbol:
buffer = calloc(strlen(token->Value.String) + 1, sizeof(char));
strncpy(buffer, token->Value.String, strlen(token->Value.String));
GetKeywordText(token->Value.Keyword, buffer);
break;
case Empty:
buffer = calloc(8, sizeof(char));
strncpy(buffer, "<>", sizeof(char) * 2);
strcpy(str, "(Empty)");
break;
case Number:
buffer = calloc(32, sizeof(char));
sprintf(buffer, "%d", token->Value.Number);
sprintf(str, "%d", token->Value.Number);
break;
case LineEnd:
buffer = calloc(8, sizeof(char));
strncpy(buffer, "\n", sizeof(char));
strcpy(str, "(LineEnd)");
break;
case FileEnd:
break;
default:
buffer = calloc(16, sizeof(char));
strncpy(buffer, token->Value.Operator, sizeof(char));
sprintf(str, "%s", token->Value.Operator);
break;
}
return buffer;
}
if (strlen(buffer) > 0) strncpy(str, buffer, sizeof(buffer) - 1);
int StringifyTokenType(TokenType type, char buffer[32]) {
if (!buffer) return 0;
memset(buffer, '\0', 32);
switch(type) {
case Empty:
strncpy(buffer, "<empty >", 8);
break;
case Register:
strncpy(buffer, "<Reg >", 8);
break;
case Mnemonic:
strncpy(buffer, "<Mnemon>", 8);
break;
case Keyword:
strncpy(buffer, "<Key >", 8);
break;
case String:
strncpy(buffer, "<string>", 8);
break;
case Number:
strncpy(buffer, "<number>", 8);
break;
case Symbol:
strncpy(buffer, "<symbol>", 8);
break;
case LineEnd:
strncpy(buffer, "<LnEnd >", 8);
break;
case FileEnd:
strncpy(buffer, "<filend>", 8);
break;
default:
snprintf(buffer, 8, "<%c >", type);
break;
}
return 8;
return str;
}