Fixed a bug when the scanner parses a number and creates the token.

This commit is contained in:
2022-03-16 00:00:17 -05:00
parent b072921304
commit 6daaaae7c2
+11 -2
View File
@@ -1,5 +1,6 @@
#include "scanner.h" #include "scanner.h"
#include "token.h" #include "token.h"
#include <string.h>
const char* source_code; const char* source_code;
//Start and Current hold the offsets that index into the string source_code. //Start and Current hold the offsets that index into the string source_code.
@@ -245,9 +246,17 @@ void ParseNumber(TokenList* list) {
} }
snprintf(lexeme, current - start, "%s", &source_code[start]); snprintf(lexeme, current - start, "%s", &source_code[start]);
double value = atof(lexeme); //Will this reference become stale on return? double* value = calloc(1, sizeof(double));
if (!value) {
fprintf(stderr, "Failed to calloc for number value. %s\n", strerror(errno));
free(lexeme);
return;
}
*value = atof(lexeme);
AddToTokenList(CreateToken(lexeme, &value, line, Number), list); AddToTokenList(CreateToken(lexeme, value, line, Number), list);
} }
char PeekNext() { char PeekNext() {