Fixed a bug in the Scanner when parsing a hex number, still not super robust but it'll work.

This commit is contained in:
2022-10-03 15:20:31 +00:00
parent f7cd87f13f
commit a0d5d62a34
+39 -3
View File
@@ -1,6 +1,8 @@
#include "../includes/scanner.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
const char* SourceCode;
int Line = 1;
@@ -8,6 +10,7 @@ int Position = 0;
int SourceLength = 0;
int ScannerAtEnd(void);
int IsPunctuation(char);
int IsWhiteSpace(char);
char PeekScanner(void);
char PeekAheadScanner(void);
void AdvanceScanner(void);
@@ -34,6 +37,8 @@ List* GenerateTokenList(const char* source) {
case ' ':
case '\r':
case '\t':
case '\v':
case '\f':
AdvanceScanner();
break; //Ignore whitespace
case '\n':
@@ -98,9 +103,21 @@ Token* ParseNumber(void) {
while(!ScannerAtEnd() && isdigit(PeekScanner()))
AdvanceScanner();
if (PeekScanner() == 'x' && isdigit(PeekAheadScanner())) {
AdvanceScanner(); //Consume the 'x'
while(!ScannerAtEnd() && isdigit(PeekScanner())) AdvanceScanner();
if (tolower(PeekScanner()) == 'x') {
char ahead = tolower(PeekAheadScanner());
if ((ahead >= 'a' && ahead <= 'f') || isdigit(ahead)) {
AdvanceScanner(); //Consume the 'x'
while(!ScannerAtEnd() && PeekScanner() != '\n' && !IsWhiteSpace(PeekScanner())) {
if (isdigit(PeekScanner())) {
AdvanceScanner();
continue;
}
if (tolower(PeekScanner()) >= 'a' && tolower(PeekScanner()) <= 'f') AdvanceScanner();
}
}
}
int length = Position - start;
@@ -119,6 +136,12 @@ Token* ParseNumber(void) {
Token* token = CreateToken(Line, NumberClass);
token->Value.Number = strtol(lexeme, NULL, 0);
if (errno != 0) {
fprintf(stderr, "[Error] Line %d: Invalid number detected. %s.\n", Line, strerror(errno));
exit(1);
}
token->Lemexe = lexeme;
return token;
@@ -285,6 +308,19 @@ int IsPunctuation(char c) {
}
}
int IsWhiteSpace(char c) {
switch(c) {
case ' ':
case '\t':
case '\v':
case '\f':
case '\r':
return 1;
default:
return 0;
}
}
Token* ParsePunctuation(char c) {
TokenPunctuation punctuation;