Fixed a bug where numbers weren't being added to the token list (I'm very dumb it seems) and added hexadecimal support to the scanner.

This commit is contained in:
2022-05-04 22:37:49 -05:00
parent 101f206dce
commit caeaaefbf0
2 changed files with 31 additions and 12 deletions
+26 -11
View File
@@ -11,6 +11,7 @@ int SourceLength = 0;
int ScannerAtEnd(void);
int IsPunctuation(char);
char PeekScanner(void);
char PeekAheadScanner(void);
void AdvanceScanner(void);
Token* ParseString(void);
Token* ParseDirective(void);
@@ -53,8 +54,8 @@ List* GenerateTokenList(const char* source) {
if (token) AddListItem(token, sizeof(Token), tokens);
break;
default:
if (c >= '0' && c <= '9') {
ParseNumber();
if (isdigit(c)) {
AddListItem(ParseNumber(), sizeof(Token), tokens);
break;
}
@@ -80,10 +81,17 @@ List* GenerateTokenList(const char* source) {
Token* ParseNumber(void) {
int start = Position;
int base = 10;
while(!ScannerAtEnd() && PeekScanner() >= '0' && PeekScanner() <= '9')
while(!ScannerAtEnd() && isdigit(PeekScanner()))
AdvanceScanner();
if (PeekScanner() == 'x' && isdigit(PeekAheadScanner())) {
base = 16;
AdvanceScanner(); //Consume the 'x'
while(!ScannerAtEnd() && isdigit(PeekScanner())) AdvanceScanner();
}
char* lexeme = calloc(Position - start + 1, sizeof(char));
if (!lexeme) {
@@ -93,7 +101,9 @@ Token* ParseNumber(void) {
memcpy(lexeme, &SourceCode[start], Position - start);
return CreateToken(NUMBER, lexeme);
if (base == 10) return CreateToken(NUMBER, lexeme);
return CreateToken(HEX, lexeme);
}
Token* ParseDirective(void) {
@@ -114,12 +124,12 @@ Token* ParseDirective(void) {
if (strcmp(directive, ".db") == 0) {
return CreateToken(DB, directive);
}
// else if (strcmp(directive, ".org") == 0) {
// fprintf(stderr, "[Warning] Org is not a supported directive.\n");
// IgnoreLine();
// return NULL;
// }
}
else if (strcmp(directive, ".org") == 0) {
fprintf(stderr, "[Warning] Org is not a supported directive.\n");
IgnoreLine();
return NULL;
}
fprintf(stderr, "[Error] Unknown assembler directive '%s'\n", directive);
@@ -179,6 +189,12 @@ char PeekScanner(void) {
return SourceCode[Position];
}
char PeekAheadScanner(void) {
if (Position + 1 >= SourceLength) return '\0';
return SourceCode[Position + 1];
}
void AdvanceScanner(void) {
if (ScannerAtEnd()) return;
@@ -197,7 +213,6 @@ int IsPunctuation(char c) {
case ')':
case ',':
return 1;
default:
return 0;
}