Fixed up the code a bit and fixed a memory leak bug (of course, there's still no good way to free Tokens but that'll be sorted out later).

This commit is contained in:
2022-05-05 20:10:10 +00:00
parent 8ec0b9b216
commit fc47bf2143
2 changed files with 15 additions and 6 deletions
+14 -5
View File
@@ -1,4 +1,6 @@
#include "../includes/scanner.h"
#include <stdlib.h>
#include <string.h>
const char* SourceCode;
int Line = 0;
@@ -93,20 +95,26 @@ Token* ParseNumber(void) {
if (length == 0) return NULL;
char* lexeme = calloc(sizeof(char), length + 1);
long* value = calloc(1, sizeof(long));
if (!lexeme) {
fprintf(stderr, "Failed to calloc space for number. %s.\n", strerror(errno));
return NULL;
}
if (!value) {
free(lexeme);
fprintf(stderr, "Failed to calloc space for the raw numeric value of a token. %s.\n", strerror(errno));
return NULL;
}
memcpy(lexeme, &SourceCode[start], length);
//Setting the base to zero means the function will detect the base.
//https://pubs.opengroup.org/onlinepubs/7908799/xsh/strtol.html
printf("Number Parsed: %s (%ld)\n", lexeme, strtol(lexeme, NULL, 0));
*value = strtol(lexeme, NULL, base);
if (base == 10) return CreateToken(lexeme, lexeme, Line, NUMBER);
if (base == 10) return CreateToken(lexeme, value, Line, NUMBER);
return CreateToken(lexeme, lexeme, Line, HEX);
return CreateToken(lexeme, value, Line, HEX);
}
Token* ParseDirective(void) {
@@ -132,6 +140,7 @@ Token* ParseDirective(void) {
}
else if (strcmp(directive, ".org") == 0) {
fprintf(stderr, "[Warning] Org is not a supported directive.\n");
free(directive);
IgnoreLine();
return NULL;
}