Incomplete, but I want to make sure the ideas I have here don't get wiped. Sadly this commit won't compile.

This commit is contained in:
2022-09-15 21:29:27 +00:00
parent 6d17dffcdf
commit 2ae60a607c
8 changed files with 238 additions and 210 deletions
+59 -23
View File
@@ -37,7 +37,8 @@ List* GenerateTokenList(const char* source) {
AdvanceScanner();
break; //Ignore whitespace
case '\n':
token = CreateToken("^", NULL, Line, LineEnd);
token = CreateToken(Line, PunctuationClass);
token->Value.Punctuation = NewLine;
AddListItem(token, sizeof(Token), tokens);
AdvanceScanner();
Line++;
@@ -95,24 +96,20 @@ Token* ParseNumber(void) {
if (length == 0) return NULL;
char* lexeme = calloc(sizeof(char), length + 1);
long* value = calloc(1, sizeof(long));
if (!lexeme) {
fprintf(stderr, "Failed to calloc space for number. %s.\n", strerror(errno));
return NULL;
}
if (!value) {
free(lexeme);
fprintf(stderr, "Failed to calloc space for the raw numeric value of a token. %s.\n", strerror(errno));
return NULL;
}
memcpy(lexeme, &SourceCode[start], length);
// Setting the base to zero means the function will pick the base.
*value = strtol(lexeme, NULL, 0);
Token* token = CreateToken(Line, NumberClass);
return CreateToken(lexeme, value, Line, NUMBER);
token->Value.Number = strtol(lexeme, NULL, 0);
token->Lemexe = lexeme;
return token;
}
Token* ParseDirective(void) {
@@ -133,12 +130,17 @@ Token* ParseDirective(void) {
memcpy(directive, &SourceCode[start], length);
Token* token = CreateToken(Line, DirectiveClass);
if (strcmp(directive, ".db") == 0) {
return CreateToken(directive, directive, Line, DB);
token->Value.Directive = DB;
return token;
}
else if (strcmp(directive, ".org") == 0) {
fprintf(stderr, "[Warning] Org is not a supported directive.\n");
free(directive);
free(token);
IgnoreLine();
return NULL;
}
@@ -178,7 +180,9 @@ Token* ParseString(void) {
AdvanceScanner(); //Consume the trailing double quote.
Token* token = CreateToken(lexeme, lexeme, Line, STRING);
Token* token = CreateToken(Line, CharacterClass);
token->Lemexe = lexeme;
return token;
}
@@ -194,7 +198,8 @@ Token* ParseIdentifier(void) {
if (length == 0) return NULL;
TokenType type;
Mnemonic mnemonics;
Registers reg;
char* lexeme = calloc(sizeof(char), length + 1);
if (!lexeme) {
@@ -204,14 +209,33 @@ Token* ParseIdentifier(void) {
memcpy(lexeme, &SourceCode[start], length);
if (IsOpcode(lexeme, &type)) return CreateToken(lexeme, lexeme, Line, type);
if (IsRegister(lexeme, &type)) return CreateToken(lexeme, lexeme, Line, type);
if (IsOpcode(lexeme, &mnemonics)) {
Token* token = CreateToken(Line, MnemonicClass);
token->Value.Mnemonic = mnemonics;
return token;
}
if (IsRegister(lexeme, &reg)) {
Token* token = CreateToken(Line, RegisterClass);
token->Value.Register = reg;
return token;
}
if (lexeme[length - 1] == ':') {
lexeme[length - 1] = '\0'; //Bit hacky, but this makes the parser's job a bit easier.
return CreateToken(lexeme, lexeme, Line, LABEL);
Token* token = CreateToken(Line, LabelClass);
token->Lemexe = lexeme;
return token;
}
return CreateToken(lexeme, lexeme, Line, IDENTIFIER);
Token* token = CreateToken(Line, IdentifierClass);
token->Lemexe = lexeme;
return token;
}
char PeekScanner(void) {
@@ -250,18 +274,30 @@ int IsPunctuation(char c) {
}
Token* ParsePunctuation(char c) {
TokenPunctuation punctuation;
switch(c) {
case '[':
return CreateToken("[", NULL, Line, LBRACKET);
punctuation = LBracket;
break;
case ']':
return CreateToken("]", NULL, Line, RBracket);
punctuation = RBracket;
break;
case '(':
return CreateToken("(", NULL, Line, LPARAM);
punctuation = LParan;
break;
case ')':
return CreateToken(")", NULL, Line, RPARAM);
punctuation = RParan;
break;
case ',':
return CreateToken(",", NULL, Line, COMMA);
punctuation = Comma;
break;
default:
return NULL;
}
Token* token = CreateToken(Line, PunctuationClass);
token->Value.Punctuation = punctuation;
return token;
}