Cleaned up the Symbol struct and updated code accordingly.

This commit is contained in:
2022-10-08 14:43:00 -05:00
parent 4b08707e67
commit c142d2c6d0
3 changed files with 37 additions and 20 deletions
+24 -11
View File
@@ -35,27 +35,32 @@ IRState* ParseTokens(List* tokens) {
{
Symbol* symbol = TryGetSymbol(t->Lemexe, MachineState.SymbolsTable);
if (symbol && symbol->Resolved) {
if (symbol && symbol->Type != UnresolvedSymbol) {
fprintf(stderr, "[Error] Line %d: Redefinition of symbol '%s'\n", t->LineNumber, t->Lemexe);
exit(1);
}
AdvanceParser(); //Consume the label token
if (!symbol) symbol = AddSymbolToTable(t->Lemexe, NULL, strlen(t->Lemexe), MachineState.SymbolsTable);
symbol->Resolved = 1;
if (!symbol) symbol = AddSymbolToTable(t->Lemexe, InstructionPointerSymbol, MachineState.SymbolsTable);
else symbol->Type = InstructionPointerSymbol;
AdvanceParser(); //Consume the NewLine
Instruction* ins = HandleOperation();
if (!ins) printf("No op %s\n", symbol->Name);
if (!ins) {
fprintf(stderr, "[Error] Line %d: Expected instruction following label %s\n", t->LineNumber, t->Lemexe);
exit(1);
}
// if (!ins) continue;
char mn[12];
// symbol->Value.Instruction = ins;
// symbol->InstructionPointer = 1;
GetMnemonicText(ins->Mnemonic, mn);
printf("Found: %s\n", mn);
symbol->Value.Instruction = ins;
}
break;
default:
@@ -93,7 +98,7 @@ Parameter* GetParameterType() {
symbol = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable);
if (!symbol) symbol = AddSymbolToTable(token->Lemexe, token->Lemexe, strlen(token->Lemexe), MachineState.SymbolsTable);
if (!symbol) symbol = AddSymbolToTable(token->Lemexe, UnresolvedSymbol, MachineState.SymbolsTable);
param->Value.Symbol = symbol->Name;
@@ -126,7 +131,7 @@ Parameter* GetParameterType() {
symbol = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable);
if (!symbol) symbol = AddSymbolToTable(token->Lemexe, NULL, strlen(token->Lemexe), MachineState.SymbolsTable);
if (!symbol) symbol = AddSymbolToTable(token->Lemexe, UnresolvedSymbol, MachineState.SymbolsTable);
param->Value.Symbol = symbol->Name;
}
@@ -222,11 +227,19 @@ Instruction* HandleOperation() {
}
void PrintSymbols(void) {
char mn[12];
printf("-----SYMBOLS-----\n");
for(int i = 0; i < MachineState.SymbolsTable->Size; i++) {
Symbol* symbol = MachineState.SymbolsTable->Symbols[i];
printf("[Resolved? %d] %s\n", symbol->Resolved, symbol->Name);
printf("[%s] %s", symbol->Type == UnresolvedSymbol ? "Unresolved" : "Resolved", symbol->Name);
if (symbol->Type == InstructionPointerSymbol) {
GetMnemonicText(symbol->Value.Instruction->Mnemonic, mn);
printf(" -> %s", mn);
}
printf("\n");
}
printf("-----SYMBOLS-----\n");
}