Added grammar checking for DB declarations.
This commit is contained in:
+6
-6
@@ -20,9 +20,6 @@ int main(int argc, char* args[]) {
|
||||
if (!ReadAllString(args[1], &source_code, &bytes_read)) return EX_IOERR;
|
||||
|
||||
List* list = GenerateTokenList(source_code);
|
||||
|
||||
ParseTokens(list);
|
||||
|
||||
char mnemonic[12];
|
||||
|
||||
for(int i = 0; i < list->size; i++) {
|
||||
@@ -39,7 +36,7 @@ int main(int argc, char* args[]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
printf(" %c ", t->Value.Punctuation);
|
||||
printf("[P]%c", t->Value.Punctuation);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -75,8 +72,11 @@ int main(int argc, char* args[]) {
|
||||
}
|
||||
|
||||
if (t->Class == CharacterClass) {
|
||||
printf("%s ", t->Lemexe);
|
||||
printf("[C]'%s'", t->Lemexe);
|
||||
}
|
||||
}
|
||||
}
|
||||
ParseTokens(list);
|
||||
|
||||
|
||||
free(source_code);
|
||||
}
|
||||
+39
-15
@@ -6,7 +6,7 @@ List* TokensList;
|
||||
int CurrentToken = 0;
|
||||
|
||||
void PrintSymbols(void);
|
||||
Instruction* HandleOperation(void);
|
||||
void HandleOperation(void);
|
||||
void RemoveCurrentToken(void);
|
||||
void HandleAssemblerDirective(void);
|
||||
void HandleAssemblerDirective(void);
|
||||
@@ -35,10 +35,9 @@ IRState* ParseTokens(List* tokens) {
|
||||
switch(t->Class) {
|
||||
case DirectiveClass: //Maybe these should be ignored, let another process handle that.
|
||||
HandleAssemblerDirective();
|
||||
RemoveCurrentToken(); // clear new line
|
||||
break;
|
||||
case MnemonicClass:
|
||||
//HandleOperation();
|
||||
HandleOperation();
|
||||
break;
|
||||
case LabelClass:
|
||||
{
|
||||
@@ -51,8 +50,6 @@ IRState* ParseTokens(List* tokens) {
|
||||
|
||||
if (!found) AddSymbolToTable(t->Lemexe, HeapSize, 1, MachineState.SymbolsTable);
|
||||
else tmp->Resolved = 1;
|
||||
|
||||
AdvanceParser(); //Consume the NewLine
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -89,23 +86,48 @@ void HandleAssemblerDirective() {
|
||||
if (found) {
|
||||
symbol->Address = HeapSize;
|
||||
symbol->Resolved = 1;
|
||||
|
||||
HeapSize += symbol->Length;
|
||||
}
|
||||
else {
|
||||
AddSymbolToTable(identifier->Lemexe, HeapSize, 1, MachineState.SymbolsTable);
|
||||
symbol = AddSymbolToTable(identifier->Lemexe, HeapSize, 1, MachineState.SymbolsTable);
|
||||
}
|
||||
|
||||
RemoveCurrentToken();
|
||||
RemoveCurrentToken(); //Remove the Idenifier name token.
|
||||
|
||||
identifier = PeekToken();
|
||||
Token* value = PeekToken();
|
||||
|
||||
if (identifier->Class == CharacterClass) {
|
||||
HeapSize += strlen(identifier->Lemexe);
|
||||
if (value->Class == CharacterClass) {
|
||||
symbol->Length = strlen(value->Lemexe);
|
||||
|
||||
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
|
||||
|
||||
if (PeekToken()->Class != NumberClass) {
|
||||
fprintf(stderr, "Syntax error line %d: Expected terminating byte.\n", PeekToken()->LineNumber);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
RemoveCurrentToken(); //Remove terminating byte
|
||||
|
||||
symbol->Length++;
|
||||
}
|
||||
else {
|
||||
HeapSize += 2;
|
||||
else if (PeekToken()->Class != PunctuationClass || PeekToken()->Value.Punctuation != NewLine) {
|
||||
fprintf(stderr, "Syntax error line %d: Expected COMMA got %c.", PeekToken()->LineNumber, PeekToken()->Value.Punctuation);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
else if (PeekToken()->Class == NumberClass) {
|
||||
symbol->Length = 2;
|
||||
|
||||
RemoveCurrentToken(); //Clear the number token.
|
||||
}
|
||||
|
||||
HeapSize += symbol->Length;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -114,7 +136,9 @@ void HandleAssemblerDirective() {
|
||||
}
|
||||
}
|
||||
|
||||
void HandleOperation(void) {
|
||||
|
||||
}
|
||||
|
||||
void PrintSymbols(void) {
|
||||
//char mn[12];
|
||||
@@ -122,7 +146,7 @@ void PrintSymbols(void) {
|
||||
for(int i = 0; i < MachineState.SymbolsTable->Size; i++) {
|
||||
Symbol* symbol = MachineState.SymbolsTable->Symbols[i];
|
||||
|
||||
printf("[%s] %s [%d]", symbol->Resolved == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address);
|
||||
printf("[%s] %s [%d] [Width: %d]", symbol->Resolved == 0 ? "Unresolved" : "Resolved", symbol->Name, symbol->Address, symbol->Length);
|
||||
|
||||
// switch(symbol->Type) {
|
||||
// case InstructionPointerSymbol:
|
||||
|
||||
Reference in New Issue
Block a user