Added support for 'labels' in the Scanner.
This commit is contained in:
+25
-12
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user