Added support for 'labels' in the Scanner.

This commit is contained in:
2022-05-04 22:58:06 -05:00
parent caeaaefbf0
commit 9fa48520c7
2 changed files with 26 additions and 12 deletions
+25 -12
View File
@@ -92,14 +92,18 @@ Token* ParseNumber(void) {
while(!ScannerAtEnd() && isdigit(PeekScanner())) AdvanceScanner();
}
char* lexeme = calloc(Position - start + 1, sizeof(char));
int length = Position - start;
if (length == 0) return NULL;
char* lexeme = calloc(sizeof(char), length);
if (!lexeme) {
fprintf(stderr, "Failed to calloc space for number.\n");
return NULL;
}
memcpy(lexeme, &SourceCode[start], Position - start);
memcpy(lexeme, &SourceCode[start], length);
if (base == 10) return CreateToken(NUMBER, lexeme);
@@ -110,17 +114,19 @@ Token* ParseDirective(void) {
int start = Position;
while(!ScannerAtEnd() && PeekScanner() != ' ' && PeekScanner() != '\n') AdvanceScanner();
if (start == Position) return NULL;
char* directive = calloc(Position - start + 1, sizeof(char));
int length = Position - start;
if (length == 0) return NULL;
char* directive = calloc(sizeof(char), length);
if (!directive) {
fprintf(stderr, "Failed to calloc for the assmebler directive.\n");
return NULL;
}
memcpy(directive, &SourceCode[start], Position - start);
memcpy(directive, &SourceCode[start], length);
if (strcmp(directive, ".db") == 0) {
return CreateToken(DB, directive);
@@ -154,8 +160,12 @@ Token* ParseString(void) {
AdvanceScanner();
}
char* lexeme = calloc(Position - start, sizeof(char));
memcpy(lexeme, &SourceCode[start], Position - start);
int length = Position - start;
if (length == 0) return NULL;
char* lexeme = calloc(sizeof(char), length);
memcpy(lexeme, &SourceCode[start], length);
AdvanceScanner(); //Consume the trailing double quote.
@@ -171,14 +181,17 @@ Token* ParseIdentifier(void) {
AdvanceScanner();
}
if (Position - start == 0) return NULL;
int length = Position - start;
if (length == 0) return NULL;
char* lexeme = calloc(Position - start, sizeof(char));
TokenType type;
memcpy(lexeme, &SourceCode[start], Position - start);
TokenType type;
char* lexeme = calloc(sizeof(char), length);
memcpy(lexeme, &SourceCode[start], length);
if (IsOpcode(lexeme, &type)) return CreateToken(type, lexeme);
if (IsRegister(lexeme, &type)) return CreateToken(type, lexeme);
if (lexeme[length - 1] == ':') return CreateToken(LABEL, lexeme);
return CreateToken(IDENTIFIER, lexeme);
}