Updated the parser to handle processing a simple load byte instruction. Added the LODB opcode to the opcode enum listing and updated the Scanner to look for 'byte' and mark it as a directive.
This commit is contained in:
+93
-7
@@ -1,6 +1,7 @@
|
||||
#include "../includes/parser.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
List* TokensList;
|
||||
int CurrentToken = 0;
|
||||
@@ -14,8 +15,12 @@ void AdvanceParser(void);
|
||||
void IgnoreParserLine(void);
|
||||
Token* PeekToken(void);
|
||||
int ParserAtEnd(void);
|
||||
int Expect(TokenClass class, void* value);
|
||||
IRState MachineState;
|
||||
|
||||
//int ExpectPuncuation(TokenPunctuation punctuation);
|
||||
//int ExpectIdentifier();
|
||||
|
||||
int HeapSize = 0;
|
||||
int PC = 0;
|
||||
|
||||
@@ -29,11 +34,11 @@ IRState* ParseTokens(List* tokens) {
|
||||
|
||||
while(!ParserAtEnd()) {
|
||||
Token* t = PeekToken();
|
||||
if (!t) break;
|
||||
if (!t || t->EndOfFile) break;
|
||||
Symbol* tmp;
|
||||
|
||||
switch(t->Class) {
|
||||
case DirectiveClass: //Maybe these should be ignored, let another process handle that.
|
||||
case DirectiveClass:
|
||||
HandleAssemblerDirective();
|
||||
break;
|
||||
case MnemonicClass:
|
||||
@@ -58,8 +63,6 @@ IRState* ParseTokens(List* tokens) {
|
||||
//IgnoreParserLine();
|
||||
break;
|
||||
}
|
||||
|
||||
RemoveCurrentToken();
|
||||
}
|
||||
|
||||
PrintSymbols();
|
||||
@@ -69,12 +72,19 @@ IRState* ParseTokens(List* tokens) {
|
||||
|
||||
void HandleAssemblerDirective() {
|
||||
Token* directive = PeekToken();
|
||||
RemoveCurrentToken();
|
||||
|
||||
switch(directive->Value.Directive){
|
||||
case DB:
|
||||
{
|
||||
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);
|
||||
@@ -83,6 +93,7 @@ void HandleAssemblerDirective() {
|
||||
fprintf(stderr, "Redefinition of %s on line %d.\n", identifier->Lemexe, identifier->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (found) {
|
||||
symbol->Address = HeapSize;
|
||||
symbol->Resolved = 1;
|
||||
@@ -128,18 +139,93 @@ void HandleAssemblerDirective() {
|
||||
}
|
||||
|
||||
HeapSize += symbol->Length;
|
||||
|
||||
RemoveCurrentToken(); //Wipe the line end token
|
||||
}
|
||||
break;
|
||||
case Load:
|
||||
{
|
||||
AdvanceParser();
|
||||
Token* reg = PeekToken();
|
||||
|
||||
if (reg->Class != RegisterClass && reg->Class != DirectiveClass) {
|
||||
fprintf(stderr, "Syntax error line %d: Expected register or directive 'byte', got %d.", reg->LineNumber, reg->Class);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (reg->Class == RegisterClass) {
|
||||
AdvanceParser();
|
||||
|
||||
if (PeekToken()->Class != PunctuationClass || PeekToken()->Value.Punctuation != Comma) {
|
||||
|
||||
exit(1);
|
||||
}
|
||||
|
||||
AdvanceParser(); //Save the comma
|
||||
|
||||
if (PeekToken()->Class != IdentifierClass && PeekToken()->Class != NumberClass){
|
||||
exit(2);
|
||||
}
|
||||
|
||||
if (PeekToken()->Class == NumberClass) {
|
||||
printf("LODWI\n");
|
||||
directive->Class = MnemonicClass;
|
||||
directive->Value.Mnemonic = LODWI;
|
||||
directive->Lemexe = "LODWI";
|
||||
}
|
||||
else {
|
||||
printf("LAA\n");
|
||||
directive->Class = MnemonicClass;
|
||||
directive->Value.Mnemonic = LAA;
|
||||
directive->Lemexe = "LAA";
|
||||
}
|
||||
|
||||
AdvanceParser();
|
||||
}
|
||||
else if (reg->Class == DirectiveClass) {
|
||||
if (reg->Value.Directive != Byte) {
|
||||
fprintf(stderr, "Syntax error: Line %d expected keyword 'byte'.\n", reg->LineNumber);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
RemoveCurrentToken(); AdvanceParser(); //byte
|
||||
|
||||
if (PeekToken()->Class != RegisterClass) {
|
||||
exit(3);
|
||||
}
|
||||
|
||||
AdvanceParser(); //reg
|
||||
|
||||
if (PeekToken()->Class != PunctuationClass) {
|
||||
exit(7);
|
||||
}
|
||||
|
||||
AdvanceParser(); //Comma
|
||||
|
||||
if (PeekToken()->Class != RegisterClass) {
|
||||
exit(4);
|
||||
}
|
||||
|
||||
AdvanceParser(); //Reg
|
||||
|
||||
directive->Class = MnemonicClass;
|
||||
directive->Value.Mnemonic = LODB;
|
||||
directive->Lemexe = "LODB";
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Synatx error on line %d.\n", directive->LineNumber);
|
||||
fprintf(stderr, "Synatx error on line %d, %s.\n", directive->LineNumber, directive->Lemexe);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
void HandleOperation(void) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void PrintSymbols(void) {
|
||||
//char mn[12];
|
||||
printf("-----SYMBOLS-----\n");
|
||||
|
||||
Reference in New Issue
Block a user