Moved some of the grunt work to their own functions to clean the code up a bit.

This commit is contained in:
2023-03-28 22:14:31 -05:00
parent 2d7aad1617
commit fa12a04bb3
+73 -60
View File
@@ -3,6 +3,10 @@
#include <stdlib.h>
#include <string.h>
typedef enum {
NoOptions = 0, ForwardParser = 1, RemoveExpected = 2
} ExpectOptions;
List* TokensList;
int CurrentToken = 0;
@@ -18,7 +22,9 @@ int ParserAtEnd(void);
int Expect(TokenClass class, void* value);
IRState MachineState;
//int ExpectPuncuation(TokenPunctuation punctuation);
void ExpectPuncuation(TokenPunctuation punctuation, ExpectOptions options);
void ExpectRegister(void);
Symbol* ExpectIdentifier(int resolved, ExpectOptions options);
//int ExpectIdentifier();
int HeapSize = 0;
@@ -35,7 +41,6 @@ IRState* ParseTokens(List* tokens) {
while(!ParserAtEnd()) {
Token* t = PeekToken();
if (!t || t->EndOfFile) break;
int last = CurrentToken;
Symbol* tmp;
switch(t->Class) {
@@ -43,7 +48,7 @@ IRState* ParseTokens(List* tokens) {
HandleAssemblerDirective();
break;
case MnemonicClass:
HandleOperation();
//HandleOperation();
break;
case LabelClass:
{
@@ -87,31 +92,7 @@ void HandleAssemblerDirective() {
{
RemoveCurrentToken();
Token* identifier = PeekToken();
if (identifier->Class != IdentifierClass) {
fprintf(stderr, "[Line %d] Expected Identifier '%c'\n", identifier->LineNumber, identifier->Value.Punctuation);
exit(30);
}
Symbol* symbol;
int found = TryGetSymbol(identifier->Lemexe, MachineState.SymbolsTable, &symbol);
if (found && symbol->Resolved) {
fprintf(stderr, "Redefinition of %s on line %d.\n", identifier->Lemexe, identifier->LineNumber);
exit(1);
}
if (found) {
symbol->Address = HeapSize;
symbol->Resolved = 1;
}
else {
symbol = AddSymbolToTable(identifier->Lemexe, HeapSize, 1, MachineState.SymbolsTable);
}
RemoveCurrentToken(); //Remove the Idenifier name token.
Symbol* symbol = ExpectIdentifier(1, RemoveExpected);
Token* value = PeekToken();
@@ -121,11 +102,7 @@ void HandleAssemblerDirective() {
RemoveCurrentToken(); //Remove the string declared by this DB command.
if (PeekToken()->Class == PunctuationClass && PeekToken()->Value.Punctuation == Comma) {
if (PeekToken()->Value.Punctuation != Comma) {
fprintf(stderr, "Syntax error line %d: Expected COMMA got %c.", PeekToken()->LineNumber, PeekToken()->Value.Punctuation);
exit(1);
}
RemoveCurrentToken(); //Remove COMMA
ExpectPuncuation(Comma, RemoveExpected);
if (PeekToken()->Class != NumberClass) {
fprintf(stderr, "Syntax error line %d: Expected terminating byte.\n", PeekToken()->LineNumber);
@@ -137,7 +114,7 @@ void HandleAssemblerDirective() {
symbol->Length++;
}
else if (PeekToken()->Class != PunctuationClass || PeekToken()->Value.Punctuation != NewLine) {
fprintf(stderr, "Syntax error line %d: Expected COMMA got %c.", PeekToken()->LineNumber, PeekToken()->Value.Punctuation);
fprintf(stderr, "Syntax error line %d: Expected COMMA.", PeekToken()->LineNumber);
exit(1);
}
}
@@ -165,19 +142,14 @@ void HandleAssemblerDirective() {
if (reg->Class == RegisterClass) {
AdvanceParser();
if (PeekToken()->Class != PunctuationClass || PeekToken()->Value.Punctuation != Comma) {
ExpectPuncuation(Comma, ForwardParser);
exit(1);
}
AdvanceParser(); //Save the comma
if (PeekToken()->Class != IdentifierClass && PeekToken()->Class != NumberClass && PeekToken()->Class != RegisterClass){
if (PeekToken()->Class != IdentifierClass && PeekToken()->Class != NumberClass && PeekToken()->Class != RegisterClass) {
fprintf(stderr, "[Line %d] Syntax error, expected either a symbol, numeric literal or a register.\n", PeekToken()->LineNumber);
exit(2);
}
if (PeekToken()->Class == NumberClass) {
printf("LODWI\n");
directive->Class = MnemonicClass;
directive->Value.Mnemonic = LODWI;
directive->Lemexe = "LODWI";
@@ -188,7 +160,6 @@ void HandleAssemblerDirective() {
directive->Lemexe = "LODW";
}
else {
printf("LAA\n");
directive->Class = MnemonicClass;
directive->Value.Mnemonic = LAA;
directive->Lemexe = "LAA";
@@ -204,23 +175,11 @@ void HandleAssemblerDirective() {
RemoveCurrentToken(); AdvanceParser(); //byte
if (PeekToken()->Class != RegisterClass) {
exit(3);
}
ExpectRegister();
AdvanceParser(); //reg
ExpectPuncuation(Comma, ForwardParser);
if (PeekToken()->Class != PunctuationClass) {
exit(7);
}
AdvanceParser(); //Comma
if (PeekToken()->Class != RegisterClass) {
exit(4);
}
AdvanceParser(); //Reg
ExpectRegister();
directive->Class = MnemonicClass;
directive->Value.Mnemonic = LODB;
@@ -230,6 +189,11 @@ void HandleAssemblerDirective() {
AdvanceParser(); //Line end
}
break;
case Store:
{
}
break;
default:
fprintf(stderr, "Synatx error on line %d, %s.\n", directive->LineNumber, directive->Lemexe);
@@ -237,10 +201,59 @@ void HandleAssemblerDirective() {
}
}
void HandleOperation(void) {
void ExpectPuncuation(TokenPunctuation punctuation, ExpectOptions options) {
Token* token = PeekToken();
if (token->Class != PunctuationClass || token->Value.Punctuation != punctuation) {
fprintf(stderr, "[Line %d] Syntac error, expected %c.\n", token->LineNumber, punctuation);
exit(1);
}
if (options & RemoveExpected) RemoveCurrentToken();
if (options & ForwardParser) AdvanceParser();
}
void ExpectRegister() {
Token* token = PeekToken();
if (token->Class != RegisterClass) {
fprintf(stderr, "[Line %d] Syntax error, expected Register.\n", token->LineNumber);
exit(2);
}
AdvanceParser();
}
Symbol* ExpectIdentifier(int resolved, ExpectOptions options) {
Token* token = PeekToken();
if (token->Class != IdentifierClass) {
fprintf(stderr, "[Line %d] Expected Identifier.\n", token->LineNumber);
exit(3);
}
Symbol* symbol;
int found = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable, &symbol);
if (found && symbol->Resolved && resolved) {
fprintf(stderr, "[Line %d] Redefinition of symbol '%s'.\n", token->LineNumber, token->Lemexe);
exit(4);
}
if (found) {
symbol->Address = HeapSize;
symbol->Resolved = resolved || symbol->Resolved;
}
else {
symbol = AddSymbolToTable(token->Lemexe, HeapSize, resolved, MachineState.SymbolsTable);
}
if (options & RemoveExpected) RemoveCurrentToken();
if (options & ForwardParser) AdvanceParser();
return symbol;
}
void PrintSymbols(void) {
//char mn[12];