Added support for 'labels' in the Scanner.
This commit is contained in:
@@ -16,6 +16,7 @@ typedef enum {
|
|||||||
COMMA,
|
COMMA,
|
||||||
STRING,
|
STRING,
|
||||||
IDENTIFIER,
|
IDENTIFIER,
|
||||||
|
LABEL,
|
||||||
NUMBER,
|
NUMBER,
|
||||||
HEX,
|
HEX,
|
||||||
//Keywords
|
//Keywords
|
||||||
|
|||||||
+25
-12
@@ -92,14 +92,18 @@ Token* ParseNumber(void) {
|
|||||||
while(!ScannerAtEnd() && isdigit(PeekScanner())) AdvanceScanner();
|
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) {
|
if (!lexeme) {
|
||||||
fprintf(stderr, "Failed to calloc space for number.\n");
|
fprintf(stderr, "Failed to calloc space for number.\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(lexeme, &SourceCode[start], Position - start);
|
memcpy(lexeme, &SourceCode[start], length);
|
||||||
|
|
||||||
if (base == 10) return CreateToken(NUMBER, lexeme);
|
if (base == 10) return CreateToken(NUMBER, lexeme);
|
||||||
|
|
||||||
@@ -110,17 +114,19 @@ Token* ParseDirective(void) {
|
|||||||
int start = Position;
|
int start = Position;
|
||||||
|
|
||||||
while(!ScannerAtEnd() && PeekScanner() != ' ' && PeekScanner() != '\n') AdvanceScanner();
|
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) {
|
if (!directive) {
|
||||||
fprintf(stderr, "Failed to calloc for the assmebler directive.\n");
|
fprintf(stderr, "Failed to calloc for the assmebler directive.\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(directive, &SourceCode[start], Position - start);
|
memcpy(directive, &SourceCode[start], length);
|
||||||
|
|
||||||
if (strcmp(directive, ".db") == 0) {
|
if (strcmp(directive, ".db") == 0) {
|
||||||
return CreateToken(DB, directive);
|
return CreateToken(DB, directive);
|
||||||
@@ -154,8 +160,12 @@ Token* ParseString(void) {
|
|||||||
AdvanceScanner();
|
AdvanceScanner();
|
||||||
}
|
}
|
||||||
|
|
||||||
char* lexeme = calloc(Position - start, sizeof(char));
|
int length = Position - start;
|
||||||
memcpy(lexeme, &SourceCode[start], Position - start);
|
|
||||||
|
if (length == 0) return NULL;
|
||||||
|
|
||||||
|
char* lexeme = calloc(sizeof(char), length);
|
||||||
|
memcpy(lexeme, &SourceCode[start], length);
|
||||||
|
|
||||||
AdvanceScanner(); //Consume the trailing double quote.
|
AdvanceScanner(); //Consume the trailing double quote.
|
||||||
|
|
||||||
@@ -171,14 +181,17 @@ Token* ParseIdentifier(void) {
|
|||||||
AdvanceScanner();
|
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;
|
||||||
TokenType type;
|
char* lexeme = calloc(sizeof(char), length);
|
||||||
memcpy(lexeme, &SourceCode[start], Position - start);
|
memcpy(lexeme, &SourceCode[start], length);
|
||||||
|
|
||||||
if (IsOpcode(lexeme, &type)) return CreateToken(type, lexeme);
|
if (IsOpcode(lexeme, &type)) return CreateToken(type, lexeme);
|
||||||
if (IsRegister(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);
|
return CreateToken(IDENTIFIER, lexeme);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user