Updated the Parser to handle the variable declaration directive.

This commit is contained in:
2022-10-08 16:15:10 -05:00
parent c142d2c6d0
commit 8b6edc39be
+55 -11
View File
@@ -7,6 +7,7 @@ int CurrentToken = 0;
void PrintSymbols(void); void PrintSymbols(void);
Instruction* HandleOperation(void); Instruction* HandleOperation(void);
void HandleAssemblerDirective(void); void HandleAssemblerDirective(void);
void HandleAssemblerDirective(void);
void AdvanceParser(void); void AdvanceParser(void);
void IgnoreParserLine(void); void IgnoreParserLine(void);
Token* PeekToken(void); Token* PeekToken(void);
@@ -26,7 +27,7 @@ IRState* ParseTokens(List* tokens) {
switch(t->Class) { switch(t->Class) {
case DirectiveClass: //Maybe these should be ignored, let another process handle that. case DirectiveClass: //Maybe these should be ignored, let another process handle that.
IgnoreParserLine(); HandleAssemblerDirective();
break; break;
case MnemonicClass: case MnemonicClass:
HandleOperation(); HandleOperation();
@@ -54,12 +55,6 @@ IRState* ParseTokens(List* tokens) {
exit(1); exit(1);
} }
char mn[12];
GetMnemonicText(ins->Mnemonic, mn);
printf("Found: %s\n", mn);
symbol->Value.Instruction = ins; symbol->Value.Instruction = ins;
} }
break; break;
@@ -76,6 +71,44 @@ IRState* ParseTokens(List* tokens) {
return &MachineState; return &MachineState;
} }
void HandleAssemblerDirective() {
Token* token = PeekToken();
char* symbolname;
//ignore sanity checks
AdvanceParser();
token = PeekToken();
if (token->Class != IdentifierClass) {
fprintf(stderr, "[Error] Line %d: expected variable name.", token->LineNumber);
IgnoreParserLine();
return;
}
symbolname = token->Lemexe;
AdvanceParser();
token = PeekToken();
if (token->Class == NumberClass) {
//Delcaring a numeric value.
Symbol* s = AddSymbolToTable(symbolname, NumericSymbol, MachineState.SymbolsTable);
s->Value.Number = token->Value.Number;
}
else if (token->Class == CharacterClass) {
Symbol* s = AddSymbolToTable(symbolname, StringSymbol, MachineState.SymbolsTable);
s->Value.Text = token->Lemexe;
IgnoreParserLine();
}
else IgnoreParserLine();
}
Parameter* GetParameterType() { Parameter* GetParameterType() {
Token* token = PeekToken(); Token* token = PeekToken();
Symbol* symbol = NULL; Symbol* symbol = NULL;
@@ -232,11 +265,22 @@ void PrintSymbols(void) {
for(int i = 0; i < MachineState.SymbolsTable->Size; i++) { for(int i = 0; i < MachineState.SymbolsTable->Size; i++) {
Symbol* symbol = MachineState.SymbolsTable->Symbols[i]; Symbol* symbol = MachineState.SymbolsTable->Symbols[i];
printf("[%s] %s", symbol->Type == UnresolvedSymbol ? "Unresolved" : "Resolved", symbol->Name); printf("[%s] ", symbol->Type == UnresolvedSymbol ? "Unresolved" : "Resolved");
if (symbol->Type == InstructionPointerSymbol) { switch(symbol->Type) {
GetMnemonicText(symbol->Value.Instruction->Mnemonic, mn); case InstructionPointerSymbol:
printf(" -> %s", mn); GetMnemonicText(symbol->Value.Instruction->Mnemonic, mn);
printf("%s -> %s", symbol->Name, mn);
break;
case NumericSymbol:
printf("[N] %s", symbol->Name);
break;
case StringSymbol:
printf("[S] %s", symbol->Name);
break;
case UnresolvedSymbol:
printf("[U] %s", symbol->Name);
break;
} }
printf("\n"); printf("\n");