Fixed a bug in the Scanner when parsing a hex number, still not super robust but it'll work.
This commit is contained in:
+38
-2
@@ -1,6 +1,8 @@
|
|||||||
#include "../includes/scanner.h"
|
#include "../includes/scanner.h"
|
||||||
|
#include <ctype.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
const char* SourceCode;
|
const char* SourceCode;
|
||||||
int Line = 1;
|
int Line = 1;
|
||||||
@@ -8,6 +10,7 @@ int Position = 0;
|
|||||||
int SourceLength = 0;
|
int SourceLength = 0;
|
||||||
int ScannerAtEnd(void);
|
int ScannerAtEnd(void);
|
||||||
int IsPunctuation(char);
|
int IsPunctuation(char);
|
||||||
|
int IsWhiteSpace(char);
|
||||||
char PeekScanner(void);
|
char PeekScanner(void);
|
||||||
char PeekAheadScanner(void);
|
char PeekAheadScanner(void);
|
||||||
void AdvanceScanner(void);
|
void AdvanceScanner(void);
|
||||||
@@ -34,6 +37,8 @@ List* GenerateTokenList(const char* source) {
|
|||||||
case ' ':
|
case ' ':
|
||||||
case '\r':
|
case '\r':
|
||||||
case '\t':
|
case '\t':
|
||||||
|
case '\v':
|
||||||
|
case '\f':
|
||||||
AdvanceScanner();
|
AdvanceScanner();
|
||||||
break; //Ignore whitespace
|
break; //Ignore whitespace
|
||||||
case '\n':
|
case '\n':
|
||||||
@@ -98,9 +103,21 @@ Token* ParseNumber(void) {
|
|||||||
while(!ScannerAtEnd() && isdigit(PeekScanner()))
|
while(!ScannerAtEnd() && isdigit(PeekScanner()))
|
||||||
AdvanceScanner();
|
AdvanceScanner();
|
||||||
|
|
||||||
if (PeekScanner() == 'x' && isdigit(PeekAheadScanner())) {
|
if (tolower(PeekScanner()) == 'x') {
|
||||||
|
char ahead = tolower(PeekAheadScanner());
|
||||||
|
|
||||||
|
if ((ahead >= 'a' && ahead <= 'f') || isdigit(ahead)) {
|
||||||
AdvanceScanner(); //Consume the 'x'
|
AdvanceScanner(); //Consume the 'x'
|
||||||
while(!ScannerAtEnd() && isdigit(PeekScanner())) AdvanceScanner();
|
|
||||||
|
while(!ScannerAtEnd() && PeekScanner() != '\n' && !IsWhiteSpace(PeekScanner())) {
|
||||||
|
if (isdigit(PeekScanner())) {
|
||||||
|
AdvanceScanner();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tolower(PeekScanner()) >= 'a' && tolower(PeekScanner()) <= 'f') AdvanceScanner();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int length = Position - start;
|
int length = Position - start;
|
||||||
@@ -119,6 +136,12 @@ Token* ParseNumber(void) {
|
|||||||
Token* token = CreateToken(Line, NumberClass);
|
Token* token = CreateToken(Line, NumberClass);
|
||||||
|
|
||||||
token->Value.Number = strtol(lexeme, NULL, 0);
|
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;
|
token->Lemexe = lexeme;
|
||||||
|
|
||||||
return token;
|
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) {
|
Token* ParsePunctuation(char c) {
|
||||||
TokenPunctuation punctuation;
|
TokenPunctuation punctuation;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user