From caeaaefbf04d17977fd15c87379b3e5af643ab81 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Wed, 4 May 2022 22:37:49 -0500 Subject: [PATCH] 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. --- Makefile | 6 +++++- src/scanner.c | 37 ++++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 5c4960a..57d55ea 100644 --- a/Makefile +++ b/Makefile @@ -29,9 +29,13 @@ $(OBJDIR): .PHONY: clean .PHONY: test +.PHONY: disass clean: rm -rf $(BINDIR)/* $(OBJDIR)/* test: - $(BIN) misc/test.asm \ No newline at end of file + $(BIN) misc/test.asm + +disass: + objdump -S --disassemble $(OBJDIR)/$(FILE).o > $(OBJDIR)/$(FILE).s \ No newline at end of file diff --git a/src/scanner.c b/src/scanner.c index e0862f6..48ffe8e 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -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; }