Updated the scanner to parse registers, assembly keywords and identifiers.
This commit is contained in:
+75
-27
@@ -1,9 +1,11 @@
|
||||
#include "../includes/scanner.h"
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
const char* SourceCode;
|
||||
int Line = 0;
|
||||
int Position = 0;
|
||||
int SourceLength = 0;
|
||||
int ScannerAtEnd(void);
|
||||
@@ -12,6 +14,8 @@ char AdvanceScanner(void);
|
||||
Token* ParseString(void);
|
||||
Token* ParseDirective(void);
|
||||
Token* ParseIdentifier(void);
|
||||
Token* ParseNumber(void);
|
||||
void IgnoreLine(void);
|
||||
|
||||
List* GenerateTokenList(const char* source) {
|
||||
List* tokens = CreateList();
|
||||
@@ -23,15 +27,20 @@ List* GenerateTokenList(const char* source) {
|
||||
SourceLength = strlen(source);
|
||||
|
||||
for(int i = 0; i < SourceLength; i++) {
|
||||
char c = AdvanceScanner();
|
||||
char c = PeekScanner();
|
||||
|
||||
switch(c) {
|
||||
case ' ':
|
||||
case '\r':
|
||||
case '\t':
|
||||
AdvanceScanner();
|
||||
break; //Ignore whitespace
|
||||
case '\n':
|
||||
AdvanceScanner();
|
||||
Line++;
|
||||
break;
|
||||
case ';':
|
||||
while(!ScannerAtEnd() && PeekScanner() != '\n') AdvanceScanner();
|
||||
|
||||
IgnoreLine();
|
||||
break;
|
||||
case '.': //directive like ".org" or ".db"
|
||||
token = ParseDirective();
|
||||
@@ -42,15 +51,15 @@ List* GenerateTokenList(const char* source) {
|
||||
if (token) AddListItem(token, sizeof(Token), tokens);
|
||||
break;
|
||||
case ',':
|
||||
AdvanceScanner();
|
||||
AddListItem(CreateToken(COMMA, ","), sizeof(Token), tokens);
|
||||
break;
|
||||
default:
|
||||
// if (c >= '0' && c <= '9') {
|
||||
// //Number
|
||||
|
||||
// break;
|
||||
// }
|
||||
if (c >= '0' && c <= '9') {
|
||||
ParseNumber();
|
||||
|
||||
break;
|
||||
}
|
||||
token = ParseIdentifier();
|
||||
|
||||
if (token) AddListItem(token, sizeof(Token), tokens);
|
||||
@@ -64,31 +73,66 @@ List* GenerateTokenList(const char* source) {
|
||||
return tokens;
|
||||
}
|
||||
|
||||
Token* ParseDirective(void) {
|
||||
char directive[10] = { 0 };
|
||||
int length = 0;
|
||||
Token* ParseNumber(void) {
|
||||
int start = Position;
|
||||
|
||||
for(; length < sizeof directive - 1; length++) {
|
||||
if (PeekScanner() == ' ') break;
|
||||
while(!ScannerAtEnd() && PeekScanner() >= '0' && PeekScanner() <= '9')
|
||||
AdvanceScanner();
|
||||
|
||||
directive[length] = AdvanceScanner();
|
||||
}
|
||||
char* lexeme = calloc(Position - start + 1, sizeof(char));
|
||||
|
||||
if (strcmp(directive, ".db") == 0) {
|
||||
return CreateToken(DB, ".db");
|
||||
} else if (strcmp(directive, ".org") == 0) {
|
||||
//fprintf(stderr, "[Warning] Org is not a supported directive.\n");
|
||||
if (!lexeme) {
|
||||
fprintf(stderr, "Failed to calloc space for number.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fprintf(stderr, "[Error] Unknown assembler directive '%s'\n", directive);
|
||||
//Ignore the line
|
||||
while(PeekScanner() != '\n') AdvanceScanner();
|
||||
memcpy(lexeme, &SourceCode[start], Position - start);
|
||||
|
||||
return CreateToken(NUMBER, lexeme);
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
if (!directive) {
|
||||
fprintf(stderr, "Failed to calloc for the assmebler directive.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(directive, &SourceCode[start], Position - start);
|
||||
|
||||
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;
|
||||
// }
|
||||
|
||||
fprintf(stderr, "[Error] Unknown assembler directive '%s'\n", directive);
|
||||
|
||||
free(directive);
|
||||
|
||||
//Ignore the line
|
||||
IgnoreLine();
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void IgnoreLine(void) {
|
||||
while(!ScannerAtEnd() && PeekScanner() != '\n') AdvanceScanner();
|
||||
}
|
||||
|
||||
Token* ParseString(void) {
|
||||
AdvanceScanner(); //Consume the first double quote.
|
||||
|
||||
int start = Position;
|
||||
|
||||
while(!ScannerAtEnd() && PeekScanner() != '"') {
|
||||
@@ -96,9 +140,9 @@ Token* ParseString(void) {
|
||||
}
|
||||
|
||||
char* lexeme = calloc(Position - start, sizeof(char));
|
||||
memccpy(lexeme, &SourceCode[start], sizeof(char), Position - start);
|
||||
memcpy(lexeme, &SourceCode[start], Position - start);
|
||||
|
||||
AdvanceScanner();
|
||||
AdvanceScanner(); //Consume the trailing double quote.
|
||||
|
||||
Token* token = CreateToken(STRING, lexeme);
|
||||
|
||||
@@ -108,20 +152,24 @@ Token* ParseString(void) {
|
||||
Token* ParseIdentifier(void) {
|
||||
int start = Position;
|
||||
|
||||
while(!ScannerAtEnd() && PeekScanner() != ' ' && PeekScanner() != '\n') {
|
||||
while(!ScannerAtEnd() && PeekScanner() != ',' && PeekScanner() != ' ' && PeekScanner() != '\n') {
|
||||
AdvanceScanner();
|
||||
}
|
||||
|
||||
if (Position - start == 0) return NULL;
|
||||
|
||||
char* lexeme = calloc(Position - start, sizeof(char));
|
||||
memccpy(lexeme, &SourceCode[start], sizeof(char), Position - start);
|
||||
TokenType type;
|
||||
memcpy(lexeme, &SourceCode[start], Position - start);
|
||||
|
||||
if (IsOpcode(lexeme, &type)) return CreateToken(type, lexeme);
|
||||
if (IsRegister(lexeme, &type)) return CreateToken(type, lexeme);
|
||||
|
||||
return CreateToken(IDENTIFIER, lexeme);
|
||||
}
|
||||
|
||||
char PeekScanner(void) {
|
||||
if (Position > SourceLength) return '\0';
|
||||
if (Position >= SourceLength) return '\0';
|
||||
|
||||
return SourceCode[Position];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user