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
+4
View File
@@ -29,9 +29,13 @@ $(OBJDIR):
.PHONY: clean .PHONY: clean
.PHONY: test .PHONY: test
.PHONY: disass
clean: clean:
rm -rf $(BINDIR)/* $(OBJDIR)/* rm -rf $(BINDIR)/* $(OBJDIR)/*
test: test:
$(BIN) misc/test.asm $(BIN) misc/test.asm
disass:
objdump -S --disassemble $(OBJDIR)/$(FILE).o > $(OBJDIR)/$(FILE).s
+25 -10
View File
@@ -11,6 +11,7 @@ int SourceLength = 0;
int ScannerAtEnd(void); int ScannerAtEnd(void);
int IsPunctuation(char); int IsPunctuation(char);
char PeekScanner(void); char PeekScanner(void);
char PeekAheadScanner(void);
void AdvanceScanner(void); void AdvanceScanner(void);
Token* ParseString(void); Token* ParseString(void);
Token* ParseDirective(void); Token* ParseDirective(void);
@@ -53,8 +54,8 @@ List* GenerateTokenList(const char* source) {
if (token) AddListItem(token, sizeof(Token), tokens); if (token) AddListItem(token, sizeof(Token), tokens);
break; break;
default: default:
if (c >= '0' && c <= '9') { if (isdigit(c)) {
ParseNumber(); AddListItem(ParseNumber(), sizeof(Token), tokens);
break; break;
} }
@@ -80,10 +81,17 @@ List* GenerateTokenList(const char* source) {
Token* ParseNumber(void) { Token* ParseNumber(void) {
int start = Position; int start = Position;
int base = 10;
while(!ScannerAtEnd() && PeekScanner() >= '0' && PeekScanner() <= '9') while(!ScannerAtEnd() && isdigit(PeekScanner()))
AdvanceScanner(); 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)); char* lexeme = calloc(Position - start + 1, sizeof(char));
if (!lexeme) { if (!lexeme) {
@@ -93,7 +101,9 @@ Token* ParseNumber(void) {
memcpy(lexeme, &SourceCode[start], Position - start); memcpy(lexeme, &SourceCode[start], Position - start);
return CreateToken(NUMBER, lexeme); if (base == 10) return CreateToken(NUMBER, lexeme);
return CreateToken(HEX, lexeme);
} }
Token* ParseDirective(void) { Token* ParseDirective(void) {
@@ -115,11 +125,11 @@ Token* ParseDirective(void) {
if (strcmp(directive, ".db") == 0) { if (strcmp(directive, ".db") == 0) {
return CreateToken(DB, directive); return CreateToken(DB, directive);
} }
// 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");
// IgnoreLine(); IgnoreLine();
// return NULL; return NULL;
// } }
fprintf(stderr, "[Error] Unknown assembler directive '%s'\n", directive); fprintf(stderr, "[Error] Unknown assembler directive '%s'\n", directive);
@@ -179,6 +189,12 @@ char PeekScanner(void) {
return SourceCode[Position]; return SourceCode[Position];
} }
char PeekAheadScanner(void) {
if (Position + 1 >= SourceLength) return '\0';
return SourceCode[Position + 1];
}
void AdvanceScanner(void) { void AdvanceScanner(void) {
if (ScannerAtEnd()) return; if (ScannerAtEnd()) return;
@@ -197,7 +213,6 @@ int IsPunctuation(char c) {
case ')': case ')':
case ',': case ',':
return 1; return 1;
default: default:
return 0; return 0;
} }