Updated the parser to validate most of the opcodes. NOP falls through via the default case since it takes no parameters but the jump instructions need some thought before proceeding.

This commit is contained in:
2023-03-29 20:57:04 -05:00
parent 2e888913be
commit a9a50055a4
2 changed files with 72 additions and 21 deletions
+65 -20
View File
@@ -13,6 +13,7 @@ int CurrentToken = 0;
void PrintSymbols(void);
void RemoveCurrentToken(void);
void HandleAssemblerDirective(void);
void HandleOpcode(void);
void AdvanceParser(void);
void IgnoreParserLine(void);
Token* PeekToken(void);
@@ -45,7 +46,7 @@ IRState* ParseTokens(List* tokens) {
HandleAssemblerDirective();
break;
case MnemonicClass:
//HandleOperation();
HandleOpcode();
break;
case LabelClass:
{
@@ -207,6 +208,69 @@ void HandleAssemblerDirective() {
}
}
void HandleOpcode(void) {
Token* opcode = PeekToken();
AdvanceParser();
switch(opcode->Value.Mnemonic) {
case CMP:
{
ExpectRegister();
ExpectPuncuation(Comma, ForwardParser);
TokenClass class = PeekToken()->Class;
if (class == NumberClass) {
opcode->Value.Mnemonic = CMPI;
opcode->Lemexe = "CMPI";
AdvanceParser();
}
else {
ExpectRegister();
}
}
break;
case ADD:
case SUB:
case AND:
case OR:
case XOR:
ExpectRegister();
ExpectPuncuation(Comma, ForwardParser);
ExpectRegister();
break;
case SHL:
case SHR:
ExpectRegister();
ExpectPuncuation(Comma, ForwardParser);
if (PeekToken()->Class != NumberClass) {
fprintf(stderr, "[Line %d] Syntax error, expected numeric literal.\n", PeekToken()->LineNumber);
exit(12);
}
AdvanceParser();
break;
case INC:
case DEC:
case PUSH:
case POP:
ExpectRegister();
break;
default:
break;
}
ExpectLineEndOrFileEnd(ForwardParser);
}
void ExpectPuncuation(TokenPunctuation punctuation, ExpectOptions options) {
Token* token = PeekToken();
@@ -320,25 +384,6 @@ void PrintSymbols(void) {
printf("[%s] %s [%d] [Width: %d]", symbol->Resolved == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address, symbol->Length);
// switch(symbol->Type) {
// case InstructionPointerSymbol:
// GetMnemonicText(symbol->Value.Instruction->Mnemonic, mn);
// printf("%s -> %s", symbol->Name, mn);
// break;
// case NumericSymbol:
// printf("[N] %s Value: %d", symbol->Name, symbol->Value.Number);
// break;
// case StringSymbol:
// if (symbol->Value.String->Terminated)
// printf("[S] %s {%s}, %d", symbol->Name, symbol->Value.String->String, symbol->Value.String->TerminatingByte);
// else
// printf("[S] %s {%s}", symbol->Name, symbol->Value.String->String);
// break;
// case UnresolvedSymbol:
// printf("[U] %s", symbol->Name);
// break;
// }
printf("\n");
}
printf("-----SYMBOLS-----\n");