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:
+1
-1
@@ -26,7 +26,7 @@ int main(int argc, char* args[]) {
|
|||||||
Token* t = (Token*) list->content[i];
|
Token* t = (Token*) list->content[i];
|
||||||
|
|
||||||
printf("[%i] '%s'", t->type, t->lexeme);
|
printf("[%i] '%s'", t->type, t->lexeme);
|
||||||
if (t->type == NUMBER || t->type == HEX) printf(" NUM: %ld", (long) t->value);
|
if (t->type == NUMBER || t->type == HEX) printf(" NUM: %ld", *((long *) t->value));
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+14
-5
@@ -1,4 +1,6 @@
|
|||||||
#include "../includes/scanner.h"
|
#include "../includes/scanner.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
const char* SourceCode;
|
const char* SourceCode;
|
||||||
int Line = 0;
|
int Line = 0;
|
||||||
@@ -93,20 +95,26 @@ Token* ParseNumber(void) {
|
|||||||
if (length == 0) return NULL;
|
if (length == 0) return NULL;
|
||||||
|
|
||||||
char* lexeme = calloc(sizeof(char), length + 1);
|
char* lexeme = calloc(sizeof(char), length + 1);
|
||||||
|
long* value = calloc(1, sizeof(long));
|
||||||
|
|
||||||
if (!lexeme) {
|
if (!lexeme) {
|
||||||
fprintf(stderr, "Failed to calloc space for number. %s.\n", strerror(errno));
|
fprintf(stderr, "Failed to calloc space for number. %s.\n", strerror(errno));
|
||||||
return NULL;
|
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);
|
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
|
*value = strtol(lexeme, NULL, base);
|
||||||
printf("Number Parsed: %s (%ld)\n", lexeme, strtol(lexeme, NULL, 0));
|
|
||||||
|
|
||||||
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) {
|
Token* ParseDirective(void) {
|
||||||
@@ -132,6 +140,7 @@ Token* ParseDirective(void) {
|
|||||||
}
|
}
|
||||||
else if (strcmp(directive, ".org") == 0) {
|
else if (strcmp(directive, ".org") == 0) {
|
||||||
fprintf(stderr, "[Warning] Org is not a supported directive.\n");
|
fprintf(stderr, "[Warning] Org is not a supported directive.\n");
|
||||||
|
free(directive);
|
||||||
IgnoreLine();
|
IgnoreLine();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user